From f6cb1efa8fba683bdc5c9b4a645f9357fe2e6208 Mon Sep 17 00:00:00 2001 From: Michael Merickel Date: Wed, 4 Dec 2019 01:13:52 -0600 Subject: negotiate the best static asset using supported encodings --- tests/test_static.py | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) (limited to 'tests/test_static.py') diff --git a/tests/test_static.py b/tests/test_static.py index a323b1d89..3d0deda3f 100644 --- a/tests/test_static.py +++ b/tests/test_static.py @@ -39,6 +39,8 @@ class Test_static_view_use_subpath_False(unittest.TestCase): self.assertEqual(inst.docroot, 'resource_name') self.assertEqual(inst.cache_max_age, 3600) self.assertEqual(inst.index, 'index.html') + self.assertEqual(inst.reload, False) + self.assertEqual(inst.content_encodings, {}) def test_call_adds_slash_path_info_empty(self): inst = self._makeOne('tests:fixtures/static') @@ -252,6 +254,8 @@ class Test_static_view_use_subpath_True(unittest.TestCase): self.assertEqual(inst.docroot, 'resource_name') self.assertEqual(inst.cache_max_age, 3600) self.assertEqual(inst.index, 'index.html') + self.assertEqual(inst.reload, False) + self.assertEqual(inst.content_encodings, {}) def test_call_adds_slash_path_info_empty(self): inst = self._makeOne('tests:fixtures/static') @@ -403,6 +407,70 @@ class Test_static_view_use_subpath_True(unittest.TestCase): self.assertRaises(HTTPNotFound, inst, context, request) +class Test_static_view_content_encodings(unittest.TestCase): + def _getTargetClass(self): + from pyramid.static import static_view + + return static_view + + def _makeOne(self, *arg, **kw): + return self._getTargetClass()(*arg, **kw) + + def _makeRequest(self, kw=None): + from pyramid.request import Request + + environ = { + 'wsgi.url_scheme': 'http', + 'wsgi.version': (1, 0), + 'SERVER_NAME': 'example.com', + 'SERVER_PORT': '6543', + 'PATH_INFO': '/', + 'SCRIPT_NAME': '', + 'REQUEST_METHOD': 'GET', + } + if kw is not None: + environ.update(kw) + return Request(environ=environ) + + def test_call_without_accept(self): + inst = self._makeOne( + 'tests:fixtures/static', content_encodings=['gzip'] + ) + request = self._makeRequest({'PATH_INFO': '/encoded.html'}) + context = DummyContext() + + res = inst(context, request) + self.assertEqual(res.headers['Vary'], 'Accept-Encoding') + self.assertNotIn('Content-Encoding', res.headers) + self.assertEqual(len(res.body), 221) + + def test_call_with_accept_gzip(self): + inst = self._makeOne( + 'tests:fixtures/static', content_encodings=['gzip'] + ) + request = self._makeRequest( + {'PATH_INFO': '/encoded.html', 'HTTP_ACCEPT_ENCODING': 'gzip'} + ) + context = DummyContext() + + res = inst(context, request) + self.assertEqual(res.headers['Vary'], 'Accept-Encoding') + self.assertEqual(res.headers['Content-Encoding'], 'gzip') + self.assertEqual(len(res.body), 187) + + 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') + result2 = inst.get_possible_files('tests:fixtures/static/encoded.html') + self.assertIs(result1, result2) + + def test_call_get_possible_files_is_not_cached(self): + inst = self._makeOne('tests:fixtures/static', reload=True) + result1 = inst.get_possible_files('tests:fixtures/static/encoded.html') + result2 = inst.get_possible_files('tests:fixtures/static/encoded.html') + self.assertIsNot(result1, result2) + + class TestQueryStringConstantCacheBuster(unittest.TestCase): def _makeOne(self, param=None): from pyramid.static import QueryStringConstantCacheBuster as cls -- cgit v1.2.3 From 8fe8725d4d20b18715291d3b45899d3389fe1d8b Mon Sep 17 00:00:00 2001 From: Michael Merickel Date: Sun, 15 Dec 2019 20:46:12 -0600 Subject: handle a missing content negotiation case where the unencoded option is not available and the client requests an encoded variant that doesn't exist --- tests/test_static.py | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) (limited to 'tests/test_static.py') 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') -- cgit v1.2.3 From 08933ad17a230792a886020e06725e44522974d4 Mon Sep 17 00:00:00 2001 From: Michael Merickel Date: Sun, 15 Dec 2019 20:52:01 -0600 Subject: fix lint --- tests/test_static.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tests/test_static.py') diff --git a/tests/test_static.py b/tests/test_static.py index 73814e222..2933e4310 100644 --- a/tests/test_static.py +++ b/tests/test_static.py @@ -533,7 +533,7 @@ class Test_static_view_content_encodings(unittest.TestCase): 'tests:fixtures/static', content_encodings=['gzip'] ) request = self._makeRequest( - {'PATH_INFO': '/only_encoded.html', 'HTTP_ACCEPT_ENCODING': 'br',} + {'PATH_INFO': '/only_encoded.html', 'HTTP_ACCEPT_ENCODING': 'br'} ) context = DummyContext() -- cgit v1.2.3 From 497e667e41b83193d7c5a4f74e9419320d755c46 Mon Sep 17 00:00:00 2001 From: Michael Merickel Date: Sun, 15 Dec 2019 20:53:52 -0600 Subject: fix failing test --- tests/test_static.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'tests/test_static.py') diff --git a/tests/test_static.py b/tests/test_static.py index 2933e4310..7b6e74a64 100644 --- a/tests/test_static.py +++ b/tests/test_static.py @@ -494,16 +494,15 @@ class Test_static_view_content_encodings(unittest.TestCase): def test_call_for_unencoded_variant_with_only_encoded_variant_no_accept( self, ): + from pyramid.httpexceptions import HTTPNotFound + 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) + self.assertRaises(HTTPNotFound, lambda: inst(context, request)) def test_call_for_unencoded_variant_with_only_encoded_variant_with_accept( self, -- cgit v1.2.3