Merge pull request #166 from lichray/http-headers

feat(req+resp): add HTTP headers property
This commit is contained in:
Kurt Griffiths
2013-08-28 09:20:12 -07:00
2 changed files with 24 additions and 0 deletions

View File

@@ -344,6 +344,22 @@ class Request(object):
"""Value of the User-Agent string, or None if missing."""
return self._get_header_by_wsgi_name('USER_AGENT')
@property
def headers(self):
"""Get HTTP headers
Build a temporary dictionary of dash-separated HTTP headers,
which can be used as a whole, like, to perform an HTTP request.
If you want to lookup a header, please use `get_header` instead.
Returns:
A dictionary of HTTP headers.
"""
return dict((k.lower().replace('_', '-'), v)
for k, v in self._headers.items())
def get_header(self, name, required=False):
"""Return a header value as a string

View File

@@ -206,6 +206,14 @@ class TestHeaders(testing.TestBase):
actual_value = self.resource.req.get_header(name)
self.assertEquals(actual_value, expected_value)
self.simulate_request(self.test_route,
headers=self.resource.req.headers)
# Compare the request HTTP headers with the original headers
for name, expected_value in req_headers.items():
actual_value = self.resource.req.get_header(name)
self.assertEquals(actual_value, expected_value)
def test_passthrough_resp_headers(self):
self.simulate_request(self.test_route)