convert header values to strings

This commit is contained in:
Raphael Deem 2016-12-13 21:24:26 -08:00
parent ef9d8710f5
commit 7e6c92dc52
2 changed files with 29 additions and 1 deletions

View File

@ -1,5 +1,5 @@
from aiofiles import open as open_async
from .cookies import CookieJar
from .cookies import CookieJar, Cookie
from mimetypes import guess_type
from os import path
from ujson import dumps as json_dumps
@ -97,6 +97,8 @@ class HTTPResponse:
headers = b''
if self.headers:
headers = b''.join(
b'%b: %b\r\n' % (name.encode(), str(value).encode('utf-8'))
if type(value) != Cookie else
b'%b: %b\r\n' % (name.encode(), value.encode('utf-8'))
for name, value in self.headers.items()
)

View File

@ -32,6 +32,32 @@ def test_text():
assert response.text == 'Hello'
def test_headers():
app = Sanic('test_text')
@app.route('/')
async def handler(request):
headers = {"spam": "great"}
return text('Hello', headers=headers)
request, response = sanic_endpoint_test(app)
assert response.headers.get('spam') == 'great'
def test_invalid_headers():
app = Sanic('test_text')
@app.route('/')
async def handler(request):
headers = {"answer": 42}
return text('Hello', headers=headers)
request, response = sanic_endpoint_test(app)
assert response.headers.get('answer') == '42'
def test_json():
app = Sanic('test_json')