From ece3cdaa2e39e93b0ee5e7bdb812e1c918b3b2ad Mon Sep 17 00:00:00 2001 From: Harsha Narayana Date: Sat, 10 Nov 2018 16:50:30 +0530 Subject: [PATCH 1/2] add unit tests for App Config, Cokkies and Request handler Signed-off-by: Harsha Narayana --- tests/test_config.py | 14 ++++++++ tests/test_cookies.py | 26 +++++++++++++- tests/test_requests.py | 80 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 119 insertions(+), 1 deletion(-) diff --git a/tests/test_config.py b/tests/test_config.py index 78596004..8cac5dfc 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -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' diff --git a/tests/test_cookies.py b/tests/test_cookies.py index 806ea4c1..b920ccd8 100644 --- a/tests/test_cookies.py +++ b/tests/test_cookies.py @@ -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" diff --git a/tests/test_requests.py b/tests/test_requests.py index c5c20149..c1afc751 100644 --- a/tests/test_requests.py +++ b/tests/test_requests.py @@ -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) == '' + + +@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' From c60ba81984f6401b011ec8b43a03d113aafdd2cf Mon Sep 17 00:00:00 2001 From: Harsha Narayana Date: Sat, 10 Nov 2018 16:54:24 +0530 Subject: [PATCH 2/2] cleanup stale test for cookie object Signed-off-by: Harsha Narayana --- tests/test_cookies.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/tests/test_cookies.py b/tests/test_cookies.py index b920ccd8..c3fd98f0 100644 --- a/tests/test_cookies.py +++ b/tests/test_cookies.py @@ -130,8 +130,3 @@ def test_cookie_set_unknown_property(): 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"