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 63db31e
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 43 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
4 changes: 2 additions & 2 deletions utilery/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -21,7 +21,7 @@


app = application = Roll()

traceback(app)

class DB:

Expand Down
5 changes: 3 additions & 2 deletions utilery/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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)
Expand Down
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()
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)

0 comments on commit 63db31e

Please sign in to comment.