mirror of
https://github.com/django/daphne.git
synced 2024-11-21 23:46:33 +03:00
This commit is contained in:
parent
7032f8e0f8
commit
78be865eb4
|
@ -219,7 +219,12 @@ class Server(object):
|
|||
"disconnected", None
|
||||
):
|
||||
return
|
||||
self.check_headers_type(message)
|
||||
try:
|
||||
self.check_headers_type(message)
|
||||
except ValueError:
|
||||
# Ensure to send SOME reply.
|
||||
protocol.basic_error(500, b"Server Error", "Server Error")
|
||||
raise
|
||||
# Let the protocol handle it
|
||||
protocol.handle_reply(message)
|
||||
|
||||
|
|
|
@ -56,12 +56,16 @@ class DaphneTestCase(unittest.TestCase):
|
|||
# Return scope, messages, response
|
||||
return test_app.get_received() + (response,)
|
||||
|
||||
def run_daphne_raw(self, data, timeout=1):
|
||||
def run_daphne_raw(self, data, *, responses=None, timeout=1):
|
||||
"""
|
||||
Runs daphne and sends it the given raw bytestring over a socket. Returns what it sends back.
|
||||
Runs Daphne and sends it the given raw bytestring over a socket.
|
||||
Accepts list of response messages the application will reply with.
|
||||
Returns what Daphne sends back.
|
||||
"""
|
||||
assert isinstance(data, bytes)
|
||||
with DaphneTestingInstance() as test_app:
|
||||
if responses is not None:
|
||||
test_app.add_send_messages(responses)
|
||||
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
s.settimeout(timeout)
|
||||
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
||||
|
|
|
@ -169,3 +169,21 @@ class TestHTTPResponse(DaphneTestCase):
|
|||
str(context.exception),
|
||||
"Header value 'True' expected to be `bytes`, but got `<class 'bool'>`",
|
||||
)
|
||||
|
||||
def test_headers_type_raw(self):
|
||||
"""
|
||||
Daphne returns a 500 error response if the application sends invalid
|
||||
headers.
|
||||
"""
|
||||
response = self.run_daphne_raw(
|
||||
b"GET / HTTP/1.0\r\n\r\n",
|
||||
responses=[
|
||||
{
|
||||
"type": "http.response.start",
|
||||
"status": 200,
|
||||
"headers": [["foo", b"bar"]],
|
||||
},
|
||||
{"type": "http.response.body", "body": b""},
|
||||
],
|
||||
)
|
||||
self.assertTrue(response.startswith(b"HTTP/1.0 500 Internal Server Error"))
|
||||
|
|
Loading…
Reference in New Issue
Block a user