File 0002-Don-t-replace-headers-of-a-WSGIHTTPException-replyin.patch of Package python-WebOb

From 4b864b06dae31061a68338fa01bdf0b72c359cc5 Mon Sep 17 00:00:00 2001
From: Martin Vidner <mvidner@suse.cz>
Date: Fri, 4 May 2012 11:18:05 +0200
Subject: [PATCH 2/4] Don't replace headers of a WSGIHTTPException replying to
 a HEAD (issue 44)

Warning, the patch was edited
to account for context differences between 1.2b and 1.1.1

https://github.com/Pylons/webob/issues/44
---
 tests/test_exc.py |   38 ++++++++++++++++++++++++++++++++++++++
 webob/exc.py      |    5 +++--
 2 files changed, 41 insertions(+), 2 deletions(-)

diff --git a/tests/test_exc.py b/tests/test_exc.py
index f0c6f69..2518c23 100644
--- a/tests/test_exc.py
+++ b/tests/test_exc.py
@@ -54,6 +54,7 @@ from webob.exc import HTTPGatewayTimeout
 from webob.exc import HTTPVersionNotSupported
 from webob.exc import HTTPInsufficientStorage
 from webob.exc import HTTPExceptionMiddleware
+from webob.exc import status_map
 from webob import exc
 
 from nose.tools import eq_, ok_, assert_equal, assert_raises
@@ -250,6 +251,43 @@ def test_WSGIHTTPException_exception_no_newstyle():
     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_code](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
diff --git a/webob/exc.py b/webob/exc.py
index 5f078e2..691596d 100644
--- a/webob/exc.py
+++ b/webob/exc.py
@@ -314,11 +314,12 @@ ${body}''')
         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
 
-- 
1.7.7

openSUSE Build Service is sponsored by