summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChris McDonough <chrism@plope.com>2012-05-02 23:07:07 -0400
committerChris McDonough <chrism@plope.com>2012-05-02 23:07:07 -0400
commitfbe891be1a84fdafb89b0f901ba7cfe10cda26a0 (patch)
tree9e08d78b88d57660b08591ac979eff3602017b9b
parent1e74e6f3fc5345d53dadb6861d2d8a3778ecf25e (diff)
parent3d65afea0d2bd529a87f72d87a986c989ebffd29 (diff)
downloadpyramid-fbe891be1a84fdafb89b0f901ba7cfe10cda26a0.tar.gz
pyramid-fbe891be1a84fdafb89b0f901ba7cfe10cda26a0.tar.bz2
pyramid-fbe891be1a84fdafb89b0f901ba7cfe10cda26a0.zip
Merge https://github.com/vipera/pyramid into vipera-master
-rw-r--r--pyramid/static.py12
-rw-r--r--pyramid/tests/test_static.py141
2 files changed, 112 insertions, 41 deletions
diff --git a/pyramid/static.py b/pyramid/static.py
index dfb602ee0..63ca58597 100644
--- a/pyramid/static.py
+++ b/pyramid/static.py
@@ -101,17 +101,17 @@ class static_view(object):
path = _secure_path(path_tuple)
if path is None:
- return HTTPNotFound('Out of bounds: %s' % request.url)
+ raise HTTPNotFound('Out of bounds: %s' % request.url)
if self.package_name: # package resource
resource_path ='%s/%s' % (self.docroot.rstrip('/'), path)
if resource_isdir(self.package_name, resource_path):
if not request.path_url.endswith('/'):
- return self.add_slash_redirect(request)
+ self.add_slash_redirect(request)
resource_path = '%s/%s' % (resource_path.rstrip('/'),self.index)
if not resource_exists(self.package_name, resource_path):
- return HTTPNotFound(request.url)
+ raise HTTPNotFound(request.url)
filepath = resource_filename(self.package_name, resource_path)
else: # filesystem file
@@ -120,10 +120,10 @@ class static_view(object):
filepath = normcase(normpath(join(self.norm_docroot, path)))
if isdir(filepath):
if not request.path_url.endswith('/'):
- return self.add_slash_redirect(request)
+ self.add_slash_redirect(request)
filepath = join(filepath, self.index)
if not exists(filepath):
- return HTTPNotFound(request.url)
+ raise HTTPNotFound(request.url)
return FileResponse(filepath, request, self.cache_max_age)
@@ -132,7 +132,7 @@ class static_view(object):
qs = request.query_string
if qs:
url = url + '?' + qs
- return HTTPMovedPermanently(url)
+ raise HTTPMovedPermanently(url)
_seps = set(['/', os.sep])
def _contains_slash(item):
diff --git a/pyramid/tests/test_static.py b/pyramid/tests/test_static.py
index 7f94df990..0141405e9 100644
--- a/pyramid/tests/test_static.py
+++ b/pyramid/tests/test_static.py
@@ -38,11 +38,17 @@ class Test_static_view_use_subpath_False(unittest.TestCase):
inst = self._makeOne('pyramid.tests:fixtures/static')
request = self._makeRequest({'PATH_INFO':''})
context = DummyContext()
- response = inst(context, request)
- response.prepare(request.environ)
- self.assertEqual(response.status, '301 Moved Permanently')
- self.assertTrue(b'http://example.com:6543/' in response.body)
-
+ from pyramid.httpexceptions import HTTPMovedPermanently
+ try:
+ response = inst(context, request)
+ except HTTPMovedPermanently as e:
+ self.assertEqual(e.code, 301)
+ self.assertTrue(b'http://example.com:6543/' in e.location)
+ else:
+ response.prepare(request.environ)
+ self.assertEqual(response.status, '301 Moved Permanently')
+ self.assertTrue(b'http://example.com:6543/' in response.body)
+
def test_path_info_slash_means_index_html(self):
inst = self._makeOne('pyramid.tests:fixtures/static')
request = self._makeRequest()
@@ -70,16 +76,26 @@ class Test_static_view_use_subpath_False(unittest.TestCase):
inst = self._makeOne('pyramid.tests:fixtures/static')
request = self._makeRequest({'PATH_INFO':'/subdir/../../minimal.pt'})
context = DummyContext()
- response = inst(context, request)
- self.assertEqual(response.status, '404 Not Found')
+ from pyramid.httpexceptions import HTTPNotFound
+ try:
+ response = inst(context, request)
+ except HTTPNotFound as e:
+ self.assertEqual(e.code, 404)
+ else:
+ self.assertEqual(response.status, '404 Not Found')
def test_oob_dotdotslash_encoded(self):
inst = self._makeOne('pyramid.tests:fixtures/static')
request = self._makeRequest(
{'PATH_INFO':'/subdir/%2E%2E%2F%2E%2E/minimal.pt'})
context = DummyContext()
- response = inst(context, request)
- self.assertEqual(response.status, '404 Not Found')
+ from pyramid.httpexceptions import HTTPNotFound
+ try:
+ response = inst(context, request)
+ except HTTPNotFound as e:
+ self.assertEqual(e.code, 404)
+ else:
+ self.assertEqual(response.status, '404 Not Found')
def test_oob_os_sep(self):
import os
@@ -88,15 +104,25 @@ class Test_static_view_use_subpath_False(unittest.TestCase):
request = self._makeRequest({'PATH_INFO':'/subdir/%s%sminimal.pt' %
(dds, dds)})
context = DummyContext()
- response = inst(context, request)
- self.assertEqual(response.status, '404 Not Found')
+ from pyramid.httpexceptions import HTTPNotFound
+ try:
+ response = inst(context, request)
+ except HTTPNotFound as e:
+ self.assertEqual(e.code, 404)
+ else:
+ self.assertEqual(response.status, '404 Not Found')
def test_resource_doesnt_exist(self):
inst = self._makeOne('pyramid.tests:fixtures/static')
request = self._makeRequest({'PATH_INFO':'/notthere'})
context = DummyContext()
- response = inst(context, request)
- self.assertEqual(response.status, '404 Not Found')
+ from pyramid.httpexceptions import HTTPNotFound
+ try:
+ response = inst(context, request)
+ except HTTPNotFound as e:
+ self.assertEqual(e.code, 404)
+ else:
+ self.assertEqual(response.status, '404 Not Found')
def test_resource_isdir(self):
inst = self._makeOne('pyramid.tests:fixtures/static')
@@ -174,8 +200,13 @@ class Test_static_view_use_subpath_False(unittest.TestCase):
inst = self._makeOne('pyramid.tests:fixtures/static')
request = self._makeRequest({'PATH_INFO':'/notthere.html'})
context = DummyContext()
- response = inst(context, request)
- self.assertEqual(response.status, '404 Not Found')
+ from pyramid.httpexceptions import HTTPNotFound
+ try:
+ response = inst(context, request)
+ except HTTPNotFound as e:
+ self.assertEqual(e.code, 404)
+ else:
+ self.assertEqual(response.status, '404 Not Found')
def test_resource_with_content_encoding(self):
inst = self._makeOne('pyramid.tests:fixtures/static')
@@ -233,11 +264,17 @@ class Test_static_view_use_subpath_True(unittest.TestCase):
request = self._makeRequest({'PATH_INFO':''})
request.subpath = ()
context = DummyContext()
- response = inst(context, request)
- response.prepare(request.environ)
- self.assertEqual(response.status, '301 Moved Permanently')
- self.assertTrue(b'http://example.com:6543/' in response.body)
-
+ from pyramid.httpexceptions import HTTPMovedPermanently
+ try:
+ response = inst(context, request)
+ except HTTPMovedPermanently as e:
+ self.assertEqual(e.code, 301)
+ self.assertTrue(b'http://example.com:6543/' in e.location)
+ else:
+ response.prepare(request.environ)
+ self.assertEqual(response.status, '301 Moved Permanently')
+ self.assertTrue(b'http://example.com:6543/' in response.body)
+
def test_path_info_slash_means_index_html(self):
inst = self._makeOne('pyramid.tests:fixtures/static')
request = self._makeRequest()
@@ -251,33 +288,52 @@ class Test_static_view_use_subpath_True(unittest.TestCase):
request = self._makeRequest()
request.subpath = ('.', 'index.html')
context = DummyContext()
- response = inst(context, request)
- self.assertEqual(response.status, '404 Not Found')
+ from pyramid.httpexceptions import HTTPNotFound
+ try:
+ response = inst(context, request)
+ except HTTPNotFound as e:
+ self.assertEqual(e.code, 404)
+ else:
+ self.assertEqual(response.status, '404 Not Found')
def test_oob_emptyelement(self):
inst = self._makeOne('pyramid.tests:fixtures/static')
request = self._makeRequest()
request.subpath = ('', 'index.html')
context = DummyContext()
- response = inst(context, request)
- self.assertEqual(response.status, '404 Not Found')
+ from pyramid.httpexceptions import HTTPNotFound
+ try:
+ response = inst(context, request)
+ except HTTPNotFound as e:
+ self.assertEqual(e.code, 404)
+ else:
+ self.assertEqual(response.status, '404 Not Found')
def test_oob_dotdotslash(self):
inst = self._makeOne('pyramid.tests:fixtures/static')
request = self._makeRequest()
request.subpath = ('subdir', '..', '..', 'minimal.pt')
context = DummyContext()
- response = inst(context, request)
- self.assertEqual(response.status, '404 Not Found')
+ from pyramid.httpexceptions import HTTPNotFound
+ try:
+ response = inst(context, request)
+ except HTTPNotFound as e:
+ self.assertEqual(e.code, 404)
+ else:
+ self.assertEqual(response.status, '404 Not Found')
def test_oob_dotdotslash_encoded(self):
inst = self._makeOne('pyramid.tests:fixtures/static')
request = self._makeRequest()
request.subpath = ('subdir', '%2E%2E', '%2E%2E', 'minimal.pt')
context = DummyContext()
- response = inst(context, request)
- self.assertEqual(response.status, '404 Not Found')
-
+ from pyramid.httpexceptions import HTTPNotFound
+ try:
+ response = inst(context, request)
+ except HTTPNotFound as e:
+ self.assertEqual(e.code, 404)
+ else:
+ self.assertEqual(response.status, '404 Not Found')
def test_oob_os_sep(self):
import os
inst = self._makeOne('pyramid.tests:fixtures/static')
@@ -285,16 +341,26 @@ class Test_static_view_use_subpath_True(unittest.TestCase):
request = self._makeRequest()
request.subpath = ('subdir', dds, dds, 'minimal.pt')
context = DummyContext()
- response = inst(context, request)
- self.assertEqual(response.status, '404 Not Found')
+ from pyramid.httpexceptions import HTTPNotFound
+ try:
+ response = inst(context, request)
+ except HTTPNotFound as e:
+ self.assertEqual(e.code, 404)
+ else:
+ self.assertEqual(response.status, '404 Not Found')
def test_resource_doesnt_exist(self):
inst = self._makeOne('pyramid.tests:fixtures/static')
request = self._makeRequest()
request.subpath = ('notthere,')
context = DummyContext()
- response = inst(context, request)
- self.assertEqual(response.status, '404 Not Found')
+ from pyramid.httpexceptions import HTTPNotFound
+ try:
+ response = inst(context, request)
+ except HTTPNotFound as e:
+ self.assertEqual(e.code, 404)
+ else:
+ self.assertEqual(response.status, '404 Not Found')
def test_resource_isdir(self):
inst = self._makeOne('pyramid.tests:fixtures/static')
@@ -361,8 +427,13 @@ class Test_static_view_use_subpath_True(unittest.TestCase):
request = self._makeRequest()
request.subpath = ('notthere.html',)
context = DummyContext()
- response = inst(context, request)
- self.assertEqual(response.status, '404 Not Found')
+ from pyramid.httpexceptions import HTTPNotFound
+ try:
+ response = inst(context, request)
+ except HTTPNotFound as e:
+ self.assertEqual(e.code, 404)
+ else:
+ self.assertEqual(response.status, '404 Not Found')
class DummyContext:
pass