Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #45 from mvidner/master

Don't replace headers of a WSGIHTTPException replying to a HEAD
  • Loading branch information...
commit d3b4f17e51a51c524db4379fae335d181ae3d985 2 parents 3615dc2 + 98f1ea3
@maluke maluke authored
Showing with 41 additions and 2 deletions.
  1. +38 −0 tests/test_exc.py
  2. +3 −2 webob/exc.py
View
38 tests/test_exc.py
@@ -7,6 +7,7 @@
from webob.exc import _HTTPMove
from webob.exc import HTTPMethodNotAllowed
from webob.exc import HTTPExceptionMiddleware
+from webob.exc import status_map
from nose.tools import eq_, ok_, assert_equal, assert_raises
@@ -208,6 +209,43 @@ def start_response(status, headers, exc_info=None):
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
View
5 webob/exc.py
@@ -327,11 +327,12 @@ def generate_response(self, environ, start_response):
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
Please sign in to comment.
Something went wrong with that request. Please try again.