diff options
| author | Michael Merickel <michael@merickel.org> | 2019-12-15 20:46:12 -0600 |
|---|---|---|
| committer | Michael Merickel <michael@merickel.org> | 2019-12-15 20:46:12 -0600 |
| commit | 8fe8725d4d20b18715291d3b45899d3389fe1d8b (patch) | |
| tree | 05fdfa5be326add83de8b3c0441a5ef3d5e86848 | |
| parent | c6340737a4826d4073f0cfa7951dccf42d0cbfcf (diff) | |
| download | pyramid-8fe8725d4d20b18715291d3b45899d3389fe1d8b.tar.gz pyramid-8fe8725d4d20b18715291d3b45899d3389fe1d8b.tar.bz2 pyramid-8fe8725d4d20b18715291d3b45899d3389fe1d8b.zip | |
handle a missing content negotiation case where the unencoded option is not available and the client requests an encoded variant that doesn't exist
| -rw-r--r-- | src/pyramid/static.py | 1 | ||||
| -rw-r--r-- | tests/fixtures/static/only_encoded.html.gz | bin | 0 -> 187 bytes | |||
| -rw-r--r-- | tests/test_static.py | 81 |
3 files changed, 82 insertions, 0 deletions
diff --git a/src/pyramid/static.py b/src/pyramid/static.py index 7870b803e..499706554 100644 --- a/src/pyramid/static.py +++ b/src/pyramid/static.py @@ -236,6 +236,7 @@ class static_view(object): for path, encoding in files: if encoding in acceptable_encodings: return path, encoding + return None, None def add_slash_redirect(self, request): url = request.path_url + '/' diff --git a/tests/fixtures/static/only_encoded.html.gz b/tests/fixtures/static/only_encoded.html.gz Binary files differnew file mode 100644 index 000000000..afcc25768 --- /dev/null +++ b/tests/fixtures/static/only_encoded.html.gz diff --git a/tests/test_static.py b/tests/test_static.py index 3d0deda3f..73814e222 100644 --- a/tests/test_static.py +++ b/tests/test_static.py @@ -458,6 +458,87 @@ class Test_static_view_content_encodings(unittest.TestCase): self.assertEqual(res.headers['Content-Encoding'], 'gzip') self.assertEqual(len(res.body), 187) + def test_call_for_encoded_variant_without_unencoded_variant_no_accept( + self, + ): + inst = self._makeOne( + 'tests:fixtures/static', content_encodings=['gzip'] + ) + request = self._makeRequest({'PATH_INFO': '/only_encoded.html.gz'}) + context = DummyContext() + + res = inst(context, request) + self.assertNotIn('Vary', res.headers) + self.assertNotIn('Content-Encoding', res.headers) + self.assertEqual(len(res.body), 187) + + def test_call_for_encoded_variant_without_unencoded_variant_with_accept( + self, + ): + inst = self._makeOne( + 'tests:fixtures/static', content_encodings=['gzip'] + ) + request = self._makeRequest( + { + 'PATH_INFO': '/only_encoded.html.gz', + 'HTTP_ACCEPT_ENCODING': 'gzip', + } + ) + context = DummyContext() + + res = inst(context, request) + self.assertNotIn('Vary', res.headers) + self.assertNotIn('Content-Encoding', res.headers) + self.assertEqual(len(res.body), 187) + + def test_call_for_unencoded_variant_with_only_encoded_variant_no_accept( + self, + ): + inst = self._makeOne( + 'tests:fixtures/static', content_encodings=['gzip'] + ) + request = self._makeRequest({'PATH_INFO': '/only_encoded.html'}) + context = DummyContext() + + res = inst(context, request) + self.assertNotIn('Vary', res.headers) + self.assertNotIn('Content-Encoding', res.headers) + self.assertEqual(len(res.body), 187) + + def test_call_for_unencoded_variant_with_only_encoded_variant_with_accept( + self, + ): + inst = self._makeOne( + 'tests:fixtures/static', content_encodings=['gzip'] + ) + request = self._makeRequest( + { + 'PATH_INFO': '/only_encoded.html', + 'HTTP_ACCEPT_ENCODING': 'gzip', + } + ) + context = DummyContext() + + res = inst(context, request) + self.assertNotIn('Vary', res.headers) + self.assertEqual(res.headers['Content-Encoding'], 'gzip') + self.assertEqual(len(res.body), 187) + + def test_call_for_unencoded_variant_with_only_encoded_variant_bad_accept( + self, + ): + from pyramid.httpexceptions import HTTPNotFound + + inst = self._makeOne( + 'tests:fixtures/static', content_encodings=['gzip'] + ) + request = self._makeRequest( + {'PATH_INFO': '/only_encoded.html', 'HTTP_ACCEPT_ENCODING': 'br',} + ) + context = DummyContext() + + self.assertRaises(HTTPNotFound, lambda: inst(context, request)) + def test_call_get_possible_files_is_cached(self): inst = self._makeOne('tests:fixtures/static') result1 = inst.get_possible_files('tests:fixtures/static/encoded.html') |
