From d876e3ed5c95116f65b3f244e4ce76ca4149dc29 Mon Sep 17 00:00:00 2001 From: Raphael Deem Date: Thu, 5 Oct 2017 22:20:36 -0700 Subject: [PATCH] fix false cookie encoding and output --- sanic/cookies.py | 3 ++- tests/test_cookies.py | 23 +++++++++++++++++++++-- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/sanic/cookies.py b/sanic/cookies.py index 16b798df..8ad8cbfc 100644 --- a/sanic/cookies.py +++ b/sanic/cookies.py @@ -98,7 +98,8 @@ class Cookie(dict): def __setitem__(self, key, value): if key not in self._keys: raise KeyError("Unknown cookie property") - return super().__setitem__(key, value) + if value is not False: + return super().__setitem__(key, value) def encode(self, encoding): output = ['%s=%s' % (self.key, _quote(self.value))] diff --git a/tests/test_cookies.py b/tests/test_cookies.py index d88288ee..84b493cb 100644 --- a/tests/test_cookies.py +++ b/tests/test_cookies.py @@ -25,6 +25,25 @@ def test_cookies(): assert response.text == 'Cookies are: working!' 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", [ (False, False), (True, True), @@ -34,7 +53,7 @@ def test_false_cookies(httponly, expected): @app.route('/') 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']['httponly'] = httponly return response @@ -43,7 +62,7 @@ def test_false_cookies(httponly, expected): response_cookies = SimpleCookie() 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(): app = Sanic('test_http2_cookies')