Skip to content

Commit

Permalink
Merge pull request #3 from doordash/assert-bug
Browse files Browse the repository at this point in the history
update prerender white list and some bug fixes
  • Loading branch information
zicwu authored Jun 21, 2016
2 parents bfdebc2 + c4a1832 commit 99173fd
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 30 deletions.
17 changes: 11 additions & 6 deletions django_seo_js/backends/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,15 @@ def __init__(self, *args, **kwargs):
self.session = requests.Session()

def build_django_response_from_requests_response(self, response):
r = HttpResponse(response.content)
if response.content:
resp = HttpResponse(response.content)
resp['Content-Length'] = len(response.content)
else:
resp = HttpResponse()

resp.status_code = response.status_code

for k, v in response.headers.items():
if k not in IGNORED_HEADERS:
r[k] = v
r['content-length'] = len(response.content)
r.status_code = response.status_code
return r
if k.lower() not in IGNORED_HEADERS:
resp[k] = v
return resp
1 change: 0 additions & 1 deletion django_seo_js/backends/prerender.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ def get_response_for_url(self, url):
'X-Prerender-Token': self.token,
}
r = self.session.get(render_url, headers=headers, allow_redirects=False)
assert r.status_code < 500

return self.build_django_response_from_requests_response(r)

Expand Down
47 changes: 28 additions & 19 deletions django_seo_js/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,36 +12,45 @@ def update_cache_for_url(url):


def request_should_be_ignored(request):
if settings.ALLOWED_URLS:
return not request_in_allowed_url_list(request)
path = request.path
if settings.IGNORE_URLS and is_path_in_black_list(path):
# black list is enabled and path is in the black list
return True

return request_in_ignore_url_list(request) or request_in_ingore_extensions_list(request)
if settings.IGNORE_EXTENSIONS and is_path_in_ignore_extensions_list(path):
return True

if settings.ALLOWED_URLS and not is_path_in_white_list(path):
# white list is enabled and path is not in the white list
return True

def request_in_ignore_url_list(request):
for url in settings.IGNORE_URLS:
if url in request.path:
return False


def is_path_in_list(path, pattern_list):
for pattern in pattern_list:
regex = fnmatch.translate(pattern)
re_obj = re.compile(regex)
if re_obj.match(path):
return True

return False


def request_in_ingore_extensions_list(request):
def is_path_in_white_list(path):
return is_path_in_list(path, settings.ALLOWED_URLS)


def is_path_in_black_list(path):
return is_path_in_list(path, settings.IGNORE_URLS)


def is_path_in_ignore_extensions_list(path):
extension = None
last_dot = request.path.rfind(".")
last_dot = path.rfind(".")
if last_dot == -1:
# No extension found
return False

extension = request.path[last_dot:]
extension = path[last_dot:]
return extension and extension in settings.IGNORE_EXTENSIONS


def request_in_allowed_url_list(request):
for pattern in settings.ALLOWED_URLS:
regex = fnmatch.translate(pattern)
re_obj = re.compile(regex)
if re_obj.match(request.path):
return True

return False
2 changes: 0 additions & 2 deletions django_seo_js/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

IGNORE_URLS = frozenset(getattr(django_settings, 'SEO_JS_IGNORE_URLS', ['/sitemap.xml']))

# ALLOWED_URLS will override IGNORE_URLS
# i.e. If a url exists at both IGNORE_URLS and ALLOWED_URLS, it's considered ALLOWED.
ALLOWED_URLS = frozenset(getattr(django_settings, 'SEO_JS_ALLOWED_URLS', []))

IGNORE_EXTENSIONS = frozenset(getattr(django_settings, 'SEO_JS_IGNORE_EXTENSIONS', (
Expand Down
6 changes: 4 additions & 2 deletions django_seo_js/tests/test_middlewares.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,18 +91,20 @@ def test_overriding_skips_custom_overrides_gifs_by_default(self):
@override_settings(
BACKEND='django_seo_js.backends.TestBackend',
ALLOWED_URLS=['/bar/*', '/test/abc/*', '/foo/'],
IGNORE_URLS=[],
IGNORE_URLS=['/bar/1234/'],
IGNORE_EXTENSIONS=[],
)
def test_overriding_with_allowed_urls(self):
self.middleware = EscapedFragmentMiddleware()
self.request.GET = {"_escaped_fragment_": None}

# In the white list, not in black list -> "Test"
self.request.path = '/bar/'
self.assertEqual(self.middleware.process_request(self.request).content, "Test")

# In the white list and black list -> None
self.request.path = '/bar/1234/'
self.assertEqual(self.middleware.process_request(self.request).content, "Test")
self.assertEqual(self.middleware.process_request(self.request), None)

self.request.path = '/bar/foo/5678/'
self.assertEqual(self.middleware.process_request(self.request).content, "Test")
Expand Down

0 comments on commit 99173fd

Please sign in to comment.