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
|
2016-10-23 09:32:16 +01:00
|
|
|
|
|
|
|
|
|
|
|
# ------------------------------------------------------------ #
|
|
|
|
# GET
|
|
|
|
# ------------------------------------------------------------ #
|
|
|
|
|
2018-08-26 15:43:14 +01:00
|
|
|
def test_cookies(app):
|
2016-10-23 09:32:16 +01:00
|
|
|
|
|
|
|
@app.route('/')
|
|
|
|
def handler(request):
|
|
|
|
response = text('Cookies are: {}'.format(request.cookies['test']))
|
|
|
|
response.cookies['right_back'] = 'at you'
|
|
|
|
return response
|
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.get('/', cookies={"test": "working!"})
|
2016-10-23 09:32:16 +01:00
|
|
|
response_cookies = SimpleCookie()
|
|
|
|
response_cookies.load(response.headers.get('Set-Cookie', {}))
|
|
|
|
|
|
|
|
assert response.text == 'Cookies are: working!'
|
|
|
|
assert response_cookies['right_back'].value == 'at you'
|
|
|
|
|
2018-07-11 09:44:21 +01:00
|
|
|
|
2017-10-06 06:20:36 +01: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):
|
2017-10-06 06:20:36 +01:00
|
|
|
|
|
|
|
@app.route('/')
|
|
|
|
def handler(request):
|
|
|
|
response = text('hello cookies')
|
|
|
|
response.cookies['hello'] = 'world'
|
|
|
|
response.cookies['hello']['httponly'] = httponly
|
|
|
|
return text(response.cookies['hello'].encode('utf8'))
|
|
|
|
|
|
|
|
request, response = app.test_client.get('/')
|
|
|
|
|
|
|
|
assert ('HttpOnly' in response.text) == expected
|
|
|
|
|
|
|
|
|
2017-01-26 00:47:14 +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):
|
2017-01-26 00:47:14 +00:00
|
|
|
|
|
|
|
@app.route('/')
|
|
|
|
def handler(request):
|
2017-10-06 06:20:36 +01:00
|
|
|
response = text('hello cookies')
|
2017-01-26 00:47:14 +00:00
|
|
|
response.cookies['right_back'] = 'at you'
|
|
|
|
response.cookies['right_back']['httponly'] = httponly
|
|
|
|
return response
|
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.get('/')
|
2017-01-26 00:47:14 +00:00
|
|
|
response_cookies = SimpleCookie()
|
|
|
|
response_cookies.load(response.headers.get('Set-Cookie', {}))
|
|
|
|
|
2017-10-06 06:20:36 +01: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):
|
2016-12-03 20:19:24 +00:00
|
|
|
|
|
|
|
@app.route('/')
|
|
|
|
async def handler(request):
|
|
|
|
response = text('Cookies are: {}'.format(request.cookies['test']))
|
|
|
|
return response
|
|
|
|
|
|
|
|
headers = {'cookie': 'test=working!'}
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.get('/', headers=headers)
|
2016-12-03 20:19:24 +00:00
|
|
|
|
|
|
|
assert response.text == 'Cookies are: working!'
|
|
|
|
|
2018-10-22 21:25:38 +01:00
|
|
|
|
2018-08-26 15:43:14 +01:00
|
|
|
def test_cookie_options(app):
|
2016-10-23 09:32:16 +01:00
|
|
|
|
|
|
|
@app.route('/')
|
|
|
|
def handler(request):
|
|
|
|
response = text("OK")
|
|
|
|
response.cookies['test'] = 'at you'
|
|
|
|
response.cookies['test']['httponly'] = True
|
2018-10-22 21:25:38 +01:00
|
|
|
response.cookies['test']['expires'] = (datetime.now() +
|
|
|
|
timedelta(seconds=10))
|
2016-10-23 09:32:16 +01:00
|
|
|
return response
|
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.get('/')
|
2016-10-23 09:32:16 +01:00
|
|
|
response_cookies = SimpleCookie()
|
|
|
|
response_cookies.load(response.headers.get('Set-Cookie', {}))
|
|
|
|
|
|
|
|
assert response_cookies['test'].value == 'at you'
|
2018-10-22 21:25:38 +01:00
|
|
|
assert response_cookies['test']['httponly'] is True
|
|
|
|
|
2017-01-25 09:53:39 +00:00
|
|
|
|
2018-08-26 15:43:14 +01:00
|
|
|
def test_cookie_deletion(app):
|
2017-01-25 09:53:39 +00:00
|
|
|
|
|
|
|
@app.route('/')
|
|
|
|
def handler(request):
|
|
|
|
response = text("OK")
|
|
|
|
del response.cookies['i_want_to_die']
|
|
|
|
response.cookies['i_never_existed'] = 'testing'
|
|
|
|
del response.cookies['i_never_existed']
|
|
|
|
return response
|
|
|
|
|
2017-02-14 19:51:20 +00:00
|
|
|
request, response = app.test_client.get('/')
|
2017-01-25 09:53:39 +00:00
|
|
|
response_cookies = SimpleCookie()
|
|
|
|
response_cookies.load(response.headers.get('Set-Cookie', {}))
|
|
|
|
|
|
|
|
assert int(response_cookies['i_want_to_die']['max-age']) == 0
|
|
|
|
with pytest.raises(KeyError):
|
2018-10-22 21:25:38 +01:00
|
|
|
response.cookies['i_never_existed']
|