add unit tests for App Config, Cokkies and Request handler
Signed-off-by: Harsha Narayana <harsha2k4@gmail.com>
This commit is contained in:
parent
4cb40f2042
commit
ece3cdaa2e
|
@ -48,6 +48,20 @@ def test_load_env_prefix():
|
|||
del environ["MYAPP_TEST_ANSWER"]
|
||||
|
||||
|
||||
def test_load_env_prefix_float_values():
|
||||
environ["MYAPP_TEST_ROI"] = "2.3"
|
||||
app = Sanic(load_env="MYAPP_")
|
||||
assert app.config.TEST_ROI == 2.3
|
||||
del environ["MYAPP_TEST_ROI"]
|
||||
|
||||
|
||||
def test_load_env_prefix_string_value():
|
||||
environ["MYAPP_TEST_TOKEN"] = "somerandomtesttoken"
|
||||
app = Sanic(load_env="MYAPP_")
|
||||
assert app.config.TEST_TOKEN == "somerandomtesttoken"
|
||||
del environ["MYAPP_TEST_TOKEN"]
|
||||
|
||||
|
||||
def test_load_from_file(app):
|
||||
config = dedent("""
|
||||
VALUE = 'some value'
|
||||
|
|
|
@ -2,7 +2,7 @@ from datetime import datetime, timedelta
|
|||
from http.cookies import SimpleCookie
|
||||
from sanic.response import text
|
||||
import pytest
|
||||
|
||||
from sanic.cookies import Cookie
|
||||
|
||||
# ------------------------------------------------------------ #
|
||||
# GET
|
||||
|
@ -111,3 +111,27 @@ def test_cookie_deletion(app):
|
|||
assert int(response_cookies['i_want_to_die']['max-age']) == 0
|
||||
with pytest.raises(KeyError):
|
||||
response.cookies['i_never_existed']
|
||||
|
||||
|
||||
def test_cookie_reserved_cookie():
|
||||
with pytest.raises(expected_exception=KeyError) as e:
|
||||
Cookie("domain", "testdomain.com")
|
||||
assert e.message == "Cookie name is a reserved word"
|
||||
|
||||
|
||||
def test_cookie_illegal_key_format():
|
||||
with pytest.raises(expected_exception=KeyError) as e:
|
||||
Cookie("testå", "test")
|
||||
assert e.message == "Cookie key contains illegal characters"
|
||||
|
||||
|
||||
def test_cookie_set_unknown_property():
|
||||
c = Cookie("test_cookie", "value")
|
||||
with pytest.raises(expected_exception=KeyError) as e:
|
||||
c["invalid"] = "value"
|
||||
assert e.message == "Unknown cookie property"
|
||||
|
||||
|
||||
def test_cookie_encoding_invalid_type():
|
||||
c = Cookie("test_cookie", "value")
|
||||
c["max-age"] = "2d"
|
||||
|
|
|
@ -363,3 +363,83 @@ def test_url_attributes_with_ssl(app, path, query, expected_url):
|
|||
assert parsed.path == request.path
|
||||
assert parsed.query == request.query_string
|
||||
assert parsed.netloc == request.host
|
||||
|
||||
|
||||
def test_form_with_multiple_values(app):
|
||||
|
||||
@app.route('/', methods=['POST'])
|
||||
async def handler(request):
|
||||
return text("OK")
|
||||
|
||||
payload="selectedItems=v1&selectedItems=v2&selectedItems=v3"
|
||||
|
||||
headers = {'content-type': 'application/x-www-form-urlencoded'}
|
||||
|
||||
request, response = app.test_client.post('/', data=payload,
|
||||
headers=headers)
|
||||
|
||||
assert request.form.getlist("selectedItems") == ["v1", "v2", "v3"]
|
||||
|
||||
|
||||
def test_request_string_representation(app):
|
||||
@app.route('/', methods=["GET"])
|
||||
async def get(request):
|
||||
return text("OK")
|
||||
|
||||
request, _ = app.test_client.get("/")
|
||||
assert repr(request) == '<Request: GET />'
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
'payload', [
|
||||
'------sanic\r\n'
|
||||
'Content-Disposition: form-data; filename="filename"; name="test"\r\n'
|
||||
'\r\n'
|
||||
'OK\r\n'
|
||||
'------sanic--\r\n',
|
||||
'------sanic\r\n'
|
||||
'content-disposition: form-data; filename="filename"; name="test"\r\n'
|
||||
'\r\n'
|
||||
'content-type: application/json; {"field": "value"}\r\n'
|
||||
'------sanic--\r\n',
|
||||
])
|
||||
def test_request_multipart_files(app, payload):
|
||||
@app.route("/", methods=["POST"])
|
||||
async def post(request):
|
||||
return text("OK")
|
||||
|
||||
headers = {'content-type': 'multipart/form-data; boundary=----sanic'}
|
||||
|
||||
request, _ = app.test_client.post(data=payload, headers=headers)
|
||||
assert request.files.get('test').name == "filename"
|
||||
|
||||
|
||||
def test_request_multipart_file_with_json_content_type(app):
|
||||
@app.route("/", methods=["POST"])
|
||||
async def post(request):
|
||||
return text("OK")
|
||||
|
||||
payload = '------sanic\r\nContent-Disposition: form-data; name="file"; filename="test.json"' \
|
||||
'\r\nContent-Type: application/json\r\n\r\n\r\n------sanic--'
|
||||
|
||||
headers = {'content-type': 'multipart/form-data; boundary=------sanic'}
|
||||
|
||||
request, _ = app.test_client.post(data=payload, headers=headers)
|
||||
assert request.files.get('file').type == 'application/json'
|
||||
|
||||
|
||||
def test_request_multipart_with_multiple_files_and_type(app):
|
||||
@app.route("/", methods=["POST"])
|
||||
async def post(request):
|
||||
return text("OK")
|
||||
|
||||
payload = '------sanic\r\nContent-Disposition: form-data; name="file"; filename="test.json"' \
|
||||
'\r\nContent-Type: application/json\r\n\r\n\r\n' \
|
||||
'------sanic\r\nContent-Disposition: form-data; name="file"; filename="some_file.pdf"\r\n' \
|
||||
'Content-Type: application/pdf\r\n\r\n\r\n------sanic--'
|
||||
headers = {'content-type': 'multipart/form-data; boundary=------sanic'}
|
||||
|
||||
request, _ = app.test_client.post(data=payload, headers=headers)
|
||||
assert len(request.files.getlist('file')) == 2
|
||||
assert request.files.getlist('file')[0].type == 'application/json'
|
||||
assert request.files.getlist('file')[1].type == 'application/pdf'
|
||||
|
|
Loading…
Reference in New Issue
Block a user