mirror of
https://github.com/django/daphne.git
synced 2025-04-21 17:22:03 +03:00
Merge pull request #34 from ekmartin/session_ws
Include cookies in request_info for websockets
This commit is contained in:
commit
89ec9ce9ed
|
@ -1,5 +1,7 @@
|
|||
import time
|
||||
|
||||
from django.http import parse_cookie
|
||||
|
||||
from channels import DEFAULT_CHANNEL_BACKEND, Channel, channel_backends
|
||||
|
||||
|
||||
|
@ -16,6 +18,7 @@ def get_protocol(base):
|
|||
self.request_info = {
|
||||
"path": request.path,
|
||||
"get": request.params,
|
||||
"cookies": parse_cookie(request.headers.get('cookie', ''))
|
||||
}
|
||||
|
||||
def onOpen(self):
|
||||
|
|
53
channels/tests/test_interfaces.py
Normal file
53
channels/tests/test_interfaces.py
Normal file
|
@ -0,0 +1,53 @@
|
|||
from django.test import TestCase
|
||||
|
||||
from channels.interfaces.websocket_autobahn import get_protocol
|
||||
|
||||
try:
|
||||
from unittest import mock
|
||||
except ImportError:
|
||||
import mock
|
||||
|
||||
|
||||
def generate_connection_request(path, params, headers):
|
||||
request = mock.Mock()
|
||||
request.path = path
|
||||
request.params = params
|
||||
request.headers = headers
|
||||
return request
|
||||
|
||||
|
||||
class WebsocketAutobahnInterfaceProtocolTestCase(TestCase):
|
||||
def test_on_connect_cookie(self):
|
||||
protocol = get_protocol(object)()
|
||||
session = "123cat"
|
||||
cookie = "somethingelse=test; sessionid={0}".format(session)
|
||||
headers = {
|
||||
"cookie": cookie
|
||||
}
|
||||
|
||||
test_request = generate_connection_request("path", {}, headers)
|
||||
protocol.onConnect(test_request)
|
||||
self.assertEqual(session, protocol.request_info["cookies"]["sessionid"])
|
||||
|
||||
def test_on_connect_no_cookie(self):
|
||||
protocol = get_protocol(object)()
|
||||
test_request = generate_connection_request("path", {}, {})
|
||||
protocol.onConnect(test_request)
|
||||
self.assertEqual({}, protocol.request_info["cookies"])
|
||||
|
||||
def test_on_connect_params(self):
|
||||
protocol = get_protocol(object)()
|
||||
params = {
|
||||
"session_key": ["123cat"]
|
||||
}
|
||||
|
||||
test_request = generate_connection_request("path", params, {})
|
||||
protocol.onConnect(test_request)
|
||||
self.assertEqual(params, protocol.request_info["get"])
|
||||
|
||||
def test_on_connect_path(self):
|
||||
protocol = get_protocol(object)()
|
||||
path = "path"
|
||||
test_request = generate_connection_request(path, {}, {})
|
||||
protocol.onConnect(test_request)
|
||||
self.assertEqual(path, protocol.request_info["path"])
|
Loading…
Reference in New Issue
Block a user