Skip to content

Commit

Permalink
Follow roll API changes
Browse files Browse the repository at this point in the history
  • Loading branch information
yohanboniface committed Aug 2, 2017
1 parent 3fcff5d commit a7e773b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 39 deletions.
61 changes: 32 additions & 29 deletions tests/test_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,127 +14,130 @@ async def on_load(config, recipes):
config.LOAD = True


def test_on_before_load(req, config):
def test_on_before_load(client, config):
assert config.BEFORE_LOAD


def test_on_load(req, config):
def test_on_load(client, config):
assert config.LOAD


@pytest.mark.asyncio
async def test_on_request_can_return_content_only(app, req):
async def test_on_request_can_return_content_only(app, client):

@app.listen('request')
async def on_request(request):
async def on_request(request, response):
assert request is not None
assert request.path == '/default/mylayer/0/0/0/pbf'
return b'on_request'
response.body = b'on_request'
return True # Shortcut request processing pipeline.

resp = await req('/default/mylayer/0/0/0/pbf')
resp = await client.get('/default/mylayer/0/0/0/pbf')
assert resp.status == b'200 OK'
assert resp.body == b'on_request'


@pytest.mark.asyncio
async def test_on_request_can_return_tuple(app, req):
async def test_on_request_can_return_tuple(app, client):

@app.listen('request')
async def on_request(request):
return '', 302, {'Location': 'http://somewhere-else.org'}
async def on_request(request, response):
response.status = 302
response.headers['Location'] = 'http://somewhere-else.org'
return True # Shortcut request processing pipeline.

resp = await req('/default/mylayer/0/0/0/pbf')
resp = await client.get('/default/mylayer/0/0/0/pbf')
assert resp.status == b'302 Found'
assert resp.headers['Location'] == 'http://somewhere-else.org'


@pytest.mark.asyncio
async def test_on_response_can_override_response_body(app, req, fetchall):
async def test_on_response_can_override_response_body(app, client, fetchall):

@app.listen('response')
async def on_response(response, request):
return b'on_response'
response.body = b'on_response'

fetchall([])

resp = await req('/default/mylayer/0/0/0/pbf')
resp = await client.get('/default/mylayer/0/0/0/pbf')
assert resp.status == b'200 OK'
assert resp.body == b'on_response'


@pytest.mark.asyncio
async def test_on_response_can_override_response_headers(app, req, fetchall):
async def test_on_response_can_override_headers(app, client, fetchall):

@app.listen('response')
async def on_response(response, request, **kwargs):
response.headers['Custom'] = 'OK'

fetchall([])

resp = await req('/default/mylayer/0/0/0/pbf')
resp = await client.get('/default/mylayer/0/0/0/pbf')
assert resp.status == b'200 OK'
assert resp.headers['Custom'] == 'OK'


@pytest.mark.asyncio
async def test_cors_add_cors_headers(req, fetchall):
async def test_cors_add_cors_headers(client, fetchall):
fetchall([])
resp = await req('/default/mylayer/0/0/0/pbf')
resp = await client.get('/default/mylayer/0/0/0/pbf')
assert resp.status == b'200 OK'
assert resp.headers['Access-Control-Allow-Origin'] == '*'


@pytest.mark.asyncio
async def test_cors_can_be_changed_in_config(app, req, fetchall, config):
async def test_cors_can_be_changed_in_config(app, client, fetchall, config):
config.CORS = 'http://mydomain.org'
await app.startup()
fetchall([])
resp = await req('/default/mylayer/0/0/0/pbf')
resp = await client.get('/default/mylayer/0/0/0/pbf')
assert resp.status == b'200 OK'
assert resp.headers['Access-Control-Allow-Origin'] == 'http://mydomain.org'


