2016-10-14 11:23:48 +01:00
|
|
|
from json import loads as json_loads, dumps as json_dumps
|
2017-03-03 16:44:50 +00:00
|
|
|
from urllib.parse import urlparse
|
|
|
|
import os
|
|
|
|
import ssl
|
2017-01-31 01:04:51 +00:00
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
2016-10-14 11:23:48 +01:00
|
|
|
from sanic import Sanic
|
2017-01-31 01:04:51 +00:00
|
|
|
from sanic.exceptions import ServerError
|
2017-03-03 16:44:50 +00:00
|
|
|
from sanic.response import json, text
|
|
|
|
|
|
|
|
from sanic.testing import HOST, PORT
|
2016-10-14 11:23:48 +01:00
|
|
|
|
2016-10-15 03:53:49 +01:00
|
|
|
|
2016-10-14 11:23:48 +01:00
|
|
|
# ------------------------------------------------------------ #
|
|
|
|
# GET
|
|
|
|
# ------------------------------------------------------------ #
|
|
|
|
|
|
|
|
def test_sync():
|
2016-10-15 03:53:49 +01:00
|
|
|
app = Sanic('test_text')
|
|
|
|
|
|
|
|
@app.route('/')
|
|
|
|
def handler(request):
|
|
|
|
return text('Hello')
|
2016-10-14 11:23:48 +01:00
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.get('/')
|
2016-10-14 11:23:48 +01:00
|
|
|
|
2016-10-15 03:53:49 +01:00
|
|
|
assert response.text == 'Hello'
|
2016-10-14 11:23:48 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_text():
|
2016-10-15 03:53:49 +01:00
|
|
|
app = Sanic('test_text')
|
2016-10-14 11:23:48 +01:00
|
|
|
|
2016-10-15 03:53:49 +01:00
|
|
|
@app.route('/')
|
|
|
|
async def handler(request):
|
|
|
|
return text('Hello')
|
2016-10-14 11:23:48 +01:00
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.get('/')
|
2016-10-14 11:23:48 +01:00
|
|
|
|
2016-10-15 03:53:49 +01:00
|
|
|
assert response.text == 'Hello'
|
2016-10-14 11:23:48 +01:00
|
|
|
|
|
|
|
|
2016-12-14 05:24:26 +00:00
|
|
|
def test_headers():
|
|
|
|
app = Sanic('test_text')
|
|
|
|
|
|
|
|
@app.route('/')
|
|
|
|
async def handler(request):
|
|
|
|
headers = {"spam": "great"}
|
|
|
|
return text('Hello', headers=headers)
|
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.get('/')
|
2016-12-14 05:24:26 +00:00
|
|
|
|
|
|
|
assert response.headers.get('spam') == 'great'
|
|
|
|
|
|
|
|
|
2016-12-30 18:15:08 +00:00
|
|
|
def test_non_str_headers():
|
2016-12-14 05:24:26 +00:00
|
|
|
app = Sanic('test_text')
|
|
|
|
|
|
|
|
@app.route('/')
|
|
|
|
async def handler(request):
|
|
|
|
headers = {"answer": 42}
|
|
|
|
return text('Hello', headers=headers)
|
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.get('/')
|
2016-12-14 05:24:26 +00:00
|
|
|
|
|
|
|
assert response.headers.get('answer') == '42'
|
Fix sanic_endpoint_test working with redirects
Before fix, it raises error like:
```
tests/test_utils.py F
================================= FAILURES =================================
______________________________ test_redirect _______________________________
app = <sanic.sanic.Sanic object at 0x1045fda20>, method = 'get', uri = '/1', gather_request = True, debug = False
server_kwargs = {}, request_args = (), request_kwargs = {}
_collect_request = <function sanic_endpoint_test.<locals>._collect_request at 0x1045ec950>
_collect_response = <function sanic_endpoint_test.<locals>._collect_response at 0x1045ec7b8>
def sanic_endpoint_test(app, method='get', uri='/', gather_request=True,
debug=False, server_kwargs={},
*request_args, **request_kwargs):
results = []
exceptions = []
if gather_request:
def _collect_request(request):
results.append(request)
app.request_middleware.appendleft(_collect_request)
async def _collect_response(sanic, loop):
try:
response = await local_request(method, uri, *request_args,
**request_kwargs)
results.append(response)
except Exception as e:
exceptions.append(e)
app.stop()
app.run(host=HOST, debug=debug, port=PORT,
after_start=_collect_response, **server_kwargs)
if exceptions:
raise ValueError("Exception during request: {}".format(exceptions))
if gather_request:
try:
> request, response = results
E ValueError: too many values to unpack (expected 2)
sanic/utils.py:47: ValueError
During handling of the above exception, another exception occurred:
utils_app = <sanic.sanic.Sanic object at 0x1045fda20>
def test_redirect(utils_app):
"""Test sanic_endpoint_test is working for redirection"""
> request, response = sanic_endpoint_test(utils_app, uri='/1')
tests/test_utils.py:33:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
app = <sanic.sanic.Sanic object at 0x1045fda20>, method = 'get', uri = '/1', gather_request = True, debug = False
server_kwargs = {}, request_args = (), request_kwargs = {}
_collect_request = <function sanic_endpoint_test.<locals>._collect_request at 0x1045ec950>
_collect_response = <function sanic_endpoint_test.<locals>._collect_response at 0x1045ec7b8>
def sanic_endpoint_test(app, method='get', uri='/', gather_request=True,
debug=False, server_kwargs={},
*request_args, **request_kwargs):
results = []
exceptions = []
if gather_request:
def _collect_request(request):
results.append(request)
app.request_middleware.appendleft(_collect_request)
async def _collect_response(sanic, loop):
try:
response = await local_request(method, uri, *request_args,
**request_kwargs)
results.append(response)
except Exception as e:
exceptions.append(e)
app.stop()
app.run(host=HOST, debug=debug, port=PORT,
after_start=_collect_response, **server_kwargs)
if exceptions:
raise ValueError("Exception during request: {}".format(exceptions))
if gather_request:
try:
request, response = results
return request, response
except:
raise ValueError(
"Request and response object expected, got ({})".format(
> results))
E ValueError: Request and response object expected, got ([{}, {}, {}, <ClientResponse(http://127.0.0.1:42101/3) [200 OK]>
E <CIMultiDictProxy('Content-Type': 'text/plain; charset=utf-8', 'Content-Length': '2', 'Connection': 'keep-alive', 'Keep-Alive': 'timeout=1')>
E ])
sanic/utils.py:52: ValueError
```
2017-02-02 09:51:33 +00:00
|
|
|
|
2016-12-13 20:20:16 +00:00
|
|
|
def test_invalid_response():
|
|
|
|
app = Sanic('test_invalid_response')
|
2016-12-14 05:24:26 +00:00
|
|
|
|
2016-12-13 20:20:16 +00:00
|
|
|
@app.exception(ServerError)
|
|
|
|
def handler_exception(request, exception):
|
|
|
|
return text('Internal Server Error.', 500)
|
2016-12-14 05:24:26 +00:00
|
|
|
|
2016-12-13 20:20:16 +00:00
|
|
|
@app.route('/')
|
|
|
|
async def handler(request):
|
|
|
|
return 'This should fail'
|
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.get('/')
|
2016-12-13 20:20:16 +00:00
|
|
|
assert response.status == 500
|
|
|
|
assert response.text == "Internal Server Error."
|
Fix sanic_endpoint_test working with redirects
Before fix, it raises error like:
```
tests/test_utils.py F
================================= FAILURES =================================
______________________________ test_redirect _______________________________
app = <sanic.sanic.Sanic object at 0x1045fda20>, method = 'get', uri = '/1', gather_request = True, debug = False
server_kwargs = {}, request_args = (), request_kwargs = {}
_collect_request = <function sanic_endpoint_test.<locals>._collect_request at 0x1045ec950>
_collect_response = <function sanic_endpoint_test.<locals>._collect_response at 0x1045ec7b8>
def sanic_endpoint_test(app, method='get', uri='/', gather_request=True,
debug=False, server_kwargs={},
*request_args, **request_kwargs):
results = []
exceptions = []
if gather_request:
def _collect_request(request):
results.append(request)
app.request_middleware.appendleft(_collect_request)
async def _collect_response(sanic, loop):
try:
response = await local_request(method, uri, *request_args,
**request_kwargs)
results.append(response)
except Exception as e:
exceptions.append(e)
app.stop()
app.run(host=HOST, debug=debug, port=PORT,
after_start=_collect_response, **server_kwargs)
if exceptions:
raise ValueError("Exception during request: {}".format(exceptions))
if gather_request:
try:
> request, response = results
E ValueError: too many values to unpack (expected 2)
sanic/utils.py:47: ValueError
During handling of the above exception, another exception occurred:
utils_app = <sanic.sanic.Sanic object at 0x1045fda20>
def test_redirect(utils_app):
"""Test sanic_endpoint_test is working for redirection"""
> request, response = sanic_endpoint_test(utils_app, uri='/1')
tests/test_utils.py:33:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
app = <sanic.sanic.Sanic object at 0x1045fda20>, method = 'get', uri = '/1', gather_request = True, debug = False
server_kwargs = {}, request_args = (), request_kwargs = {}
_collect_request = <function sanic_endpoint_test.<locals>._collect_request at 0x1045ec950>
_collect_response = <function sanic_endpoint_test.<locals>._collect_response at 0x1045ec7b8>
def sanic_endpoint_test(app, method='get', uri='/', gather_request=True,
debug=False, server_kwargs={},
*request_args, **request_kwargs):
results = []
exceptions = []
if gather_request:
def _collect_request(request):
results.append(request)
app.request_middleware.appendleft(_collect_request)
async def _collect_response(sanic, loop):
try:
response = await local_request(method, uri, *request_args,
**request_kwargs)
results.append(response)
except Exception as e:
exceptions.append(e)
app.stop()
app.run(host=HOST, debug=debug, port=PORT,
after_start=_collect_response, **server_kwargs)
if exceptions:
raise ValueError("Exception during request: {}".format(exceptions))
if gather_request:
try:
request, response = results
return request, response
except:
raise ValueError(
"Request and response object expected, got ({})".format(
> results))
E ValueError: Request and response object expected, got ([{}, {}, {}, <ClientResponse(http://127.0.0.1:42101/3) [200 OK]>
E <CIMultiDictProxy('Content-Type': 'text/plain; charset=utf-8', 'Content-Length': '2', 'Connection': 'keep-alive', 'Keep-Alive': 'timeout=1')>
E ])
sanic/utils.py:52: ValueError
```
2017-02-02 09:51:33 +00:00
|
|
|
|
|
|
|
|
2016-10-14 11:23:48 +01:00
|
|
|
def test_json():
|
2016-10-15 03:53:49 +01:00
|
|
|
app = Sanic('test_json')
|
2016-10-14 11:23:48 +01:00
|
|
|
|
2016-10-15 03:53:49 +01:00
|
|
|
@app.route('/')
|
|
|
|
async def handler(request):
|
|
|
|
return json({"test": True})
|
2016-10-14 11:23:48 +01:00
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.get('/')
|
2016-10-14 11:23:48 +01:00
|
|
|
|
2017-03-06 08:33:34 +00:00
|
|
|
results = json_loads(response.text)
|
2016-10-14 11:23:48 +01:00
|
|
|
|
2016-10-15 03:53:49 +01:00
|
|
|
assert results.get('test') == True
|
2016-10-14 11:23:48 +01:00
|
|
|
|
2017-03-06 08:33:34 +00:00
|
|
|
def test_empty_json():
|
|
|
|
app = Sanic('test_json')
|
|
|
|
|
|
|
|
@app.route('/')
|
|
|
|
async def handler(request):
|
|
|
|
assert request.json == None
|
|
|
|
return json(request.json)
|
|
|
|
|
|
|
|
request, response = app.test_client.get('/')
|
|
|
|
assert response.status == 200
|
|
|
|
assert response.text == 'null'
|
2016-10-14 11:23:48 +01:00
|
|
|
|
2016-12-08 04:33:56 +00:00
|
|
|
def test_invalid_json():
|
|
|
|
app = Sanic('test_json')
|
|
|
|
|
|
|
|
@app.route('/')
|
|
|
|
async def handler(request):
|
2017-03-06 08:33:34 +00:00
|
|
|
return json(request.json)
|
2016-12-08 04:33:56 +00:00
|
|
|
|
|
|
|
data = "I am not json"
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.get('/', data=data)
|
2016-12-08 04:33:56 +00:00
|
|
|
|
|
|
|
assert response.status == 400
|
|
|
|
|
|
|
|
|
2016-10-14 11:23:48 +01:00
|
|
|
def test_query_string():
|
2016-10-15 03:53:49 +01:00
|
|
|
app = Sanic('test_query_string')
|
|
|
|
|
|
|
|
@app.route('/')
|
|
|
|
async def handler(request):
|
|
|
|
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(
|
|
|
|
'/', params=[("test1", "1"), ("test2", "false"), ("test2", "true")])
|
2016-10-14 11:23:48 +01:00
|
|
|
|
2016-10-15 03:53:49 +01:00
|
|
|
assert request.args.get('test1') == '1'
|
|
|
|
assert request.args.get('test2') == 'false'
|
2016-10-14 11:23:48 +01:00
|
|
|
|
|
|
|
|
2016-12-26 11:48:53 +00:00
|
|
|
def test_token():
|
|
|
|
app = Sanic('test_post_token')
|
|
|
|
|
|
|
|
@app.route('/')
|
|
|
|
async def handler(request):
|
|
|
|
return text('OK')
|
|
|
|
|
|
|
|
# uuid4 generated token.
|
2017-04-17 16:11:08 +01:00
|
|
|
token = 'Token a1d895e0-553a-421a-8e22-5ff8ecb48cbf'
|
2016-12-26 11:48:53 +00:00
|
|
|
headers = {
|
|
|
|
'content-type': 'application/json',
|
2017-04-17 16:11:08 +01:00
|
|
|
'Authorization': '{}'.format(token)
|
2016-12-26 11:48:53 +00:00
|
|
|
}
|
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.get('/', headers=headers)
|
2016-12-26 11:48:53 +00:00
|
|
|
|
|
|
|
assert request.token == token
|
|
|
|
|
2016-10-14 11:23:48 +01:00
|
|
|
# ------------------------------------------------------------ #
|
|
|
|
# POST
|
|
|
|
# ------------------------------------------------------------ #
|
|
|
|
|
|
|
|
def test_post_json():
|
2016-10-15 03:53:49 +01:00
|
|
|
app = Sanic('test_post_json')
|
2016-10-14 11:23:48 +01:00
|
|
|
|
2017-02-23 02:36:08 +00:00
|
|
|
@app.route('/', methods=['POST'])
|
2016-10-15 03:53:49 +01:00
|
|
|
async def handler(request):
|
|
|
|
return text('OK')
|
2016-10-14 11:23:48 +01:00
|
|
|
|
2016-10-15 03:53:49 +01:00
|
|
|
payload = {'test': 'OK'}
|
|
|
|
headers = {'content-type': 'application/json'}
|
2016-10-14 11:23:48 +01:00
|
|
|
|
2017-02-23 02:36:08 +00:00
|
|
|
request, response = app.test_client.post(
|
2017-02-14 19:51:20 +00:00
|
|
|
'/', data=json_dumps(payload), headers=headers)
|
2016-10-14 11:23:48 +01:00
|
|
|
|
2016-10-15 03:53:49 +01:00
|
|
|
assert request.json.get('test') == 'OK'
|
|
|
|
assert response.text == 'OK'
|
2016-10-19 08:23:44 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_post_form_urlencoded():
|
|
|
|
app = Sanic('test_post_form_urlencoded')
|
|
|
|
|
2017-02-23 02:36:08 +00:00
|
|
|
@app.route('/', methods=['POST'])
|
2016-10-19 08:23:44 +01:00
|
|
|
async def handler(request):
|
|
|
|
return text('OK')
|
|
|
|
|
|
|
|
payload = 'test=OK'
|
|
|
|
headers = {'content-type': 'application/x-www-form-urlencoded'}
|
|
|
|
|
2017-02-23 02:36:08 +00:00
|
|
|
request, response = app.test_client.post('/', data=payload, headers=headers)
|
2016-10-19 08:23:44 +01:00
|
|
|
|
|
|
|
assert request.form.get('test') == 'OK'
|
|
|
|
|
|
|
|
|
|
|
|
def test_post_form_multipart_form_data():
|
|
|
|
app = Sanic('test_post_form_multipart_form_data')
|
|
|
|
|
2017-02-23 02:36:08 +00:00
|
|
|
@app.route('/', methods=['POST'])
|
2016-10-19 08:23:44 +01:00
|
|
|
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'}
|
|
|
|
|
2017-02-23 02:36:08 +00:00
|
|
|
request, response = app.test_client.post(data=payload, headers=headers)
|
2016-10-19 08:23:44 +01:00
|
|
|
|
|
|
|
assert request.form.get('test') == 'OK'
|
2017-03-03 16:44:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
'path,query,expected_url', [
|
|
|
|
('/foo', '', 'http://{}:{}/foo'),
|
|
|
|
('/bar/baz', '', 'http://{}:{}/bar/baz'),
|
|
|
|
('/moo/boo', 'arg1=val1', 'http://{}:{}/moo/boo?arg1=val1')
|
|
|
|
])
|
|
|
|
def test_url_attributes_no_ssl(path, query, expected_url):
|
|
|
|
app = Sanic('test_url_attrs_no_ssl')
|
|
|
|
|
|
|
|
async def handler(request):
|
|
|
|
return text('OK')
|
|
|
|
|
|
|
|
app.add_route(handler, path)
|
|
|
|
|
|
|
|
request, response = app.test_client.get(path + '?{}'.format(query))
|
|
|
|
assert request.url == expected_url.format(HOST, PORT)
|
|
|
|
|
|
|
|
parsed = urlparse(request.url)
|
|
|
|
|
|
|
|
assert parsed.scheme == request.scheme
|
|
|
|
assert parsed.path == request.path
|
|
|
|
assert parsed.query == request.query_string
|
2017-03-03 18:32:32 +00:00
|
|
|
assert parsed.netloc == request.host
|
2017-03-03 16:44:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
'path,query,expected_url', [
|
|
|
|
('/foo', '', 'https://{}:{}/foo'),
|
|
|
|
('/bar/baz', '', 'https://{}:{}/bar/baz'),
|
|
|
|
('/moo/boo', 'arg1=val1', 'https://{}:{}/moo/boo?arg1=val1')
|
|
|
|
])
|
|
|
|
def test_url_attributes_with_ssl(path, query, expected_url):
|
|
|
|
app = Sanic('test_url_attrs_with_ssl')
|
|
|
|
|
|
|
|
current_dir = os.path.dirname(os.path.realpath(__file__))
|
|
|
|
context = ssl.create_default_context(purpose=ssl.Purpose.CLIENT_AUTH)
|
|
|
|
context.load_cert_chain(
|
|
|
|
os.path.join(current_dir, 'certs/selfsigned.cert'),
|
|
|
|
keyfile=os.path.join(current_dir, 'certs/selfsigned.key'))
|
|
|
|
|
|
|
|
async def handler(request):
|
|
|
|
return text('OK')
|
|
|
|
|
|
|
|
app.add_route(handler, path)
|
|
|
|
|
|
|
|
request, response = app.test_client.get(
|
|
|
|
'https://{}:{}'.format(HOST, PORT) + path + '?{}'.format(query),
|
|
|
|
server_kwargs={'ssl': context})
|
|
|
|
assert request.url == expected_url.format(HOST, PORT)
|
|
|
|
|
|
|
|
parsed = urlparse(request.url)
|
|
|
|
|
|
|
|
assert parsed.scheme == request.scheme
|
|
|
|
assert parsed.path == request.path
|
|
|
|
assert parsed.query == request.query_string
|
2017-03-03 18:32:32 +00:00
|
|
|
assert parsed.netloc == request.host
|