1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
|
Fix for bug report #695050 (response header bug) imported from Ubuntu (LP #920197).
--- a/tests/test_exc.py
+++ b/tests/test_exc.py
@@ -55,6 +55,7 @@
from webob.exc import HTTPInsufficientStorage
from webob.exc import HTTPExceptionMiddleware
from webob import exc
+from webob.exc import status_map
from nose.tools import eq_, ok_, assert_equal, assert_raises
@@ -250,6 +251,43 @@
exc.newstyle_exceptions = False
assert_equal( excep(environ,start_response), [] )
+def test_HTTPOk_head_of_proxied_head():
+ # first set up a response to a HEAD request
+ HELLO_WORLD = "Hi!\n"
+ CONTENT_TYPE = "application/hello"
+ def head_app(environ, start_response):
+ """An application object that understands HEAD"""
+ status = '200 OK'
+ response_headers = [('Content-Type', CONTENT_TYPE),
+ ('Content-Length', len(HELLO_WORLD))]
+ start_response(status, response_headers)
+
+ if environ['REQUEST_METHOD'] == 'HEAD':
+ return []
+ else:
+ return [HELLO_WORLD]
+
+ def verify_response(resp, description):
+ assert_equal(resp.content_type, CONTENT_TYPE, description)
+ assert_equal(resp.content_length, len(HELLO_WORLD), description)
+ assert_equal(resp.body, '', description)
+
+ req = Request.blank('/', method='HEAD')
+ resp1 = req.get_response(head_app)
+ verify_response(resp1, "first response")
+
+ # Copy the response like a proxy server would.
+ # Copying an empty body has set content_length
+ # so copy the headers only afterwards.
+ resp2 = status_map[resp1.status_int](request=req)
+ resp2.body = resp1.body
+ resp2.headerlist = resp1.headerlist
+ verify_response(resp2, "copied response")
+
+ # evaluate it again
+ resp3 = req.get_response(resp2)
+ verify_response(resp3, "evaluated copy")
+
def test_HTTPMove():
def start_response(status, headers, exc_info=None):
pass
--- a/webob/exc.py
+++ b/webob/exc.py
@@ -314,11 +314,12 @@
return resp(environ, start_response)
def __call__(self, environ, start_response):
- if self.body or self.empty_body:
+ is_head = environ['REQUEST_METHOD'] == 'HEAD'
+ if self.body or self.empty_body or is_head:
app_iter = Response.__call__(self, environ, start_response)
else:
app_iter = self.generate_response(environ, start_response)
- if environ['REQUEST_METHOD'] == 'HEAD':
+ if is_head:
app_iter = []
return app_iter
|