Do not add a default content type when replying

WebOb includes a default content_type, and that's incorrect for status
code such as 304. This fixes that by generalizing the previous fix we
introduced for the CORS middleware.

Change-Id: I57c47ee5627b17a4e266b9ac4bc038dfccb35012
This commit is contained in:
Julien Danjou 2016-05-10 17:05:24 +02:00
parent 6f1db02ef0
commit f553a614de
3 changed files with 26 additions and 8 deletions

View File

@ -17,10 +17,22 @@
from inspect import getargspec
import webob.dec
import webob.request
import webob.response
from oslo_config import cfg
class NoContentTypeResponse(webob.response.Response):
default_content_type = None # prevents webob assigning content type
class NoContentTypeRequest(webob.request.Request):
ResponseClass = NoContentTypeResponse
class ConfigurableMiddleware(object):
"""Base WSGI middleware wrapper.
@ -106,7 +118,7 @@ class ConfigurableMiddleware(object):
"""Do whatever you'd like to the response."""
return response
@webob.dec.wsgify
@webob.dec.wsgify(RequestClass=NoContentTypeRequest)
def __call__(self, req):
response = self.process_request(req)
if response:

View File

@ -20,7 +20,7 @@ from oslo_config import cfg
from oslo_middleware import base
import six
import webob.exc
import webob.response
LOG = logging.getLogger(__name__)
@ -98,11 +98,6 @@ class InvalidOriginError(Exception):
'CORS request from origin \'%s\' not permitted.' % origin)
class _NoContentTypeResponse(webob.response.Response):
default_content_type = None # prevents webob assigning content type
class CORS(base.ConfigurableMiddleware):
"""CORS Middleware.
@ -325,7 +320,7 @@ class CORS(base.ConfigurableMiddleware):
# underlying middleware's response content needs to be persisted.
# Otherwise, create a new response.
if 200 > response.status_code or response.status_code >= 300:
response = _NoContentTypeResponse(status=webob.exc.HTTPOk.code)
response = base.NoContentTypeResponse(status=webob.exc.HTTPOk.code)
# Does the request have an origin header? (Section 6.2.1)
if 'Origin' not in request.headers:

View File

@ -58,6 +58,17 @@ class TestBase(BaseTestCase):
self.assertTrue(self.application.called_without_request)
def test_no_content_type_added(self):
class TestMiddleware(Middleware):
@staticmethod
def process_request(req):
return "foobar"
m = TestMiddleware(None)
request = webob.Request({}, method='GET')
response = request.get_response(m)
self.assertNotIn('Content-Type', response.headers)
def test_paste_deploy_legacy(self):
app = LegacyMiddlewareTest.factory(
{'global': True}, local=True)(application)