Skip to content
This repository
Browse code

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
Sergey Schetinin maluke authored

Showing 2 changed files with 41 additions and 2 deletions. Show diff stats Hide diff stats

  1. +38 0 tests/test_exc.py
  2. +3 2 webob/exc.py
38 tests/test_exc.py
@@ -7,6 +7,7 @@
7 7 from webob.exc import _HTTPMove
8 8 from webob.exc import HTTPMethodNotAllowed
9 9 from webob.exc import HTTPExceptionMiddleware
  10 +from webob.exc import status_map
10 11
11 12 from nose.tools import eq_, ok_, assert_equal, assert_raises
12 13
@@ -208,6 +209,43 @@ def start_response(status, headers, exc_info=None):
208 209 exc.newstyle_exceptions = False
209 210 assert_equal( excep(environ,start_response), [] )
210 211
  212 +def test_HTTPOk_head_of_proxied_head():
  213 + # first set up a response to a HEAD request
  214 + HELLO_WORLD = "Hi!\n"
  215 + CONTENT_TYPE = "application/hello"
  216 + def head_app(environ, start_response):
  217 + """An application object that understands HEAD"""
  218 + status = '200 OK'
  219 + response_headers = [('Content-Type', CONTENT_TYPE),
  220 + ('Content-Length', len(HELLO_WORLD))]
  221 + start_response(status, response_headers)
  222 +
  223 + if environ['REQUEST_METHOD'] == 'HEAD':
  224 + return []
  225 + else:
  226 + return [HELLO_WORLD]
  227 +
  228 + def verify_response(resp, description):
  229 + assert_equal(resp.content_type, CONTENT_TYPE, description)
  230 + assert_equal(resp.content_length, len(HELLO_WORLD), description)
  231 + assert_equal(resp.body, '', description)
  232 +
  233 + req = Request.blank('/', method='HEAD')
  234 + resp1 = req.get_response(head_app)
  235 + verify_response(resp1, "first response")
  236 +
  237 + # Copy the response like a proxy server would.
  238 + # Copying an empty body has set content_length
  239 + # so copy the headers only afterwards.
  240 + resp2 = status_map[resp1.status_int](request=req)
  241 + resp2.body = resp1.body
  242 + resp2.headerlist = resp1.headerlist
  243 + verify_response(resp2, "copied response")
  244 +
  245 + # evaluate it again
  246 + resp3 = req.get_response(resp2)
  247 + verify_response(resp3, "evaluated copy")
  248 +
211 249 def test_HTTPMove():
212 250 def start_response(status, headers, exc_info=None):
213 251 pass
5 webob/exc.py
@@ -327,11 +327,12 @@ def generate_response(self, environ, start_response):
327 327 return resp(environ, start_response)
328 328
329 329 def __call__(self, environ, start_response):
330   - if self.body or self.empty_body:
  330 + is_head = environ['REQUEST_METHOD'] == 'HEAD'
  331 + if self.body or self.empty_body or is_head:
331 332 app_iter = Response.__call__(self, environ, start_response)
332 333 else:
333 334 app_iter = self.generate_response(environ, start_response)
334   - if environ['REQUEST_METHOD'] == 'HEAD':
  335 + if is_head:
335 336 app_iter = []
336 337 return app_iter
337 338

0 comments on commit d3b4f17

Please sign in to comment.
Something went wrong with that request. Please try again.