diff --git a/swift/common/swob.py b/swift/common/swob.py index 22af356612..9013541e89 100755 --- a/swift/common/swob.py +++ b/swift/common/swob.py @@ -1008,6 +1008,8 @@ class Response(object): return self.host_url + self.location def __call__(self, env, start_response): + if not self.request: + self.request = Request(env) self.environ = env app_iter = self._response_iter(self.app_iter, self._body) if 'location' in self.headers: diff --git a/test/unit/common/test_swob.py b/test/unit/common/test_swob.py index e5c3ab6136..06852504bf 100755 --- a/test/unit/common/test_swob.py +++ b/test/unit/common/test_swob.py @@ -462,6 +462,24 @@ class TestResponse(unittest.TestCase): resp.body = u'\N{SNOWMAN}' self.assertEquals(resp.body, u'\N{SNOWMAN}'.encode('utf-8')) + def test_call_reifies_request_if_necessary(self): + """ + The actual bug was a HEAD response coming out with a body because the + Request object wasn't passed into the Response object's constructor. + The Response object's __call__ method should be able to reify a + Request object from the env it gets passed. + """ + def test_app(environ, start_response): + start_response('200 OK', []) + return ['hi'] + req = swift.common.swob.Request.blank('/') + req.method = 'HEAD' + status, headers, app_iter = req.call_application(test_app) + resp = swift.common.swob.Response(status=status, headers=dict(headers), + app_iter=app_iter) + output_iter = resp(req.environ, lambda *_: None) + self.assertEquals(list(output_iter), ['']) + def test_location_rewrite(self): def start_response(env, headers): pass