Skip to content

Commit

Permalink
Corrected docs & tests re that headers must use bytestrings. (#1246)
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiask authored and carltongibson committed Feb 12, 2019
1 parent 6f8bf16 commit a660d81
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
10 changes: 5 additions & 5 deletions docs/topics/consumers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ primitives to implement a HTTP endpoint::
async def handle(self, body):
await asyncio.sleep(10)
await self.send_response(200, b"Your response bytes", headers=[
("Content-Type", "text/plain"),
(b"Content-Type", b"text/plain"),
])

You are expected to implement your own ``handle`` method. The
Expand All @@ -352,7 +352,7 @@ be explained in detail later::
class LongPollConsumer(AsyncHttpConsumer):
async def handle(self, body):
await self.send_headers(headers=[
("Content-Type", "application/json"),
(b"Content-Type", b"application/json"),
])
# Headers are only sent after the first body event.
# Set "more_body" to tell the interface server to not
Expand All @@ -372,9 +372,9 @@ Of course you can also use those primitives to implement a HTTP endpoint for
class ServerSentEventsConsumer(AsyncHttpConsumer):
async def handle(self, body):
await self.send_headers(headers=[
("Cache-Control", "no-cache"),
("Content-Type", "text/event-stream"),
("Transfer-Encoding", "chunked"),
(b"Cache-Control", b"no-cache"),
(b"Content-Type", b"text/event-stream"),
(b"Transfer-Encoding", b"chunked"),
])
while True:
payload = "data: %s\n\n" % datetime.now().isoformat()
Expand Down
4 changes: 2 additions & 2 deletions tests/test_generic_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ async def handle(self, body):
await self.send_response(
200,
json.dumps({"value": data["value"]}).encode("utf-8"),
headers={"Content-Type": "application/json"},
headers={b"Content-Type": b"application/json"},
)

# Open a connection
Expand All @@ -31,4 +31,4 @@ async def handle(self, body):
response = await communicator.get_response()
assert response["body"] == b'{"value": 42}'
assert response["status"] == 200
assert response["headers"] == [("Content-Type", "application/json")]
assert response["headers"] == [(b"Content-Type", b"application/json")]

0 comments on commit a660d81

Please sign in to comment.