From 63db31e210aa685e0a00c4b5dd5b868562fbc606 Mon Sep 17 00:00:00 2001 From: Yohan Boniface Date: Wed, 2 Aug 2017 16:36:43 +0200 Subject: [PATCH] Follow roll API changes --- tests/test_plugins.py | 61 +++++++++++++++++++++++-------------------- utilery/core.py | 4 +-- utilery/models.py | 5 ++-- utilery/views.py | 18 ++++++------- 4 files changed, 45 insertions(+), 43 deletions(-) diff --git a/tests/test_plugins.py b/tests/test_plugins.py index 0ad8818..0c84e4f 100644 --- a/tests/test_plugins.py +++ b/tests/test_plugins.py @@ -14,56 +14,59 @@ 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): @@ -71,31 +74,31 @@ async def on_response(response, request, **kwargs): 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'] = [] @@ -103,31 +106,31 @@ async def test_cors_can_be_cancelled_in_config(app, req, fetchall, config): 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'] = [] @@ -135,6 +138,6 @@ async def test_cache_can_be_cancelled_in_config(app, req, fetchall, config): 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 diff --git a/utilery/core.py b/utilery/core.py index c1f7e8f..d9cf74c 100644 --- a/utilery/core.py +++ b/utilery/core.py @@ -4,7 +4,7 @@ import asyncpg from roll import Roll -from roll.extensions import cors +from roll.extensions import cors, traceback import yaml from . import config @@ -21,7 +21,7 @@ app = application = Roll() - +traceback(app) class DB: diff --git a/utilery/models.py b/utilery/models.py index fccc877..e73c181 100644 --- a/utilery/models.py +++ b/utilery/models.py @@ -83,7 +83,7 @@ def __init__(self, names, z, x, y, namespace='default'): self.recipe = RECIPES[namespace] self.namespace = namespace - async def __call__(self): + async def __call__(self, response): names = self.recipe.layers.keys() if self.ALL else self.names for name in names: if name not in self.recipe.layers: @@ -92,7 +92,8 @@ async def __call__(self): await self.process_layer(self.recipe.layers[name]) self.post_process() - return self.content, 200, {'Content-Type': self.CONTENT_TYPE} + response.body = self.content + response.headers['Content-Type'] = self.CONTENT_TYPE async def process_layer(self, layer): layer_data = await self.query_layer(layer) diff --git a/utilery/views.py b/utilery/views.py index 8097338..af89ec6 100644 --- a/utilery/views.py +++ b/utilery/views.py @@ -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(): @@ -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 @@ -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() + await tile(response) @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() + await tile(response) @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() + await tile(response)