Skip to content

Commit 6988980

Browse files
committed
Follow roll API changes
1 parent 3fcff5d commit 6988980

File tree

3 files changed

+43
-41
lines changed

3 files changed

+43
-41
lines changed

tests/test_plugins.py

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -14,127 +14,130 @@ async def on_load(config, recipes):
1414
config.LOAD = True
1515

1616

17-
def test_on_before_load(req, config):
17+
def test_on_before_load(client, config):
1818
assert config.BEFORE_LOAD
1919

2020

21-
def test_on_load(req, config):
21+
def test_on_load(client, config):
2222
assert config.LOAD
2323

2424

2525
@pytest.mark.asyncio
26-
async def test_on_request_can_return_content_only(app, req):
26+
async def test_on_request_can_return_content_only(app, client):
2727

2828
@app.listen('request')
29-
async def on_request(request):
29+
async def on_request(request, response):
3030
assert request is not None
3131
assert request.path == '/default/mylayer/0/0/0/pbf'
32-
return b'on_request'
32+
response.body = b'on_request'
33+
return True # Shortcut request processing pipeline.
3334

34-
resp = await req('/default/mylayer/0/0/0/pbf')
35+
resp = await client.get('/default/mylayer/0/0/0/pbf')
3536
assert resp.status == b'200 OK'
3637
assert resp.body == b'on_request'
3738

3839

3940
@pytest.mark.asyncio
40-
async def test_on_request_can_return_tuple(app, req):
41+
async def test_on_request_can_return_tuple(app, client):
4142

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

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

5053

5154
@pytest.mark.asyncio
52-
async def test_on_response_can_override_response_body(app, req, fetchall):
55+
async def test_on_response_can_override_response_body(app, client, fetchall):
5356

5457
@app.listen('response')
5558
async def on_response(response, request):
56-
return b'on_response'
59+
response.body = b'on_response'
5760

5861
fetchall([])
5962

60-
resp = await req('/default/mylayer/0/0/0/pbf')
63+
resp = await client.get('/default/mylayer/0/0/0/pbf')
6164
assert resp.status == b'200 OK'
6265
assert resp.body == b'on_response'
6366

6467

6568
@pytest.mark.asyncio
66-
async def test_on_response_can_override_response_headers(app, req, fetchall):
69+
async def test_on_response_can_override_headers(app, client, fetchall):
6770

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

7275
fetchall([])
7376

74-
resp = await req('/default/mylayer/0/0/0/pbf')
77+
resp = await client.get('/default/mylayer/0/0/0/pbf')
7578
assert resp.status == b'200 OK'
7679
assert resp.headers['Custom'] == 'OK'
7780

7881

7982
@pytest.mark.asyncio
80-
async def test_cors_add_cors_headers(req, fetchall):
83+
async def test_cors_add_cors_headers(client, fetchall):
8184
fetchall([])
82-
resp = await req('/default/mylayer/0/0/0/pbf')
85+
resp = await client.get('/default/mylayer/0/0/0/pbf')
8386
assert resp.status == b'200 OK'
8487
assert resp.headers['Access-Control-Allow-Origin'] == '*'
8588

8689

8790
@pytest.mark.asyncio
88-
async def test_cors_can_be_changed_in_config(app, req, fetchall, config):
91+
async def test_cors_can_be_changed_in_config(app, client, fetchall, config):
8992
config.CORS = 'http://mydomain.org'
9093
await app.startup()
9194
fetchall([])
92-
resp = await req('/default/mylayer/0/0/0/pbf')
95+
resp = await client.get('/default/mylayer/0/0/0/pbf')
9396
assert resp.status == b'200 OK'
9497
assert resp.headers['Access-Control-Allow-Origin'] == 'http://mydomain.org'
9598

9699

97100
@pytest.mark.asyncio
98-
async def test_cors_can_be_cancelled_in_config(app, req, fetchall, config):
101+
async def test_cors_can_be_cancelled_in_config(app, client, fetchall, config):
99102
# cors has already been registered during app startup when loaded as
100103
# fixture. Reset this.
101104
app.hooks['response'] = []
102105
config.CORS = False
103106
config.LOADED = False
104107
await app.startup()
105108
fetchall([])
106-
resp = await req('/default/mylayer/0/0/0/pbf')
109+
resp = await client.get('/default/mylayer/0/0/0/pbf')
107110
assert resp.status == b'200 OK'
108111
assert 'Access-Control-Allow-Origin' not in resp.headers
109112

110113

