Merge pull request #961 from r0fls/fix-920

fix false cookie encoding and output
This commit is contained in:
Raphael Deem 2017-10-06 23:57:30 -07:00 committed by GitHub
commit 8018c9b91d
2 changed files with 23 additions and 3 deletions

View File

@ -98,6 +98,7 @@ class Cookie(dict):
def __setitem__(self, key, value): def __setitem__(self, key, value):
if key not in self._keys: if key not in self._keys:
raise KeyError("Unknown cookie property") raise KeyError("Unknown cookie property")
if value is not False:
return super().__setitem__(key, value) return super().__setitem__(key, value)
def encode(self, encoding): def encode(self, encoding):

View File

@ -25,6 +25,25 @@ def test_cookies():
assert response.text == 'Cookies are: working!' assert response.text == 'Cookies are: working!'
assert response_cookies['right_back'].value == 'at you' assert response_cookies['right_back'].value == 'at you'
@pytest.mark.parametrize("httponly,expected", [
(False, False),
(True, True),
])
def test_false_cookies_encoded(httponly, expected):
app = Sanic('test_text')
@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
@pytest.mark.parametrize("httponly,expected", [ @pytest.mark.parametrize("httponly,expected", [
(False, False), (False, False),
(True, True), (True, True),
@ -34,7 +53,7 @@ def test_false_cookies(httponly, expected):
@app.route('/') @app.route('/')
def handler(request): def handler(request):
response = text('Cookies are: {}'.format(request.cookies['test'])) response = text('hello cookies')
response.cookies['right_back'] = 'at you' response.cookies['right_back'] = 'at you'
response.cookies['right_back']['httponly'] = httponly response.cookies['right_back']['httponly'] = httponly
return response return response
@ -43,7 +62,7 @@ def test_false_cookies(httponly, expected):
response_cookies = SimpleCookie() response_cookies = SimpleCookie()
response_cookies.load(response.headers.get('Set-Cookie', {})) response_cookies.load(response.headers.get('Set-Cookie', {}))
'HttpOnly' in response_cookies == expected assert ('HttpOnly' in response_cookies['right_back'].output()) == expected
def test_http2_cookies(): def test_http2_cookies():
app = Sanic('test_http2_cookies') app = Sanic('test_http2_cookies')