mirror of
https://github.com/django/daphne.git
synced 2025-07-11 00:12:18 +03:00
Include cookies in request_info for websockets
This commit is contained in:
parent
0c03ea7780
commit
4f87d53adc
|
@ -1,5 +1,7 @@
|
||||||
import time
|
import time
|
||||||
|
|
||||||
|
from django.http import parse_cookie
|
||||||
|
|
||||||
from channels import DEFAULT_CHANNEL_BACKEND, Channel, channel_backends
|
from channels import DEFAULT_CHANNEL_BACKEND, Channel, channel_backends
|
||||||
|
|
||||||
|
|
||||||
|
@ -16,6 +18,7 @@ def get_protocol(base):
|
||||||
self.request_info = {
|
self.request_info = {
|
||||||
"path": request.path,
|
"path": request.path,
|
||||||
"get": request.params,
|
"get": request.params,
|
||||||
|
"cookies": parse_cookie(request.headers.get('cookie', ''))
|
||||||
}
|
}
|
||||||
|
|
||||||
def onOpen(self):
|
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"])
|
2
tox.ini
2
tox.ini
|
@ -10,8 +10,10 @@ envlist =
|
||||||
setenv =
|
setenv =
|
||||||
PYTHONPATH = {toxinidir}:{toxinidir}
|
PYTHONPATH = {toxinidir}:{toxinidir}
|
||||||
deps =
|
deps =
|
||||||
|
autobahn
|
||||||
six
|
six
|
||||||
redis==2.10.5
|
redis==2.10.5
|
||||||
|
py27: mock
|
||||||
flake8: flake8
|
flake8: flake8
|
||||||
isort: isort
|
isort: isort
|
||||||
django-16: Django>=1.6,<1.7
|
django-16: Django>=1.6,<1.7
|
||||||
|
|
Loading…
Reference in New Issue
Block a user