2016-10-23 09:32:16 +01:00
|
|
|
from datetime import datetime, timedelta
|
|
|
|
from http.cookies import SimpleCookie
|
|
|
|
from sanic import Sanic
|
|
|
|
from sanic.response import json, text
|
|
|
|
from sanic.utils import sanic_endpoint_test
|
2017-01-26 00:47:14 +00:00
|
|
|
import pytest
|
2016-10-23 09:32:16 +01:00
|
|
|
|
|
|
|
|
|
|
|
# ------------------------------------------------------------ #
|
|
|
|
# GET
|
|
|
|
# ------------------------------------------------------------ #
|
|
|
|
|
|
|
|
def test_cookies():
|
|
|
|
app = Sanic('test_text')
|
|
|
|
|
|
|
|
@app.route('/')
|
|
|
|
def handler(request):
|
|
|
|
response = text('Cookies are: {}'.format(request.cookies['test']))
|
|
|
|
response.cookies['right_back'] = 'at you'
|
|
|
|
return response
|
|
|
|
|
|
|
|
request, response = sanic_endpoint_test(app, cookies={"test": "working!"})
|
|
|
|
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'
|
|
|
|
|
2017-01-26 00:47:14 +00:00
|
|
|
@pytest.mark.parametrize("httponly,expected", [
|
|
|
|
(False, False),
|
|
|
|
(True, True),
|
|
|
|
])
|
|
|
|
def test_false_cookies(httponly, expected):
|
|
|
|
app = Sanic('test_text')
|
|
|
|
|
|
|
|
@app.route('/')
|
|
|
|
def handler(request):
|
|
|
|
response = text('Cookies are: {}'.format(request.cookies['test']))
|
|
|
|
response.cookies['right_back'] = 'at you'
|
|
|
|
response.cookies['right_back']['httponly'] = httponly
|
|
|
|
return response
|
|
|
|
|
|
|
|
request, response = sanic_endpoint_test(app)
|
|
|
|
response_cookies = SimpleCookie()
|
|
|
|
response_cookies.load(response.headers.get('Set-Cookie', {}))
|
|
|
|
|
|
|
|
'HttpOnly' in response_cookies == expected
|
|
|
|
|
2016-12-03 20:19:24 +00:00
|
|
|
def test_http2_cookies():
|
|
|
|
app = Sanic('test_http2_cookies')
|
|
|
|
|
|
|
|
@app.route('/')
|
|
|
|
async def handler(request):
|
|
|
|
response = text('Cookies are: {}'.format(request.cookies['test']))
|
|
|
|
return response
|
|
|
|
|
|
|
|
headers = {'cookie': 'test=working!'}
|
|
|
|
request, response = sanic_endpoint_test(app, headers=headers)
|
|
|
|
|
|
|
|
assert response.text == 'Cookies are: working!'
|
|
|
|
|
2016-10-23 09:32:16 +01:00
|
|
|
def test_cookie_options():
|
|
|
|
app = Sanic('test_text')
|
|
|
|
|
|
|
|
@app.route('/')
|
|
|
|
def handler(request):
|
|
|
|
response = text("OK")
|
|
|
|
response.cookies['test'] = 'at you'
|
|
|
|
response.cookies['test']['httponly'] = True
|
|
|
|
response.cookies['test']['expires'] = datetime.now() + timedelta(seconds=10)
|
|
|
|
return response
|
|
|
|
|
|
|
|
request, response = sanic_endpoint_test(app)
|
|
|
|
response_cookies = SimpleCookie()
|
|
|
|
response_cookies.load(response.headers.get('Set-Cookie', {}))
|
|
|
|
|
|
|
|
assert response_cookies['test'].value == 'at you'
|
2017-01-26 00:47:14 +00:00
|
|
|
assert response_cookies['test']['httponly'] == True
|