Skip to content
This repository has been archived by the owner on Jun 10, 2024. It is now read-only.

Commit

Permalink
Merge 4010c9f into af1b97b
Browse files Browse the repository at this point in the history
  • Loading branch information
CptLemming committed Nov 25, 2015
2 parents af1b97b + 4010c9f commit 1661902
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
2 changes: 1 addition & 1 deletion responsive/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def process_response(self, request, response):
'cookie_age': 60 * 60 * 24 * settings.RESPONSIVE_COOKIE_AGE, # convert to secs
'cookie_expires': expires.strftime('%a, %d %b %Y %H:%M:%S GMT')
})
pattern = re.compile(b'<head(.*?)>', re.IGNORECASE)
pattern = re.compile(b'<head()>|<head (.*?)>', re.IGNORECASE)
response.content = pattern.sub(b'<head\g<1>>' + smart_bytes(snippet), response.content)

if response.get('Content-Length', None):
Expand Down
27 changes: 23 additions & 4 deletions tests/test_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from responsive.utils import Device


def hello(request):
def html_with_head(request):
html = """
<html>
<head>
Expand All @@ -24,6 +24,17 @@ def hello(request):
return HttpResponse(html)


def html_without_head(request):
html = """
<html>
<body>
<header>Test</header>
</body>
</html>
"""
return HttpResponse(html)


class MiddlewareTest(TestCase):

def setUp(self):
Expand Down Expand Up @@ -58,20 +69,28 @@ def test_process_request_with_a_bad_cookie(self):
self.middleware.process_request(request)
self.assertTrue(getattr(request, 'INVALID_RESPONSIVE_COOKIE'))

def test_reposnsive_htnl_snippet_is_injected_into_response(self):
def test_reponsive_html_snippet_is_injected_into_response(self):
request = self.factory.get('/')
response = hello(request)
response = html_with_head(request)
# test no <script> tag before any processing by the middleware
self.assertFalse(b'</script>' in response.content)
processed_response = self.middleware.process_response(request, response)
self.assertTrue(b'</script>' in processed_response.content)

def test_responsive_snippet_not_injected_without_head(self):
request = self.factory.get('/')
response = html_without_head(request)
# test no <script> tag before any processing by the middleware
self.assertFalse(b'</script>' in response.content)
processed_response = self.middleware.process_response(request, response)
self.assertFalse(b'</script>' in processed_response.content)

def test_snippet_is_not_injected_if_reponsive_cookie_already_exists(self):
request = self.factory.get('/')
request.COOKIES = {
settings.RESPONSIVE_COOKIE_NAME: '1024:768:2' # valid responsive cookie
}
response = hello(request)
response = html_with_head(request)
processed_response = self.middleware.process_response(request, response)
self.assertFalse(b'</script>' in processed_response.content)

Expand Down

0 comments on commit 1661902

Please sign in to comment.