Skip to content

Commit

Permalink
Fix test and add new test for Content-Type header of empty exception …
Browse files Browse the repository at this point in the history
…body.
  • Loading branch information
mauritsvanrees committed Jan 9, 2023
1 parent ea741c2 commit a1c38c0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/ZPublisher/WSGIPublisher.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,19 +149,22 @@ def _exc_view_created_response(exc, request, response):
for key, value in exc.headers.items():
response.setHeader(key, value)

# Set the response body to the result of calling the view.
# Call the view so we can use it as the response body.
body = view()
response.setBody(body)

# Explicitly set the content type header if it's not there yet so
# the response does not get served with the text/plain default.
# But only do this when there is a body.
# An empty body may indicate a 304 NotModified from Plone,
# and setting a content type header will change the stored header
# in for example Varnish.
# See https://github.com/zopefoundation/Zope/pull/1088
if body and not response.getHeader('Content-Type'):
response.setHeader('Content-Type', 'text/html')

# Note: setBody would set the Content-Type header to text/plain
# if it is not set yet, except when the body is empty.
response.setBody(body)
return True

return False
Expand Down
20 changes: 20 additions & 0 deletions src/ZPublisher/tests/test_WSGIPublisher.py
Original file line number Diff line number Diff line change
Expand Up @@ -919,6 +919,26 @@ def testWithStandardErrorMessage(self):
# After rendering the response content-type header is set
self.assertIn('text/html', response.getHeader('Content-Type'))

def testWithEmptyErrorMessage(self):
from OFS.DTMLMethod import addDTMLMethod
from zExceptions import NotFound
self._registerStandardErrorView()
response = self.app.REQUEST.RESPONSE
addDTMLMethod(self.app, 'standard_error_message', file='')
self.app.standard_error_message.raw = ''

# The response content-type header is not set before rendering
# the standard error template
self.assertFalse(response.getHeader('Content-Type'))

try:
self.assertTrue(self._callFUT(NotFound))
finally:
self._unregisterStandardErrorView()

# After rendering the response still no content-type header is set
self.assertFalse(response.getHeader('Content-Type'))


class WSGIPublisherTests(FunctionalTestCase):

Expand Down

0 comments on commit a1c38c0

Please sign in to comment.