Permalink
Browse files

Fix. pyroutes was generating *two* Content-Tyṕe headers in the final

response

Example:

@route('/foo')
def foo_handler(req):
  return Response('Hello', headers=[("Content-Type", "text/plain")])

$ curl http://localhost:8001/foo
HTTP/1.0 200 OK
Date: Sat, 17 Apr 2010 17:30:30 GMT
Server: WSGIServer/0.1 Python/2.6.4
Content-Type: text/html; charset=utf-8
Content-Type: text/plain
Content-Length: 5
  • Loading branch information...
daltonmatos authored and klette committed Apr 17, 2010
1 parent 023663b commit 44d1708f83e647aa6dfe5442e139e0a5f9ad6f42
Showing with 12 additions and 1 deletion.
  1. +5 −1 pyroutes/http/response.py
  2. +7 −0 tests/response_test.py
@@ -73,8 +73,12 @@ def __init__(self, content=None, headers=None, status_code='200 OK',
else:
self.content = content
+ header_names = []
+ if headers:
+ header_names = map(lambda h: h[0], headers);
+
self.headers = []
- if default_content_header:
+ if default_content_header and 'Content-Type' not in header_names:
# Whether or not this is a safe operation may be python/wsgiref
# version dependent. Should be tested and documented.
self.headers.append(('Content-Type', settings.DEFAULT_CONTENT_TYPE))
View
@@ -17,6 +17,13 @@ def test_init(self):
self.assertEqual(response.headers, [('Content-Type', settings.DEFAULT_CONTENT_TYPE), ('Content-Length', 1000)])
self.assertEqual(response.status_code, '500 Error')
+ def test_duplicated_content_type_header(self):
+ content_header = [("Content-Type", "application/pdf")]
+ response = Response(headers=content_header)
+ self.assertTrue(len(response.headers) == 1, "Should contain only one Content-Type header")
+ # And this header should not be settings.DEFAULT_CONTENT_TYPE
+ self.assertEqual(response.headers, content_header)
+
class TestExceptions(unittest.TestCase):
def test_base_exception(self):
self.assertRaises(TypeError, HttpException)

0 comments on commit 44d1708

Please sign in to comment.