111114
@pytest.mark.asyncio
112-
async def test_cache_add_cache_control_headers(req, fetchall):
115+
async def test_cache_add_cache_control_headers(client, fetchall):
113116
fetchall([])
114-
resp = await req('/default/mylayer/0/0/0/pbf')
117+
resp = await client.get('/default/mylayer/0/0/0/pbf')
115118
assert resp.status == b'200 OK'
116-
assert resp.headers['Cache-Control'] == 'max-age=3600'
119+
assert resp.headers['Cache-Control'] == 'public,max-age=3600'
117120

118121

119122
@pytest.mark.asyncio
120-
async def test_max_age_can_be_changed_in_config(app, req, fetchall, config):
123+
async def test_max_age_can_be_changed_in_config(app, client, fetchall, config):
121124
config.MAX_AGE = 86400
122125
await app.startup()
123126
fetchall([])
124-
resp = await req('/default/mylayer/0/0/0/pbf')
127+
resp = await client.get('/default/mylayer/0/0/0/pbf')
125128
assert resp.status == b'200 OK'
126-
assert resp.headers['Cache-Control'] == 'max-age=86400'
129+
assert resp.headers['Cache-Control'] == 'public,max-age=86400'
127130

128131

129132
@pytest.mark.asyncio
130-
async def test_cache_can_be_cancelled_in_config(app, req, fetchall, config):
133+
async def test_cache_can_be_cancelled_in_config(app, client, fetchall, config):
131134
# cors has already been registered during app startup when loaded as
132135
# fixture. Reset this.
133136
app.hooks['response'] = []
134137
config.MAX_AGE = False
135138
config.LOADED = False
136139
await app.startup()
137140
fetchall([])
138-
resp = await req('/default/mylayer/0/0/0/pbf')
141+
resp = await client.get('/default/mylayer/0/0/0/pbf')
139142
assert resp.status == b'200 OK'
140143
assert 'Cache-Control' not in resp.headers

utilery/models.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def __init__(self, names, z, x, y, namespace='default'):
8383
self.recipe = RECIPES[namespace]
8484
self.namespace = namespace
8585

86-
async def __call__(self):
86+
async def __call__(self, response):
8787
names = self.recipe.layers.keys() if self.ALL else self.names
8888
for name in names:
8989
if name not in self.recipe.layers:
@@ -92,7 +92,8 @@ async def __call__(self):
9292
await self.process_layer(self.recipe.layers[name])
9393
self.post_process()
9494

95-
return self.content, 200, {'Content-Type': self.CONTENT_TYPE}
95+
response.body = self.content
96+
response.headers['Content-Type'] = self.CONTENT_TYPE
9697

9798
async def process_layer(self, layer):
9899
layer_data = await self.query_layer(layer)

utilery/views.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
import ujson as json
2-
31
from . import config
42
from .core import RECIPES, app
53
from .models import PBF, JSON, GeoJSON
64

75

86
@app.route('/tilejson/mvt.json')
9-
async def tilejson(request):
7+
async def tilejson(request, response):
108
base = config.TILEJSON
119
base['vector_layers'] = []
1210
for recipe in RECIPES.values():
@@ -15,7 +13,7 @@ async def tilejson(request):
1513
"description": layer.description,
1614
"id": layer.id
1715
})
18-
return json.dumps(base), 200, {}
16+
response.json = base
1917

2018

2119
# TODO use .ext instead of /ext
@@ -24,20 +22,20 @@ async def tilejson(request):
2422
@app.route('/:names/:z/:x/:y/pbf')
2523
@app.route('/:namespace/:names/:z/:x/:y/mvt')
2624
@app.route('/:names/:z/:x/:y/mvt')
27-
async def pbf(request, names, z, x, y, namespace='default'):
25+
async def pbf(request, response, names, z, x, y, namespace='default'):
2826
tile = PBF(names, z, x, y, namespace)
29-
return await tile()
27+
await tile(response)
3028

3129

3230
@app.route('/:namespace/:names/:z/:x/:y/json')
3331
@app.route('/:names/:z/:x/:y/json')
34-
async def json_(request, names, z, x, y, namespace='default'):
32+
async def json_(request, response, names, z, x, y, namespace='default'):
3533
tile = JSON(names, z, x, y, namespace)
36-
return await tile()
34+
await tile(response)
3735

3836

3937
@app.route('/:namespace/:names/:z/:x/:y/geojson')
4038
@app.route('/:names/:z/:x/:y/geojson')
41-
async def geojson(request, names, z, x, y, namespace='default'):
39+
async def geojson(request, response, names, z, x, y, namespace='default'):
4240
tile = GeoJSON(names, z, x, y, namespace)
43-
return await tile()
41+
await tile(response)

0 commit comments

Comments
 (0)