2016-10-14 11:23:48 +01:00
|
|
|
from json import loads as json_loads, dumps as json_dumps
|
|
|
|
from sanic import Sanic
|
|
|
|
from sanic.response import json, text
|
2016-10-14 23:38:43 +01:00
|
|
|
from sanic.utils import sanic_endpoint_test
|
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
|
|
|
|
# ------------------------------------------------------------ #
|
|
|
|
|
|
|
|
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
|
|
|
|
2016-10-15 03:53:49 +01:00
|
|
|
request, response = sanic_endpoint_test(app, uri='/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
|
|
|
|
2016-10-15 03:53:49 +01:00
|
|
|
request, response = sanic_endpoint_test(app, uri='/folder/test123')
|
|
|
|
|
|
|
|
assert response.text == 'OK'
|
|
|
|
assert results[0] == 'test123'
|
2016-10-14 11:23:48 +01:00
|
|
|
|
2016-10-16 21:41:56 +01:00
|
|
|
request, response = sanic_endpoint_test(app, uri='/folder/favicon.ico')
|
|
|
|
|
|
|
|
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
|
|
|
|
2016-10-15 03:53:49 +01:00
|
|
|
request, response = sanic_endpoint_test(app, uri='/folder/12345')
|
|
|
|
assert response.text == 'OK'
|
|
|
|
assert type(results[0]) is int
|
2016-10-14 11:23:48 +01:00
|
|
|
|
2016-10-15 03:53:49 +01:00
|
|
|
request, response = sanic_endpoint_test(app, uri='/folder/asdf')
|
|
|
|
assert response.status == 404
|
2016-10-14 11:23:48 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_dynamic_route_number():
|
2016-10-15 03:53:49 +01:00
|
|
|
app = Sanic('test_dynamic_route_int')
|
|
|
|
|
|
|
|
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
|
|
|
|
2016-10-15 03:53:49 +01:00
|
|
|
request, response = sanic_endpoint_test(app, uri='/weight/12345')
|
|
|
|
assert response.text == 'OK'
|
|
|
|
assert type(results[0]) is float
|
2016-10-14 11:23:48 +01:00
|
|
|
|
2016-10-15 03:53:49 +01:00
|
|
|
request, response = sanic_endpoint_test(app, uri='/weight/1234.56')
|
|
|
|
assert response.status == 200
|
2016-10-14 11:23:48 +01:00
|
|
|
|
2016-10-15 03:53:49 +01:00
|
|
|
request, response = sanic_endpoint_test(app, uri='/weight/1234-56')
|
|
|
|
assert response.status == 404
|
2016-10-14 11:23:48 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_dynamic_route_regex():
|
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
|
|
|
@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
|
|
|
|
2016-10-15 03:53:49 +01:00
|
|
|
request, response = sanic_endpoint_test(app, uri='/folder/test')
|
|
|
|
assert response.status == 200
|
2016-10-14 11:23:48 +01:00
|
|
|
|
2016-10-15 03:53:49 +01:00
|
|
|
request, response = sanic_endpoint_test(app, uri='/folder/test1')
|
|
|
|
assert response.status == 404
|
2016-10-14 11:23:48 +01:00
|
|
|
|
2016-10-15 03:53:49 +01:00
|
|
|
request, response = sanic_endpoint_test(app, uri='/folder/test-123')
|
|
|
|
assert response.status == 404
|
2016-10-14 11:23:48 +01:00
|
|
|
|
2016-10-15 03:53:49 +01:00
|
|
|
request, response = sanic_endpoint_test(app, uri='/folder/')
|
|
|
|
assert response.status == 200
|