2016-10-23 09:32:16 +01:00
|
|
|
from datetime import datetime, timedelta
|
|
|
|
from http.cookies import SimpleCookie
|
2018-10-22 21:25:38 +01:00
|
|
|
from sanic.response import text
|
2017-01-25 09:53:39 +00:00
|
|
|
import pytest
|
2019-01-03 23:01:54 +00:00
|
|
|
from sanic.cookies import Cookie, DEFAULT_MAX_AGE
|
2016-10-23 09:32:16 +01:00
|
|
|
|
|
|
|
# ------------------------------------------------------------ #
|
|
|
|
# GET
|
|
|
|
# ------------------------------------------------------------ #
|
|
|
|
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
def test_cookies(app):
|
|
|
|
@app.route("/")
|
2016-10-23 09:32:16 +01:00
|
|
|
def handler(request):
|
2018-12-30 11:18:06 +00:00
|
|
|
response = text("Cookies are: {}".format(request.cookies["test"]))
|
|
|
|
response.cookies["right_back"] = "at you"
|
2016-10-23 09:32:16 +01:00
|
|
|
return response
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.get("/", cookies={"test": "working!"})
|
2016-10-23 09:32:16 +01:00
|
|
|
response_cookies = SimpleCookie()
|
2018-12-30 11:18:06 +00:00
|
|
|
response_cookies.load(response.headers.get("Set-Cookie", {}))
|
2016-10-23 09:32:16 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
assert response.text == "Cookies are: working!"
|
|
|
|
assert response_cookies["right_back"].value == "at you"
|
2016-10-23 09:32:16 +01:00
|
|
|
|
2018-07-11 09:44:21 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@pytest.mark.parametrize("httponly,expected", [(False, False), (True, True)])
|
2018-08-26 15:43:14 +01:00
|
|
|
def test_false_cookies_encoded(app, httponly, expected):
|
2018-12-30 11:18:06 +00:00
|
|
|
@app.route("/")
|
2017-10-06 06:20:36 +01:00
|
|
|
def handler(request):
|
2018-12-30 11:18:06 +00:00
|
|
|
response = text("hello cookies")
|
|
|
|
response.cookies["hello"] = "world"
|
|
|
|
response.cookies["hello"]["httponly"] = httponly
|
|
|
|
return text(response.cookies["hello"].encode("utf8"))
|
2017-10-06 06:20:36 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.get("/")
|
2017-10-06 06:20:36 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
assert ("HttpOnly" in response.text) == expected
|
2017-10-06 06:20:36 +01:00
|
|
|
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@pytest.mark.parametrize("httponly,expected", [(False, False), (True, True)])
|
2018-08-26 15:43:14 +01:00
|
|
|
def test_false_cookies(app, httponly, expected):
|
2018-12-30 11:18:06 +00:00
|
|
|
@app.route("/")
|
2017-01-26 00:47:14 +00:00
|
|
|
def handler(request):
|
2018-12-30 11:18:06 +00:00
|
|
|
response = text("hello cookies")
|
|
|
|
response.cookies["right_back"] = "at you"
|
|
|
|
response.cookies["right_back"]["httponly"] = httponly
|
2017-01-26 00:47:14 +00:00
|
|
|
return response
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.get("/")
|
2017-01-26 00:47:14 +00:00
|
|
|
response_cookies = SimpleCookie()
|
2018-12-30 11:18:06 +00:00
|
|
|
response_cookies.load(response.headers.get("Set-Cookie", {}))
|
2017-01-26 00:47:14 +00:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
assert ("HttpOnly" in response_cookies["right_back"].output()) == expected
|
2017-01-26 00:47:14 +00:00
|
|
|
|
2018-10-22 21:25:38 +01:00
|
|
|
|
2018-08-26 15:43:14 +01:00
|
|
|
def test_http2_cookies(app):
|
2018-12-30 11:18:06 +00:00
|
|
|
@app.route("/")
|
2016-12-03 20:19:24 +00:00
|
|
|
async def handler(request):
|
2018-12-30 11:18:06 +00:00
|
|
|
response = text("Cookies are: {}".format(request.cookies["test"]))
|
2016-12-03 20:19:24 +00:00
|
|
|
return response
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
headers = {"cookie": "test=working!"}
|
|
|
|
request, response = app.test_client.get("/", headers=headers)
|
2016-12-03 20:19:24 +00:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
assert response.text == "Cookies are: working!"
|
2016-12-03 20:19:24 +00:00
|
|
|
|
2018-10-22 21:25:38 +01:00
|
|
|
|
2018-08-26 15:43:14 +01:00
|
|
|
def test_cookie_options(app):
|
2018-12-30 11:18:06 +00:00
|
|
|
@app.route("/")
|
2016-10-23 09:32:16 +01:00
|
|
|
def handler(request):
|
|
|
|
response = text("OK")
|
2018-12-30 11:18:06 +00:00
|
|
|
response.cookies["test"] = "at you"
|
|
|
|
response.cookies["test"]["httponly"] = True
|
|
|
|
response.cookies["test"]["expires"] = datetime.now() + timedelta(
|
|
|
|
seconds=10
|
|
|
|
)
|
2016-10-23 09:32:16 +01:00
|
|
|
return response
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.get("/")
|
2016-10-23 09:32:16 +01:00
|
|
|
response_cookies = SimpleCookie()
|
2018-12-30 11:18:06 +00:00
|
|
|
response_cookies.load(response.headers.get("Set-Cookie", {}))
|
2016-10-23 09:32:16 +01:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
assert response_cookies["test"].value == "at you"
|
|
|
|
assert response_cookies["test"]["httponly"] is True
|
2018-10-22 21:25:38 +01:00
|
|
|
|
2017-01-25 09:53:39 +00:00
|
|
|
|
2018-08-26 15:43:14 +01:00
|
|
|
def test_cookie_deletion(app):
|
2018-12-30 11:18:06 +00:00
|
|
|
@app.route("/")
|
2017-01-25 09:53:39 +00:00
|
|
|
def handler(request):
|
|
|
|
response = text("OK")
|
2018-12-30 11:18:06 +00:00
|
|
|
del response.cookies["i_want_to_die"]
|
|
|
|
response.cookies["i_never_existed"] = "testing"
|
|
|
|
del response.cookies["i_never_existed"]
|
2017-01-25 09:53:39 +00:00
|
|
|
return response
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.get("/")
|
2017-01-25 09:53:39 +00:00
|
|
|
response_cookies = SimpleCookie()
|
2018-12-30 11:18:06 +00:00
|
|
|
response_cookies.load(response.headers.get("Set-Cookie", {}))
|
2017-01-25 09:53:39 +00:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
assert int(response_cookies["i_want_to_die"]["max-age"]) == 0
|
2017-01-25 09:53:39 +00:00
|
|
|
with pytest.raises(KeyError):
|
2018-12-30 11:18:06 +00:00
|
|
|
response.cookies["i_never_existed"]
|
2018-11-10 11:20:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
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"
|
2018-12-22 15:21:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_cookie_set_same_key(app):
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
cookies = {"test": "wait"}
|
2018-12-22 15:21:45 +00:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@app.get("/")
|
2018-12-22 15:21:45 +00:00
|
|
|
def handler(request):
|
2018-12-30 11:18:06 +00:00
|
|
|
response = text("pass")
|
|
|
|
response.cookies["test"] = "modified"
|
|
|
|
response.cookies["test"] = "pass"
|
2018-12-22 15:21:45 +00:00
|
|
|
return response
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.get("/", cookies=cookies)
|
2018-12-22 15:21:45 +00:00
|
|
|
assert response.status == 200
|
2018-12-30 11:18:06 +00:00
|
|
|
assert response.cookies["test"].value == "pass"
|
2018-12-22 15:21:45 +00:00
|
|
|
|
|
|
|
|
2019-01-07 23:39:37 +00:00
|
|
|
@pytest.mark.parametrize("max_age", ["0", 30, 30.0, 30.1, "30", "test"])
|
2018-12-22 15:21:45 +00:00
|
|
|
def test_cookie_max_age(app, max_age):
|
2018-12-30 11:18:06 +00:00
|
|
|
cookies = {"test": "wait"}
|
2018-12-22 15:21:45 +00:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@app.get("/")
|
2018-12-22 15:21:45 +00:00
|
|
|
def handler(request):
|
2018-12-30 11:18:06 +00:00
|
|
|
response = text("pass")
|
|
|
|
response.cookies["test"] = "pass"
|
|
|
|
response.cookies["test"]["max-age"] = max_age
|
2018-12-22 15:21:45 +00:00
|
|
|
return response
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.get("/", cookies=cookies)
|
2018-12-22 15:21:45 +00:00
|
|
|
assert response.status == 200
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
assert response.cookies["test"].value == "pass"
|
2019-01-03 23:01:54 +00:00
|
|
|
|
2019-01-07 23:39:37 +00:00
|
|
|
if str(max_age).isdigit() and int(max_age) == float(max_age):
|
2019-01-03 23:01:54 +00:00
|
|
|
assert response.cookies["test"]["max-age"] == str(max_age)
|
|
|
|
else:
|
|
|
|
assert response.cookies["test"]["max-age"] == str(DEFAULT_MAX_AGE)
|
2018-12-22 15:21:45 +00:00
|
|
|
|
|
|
|
|
2019-02-06 18:29:33 +00:00
|
|
|
@pytest.mark.parametrize("expires", [datetime.now() + timedelta(seconds=60)])
|
2018-12-22 15:21:45 +00:00
|
|
|
def test_cookie_expires(app, expires):
|
2018-12-30 11:18:06 +00:00
|
|
|
cookies = {"test": "wait"}
|
2018-12-22 15:21:45 +00:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@app.get("/")
|
2018-12-22 15:21:45 +00:00
|
|
|
def handler(request):
|
2018-12-30 11:18:06 +00:00
|
|
|
response = text("pass")
|
|
|
|
response.cookies["test"] = "pass"
|
|
|
|
response.cookies["test"]["expires"] = expires
|
2018-12-22 15:21:45 +00:00
|
|
|
return response
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.get("/", cookies=cookies)
|
2018-12-22 15:21:45 +00:00
|
|
|
assert response.status == 200
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
assert response.cookies["test"].value == "pass"
|
2018-12-22 15:21:45 +00:00
|
|
|
|
|
|
|
if isinstance(expires, datetime):
|
|
|
|
expires = expires.strftime("%a, %d-%b-%Y %T GMT")
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
assert response.cookies["test"]["expires"] == expires
|
2019-02-06 18:29:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("expires", ["Fri, 21-Dec-2018 15:30:00 GMT"])
|
|
|
|
def test_cookie_expires_illegal_instance_type(expires):
|
|
|
|
c = Cookie("test_cookie", "value")
|
|
|
|
with pytest.raises(expected_exception=TypeError) as e:
|
|
|
|
c["expires"] = expires
|
|
|
|
assert e.message == "Cookie 'expires' property must be a datetime"
|