summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChris McDonough <chrism@plope.com>2011-04-17 00:16:53 -0400
committerChris McDonough <chrism@plope.com>2011-04-17 00:16:53 -0400
commitd0b417e349922adfefebff0dc6609e114d48e3e0 (patch)
tree2e26db51bfee845af1bf1a305fbba04ab4730453
parent23e0700e14736c102f434cb4c25ad9321150e342 (diff)
downloadpyramid-d0b417e349922adfefebff0dc6609e114d48e3e0.tar.gz
pyramid-d0b417e349922adfefebff0dc6609e114d48e3e0.tar.bz2
pyramid-d0b417e349922adfefebff0dc6609e114d48e3e0.zip
condition coverage for the pyramid.static module; simplify PackageURLParser, removing some impossible-to-reach code
-rw-r--r--pyramid/static.py11
-rw-r--r--pyramid/tests/test_static.py48
2 files changed, 52 insertions, 7 deletions
diff --git a/pyramid/static.py b/pyramid/static.py
index 3866126ac..80981f0b8 100644
--- a/pyramid/static.py
+++ b/pyramid/static.py
@@ -37,21 +37,18 @@ class PackageURLParser(StaticURLParser):
filename = request.path_info_pop(environ)
resource = os.path.normcase(os.path.normpath(
self.resource_name + '/' + filename))
- if ( (self.root_resource is not None) and
- (not resource.startswith(self.root_resource)) ):
+ if not resource.startswith(self.root_resource):
# Out of bounds
return self.not_found(environ, start_response)
if not pkg_resources.resource_exists(self.package_name, resource):
return self.not_found(environ, start_response)
if pkg_resources.resource_isdir(self.package_name, resource):
# @@: Cache?
- child_root = (self.root_resource is not None and
- self.root_resource or self.resource_name)
return self.__class__(
- self.package_name, resource, root_resource=child_root,
+ self.package_name, resource, root_resource=self.resource_name,
cache_max_age=self.cache_max_age)(environ, start_response)
- if (environ.get('PATH_INFO')
- and environ.get('PATH_INFO') != '/'): # pragma: no cover
+ pi = environ.get('PATH_INFO')
+ if pi and pi != '/':
return self.error_extra_path(environ, start_response)
full = pkg_resources.resource_filename(self.package_name, resource)
if_none_match = environ.get('HTTP_IF_NONE_MATCH')
diff --git a/pyramid/tests/test_static.py b/pyramid/tests/test_static.py
index acf5a754b..cd3006689 100644
--- a/pyramid/tests/test_static.py
+++ b/pyramid/tests/test_static.py
@@ -92,6 +92,14 @@ class TestPackageURLParser(unittest.TestCase):
body = response[0]
self.failUnless('<html>static</html>' in body)
+ def test_resource_has_extra_path_info(self):
+ environ = self._makeEnviron(PATH_INFO='/static/index.html/more')
+ inst = self._makeOne('pyramid.tests', 'fixtures')
+ sr = DummyStartResponse()
+ response = inst(environ, sr)
+ body = response[0]
+ self.failUnless("The trailing path '/more' is not allowed" in body)
+
def test_resource_is_file_with_cache_max_age(self):
environ = self._makeEnviron(PATH_INFO='/index.html')
inst = self._makeOne('pyramid.tests', 'fixtures/static',
@@ -122,6 +130,15 @@ class TestPackageURLParser(unittest.TestCase):
['Accept-Ranges', 'Content-Length', 'Content-Range',
'Content-Type', 'ETag', 'Last-Modified'])
+ def test_with_root_resource(self):
+ environ = self._makeEnviron(PATH_INFO='/static/index.html')
+ inst = self._makeOne('pyramid.tests', 'fixtures',
+ root_resource='fixtures/static')
+ sr = DummyStartResponse()
+ response = inst(environ, sr)
+ body = response[0]
+ self.failUnless('<html>static</html>' in body)
+
def test_if_none_match(self):
class DummyEq(object):
def __eq__(self, other):
@@ -136,6 +153,18 @@ class TestPackageURLParser(unittest.TestCase):
self.assertEqual(sr.headerlist[0][0], 'ETag')
self.assertEqual(response[0], '')
+ def test_if_none_match_miss(self):
+ class DummyEq(object):
+ def __eq__(self, other):
+ return False
+ dummy_eq = DummyEq()
+ environ = self._makeEnviron(HTTP_IF_NONE_MATCH=dummy_eq)
+ inst = self._makeOne('pyramid.tests', 'fixtures/static')
+ sr = DummyStartResponse()
+ inst(environ, sr)
+ self.assertEqual(len(sr.headerlist), 6)
+ self.assertEqual(sr.status, '200 OK')
+
def test_repr(self):
import os.path
inst = self._makeOne('pyramid.tests', 'fixtures/static')
@@ -258,6 +287,14 @@ class TestStaticURLInfo(unittest.TestCase):
request = DummyRequest()
self.assertRaises(ValueError, inst.generate, 'path', request)
+ def test_generate_registration_miss(self):
+ inst = self._makeOne(None)
+ inst.registrations = [('name', 'spec', False),
+ ('http://example.com/foo/', 'package:path/',True)]
+ request = DummyRequest()
+ result = inst.generate('package:path/abc', request)
+ self.assertEqual(result, 'http://example.com/foo/abc')
+
def test_generate_slash_in_name1(self):
inst = self._makeOne(None)
inst.registrations = [('http://example.com/foo/', 'package:path/',True)]
@@ -333,6 +370,17 @@ class TestStaticURLInfo(unittest.TestCase):
permission='abc')
self.assertEqual(config.kw['view_permission'], 'abc')
+ def test_add_viewname_with_view_permission(self):
+ class Config:
+ def add_route(self, *arg, **kw):
+ self.arg = arg
+ self.kw = kw
+ config = Config()
+ inst = self._makeOne(config)
+ inst.add('view', 'anotherpackage:path', cache_max_age=1,
+ view_permission='abc')
+ self.assertEqual(config.kw['view_permission'], 'abc')
+
class DummyStartResponse:
def __call__(self, status, headerlist, exc_info=None):
self.status = status