2016-10-20 12:33:28 +01:00
|
|
|
import pytest
|
|
|
|
|
2016-10-14 11:23:48 +01:00
|
|
|
from sanic import Sanic
|
2016-10-20 12:33:28 +01:00
|
|
|
from sanic.response import text
|
2016-12-30 09:36:57 +00:00
|
|
|
from sanic.router import RouteExists, RouteDoesNotExist
|
2016-10-14 11:23:48 +01:00
|
|
|
|
2016-10-15 03:53:49 +01:00
|
|
|
|
2016-10-14 11:23:48 +01:00
|
|
|
# ------------------------------------------------------------ #
|
|
|
|
# UTF-8
|
|
|
|
# ------------------------------------------------------------ #
|
|
|
|
|
2017-01-20 18:00:51 +00:00
|
|
|
def test_shorthand_routes_get():
|
|
|
|
app = Sanic('test_shorhand_routes_get')
|
2017-01-20 07:47:07 +00:00
|
|
|
|
2017-01-20 18:00:51 +00:00
|
|
|
@app.get('/get')
|
2017-01-20 07:47:07 +00:00
|
|
|
def handler(request):
|
|
|
|
return text('OK')
|
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.get('/get')
|
2017-01-20 18:00:51 +00:00
|
|
|
assert response.text == 'OK'
|
2017-01-20 07:47:07 +00:00
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.post('/get')
|
2017-01-20 18:00:51 +00:00
|
|
|
assert response.status == 405
|
2017-01-20 07:47:07 +00:00
|
|
|
|
2017-01-20 18:00:51 +00:00
|
|
|
def test_shorthand_routes_post():
|
|
|
|
app = Sanic('test_shorhand_routes_post')
|
2017-01-20 07:47:07 +00:00
|
|
|
|
2017-01-20 18:00:51 +00:00
|
|
|
@app.post('/post')
|
2017-01-20 07:47:07 +00:00
|
|
|
def handler(request):
|
|
|
|
return text('OK')
|
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.post('/post')
|
2017-01-20 07:47:07 +00:00
|
|
|
assert response.text == 'OK'
|
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.get('/post')
|
2017-01-20 07:47:07 +00:00
|
|
|
assert response.status == 405
|
|
|
|
|
2017-01-20 18:00:51 +00:00
|
|
|
def test_shorthand_routes_put():
|
|
|
|
app = Sanic('test_shorhand_routes_put')
|
|
|
|
|
|
|
|
@app.put('/put')
|
|
|
|
def handler(request):
|
|
|
|
return text('OK')
|
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.put('/put')
|
2017-01-20 07:47:07 +00:00
|
|
|
assert response.text == 'OK'
|
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.get('/put')
|
2017-01-20 07:47:07 +00:00
|
|
|
assert response.status == 405
|
|
|
|
|
2017-01-20 18:00:51 +00:00
|
|
|
def test_shorthand_routes_patch():
|
|
|
|
app = Sanic('test_shorhand_routes_patch')
|
|
|
|
|
|
|
|
@app.patch('/patch')
|
|
|
|
def handler(request):
|
|
|
|
return text('OK')
|
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.patch('/patch')
|
2017-01-20 07:47:07 +00:00
|
|
|
assert response.text == 'OK'
|
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.get('/patch')
|
2017-01-20 07:47:07 +00:00
|
|
|
assert response.status == 405
|
|
|
|
|
2017-01-20 18:00:51 +00:00
|
|
|
def test_shorthand_routes_head():
|
|
|
|
app = Sanic('test_shorhand_routes_head')
|
|
|
|
|
|
|
|
@app.head('/head')
|
|
|
|
def handler(request):
|
|
|
|
return text('OK')
|
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.head('/head')
|
2017-01-20 18:00:51 +00:00
|
|
|
assert response.status == 200
|
2017-01-20 07:47:07 +00:00
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.get('/head')
|
2017-01-20 07:47:07 +00:00
|
|
|
assert response.status == 405
|
|
|
|
|
2017-01-20 18:00:51 +00:00
|
|
|
def test_shorthand_routes_options():
|
|
|
|
app = Sanic('test_shorhand_routes_options')
|
2017-01-20 07:47:07 +00:00
|
|
|
|
2017-01-20 18:00:51 +00:00
|
|
|
@app.options('/options')
|
|
|
|
def handler(request):
|
|
|
|
return text('OK')
|
2017-01-20 07:47:07 +00:00
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.options('/options')
|
2017-01-20 18:00:51 +00:00
|
|
|
assert response.status == 200
|
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.get('/options')
|
2017-01-20 18:00:51 +00:00
|
|
|
assert response.status == 405
|
2017-01-20 07:47:07 +00:00
|
|
|
|
2016-10-20 12:33:28 +01:00
|
|
|
def test_static_routes():
|
|
|
|
app = Sanic('test_dynamic_route')
|
|
|
|
|
|
|
|
@app.route('/test')
|
|
|
|
async def handler1(request):
|
|
|
|
return text('OK1')
|
|
|
|
|
|
|
|
@app.route('/pizazz')
|
|
|
|
async def handler2(request):
|
|
|
|
return text('OK2')
|
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.get('/test')
|
2016-10-20 12:33:28 +01:00
|
|
|
assert response.text == 'OK1'
|
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.get('/pizazz')
|
2016-10-20 12:33:28 +01:00
|
|
|
assert response.text == 'OK2'
|
|
|
|
|
|
|
|
|
2016-10-14 11:23:48 +01:00
|
|
|
def test_dynamic_route():
|
2016-10-15 03:53:49 +01:00
|
|
|
app = Sanic('test_dynamic_route')
|
|
|
|
|
|
|
|
results = []
|
2016-10-14 11:23:48 +01:00
|
|
|
|
2016-10-15 03:53:49 +01:00
|
|
|
@app.route('/folder/<name>')
|
|
|
|
async def handler(request, name):
|
|
|
|
results.append(name)
|
|
|
|
return text('OK')
|
2016-10-14 11:23:48 +01:00
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.get('/folder/test123')
|
2016-10-14 11:23:48 +01:00
|
|
|
|
2016-10-15 03:53:49 +01:00
|
|
|
assert response.text == 'OK'
|
|
|
|
assert results[0] == 'test123'
|
2016-10-14 11:23:48 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_dynamic_route_string():
|
2016-10-15 03:53:49 +01:00
|
|
|
app = Sanic('test_dynamic_route_string')
|
2016-10-14 11:23:48 +01:00
|
|
|
|
2016-10-15 03:53:49 +01:00
|
|
|
results = []
|
2016-10-14 11:23:48 +01:00
|
|
|
|
2016-10-15 03:53:49 +01:00
|
|
|
@app.route('/folder/<name:string>')
|
|
|
|
async def handler(request, name):
|
|
|
|
results.append(name)
|
|
|
|
return text('OK')
|
2016-10-14 11:23:48 +01:00
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.get('/folder/test123')
|
2016-10-15 03:53:49 +01:00
|
|
|
|
|
|
|
assert response.text == 'OK'
|
|
|
|
assert results[0] == 'test123'
|
2016-10-14 11:23:48 +01:00
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.get('/folder/favicon.ico')
|
2016-10-16 21:41:56 +01:00
|
|
|
|
|
|
|
assert response.text == 'OK'
|
|
|
|
assert results[1] == 'favicon.ico'
|
|
|
|
|
2016-10-14 11:23:48 +01:00
|
|
|
|
|
|
|
def test_dynamic_route_int():
|
2016-10-15 03:53:49 +01:00
|
|
|
app = Sanic('test_dynamic_route_int')
|
2016-10-14 11:23:48 +01:00
|
|
|
|
2016-10-15 03:53:49 +01:00
|
|
|
results = []
|
2016-10-14 11:23:48 +01:00
|
|
|
|
2016-10-15 03:53:49 +01:00
|
|
|
@app.route('/folder/<folder_id:int>')
|
|
|
|
async def handler(request, folder_id):
|
|
|
|
results.append(folder_id)
|
|
|
|
return text('OK')
|
2016-10-14 11:23:48 +01:00
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.get('/folder/12345')
|
2016-10-15 03:53:49 +01:00
|
|
|
assert response.text == 'OK'
|
|
|
|
assert type(results[0]) is int
|
2016-10-14 11:23:48 +01:00
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.get('/folder/asdf')
|
2016-10-15 03:53:49 +01:00
|
|
|
assert response.status == 404
|
2016-10-14 11:23:48 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_dynamic_route_number():
|
2016-11-25 07:10:25 +00:00
|
|
|
app = Sanic('test_dynamic_route_number')
|
2016-10-15 03:53:49 +01:00
|
|
|
|
|
|
|
results = []
|
2016-10-14 11:23:48 +01:00
|
|
|
|
2016-10-15 03:53:49 +01:00
|
|
|
@app.route('/weight/<weight:number>')
|
|
|
|
async def handler(request, weight):
|
|
|
|
results.append(weight)
|
|
|
|
return text('OK')
|
2016-10-14 11:23:48 +01:00
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.get('/weight/12345')
|
2016-10-15 03:53:49 +01:00
|
|
|
assert response.text == 'OK'
|
|
|
|
assert type(results[0]) is float
|
2016-10-14 11:23:48 +01:00
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.get('/weight/1234.56')
|
2016-10-15 03:53:49 +01:00
|
|
|
assert response.status == 200
|
2016-10-14 11:23:48 +01:00
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.get('/weight/1234-56')
|
2016-10-15 03:53:49 +01:00
|
|
|
assert response.status == 404
|
2016-10-14 11:23:48 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_dynamic_route_regex():
|
2016-11-25 07:10:25 +00:00
|
|
|
app = Sanic('test_dynamic_route_regex')
|
2016-10-14 11:23:48 +01:00
|
|
|
|
2016-10-15 03:53:49 +01:00
|
|
|
@app.route('/folder/<folder_id:[A-Za-z0-9]{0,4}>')
|
|
|
|
async def handler(request, folder_id):
|
|
|
|
return text('OK')
|
2016-10-14 11:23:48 +01:00
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.get('/folder/test')
|
2016-10-15 03:53:49 +01:00
|
|
|
assert response.status == 200
|
2016-10-14 11:23:48 +01:00
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.get('/folder/test1')
|
2016-10-15 03:53:49 +01:00
|
|
|
assert response.status == 404
|
2016-10-14 11:23:48 +01:00
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.get('/folder/test-123')
|
2016-10-15 03:53:49 +01:00
|
|
|
assert response.status == 404
|
2016-10-14 11:23:48 +01:00
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.get('/folder/')
|
2016-10-15 03:53:49 +01:00
|
|
|
assert response.status == 200
|
2016-10-20 12:33:28 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_dynamic_route_unhashable():
|
|
|
|
app = Sanic('test_dynamic_route_unhashable')
|
|
|
|
|
|
|
|
@app.route('/folder/<unhashable:[A-Za-z0-9/]+>/end/')
|
|
|
|
async def handler(request, unhashable):
|
|
|
|
return text('OK')
|
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.get('/folder/test/asdf/end/')
|
2016-10-20 12:33:28 +01:00
|
|
|
assert response.status == 200
|
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.get('/folder/test///////end/')
|
2016-10-20 12:33:28 +01:00
|
|
|
assert response.status == 200
|
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.get('/folder/test/end/')
|
2016-10-20 12:33:28 +01:00
|
|
|
assert response.status == 200
|
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.get('/folder/test/nope/')
|
2016-10-20 12:33:28 +01:00
|
|
|
assert response.status == 404
|
|
|
|
|
|
|
|
|
|
|
|
def test_route_duplicate():
|
2016-11-25 07:10:25 +00:00
|
|
|
app = Sanic('test_route_duplicate')
|
2016-10-20 12:33:28 +01:00
|
|
|
|
|
|
|
with pytest.raises(RouteExists):
|
|
|
|
@app.route('/test')
|
|
|
|
async def handler1(request):
|
|
|
|
pass
|
|
|
|
|
|
|
|
@app.route('/test')
|
|
|
|
async def handler2(request):
|
|
|
|
pass
|
|
|
|
|
|
|
|
with pytest.raises(RouteExists):
|
|
|
|
@app.route('/test/<dynamic>/')
|
|
|
|
async def handler1(request, dynamic):
|
|
|
|
pass
|
|
|
|
|
|
|
|
@app.route('/test/<dynamic>/')
|
|
|
|
async def handler2(request, dynamic):
|
|
|
|
pass
|
2016-10-23 14:58:57 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_method_not_allowed():
|
|
|
|
app = Sanic('test_method_not_allowed')
|
|
|
|
|
|
|
|
@app.route('/test', methods=['GET'])
|
|
|
|
async def handler(request):
|
|
|
|
return text('OK')
|
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.get('/test')
|
2016-10-23 14:58:57 +01:00
|
|
|
assert response.status == 200
|
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.post('/test')
|
2016-10-23 14:58:57 +01:00
|
|
|
assert response.status == 405
|
2016-11-25 07:10:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_static_add_route():
|
|
|
|
app = Sanic('test_static_add_route')
|
|
|
|
|
|
|
|
async def handler1(request):
|
|
|
|
return text('OK1')
|
|
|
|
|
|
|
|
async def handler2(request):
|
|
|
|
return text('OK2')
|
|
|
|
|
|
|
|
app.add_route(handler1, '/test')
|
|
|
|
app.add_route(handler2, '/test2')
|
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.get('/test')
|
2016-11-25 07:10:25 +00:00
|
|
|
assert response.text == 'OK1'
|
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.get('/test2')
|
2016-11-25 07:10:25 +00:00
|
|
|
assert response.text == 'OK2'
|
|
|
|
|
|
|
|
|
|
|
|
def test_dynamic_add_route():
|
|
|
|
app = Sanic('test_dynamic_add_route')
|
|
|
|
|
|
|
|
results = []
|
|
|
|
|
|
|
|
async def handler(request, name):
|
|
|
|
results.append(name)
|
|
|
|
return text('OK')
|
|
|
|
|
|
|
|
app.add_route(handler, '/folder/<name>')
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.get('/folder/test123')
|
2016-11-25 07:10:25 +00:00
|
|
|
|
|
|
|
assert response.text == 'OK'
|
|
|
|
assert results[0] == 'test123'
|
|
|
|
|
|
|
|
|
|
|
|
def test_dynamic_add_route_string():
|
|
|
|
app = Sanic('test_dynamic_add_route_string')
|
|
|
|
|
|
|
|
results = []
|
|
|
|
|
|
|
|
async def handler(request, name):
|
|
|
|
results.append(name)
|
|
|
|
return text('OK')
|
|
|
|
|
|
|
|
app.add_route(handler, '/folder/<name:string>')
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.get('/folder/test123')
|
2016-11-25 07:10:25 +00:00
|
|
|
|
|
|
|
assert response.text == 'OK'
|
|
|
|
assert results[0] == 'test123'
|
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.get('/folder/favicon.ico')
|
2016-11-25 07:10:25 +00:00
|
|
|
|
|
|
|
assert response.text == 'OK'
|
|
|
|
assert results[1] == 'favicon.ico'
|
|
|
|
|
|
|
|
|
|
|
|
def test_dynamic_add_route_int():
|
|
|
|
app = Sanic('test_dynamic_add_route_int')
|
|
|
|
|
|
|
|
results = []
|
|
|
|
|
|
|
|
async def handler(request, folder_id):
|
|
|
|
results.append(folder_id)
|
|
|
|
return text('OK')
|
|
|
|
|
|
|
|
app.add_route(handler, '/folder/<folder_id:int>')
|
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.get('/folder/12345')
|
2016-11-25 07:10:25 +00:00
|
|
|
assert response.text == 'OK'
|
|
|
|
assert type(results[0]) is int
|
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.get('/folder/asdf')
|
2016-11-25 07:10:25 +00:00
|
|
|
assert response.status == 404
|
|
|
|
|
|
|
|
|
|
|
|
def test_dynamic_add_route_number():
|
|
|
|
app = Sanic('test_dynamic_add_route_number')
|
|
|
|
|
|
|
|
results = []
|
|
|
|
|
|
|
|
async def handler(request, weight):
|
|
|
|
results.append(weight)
|
|
|
|
return text('OK')
|
|
|
|
|
|
|
|
app.add_route(handler, '/weight/<weight:number>')
|
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.get('/weight/12345')
|
2016-11-25 07:10:25 +00:00
|
|
|
assert response.text == 'OK'
|
|
|
|
assert type(results[0]) is float
|
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.get('/weight/1234.56')
|
2016-11-25 07:10:25 +00:00
|
|
|
assert response.status == 200
|
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.get('/weight/1234-56')
|
2016-11-25 07:10:25 +00:00
|
|
|
assert response.status == 404
|
|
|
|
|
|
|
|
|
|
|
|
def test_dynamic_add_route_regex():
|
|
|
|
app = Sanic('test_dynamic_route_int')
|
|
|
|
|
|
|
|
async def handler(request, folder_id):
|
|
|
|
return text('OK')
|
|
|
|
|
|
|
|
app.add_route(handler, '/folder/<folder_id:[A-Za-z0-9]{0,4}>')
|
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.get('/folder/test')
|
2016-11-25 07:10:25 +00:00
|
|
|
assert response.status == 200
|
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.get('/folder/test1')
|
2016-11-25 07:10:25 +00:00
|
|
|
assert response.status == 404
|
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.get('/folder/test-123')
|
2016-11-25 07:10:25 +00:00
|
|
|
assert response.status == 404
|
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.get('/folder/')
|
2016-11-25 07:10:25 +00:00
|
|
|
assert response.status == 200
|
|
|
|
|
|
|
|
|
|
|
|
def test_dynamic_add_route_unhashable():
|
|
|
|
app = Sanic('test_dynamic_add_route_unhashable')
|
|
|
|
|
|
|
|
async def handler(request, unhashable):
|
|
|
|
return text('OK')
|
|
|
|
|
|
|
|
app.add_route(handler, '/folder/<unhashable:[A-Za-z0-9/]+>/end/')
|
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.get('/folder/test/asdf/end/')
|
2016-11-25 07:10:25 +00:00
|
|
|
assert response.status == 200
|
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.get('/folder/test///////end/')
|
2016-11-25 07:10:25 +00:00
|
|
|
assert response.status == 200
|
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.get('/folder/test/end/')
|
2016-11-25 07:10:25 +00:00
|
|
|
assert response.status == 200
|
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.get('/folder/test/nope/')
|
2016-11-25 07:10:25 +00:00
|
|
|
assert response.status == 404
|
|
|
|
|
|
|
|
|
|
|
|
def test_add_route_duplicate():
|
|
|
|
app = Sanic('test_add_route_duplicate')
|
|
|
|
|
|
|
|
with pytest.raises(RouteExists):
|
|
|
|
async def handler1(request):
|
|
|
|
pass
|
|
|
|
|
|
|
|
async def handler2(request):
|
|
|
|
pass
|
|
|
|
|
|
|
|
app.add_route(handler1, '/test')
|
|
|
|
app.add_route(handler2, '/test')
|
|
|
|
|
|
|
|
with pytest.raises(RouteExists):
|
|
|
|
async def handler1(request, dynamic):
|
|
|
|
pass
|
|
|
|
|
|
|
|
async def handler2(request, dynamic):
|
|
|
|
pass
|
|
|
|
|
|
|
|
app.add_route(handler1, '/test/<dynamic>/')
|
|
|
|
app.add_route(handler2, '/test/<dynamic>/')
|
|
|
|
|
|
|
|
|
|
|
|
def test_add_route_method_not_allowed():
|
|
|
|
app = Sanic('test_add_route_method_not_allowed')
|
|
|
|
|
|
|
|
async def handler(request):
|
|
|
|
return text('OK')
|
|
|
|
|
|
|
|
app.add_route(handler, '/test', methods=['GET'])
|
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.get('/test')
|
2016-11-25 07:10:25 +00:00
|
|
|
assert response.status == 200
|
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.post('/test')
|
2016-11-25 07:10:25 +00:00
|
|
|
assert response.status == 405
|
2016-12-30 09:36:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_remove_static_route():
|
|
|
|
app = Sanic('test_remove_static_route')
|
|
|
|
|
|
|
|
async def handler1(request):
|
|
|
|
return text('OK1')
|
|
|
|
|
|
|
|
async def handler2(request):
|
|
|
|
return text('OK2')
|
|
|
|
|
|
|
|
app.add_route(handler1, '/test')
|
|
|
|
app.add_route(handler2, '/test2')
|
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.get('/test')
|
2016-12-30 09:36:57 +00:00
|
|
|
assert response.status == 200
|
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.get('/test2')
|
2016-12-30 09:36:57 +00:00
|
|
|
assert response.status == 200
|
|
|
|
|
|
|
|
app.remove_route('/test')
|
|
|
|
app.remove_route('/test2')
|
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.get('/test')
|
2016-12-30 09:36:57 +00:00
|
|
|
assert response.status == 404
|
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.get('/test2')
|
2016-12-30 09:36:57 +00:00
|
|
|
assert response.status == 404
|
|
|
|
|
|
|
|
|
|
|
|
def test_remove_dynamic_route():
|
|
|
|
app = Sanic('test_remove_dynamic_route')
|
|
|
|
|
|
|
|
async def handler(request, name):
|
|
|
|
return text('OK')
|
|
|
|
|
|
|
|
app.add_route(handler, '/folder/<name>')
|
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.get('/folder/test123')
|
2016-12-30 09:36:57 +00:00
|
|
|
assert response.status == 200
|
|
|
|
|
|
|
|
app.remove_route('/folder/<name>')
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.get('/folder/test123')
|
2016-12-30 09:36:57 +00:00
|
|
|
assert response.status == 404
|
|
|
|
|
|
|
|
|
|
|
|
def test_remove_inexistent_route():
|
|
|
|
app = Sanic('test_remove_inexistent_route')
|
|
|
|
|
|
|
|
with pytest.raises(RouteDoesNotExist):
|
|
|
|
app.remove_route('/test')
|
|
|
|
|
|
|
|
|
|
|
|
def test_remove_unhashable_route():
|
|
|
|
app = Sanic('test_remove_unhashable_route')
|
|
|
|
|
|
|
|
async def handler(request, unhashable):
|
|
|
|
return text('OK')
|
|
|
|
|
|
|
|
app.add_route(handler, '/folder/<unhashable:[A-Za-z0-9/]+>/end/')
|
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.get('/folder/test/asdf/end/')
|
2016-12-30 09:36:57 +00:00
|
|
|
assert response.status == 200
|
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.get('/folder/test///////end/')
|
2016-12-30 09:36:57 +00:00
|
|
|
assert response.status == 200
|
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.get('/folder/test/end/')
|
2016-12-30 09:36:57 +00:00
|
|
|
assert response.status == 200
|
|
|
|
|
|
|
|
app.remove_route('/folder/<unhashable:[A-Za-z0-9/]+>/end/')
|
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.get('/folder/test/asdf/end/')
|
2016-12-30 09:36:57 +00:00
|
|
|
assert response.status == 404
|
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.get('/folder/test///////end/')
|
2016-12-30 09:36:57 +00:00
|
|
|
assert response.status == 404
|
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.get('/folder/test/end/')
|
2016-12-30 09:36:57 +00:00
|
|
|
assert response.status == 404
|
|
|
|
|
|
|
|
|
|
|
|
def test_remove_route_without_clean_cache():
|
|
|
|
app = Sanic('test_remove_static_route')
|
|
|
|
|
|
|
|
async def handler(request):
|
|
|
|
return text('OK')
|
|
|
|
|
|
|
|
app.add_route(handler, '/test')
|
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.get('/test')
|
2016-12-30 09:36:57 +00:00
|
|
|
assert response.status == 200
|
|
|
|
|
|
|
|
app.remove_route('/test', clean_cache=True)
|
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.get('/test')
|
2016-12-30 09:36:57 +00:00
|
|
|
assert response.status == 404
|
|
|
|
|
|
|
|
app.add_route(handler, '/test')
|
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.get('/test')
|
2016-12-30 09:36:57 +00:00
|
|
|
assert response.status == 200
|
|
|
|
|
|
|
|
app.remove_route('/test', clean_cache=False)
|
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.get('/test')
|
2016-12-30 09:36:57 +00:00
|
|
|
assert response.status == 200
|
2017-01-13 12:18:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_overload_routes():
|
|
|
|
app = Sanic('test_dynamic_route')
|
|
|
|
|
|
|
|
@app.route('/overload', methods=['GET'])
|
|
|
|
async def handler1(request):
|
|
|
|
return text('OK1')
|
|
|
|
|
|
|
|
@app.route('/overload', methods=['POST', 'PUT'])
|
|
|
|
async def handler2(request):
|
|
|
|
return text('OK2')
|
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.get('/overload')
|
2017-01-13 12:18:28 +00:00
|
|
|
assert response.text == 'OK1'
|
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.post('/overload')
|
2017-01-13 12:18:28 +00:00
|
|
|
assert response.text == 'OK2'
|
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.put('/overload')
|
2017-01-13 12:18:28 +00:00
|
|
|
assert response.text == 'OK2'
|
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.delete('/overload')
|
2017-01-13 12:18:28 +00:00
|
|
|
assert response.status == 405
|
|
|
|
|
|
|
|
with pytest.raises(RouteExists):
|
|
|
|
@app.route('/overload', methods=['PUT', 'DELETE'])
|
|
|
|
async def handler3(request):
|
|
|
|
return text('Duplicated')
|
|
|
|
|
|
|
|
|
|
|
|
def test_unmergeable_overload_routes():
|
|
|
|
app = Sanic('test_dynamic_route')
|
|
|
|
|
2017-01-19 14:56:51 +00:00
|
|
|
@app.route('/overload_whole', methods=None)
|
2017-01-13 12:18:28 +00:00
|
|
|
async def handler1(request):
|
|
|
|
return text('OK1')
|
|
|
|
|
|
|
|
with pytest.raises(RouteExists):
|
|
|
|
@app.route('/overload_whole', methods=['POST', 'PUT'])
|
|
|
|
async def handler2(request):
|
|
|
|
return text('Duplicated')
|
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.get('/overload_whole')
|
2017-01-13 12:18:28 +00:00
|
|
|
assert response.text == 'OK1'
|
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.post('/overload_whole')
|
2017-01-13 12:18:28 +00:00
|
|
|
assert response.text == 'OK1'
|
|
|
|
|
|
|
|
|
|
|
|
@app.route('/overload_part', methods=['GET'])
|
|
|
|
async def handler1(request):
|
|
|
|
return text('OK1')
|
|
|
|
|
|
|
|
with pytest.raises(RouteExists):
|
|
|
|
@app.route('/overload_part')
|
|
|
|
async def handler2(request):
|
|
|
|
return text('Duplicated')
|
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.get('/overload_part')
|
2017-01-13 12:18:28 +00:00
|
|
|
assert response.text == 'OK1'
|
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.post('/overload_part')
|
2017-01-13 12:18:28 +00:00
|
|
|
assert response.status == 405
|