summaryrefslogtreecommitdiff
path: root/tests
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
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')
-rw-r--r--tests/fixtures/static/encoded.html15
-rw-r--r--tests/fixtures/static/encoded.html.gzbin0 -> 187 bytes
-rw-r--r--tests/pkgs/static_encodings/__init__.py2
-rw-r--r--tests/test_config/test_views.py4
-rw-r--r--tests/test_integration.py55
-rw-r--r--tests/test_static.py68
6 files changed, 141 insertions, 3 deletions
diff --git a/tests/fixtures/static/encoded.html b/tests/fixtures/static/encoded.html
new file mode 100644
index 000000000..0999b4f1b
--- /dev/null
+++ b/tests/fixtures/static/encoded.html
@@ -0,0 +1,15 @@
+<!--
+ when modified, re-run:
+ gzip -k encoded.html
+-->
+<html>
+<head>
+<title>
+A Simple HTML Document
+</title>
+</head>
+<body>
+<p>This is a very simple HTML document</p>
+<p>It only has two paragraphs</p>
+</body>
+</html>
diff --git a/tests/fixtures/static/encoded.html.gz b/tests/fixtures/static/encoded.html.gz
new file mode 100644
index 000000000..afcc25768
--- /dev/null
+++ b/tests/fixtures/static/encoded.html.gz
Binary files differ
diff --git a/tests/pkgs/static_encodings/__init__.py b/tests/pkgs/static_encodings/__init__.py
new file mode 100644
index 000000000..3b86a1a15
--- /dev/null
+++ b/tests/pkgs/static_encodings/__init__.py
@@ -0,0 +1,2 @@
+def includeme(config):
+ config.add_static_view('/', 'tests:fixtures', content_encodings=['gzip'])
diff --git a/tests/test_config/test_views.py b/tests/test_config/test_views.py
index a1e975756..baa87dd6b 100644
--- a/tests/test_config/test_views.py
+++ b/tests/test_config/test_views.py
@@ -4174,7 +4174,9 @@ class DummyRegistry:
utility = None
def __init__(self):
- self.settings = {}
+ self.settings = {
+ 'pyramid.reload_assets': False,
+ }
def queryUtility(self, type_or_iface, name=None, default=None):
return self.utility or default
diff --git a/tests/test_integration.py b/tests/test_integration.py
index 331542d7d..8a4575d7b 100644
--- a/tests/test_integration.py
+++ b/tests/test_integration.py
@@ -73,8 +73,8 @@ class IntegrationBase(object):
root_factory=self.root_factory, package=self.package
)
config.include(self.package)
- app = config.make_wsgi_app()
- self.testapp = TestApp(app)
+ self.app = config.make_wsgi_app()
+ self.testapp = TestApp(self.app)
self.config = config
def tearDown(self):
@@ -227,6 +227,57 @@ class TestStaticAppUsingAssetSpec(StaticAppBase, unittest.TestCase):
package = 'tests.pkgs.static_assetspec'
+class TestStaticAppWithEncodings(IntegrationBase, unittest.TestCase):
+ package = 'tests.pkgs.static_encodings'
+
+ # XXX webtest actually runs response.decode_content() and so we can't
+ # use it to test gzip- or deflate-encoded responses to see if they
+ # were transferred correctly
+ def _getResponse(self, *args, **kwargs):
+ from pyramid.request import Request
+
+ req = Request.blank(*args, **kwargs)
+ return req.get_response(self.app)
+
+ def test_no_accept(self):
+ res = self._getResponse('/static/encoded.html')
+ self.assertEqual(res.headers['Vary'], 'Accept-Encoding')
+ self.assertNotIn('Content-Encoding', res.headers)
+ _assertBody(
+ res.body, os.path.join(here, 'fixtures/static/encoded.html')
+ )
+
+ def test_unsupported_accept(self):
+ res = self._getResponse(
+ '/static/encoded.html',
+ headers={'Accept-Encoding': 'br, foo, bar'},
+ )
+ self.assertEqual(res.headers['Vary'], 'Accept-Encoding')
+ self.assertNotIn('Content-Encoding', res.headers)
+ _assertBody(
+ res.body, os.path.join(here, 'fixtures/static/encoded.html')
+ )
+
+ def test_accept_gzip(self):
+ res = self._getResponse(
+ '/static/encoded.html',
+ headers={'Accept-Encoding': 'br, foo, gzip'},
+ )
+ self.assertEqual(res.headers['Vary'], 'Accept-Encoding')
+ self.assertEqual(res.headers['Content-Encoding'], 'gzip')
+ _assertBody(
+ res.body, os.path.join(here, 'fixtures/static/encoded.html.gz')
+ )
+
+ def test_accept_gzip_returns_identity(self):
+ res = self._getResponse(
+ '/static/index.html', headers={'Accept-Encoding': 'gzip'}
+ )
+ self.assertNotIn('Vary', res.headers)
+ self.assertNotIn('Content-Encoding', res.headers)
+ _assertBody(res.body, os.path.join(here, 'fixtures/static/index.html'))
+
+
class TestStaticAppNoSubpath(unittest.TestCase):
staticapp = static_view(os.path.join(here, 'fixtures'), use_subpath=False)
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