From 92012fbc272afb1f17bf60842c951aa8e08d9461 Mon Sep 17 00:00:00 2001 From: Andrew Godwin Date: Wed, 29 Jun 2016 12:16:17 -0700 Subject: [PATCH] Fixed #87: Don't drop headers and status on empty streaming responses --- channels/handler.py | 5 ++-- channels/tests/test_handler.py | 48 ++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 3 deletions(-) diff --git a/channels/handler.py b/channels/handler.py index ee9e150..42cb44f 100644 --- a/channels/handler.py +++ b/channels/handler.py @@ -288,9 +288,8 @@ class AsgiHandler(base.BaseHandler): yield message message = {} # Final closing message - yield { - "more_content": False, - } + message["more_content"] = False + yield message # Other responses just need chunking else: # Yield chunks of response diff --git a/channels/tests/test_handler.py b/channels/tests/test_handler.py index 255f0b1..d529c01 100644 --- a/channels/tests/test_handler.py +++ b/channels/tests/test_handler.py @@ -143,6 +143,54 @@ class HandlerTests(ChannelTestCase): self.assertEqual(reply_messages[1]["content"], b"andhereistherest") self.assertEqual(reply_messages[1].get("more_content", False), False) + def test_empty(self): + """ + Tests an empty response + """ + # Make stub request and desired response + Channel("test").send({ + "reply_channel": "test", + "http_version": "1.1", + "method": "GET", + "path": b"/test/", + }) + response = HttpResponse(b"", status=304) + # Run the handler + handler = FakeAsgiHandler(response) + reply_messages = list( + handler(self.get_next_message("test", require=True)) + ) + # Make sure we got the right number of messages + self.assertEqual(len(reply_messages), 1) + # Make sure the messages look correct + self.assertEqual(reply_messages[0].get("content", b""), b"") + self.assertEqual(reply_messages[0]["status"], 304) + self.assertEqual(reply_messages[0]["more_content"], False) + + def test_empty_streaming(self): + """ + Tests an empty streaming response + """ + # Make stub request and desired response + Channel("test").send({ + "reply_channel": "test", + "http_version": "1.1", + "method": "GET", + "path": b"/test/", + }) + response = StreamingHttpResponse([], status=304) + # Run the handler + handler = FakeAsgiHandler(response) + reply_messages = list( + handler(self.get_next_message("test", require=True)) + ) + # Make sure we got the right number of messages + self.assertEqual(len(reply_messages), 1) + # Make sure the messages look correct + self.assertEqual(reply_messages[0].get("content", b""), b"") + self.assertEqual(reply_messages[0]["status"], 304) + self.assertEqual(reply_messages[0]["more_content"], False) + def test_chunk_bytes(self): """ Makes sure chunk_bytes works correctly