Don't allow webob to set a default content type

Webob sets the Content-Type of responses to 'text/html' if nothing else
is set. There are a number of bugs out for webob with this but it seems
unlikely it will be fixed upstream.

We don't want auth_token middleware automatically setting content-types
on application responses so create our own response object with an empty
default_content_type so it's not set.

Change-Id: I947f384e170a8d2685b71e1a6611c6241e827196
Closes-Bug: #1466499
This commit is contained in:
Jamie Lennox 2015-06-23 10:22:25 +08:00
parent aba3846d8c
commit d597d3dd13
2 changed files with 32 additions and 0 deletions

View File

@ -53,8 +53,17 @@ def _v3_to_v2_catalog(catalog):
return v2_services
# NOTE(jamielennox): this should probably be moved into its own file, but at
# the moment there's no real logic here so just keep it locally.
class _AuthTokenResponse(webob.Response):
default_content_type = None # prevents webob assigning a content type
class _AuthTokenRequest(webob.Request):
ResponseClass = _AuthTokenResponse
_HEADER_TEMPLATE = {
'X%s-Domain-Id': 'domain_id',
'X%s-Domain-Name': 'domain_name',

View File

@ -1321,6 +1321,29 @@ class CommonAuthTokenMiddlewareTest(object):
self.assertFalse(token_auth.has_service_token)
self.assertIsNone(token_auth.service)
def test_doesnt_auto_set_content_type(self):
# webob will set content_type = 'text/html' by default if nothing is
# provided. We don't want our middleware messing with the content type
# of the underlying applications.
text = uuid.uuid4().hex
def _middleware(environ, start_response):
start_response(200, [])
return text
def _start_response(status_code, headerlist, exc_info=None):
self.assertIn('200', status_code) # will be '200 OK'
self.assertEqual([], headerlist)
m = auth_token.AuthProtocol(_middleware, self.conf)
env = {'REQUEST_METHOD': 'GET',
'HTTP_X_AUTH_TOKEN': self.token_dict['uuid_token_default']}
r = m(env, _start_response)
self.assertEqual(text, r)
class V2CertDownloadMiddlewareTest(BaseAuthTokenMiddlewareTest,
testresources.ResourcedTestCase):