From f4c9b02ae31e53d24c74d4afe87a467e301ae571 Mon Sep 17 00:00:00 2001 From: Drew French Date: Thu, 22 Dec 2016 14:46:09 -0800 Subject: [PATCH] Valid cookie serialization for the test HTTPClient (#453) * valid cookie serialization * Added set cookie test * delimiter fix * more cases * quote fix * cleanup * fix * lint cleanup * more lint clean up --- channels/tests/http.py | 10 +++++++++- channels/tests/test_http.py | 25 +++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 channels/tests/test_http.py diff --git a/channels/tests/http.py b/channels/tests/http.py index e17e785..9759063 100644 --- a/channels/tests/http.py +++ b/channels/tests/http.py @@ -5,6 +5,8 @@ import six from django.apps import apps from django.conf import settings +from django.http.cookie import SimpleCookie + from ..sessions import session_for_reply_channel from .base import Client @@ -125,4 +127,10 @@ class HttpClient(Client): def _encoded_cookies(cookies): """Encode dict of cookies to ascii string""" - return ('&'.join('{0}={1}'.format(k, v) for k, v in cookies.items())).encode("ascii") + + cookie_encoder = SimpleCookie() + + for k, v in cookies.items(): + cookie_encoder[k] = v + + return cookie_encoder.output(header='', sep=';').encode("ascii") diff --git a/channels/tests/test_http.py b/channels/tests/test_http.py new file mode 100644 index 0000000..1c7c29a --- /dev/null +++ b/channels/tests/test_http.py @@ -0,0 +1,25 @@ +from __future__ import unicode_literals + +from django.http.cookie import parse_cookie + +from channels.tests import ChannelTestCase +from channels.tests.http import HttpClient + + +class HttpClientTests(ChannelTestCase): + def test_cookies(self): + client = HttpClient() + client.set_cookie('foo', 'not-bar') + client.set_cookie('foo', 'bar') + client.set_cookie('qux', 'qu;x') + + # Django's interpretation of the serialized cookie. + cookie_dict = parse_cookie(client.headers['cookie'].decode('ascii')) + + self.assertEqual(client.get_cookies(), + cookie_dict) + + self.assertEqual({'foo': 'bar', + 'qux': 'qu;x', + 'sessionid': client.get_cookies()['sessionid']}, + cookie_dict)