Skip to content

Commit

Permalink
Don't raise KeyError if the client doesn't send Accept-Encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
terite authored and lukearno committed Oct 20, 2014
1 parent 4ab8a74 commit 6ffb96d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
2 changes: 1 addition & 1 deletion static/apps.py
Expand Up @@ -100,7 +100,7 @@ def __call__(self, environ, start_response):
return self.moved_permanently(environ, start_response, headers)
else:
full_path = self._full_path(path_info + self.index_file)
prezipped = ('gzip' in environ['HTTP_ACCEPT_ENCODING']
prezipped = ('gzip' in environ.get('HTTP_ACCEPT_ENCODING', '')
and path.exists(full_path + '.gz'))
if prezipped:
full_path += '.gz'
Expand Down
23 changes: 22 additions & 1 deletion tests/test_behaviors.py
Expand Up @@ -15,6 +15,15 @@
import static


class StripAcceptEncoding(object):
def __init__(self, application):
self.application = application

def __call__(self, environ, start_response):
environ.pop('HTTP_ACCEPT_ENCODING', None)
return self.application(environ, start_response)


class Intercepted(TestCase):

def setUp(self):
Expand Down Expand Up @@ -214,8 +223,12 @@ def test_client_gets_a_404_for_a_missing_file(self):

class StaticClingWithPrezipping(Intercepted):

def setUp(self):
self._app = static.Cling('tests/data/prezip')
super(StaticClingWithPrezipping, self).setUp()

def get_app(self):
return static.Cling('tests/data/prezip')
return self._app

def test_client_gets_prezipped_content(self):
self.assert_response(
Expand All @@ -225,6 +238,14 @@ def test_client_gets_prezipped_content(self):
file_content="tests/data/prezip/static.txt.gz")

def test_client_gets_non_prezipped_when_no_accept_encoding_present(self):
# strip HTTP_ACCEPT_ENCODING from the environ, to simulate not getting the header at all
self._app = StripAcceptEncoding(self.get_app())
self.assert_response(
'GET', '/static.txt', {},
200,
file_content="tests/data/prezip/static.txt")

def test_client_gets_non_prezipped_when_accept_missing_gzip(self):
self.assert_response(
'GET', '/static.txt', {},
200,
Expand Down

0 comments on commit 6ffb96d

Please sign in to comment.