Skip to content

Commit

Permalink
Checking presence of EXCLUDE_FROM_MINIFYING (closes #7)
Browse files Browse the repository at this point in the history
  • Loading branch information
fsouza committed May 22, 2011
1 parent 6313521 commit fe3b789
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
12 changes: 7 additions & 5 deletions htmlmin/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ class HtmlMinifyMiddleware(object):

def can_minify_response(self, request, response):
is_request_ok = True
for url_pattern in settings.EXCLUDE_FROM_MINIFYING:
regex = re.compile(url_pattern)
if regex.match(request.path.lstrip('/')):
is_request_ok = False
break

if hasattr(settings, 'EXCLUDE_FROM_MINIFYING'):
for url_pattern in settings.EXCLUDE_FROM_MINIFYING:
regex = re.compile(url_pattern)
if regex.match(request.path.lstrip('/')):
is_request_ok = False
break

is_response_ok = response.status_code == 200 and 'text/html' in response['Content-Type']
return is_request_ok and is_response_ok
Expand Down
Empty file.
11 changes: 11 additions & 0 deletions htmlmin/tests/test_middleware.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
import sys
import unittest
from django.conf import settings
from htmlmin.middleware import HtmlMinifyMiddleware
from htmlmin.tests import TESTS_DIR
from mocks import RequestMock, ResponseMock
Expand Down Expand Up @@ -54,3 +55,13 @@ def test_middleware_should_not_minify_the_response_on_urls_that_are_configured_t
response_mock = ResponseMock()
response = HtmlMinifyMiddleware().process_response(RequestMock('/raw/'), response_mock)
assert_equals(html_not_minified, response.content)

def test_middleware_should_minify_if_exclude_from_minifying_is_not_set(self):
old = settings.EXCLUDE_FROM_MINIFYING
del settings.EXCLUDE_FROM_MINIFYING

html_minified = "<!DOCTYPE html><html> <body>some text here</body> </html>"
response = HtmlMinifyMiddleware().process_response(RequestMock(), ResponseMock())
assert_equals(html_minified, response.content)

settings.EXCLUDE_FROM_MINIFYING = old

0 comments on commit fe3b789

Please sign in to comment.