Skip to content

Commit

Permalink
middleware: check if content-type is defined
Browse files Browse the repository at this point in the history
closes #98
  • Loading branch information
andrewsmedina committed Mar 21, 2016
1 parent 3cb2e19 commit 3aa4916
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion htmlmin/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def can_minify_response(self, request, response):
req_ok = False
break

resp_ok = 'text/html' in response['Content-Type']
resp_ok = 'text/html' in response.get('Content-Type', '')
if hasattr(response, 'minify_response'):
resp_ok = resp_ok and response.minify_response
return req_ok and resp_ok
Expand Down
10 changes: 10 additions & 0 deletions htmlmin/tests/test_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ def test_should_minify_with_any_charset(self):
minified = "<html><head></head><body>some text here</body></html>"
self.assertEqual(minified, response.content)

def test_should_not_minify_without_content(self):
response_mock = ResponseMock()
del response_mock['Content-Type']
response = HtmlMinifyMiddleware().process_response(
RequestMock(), response_mock,
)

html_not_minified = "<html> <body>some text here</body> </html>"
self.assertEqual(html_not_minified, response.content)

def test_should_not_minify_not_html_content(self):
response_mock = ResponseMock()
response_mock['Content-Type'] = 'application/json'
Expand Down

0 comments on commit 3aa4916

Please sign in to comment.