mirror of
https://github.com/django/daphne.git
synced 2025-04-20 08:42:18 +03:00
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
This commit is contained in:
parent
cb0a9bef4b
commit
f4c9b02ae3
|
@ -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")
|
||||
|
|
25
channels/tests/test_http.py
Normal file
25
channels/tests/test_http.py
Normal file
|
@ -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)
|
Loading…
Reference in New Issue
Block a user