From c3ac3a0a6049e9a72405b6998e3a45aa7ffeb3bb Mon Sep 17 00:00:00 2001 From: David Marquis Date: Mon, 30 Jan 2017 09:36:32 -0500 Subject: [PATCH] Fix #72: Adding test for no origin header use case --- daphne/tests/test_ws.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/daphne/tests/test_ws.py b/daphne/tests/test_ws.py index 0efa8d7..b32b798 100644 --- a/daphne/tests/test_ws.py +++ b/daphne/tests/test_ws.py @@ -119,3 +119,32 @@ class TestWebSocketProtocol(TestCase): response = self.tr.value() self.assertIn(b"HTTP/1.1 101 Switching Protocols\r\n", response) self.assertIn(b"Sec-WebSocket-Accept: HSmrc0sMlYUkAGmm5OPpG2HaGWk=\r\n", response) + + def test_connection_with_no_origin_is_accepted(self): + # Send a simple request to the protocol + self.proto.dataReceived( + b"GET /chat HTTP/1.1\r\n" + b"Host: somewhere.com\r\n" + b"Upgrade: websocket\r\n" + b"Connection: Upgrade\r\n" + b"Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw==\r\n" + b"Sec-WebSocket-Protocol: chat, superchat\r\n" + b"Sec-WebSocket-Version: 13\r\n" + b"\r\n" + ) + + # Get the resulting message off of the channel layer + _, message = self.channel_layer.receive_many(["websocket.connect"]) + self.assertNotIn(b'origin', [header_tuple[0] for header_tuple in message['headers']]) + self.assertTrue(message['reply_channel'].startswith("websocket.send!")) + + # Accept the connection + self.factory.dispatch_reply( + message['reply_channel'], + {'accept': True} + ) + + # Make sure that we get a 101 Switching Protocols back + response = self.tr.value() + self.assertIn(b"HTTP/1.1 101 Switching Protocols\r\n", response) + self.assertIn(b"Sec-WebSocket-Accept: HSmrc0sMlYUkAGmm5OPpG2HaGWk=\r\n", response)