sanic/tests/test_requests.py

197 lines
4.8 KiB
Python
Raw Normal View History

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
from sanic.exceptions import ServerError
# ------------------------------------------------------------ #
# GET
# ------------------------------------------------------------ #
def test_sync():
app = Sanic('test_text')
@app.route('/')
def handler(request):
return text('Hello')
request, response = sanic_endpoint_test(app)
assert response.text == 'Hello'
def test_text():
app = Sanic('test_text')
@app.route('/')
async def handler(request):
return text('Hello')
request, response = sanic_endpoint_test(app)
assert response.text == 'Hello'
def test_invalid_response():
app = Sanic('test_invalid_response')
@app.exception(ServerError)
def handler_exception(request, exception):
return text('Internal Server Error.', 500)
@app.route('/')
async def handler(request):
return 'This should fail'
request, response = sanic_endpoint_test(app)
assert response.status == 500
assert response.text == "Internal Server Error."
def test_json():
app = Sanic('test_json')
@app.route('/')
async def handler(request):
return json({"test": True})
request, response = sanic_endpoint_test(app)
try:
results = json_loads(response.text)
except:
raise ValueError("Expected JSON response but got '{}'".format(response))
assert results.get('test') == True
2016-12-08 04:33:56 +00:00
def test_invalid_json():
app = Sanic('test_json')
@app.route('/')
async def handler(request):
return json(request.json())
data = "I am not json"
request, response = sanic_endpoint_test(app, data=data)
assert response.status == 400
def test_query_string():
app = Sanic('test_query_string')
@app.route('/')
async def handler(request):
return text('OK')
2016-11-05 18:12:55 +00:00
request, response = sanic_endpoint_test(app, params=[("test1", "1"), ("test2", "false"), ("test2", "true")])
assert request.args.get('test1') == '1'
assert request.args.get('test2') == 'false'
# ------------------------------------------------------------ #
# POST
# ------------------------------------------------------------ #
def test_post_json():
app = Sanic('test_post_json')
@app.route('/')
async def handler(request):
return text('OK')
payload = {'test': 'OK'}
headers = {'content-type': 'application/json'}
request, response = sanic_endpoint_test(app, data=json_dumps(payload), headers=headers)
assert request.json.get('test') == 'OK'
assert response.text == 'OK'
2016-10-19 08:23:44 +01:00
def test_post_token():
app = Sanic('test_post_token')
@app.route('/')
async def handler(request):
return text('OK')
payload = {'test': 'OK'}
# uuid4 generated token.
token = 'a1d895e0-553a-421a-8e22-5ff8ecb48cbf'
headers = {
'content-type': 'application/json',
'Authorization': 'Token {}'.format(token)
}
request, response = sanic_endpoint_test(app, data=json_dumps(payload), headers=headers)
assert request.token == token
assert response.text == 'OK'
# uuid4 generated token.
token = 'a1d895e0-553a-421a-8e22'
headers = {
'content-type': 'application/json',
'Authorization': 'Bearer {}'.format(token)
}
request, response = sanic_endpoint_test(app, data=json_dumps(payload), headers=headers)
assert request.token == token
assert response.text == 'OK'
# uuid4 generated token.
token = '421a-8e22-5ff8ecb48cbf'
headers = {
'content-type': 'application/json',
'Authorization': 'Basic {}'.format(token)
}
request, response = sanic_endpoint_test(app, data=json_dumps(payload), headers=headers)
assert request.token == token
assert response.text == 'OK'
2016-10-19 08:23:44 +01:00
def test_post_form_urlencoded():
app = Sanic('test_post_form_urlencoded')
@app.route('/')
async def handler(request):
return text('OK')
payload = 'test=OK'
headers = {'content-type': 'application/x-www-form-urlencoded'}
request, response = sanic_endpoint_test(app, data=payload, headers=headers)
assert request.form.get('test') == 'OK'
def test_post_form_multipart_form_data():
app = Sanic('test_post_form_multipart_form_data')
@app.route('/')
async def handler(request):
return text('OK')
payload = '------sanic\r\n' \
'Content-Disposition: form-data; name="test"\r\n' \
'\r\n' \
'OK\r\n' \
'------sanic--\r\n'
headers = {'content-type': 'multipart/form-data; boundary=----sanic'}
request, response = sanic_endpoint_test(app, data=payload, headers=headers)
assert request.form.get('test') == 'OK'