Don't apply HTTP timeout to WebSocket connections!

This commit is contained in:
Andrew Godwin 2018-02-04 12:08:57 -08:00
parent 6eeb280e1b
commit 105e1d5436
3 changed files with 34 additions and 1 deletions

View File

@ -118,6 +118,7 @@ class WebRequest(http.Request):
protocol.dataReceived(data) protocol.dataReceived(data)
# Remove our HTTP reply channel association # Remove our HTTP reply channel association
logger.debug("Upgraded connection %s to WebSocket", self.client_addr) logger.debug("Upgraded connection %s to WebSocket", self.client_addr)
self.server.discard_protocol(self)
# Resume the producer so we keep getting data, if it's available as a method # Resume the producer so we keep getting data, if it's available as a method
self.channel._networkProducer.resumeProducing() self.channel._networkProducer.resumeProducing()

View File

@ -18,8 +18,9 @@ class DaphneTestingInstance:
Works as a context manager. Works as a context manager.
""" """
def __init__(self, xff=False): def __init__(self, xff=False, http_timeout=60):
self.xff = xff self.xff = xff
self.http_timeout = http_timeout
self.host = "127.0.0.1" self.host = "127.0.0.1"
def port_in_use(self, port): def port_in_use(self, port):
@ -59,6 +60,8 @@ class DaphneTestingInstance:
# Optionally enable X-Forwarded-For support. # Optionally enable X-Forwarded-For support.
if self.xff: if self.xff:
daphne_args += ["--proxy-headers"] daphne_args += ["--proxy-headers"]
if self.http_timeout:
daphne_args += ["--http-timeout=%i" % self.http_timeout]
# Start up process and make sure it begins listening. Try this 3 times. # Start up process and make sure it begins listening. Try this 3 times.
for _ in range(3): for _ in range(3):
self.process = subprocess.Popen(daphne_args + ["daphne.test_application:TestApplication"]) self.process = subprocess.Popen(daphne_args + ["daphne.test_application:TestApplication"])

View File

@ -1,6 +1,7 @@
# coding: utf8 # coding: utf8
import collections import collections
import time
from urllib import parse from urllib import parse
from hypothesis import given, settings from hypothesis import given, settings
@ -242,3 +243,31 @@ class TestWebsocket(DaphneTestCase):
# Make sure it got our frame # Make sure it got our frame
_, messages = test_app.get_received() _, messages = test_app.get_received()
assert messages[1] == {"type": "websocket.receive", "bytes": b"what is here? \xe2"} assert messages[1] == {"type": "websocket.receive", "bytes": b"what is here? \xe2"}
def test_http_timeout(self):
"""
Tests that the HTTP timeout doesn't kick in for WebSockets
"""
with DaphneTestingInstance(http_timeout=1) as test_app:
# Connect
test_app.add_send_messages([
{
"type": "websocket.accept",
}
])
sock, _ = self.websocket_handshake(test_app)
_, messages = test_app.get_received()
self.assert_valid_websocket_connect_message(messages[0])
# Wait 2 seconds
time.sleep(2)
# Prep frame for it to send
test_app.add_send_messages([
{
"type": "websocket.send",
"text": "cake",
}
])
# Send it a frame
self.websocket_send_frame(sock, "still alive?")
# Receive a frame and make sure it's correct
assert self.websocket_receive_frame(sock) == "cake"