@pytest.mark.asyncio
async def test_cors_can_be_cancelled_in_config(app, req, fetchall, config):
async def test_cors_can_be_cancelled_in_config(app, client, fetchall, config):
# cors has already been registered during app startup when loaded as
# fixture. Reset this.
app.hooks['response'] = []
config.CORS = False
config.LOADED = False
await app.startup()
fetchall([])
resp = await req('/default/mylayer/0/0/0/pbf')
resp = await client.get('/default/mylayer/0/0/0/pbf')
assert resp.status == b'200 OK'
assert 'Access-Control-Allow-Origin' not in resp.headers


@pytest.mark.asyncio
async def test_cache_add_cache_control_headers(req, fetchall):
async def test_cache_add_cache_control_headers(client, fetchall):
fetchall([])
resp = await req('/default/mylayer/0/0/0/pbf')
resp = await client.get('/default/mylayer/0/0/0/pbf')
assert resp.status == b'200 OK'
assert resp.headers['Cache-Control'] == 'max-age=3600'
assert resp.headers['Cache-Control'] == 'public,max-age=3600'


@pytest.mark.asyncio
async def test_max_age_can_be_changed_in_config(app, req, fetchall, config):
async def test_max_age_can_be_changed_in_config(app, client, fetchall, config):
config.MAX_AGE = 86400
await app.startup()
fetchall([])
resp = await req('/default/mylayer/0/0/0/pbf')
resp = await client.get('/default/mylayer/0/0/0/pbf')
assert resp.status == b'200 OK'
assert resp.headers['Cache-Control'] == 'max-age=86400'
assert resp.headers['Cache-Control'] == 'public,max-age=86400'


@pytest.mark.asyncio
async def test_cache_can_be_cancelled_in_config(app, req, fetchall, config):
async def test_cache_can_be_cancelled_in_config(app, client, fetchall, config):
# cors has already been registered during app startup when loaded as
# fixture. Reset this.
app.hooks['response'] = []
config.MAX_AGE = False
config.LOADED = False
await app.startup()
fetchall([])
resp = await req('/default/mylayer/0/0/0/pbf')
resp = await client.get('/default/mylayer/0/0/0/pbf')
assert resp.status == b'200 OK'
assert 'Cache-Control' not in resp.headers
18 changes: 8 additions & 10 deletions utilery/views.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import ujson as json

from . import config
from .core import RECIPES, app
from .models import PBF, JSON, GeoJSON


@app.route('/tilejson/mvt.json')
async def tilejson(request):
async def tilejson(request, response):
base = config.TILEJSON
base['vector_layers'] = []
for recipe in RECIPES.values():
Expand All @@ -15,7 +13,7 @@ async def tilejson(request):
"description": layer.description,
"id": layer.id
})
return json.dumps(base), 200, {}
response.json = base


# TODO use .ext instead of /ext
Expand All @@ -24,20 +22,20 @@ async def tilejson(request):
@app.route('/:names/:z/:x/:y/pbf')
@app.route('/:namespace/:names/:z/:x/:y/mvt')
@app.route('/:names/:z/:x/:y/mvt')
async def pbf(request, names, z, x, y, namespace='default'):
async def pbf(request, response, names, z, x, y, namespace='default'):
tile = PBF(names, z, x, y, namespace)
return await tile()
response.body = await tile()


@app.route('/:namespace/:names/:z/:x/:y/json')
@app.route('/:names/:z/:x/:y/json')
async def json_(request, names, z, x, y, namespace='default'):
async def json_(request, response, names, z, x, y, namespace='default'):
tile = JSON(names, z, x, y, namespace)
return await tile()
response.body = await tile()


@app.route('/:namespace/:names/:z/:x/:y/geojson')
@app.route('/:names/:z/:x/:y/geojson')
async def geojson(request, names, z, x, y, namespace='default'):
async def geojson(request, response, names, z, x, y, namespace='default'):
tile = GeoJSON(names, z, x, y, namespace)
return await tile()
response.body = await tile()

0 comments on commit a7e773b

Please sign in to comment.