summaryrefslogtreecommitdiff
path: root/tests/test_static.py
diff options
context:
space:
mode:
authorMichael Merickel <michael@merickel.org>2019-12-15 20:18:16 -0600
committerGitHub <noreply@github.com>2019-12-15 20:18:16 -0600
commitc6340737a4826d4073f0cfa7951dccf42d0cbfcf (patch)
treee5605243e0b52f6444c9f97771ddc89e96c9572b /tests/test_static.py
parent948b692469cdcaeb38f37982f0810954c545b920 (diff)
parentf6cb1efa8fba683bdc5c9b4a645f9357fe2e6208 (diff)
downloadpyramid-c6340737a4826d4073f0cfa7951dccf42d0cbfcf.tar.gz
pyramid-c6340737a4826d4073f0cfa7951dccf42d0cbfcf.tar.bz2
pyramid-c6340737a4826d4073f0cfa7951dccf42d0cbfcf.zip
Merge pull request #3537 from mmerickel/negotiate-static-encoding
negotiate the best static asset using supported encodings
Diffstat (limited to 'tests/test_static.py')
-rw-r--r--tests/test_static.py68
1 files changed, 68 insertions, 0 deletions
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