From 26fa870540f175eb4046a836abb465ebf1a14612 Mon Sep 17 00:00:00 2001 From: Andrew Godwin Date: Wed, 29 Nov 2017 21:27:24 -0800 Subject: [PATCH] Move to "body" everywhere in HTTP messages --- daphne/http_protocol.py | 10 ++++----- setup.py | 1 + tests/http_base.py | 2 +- tests/test_http_request.py | 2 +- tests/test_http_response.py | 42 ++++++++++++++++++------------------- 5 files changed, 29 insertions(+), 28 deletions(-) diff --git a/daphne/http_protocol.py b/daphne/http_protocol.py index 7a1a7fc..aca0c68 100755 --- a/daphne/http_protocol.py +++ b/daphne/http_protocol.py @@ -207,13 +207,13 @@ class WebRequest(http.Request): header = header.encode("latin1") self.responseHeaders.addRawHeader(header, value) logger.debug("HTTP %s response started for %s", message["status"], self.client_addr) - elif message["type"] == "http.response.content": + elif message["type"] == "http.response.body": if not self._response_started: raise ValueError("HTTP response has not yet been started but got %s" % message["type"]) # Write out body - http.Request.write(self, message.get("content", b"")) + http.Request.write(self, message.get("body", b"")) # End if there's no more content - if not message.get("more_content", False): + if not message.get("more_body", False): self.finish() logger.debug("HTTP response complete for %s", self.client_addr) try: @@ -281,8 +281,8 @@ class WebRequest(http.Request): ], }) self.handle_reply({ - "type": "http.response.content", - "content": (self.error_template % { + "type": "http.response.body", + "body": (self.error_template % { "title": six.text_type(status) + " " + status_text.decode("ascii"), "body": body, }).encode("utf8"), diff --git a/setup.py b/setup.py index c33fd82..4313762 100755 --- a/setup.py +++ b/setup.py @@ -34,6 +34,7 @@ setup( "tests": [ "hypothesis", "pytest", + "pytest-asyncio~=0.8", ], }, entry_points={"console_scripts": [ diff --git a/tests/http_base.py b/tests/http_base.py index 4d21b9e..5d61e50 100644 --- a/tests/http_base.py +++ b/tests/http_base.py @@ -160,7 +160,7 @@ class DaphneTestCase(unittest.TestCase): xff=xff, responses=[ {"type": "http.response.start", "status": 200}, - {"type": "http.response.content", "content": b"OK"}, + {"type": "http.response.body", "body": b"OK"}, ], ) return scope, messages diff --git a/tests/test_http_request.py b/tests/test_http_request.py index 879312f..22bdae7 100644 --- a/tests/test_http_request.py +++ b/tests/test_http_request.py @@ -83,7 +83,7 @@ class TestHTTPRequest(DaphneTestCase): # Check overall keys self.assert_key_sets( required_keys={"type"}, - optional_keys={"body", "more_content"}, + optional_keys={"body", "more_body"}, actual_keys=message.keys(), ) # Check that it is the right type diff --git a/tests/test_http_response.py b/tests/test_http_response.py index 3f53a40..4f514e0 100644 --- a/tests/test_http_response.py +++ b/tests/test_http_response.py @@ -31,8 +31,8 @@ class TestHTTPResponse(DaphneTestCase): "status": 200, }, { - "type": "http.response.content", - "content": b"hello world", + "type": "http.response.body", + "body": b"hello world", }, ]) self.assertEqual(response.status, 200) @@ -51,8 +51,8 @@ class TestHTTPResponse(DaphneTestCase): "type": "http.response.start", }, { - "type": "http.response.content", - "content": b"hello world", + "type": "http.response.body", + "body": b"hello world", }, ]) @@ -66,8 +66,8 @@ class TestHTTPResponse(DaphneTestCase): "status": 201, }, { - "type": "http.response.content", - "content": b"i made a thing!", + "type": "http.response.body", + "body": b"i made a thing!", }, ]) self.assertEqual(response.status, 201) @@ -83,13 +83,13 @@ class TestHTTPResponse(DaphneTestCase): "status": 201, }, { - "type": "http.response.content", - "content": b"chunk 1 ", - "more_content": True, + "type": "http.response.body", + "body": b"chunk 1 ", + "more_body": True, }, { - "type": "http.response.content", - "content": b"chunk 2", + "type": "http.response.body", + "body": b"chunk 2", }, ]) self.assertEqual(response.status, 201) @@ -105,17 +105,17 @@ class TestHTTPResponse(DaphneTestCase): "status": 201, }, { - "type": "http.response.content", - "content": b"chunk 1 ", - "more_content": True, + "type": "http.response.body", + "body": b"chunk 1 ", + "more_body": True, }, { - "type": "http.response.content", - "content": b"chunk 2", - "more_content": True, + "type": "http.response.body", + "body": b"chunk 2", + "more_body": True, }, { - "type": "http.response.content", + "type": "http.response.body", }, ]) self.assertEqual(response.status, 201) @@ -133,8 +133,8 @@ class TestHTTPResponse(DaphneTestCase): "status": 200, }, { - "type": "http.response.content", - "content": body, + "type": "http.response.body", + "body": body, }, ]) self.assertEqual(response.status, 200) @@ -151,7 +151,7 @@ class TestHTTPResponse(DaphneTestCase): "headers": self.normalize_headers(headers), }, { - "type": "http.response.content", + "type": "http.response.body", }, ]) # Check headers in a sensible way. Ignore transfer-encoding.