From 99e617c44d4f55041f2da31c81cff520d403b80a Mon Sep 17 00:00:00 2001 From: Marin Rukavina Date: Fri, 27 Apr 2012 23:52:32 +0200 Subject: Updated static_view to raise HTTP exceptions instead of returning --- pyramid/static.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 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): -- cgit v1.2.3 From 04af145ab4b19d6fe0c4a5087c1722868d6fcedc Mon Sep 17 00:00:00 2001 From: Marin Rukavina Date: Wed, 2 May 2012 02:24:39 +0200 Subject: Fixed up the tests and returned HTTPMovedPermanently --- pyramid/static.py | 6 +-- pyramid/tests/test_static.py | 109 +++++++++++++++++++++++++++++++++---------- 2 files changed, 87 insertions(+), 28 deletions(-) diff --git a/pyramid/static.py b/pyramid/static.py index 63ca58597..50b274dae 100644 --- a/pyramid/static.py +++ b/pyramid/static.py @@ -108,7 +108,7 @@ class static_view(object): resource_path ='%s/%s' % (self.docroot.rstrip('/'), path) if resource_isdir(self.package_name, resource_path): if not request.path_url.endswith('/'): - self.add_slash_redirect(request) + return self.add_slash_redirect(request) resource_path = '%s/%s' % (resource_path.rstrip('/'),self.index) if not resource_exists(self.package_name, resource_path): raise HTTPNotFound(request.url) @@ -120,7 +120,7 @@ class static_view(object): filepath = normcase(normpath(join(self.norm_docroot, path))) if isdir(filepath): if not request.path_url.endswith('/'): - self.add_slash_redirect(request) + return self.add_slash_redirect(request) filepath = join(filepath, self.index) if not exists(filepath): raise HTTPNotFound(request.url) @@ -132,7 +132,7 @@ class static_view(object): qs = request.query_string if qs: url = url + '?' + qs - raise HTTPMovedPermanently(url) + return 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..300647099 100644 --- a/pyramid/tests/test_static.py +++ b/pyramid/tests/test_static.py @@ -70,16 +70,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 +98,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 +194,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') @@ -251,33 +276,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 +329,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 +415,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 -- cgit v1.2.3 From 3d65afea0d2bd529a87f72d87a986c989ebffd29 Mon Sep 17 00:00:00 2001 From: Marin Rukavina Date: Wed, 2 May 2012 02:55:03 +0200 Subject: Updated tests for static files and made static.py raise all HTTP exceptions --- pyramid/static.py | 6 +++--- pyramid/tests/test_static.py | 32 ++++++++++++++++++++++---------- 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/pyramid/static.py b/pyramid/static.py index 50b274dae..63ca58597 100644 --- a/pyramid/static.py +++ b/pyramid/static.py @@ -108,7 +108,7 @@ class static_view(object): 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): raise HTTPNotFound(request.url) @@ -120,7 +120,7 @@ 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): raise HTTPNotFound(request.url) @@ -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 300647099..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() @@ -258,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() -- cgit v1.2.3 From e7c4dd632daa71f72bd5e7b6824621cad0dac7cc Mon Sep 17 00:00:00 2001 From: Chris McDonough Date: Wed, 2 May 2012 23:14:25 -0400 Subject: simplify tests --- pyramid/tests/test_static.py | 105 +++++++------------------------------------ 1 file changed, 15 insertions(+), 90 deletions(-) diff --git a/pyramid/tests/test_static.py b/pyramid/tests/test_static.py index 0141405e9..94497d4f6 100644 --- a/pyramid/tests/test_static.py +++ b/pyramid/tests/test_static.py @@ -39,15 +39,7 @@ class Test_static_view_use_subpath_False(unittest.TestCase): request = self._makeRequest({'PATH_INFO':''}) context = DummyContext() 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) + self.assertRaises(HTTPMovedPermanently, inst, context, request) def test_path_info_slash_means_index_html(self): inst = self._makeOne('pyramid.tests:fixtures/static') @@ -77,12 +69,7 @@ class Test_static_view_use_subpath_False(unittest.TestCase): request = self._makeRequest({'PATH_INFO':'/subdir/../../minimal.pt'}) context = DummyContext() 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') + self.assertRaises(HTTPNotFound, inst, context, request) def test_oob_dotdotslash_encoded(self): inst = self._makeOne('pyramid.tests:fixtures/static') @@ -90,12 +77,7 @@ class Test_static_view_use_subpath_False(unittest.TestCase): {'PATH_INFO':'/subdir/%2E%2E%2F%2E%2E/minimal.pt'}) context = DummyContext() 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') + self.assertRaises(HTTPNotFound, inst, context, request) def test_oob_os_sep(self): import os @@ -105,24 +87,14 @@ class Test_static_view_use_subpath_False(unittest.TestCase): (dds, dds)}) context = DummyContext() 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') + self.assertRaises(HTTPNotFound, inst, context, request) def test_resource_doesnt_exist(self): inst = self._makeOne('pyramid.tests:fixtures/static') request = self._makeRequest({'PATH_INFO':'/notthere'}) context = DummyContext() 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') + self.assertRaises(HTTPNotFound, inst, context, request) def test_resource_isdir(self): inst = self._makeOne('pyramid.tests:fixtures/static') @@ -201,12 +173,7 @@ class Test_static_view_use_subpath_False(unittest.TestCase): request = self._makeRequest({'PATH_INFO':'/notthere.html'}) context = DummyContext() 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') + self.assertRaises(HTTPNotFound, inst, context, request) def test_resource_with_content_encoding(self): inst = self._makeOne('pyramid.tests:fixtures/static') @@ -265,15 +232,7 @@ class Test_static_view_use_subpath_True(unittest.TestCase): request.subpath = () context = DummyContext() 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) + self.assertRaises(HTTPMovedPermanently, inst, context, request) def test_path_info_slash_means_index_html(self): inst = self._makeOne('pyramid.tests:fixtures/static') @@ -289,12 +248,7 @@ class Test_static_view_use_subpath_True(unittest.TestCase): request.subpath = ('.', 'index.html') context = DummyContext() 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') + self.assertRaises(HTTPNotFound, inst, context, request) def test_oob_emptyelement(self): inst = self._makeOne('pyramid.tests:fixtures/static') @@ -302,12 +256,7 @@ class Test_static_view_use_subpath_True(unittest.TestCase): request.subpath = ('', 'index.html') context = DummyContext() 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') + self.assertRaises(HTTPNotFound, inst, context, request) def test_oob_dotdotslash(self): inst = self._makeOne('pyramid.tests:fixtures/static') @@ -315,12 +264,7 @@ class Test_static_view_use_subpath_True(unittest.TestCase): request.subpath = ('subdir', '..', '..', 'minimal.pt') context = DummyContext() 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') + self.assertRaises(HTTPNotFound, inst, context, request) def test_oob_dotdotslash_encoded(self): inst = self._makeOne('pyramid.tests:fixtures/static') @@ -328,12 +272,8 @@ class Test_static_view_use_subpath_True(unittest.TestCase): request.subpath = ('subdir', '%2E%2E', '%2E%2E', 'minimal.pt') context = DummyContext() 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') + self.assertRaises(HTTPNotFound, inst, context, request) + def test_oob_os_sep(self): import os inst = self._makeOne('pyramid.tests:fixtures/static') @@ -342,12 +282,7 @@ class Test_static_view_use_subpath_True(unittest.TestCase): request.subpath = ('subdir', dds, dds, 'minimal.pt') context = DummyContext() 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') + self.assertRaises(HTTPNotFound, inst, context, request) def test_resource_doesnt_exist(self): inst = self._makeOne('pyramid.tests:fixtures/static') @@ -355,12 +290,7 @@ class Test_static_view_use_subpath_True(unittest.TestCase): request.subpath = ('notthere,') context = DummyContext() 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') + self.assertRaises(HTTPNotFound, inst, context, request) def test_resource_isdir(self): inst = self._makeOne('pyramid.tests:fixtures/static') @@ -428,12 +358,7 @@ class Test_static_view_use_subpath_True(unittest.TestCase): request.subpath = ('notthere.html',) context = DummyContext() 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') + self.assertRaises(HTTPNotFound, inst, context, request) class DummyContext: pass -- cgit v1.2.3 From 988035afbb745237ea8bec0a7c5e4552d2fc98ba Mon Sep 17 00:00:00 2001 From: Chris McDonough Date: Wed, 2 May 2012 23:15:46 -0400 Subject: add a change note --- CHANGES.txt | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGES.txt b/CHANGES.txt index 67b03d59b..34d60090d 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -34,3 +34,7 @@ Features - Don't add a ``?`` to URLs generated by request.route_url if the ``_query`` argument is provided but empty. + +- The static view machinery now raises (rather than returns) ``HTTPNotFound`` + and ``HTTPMovedPermanently`` exceptions, so these can be caught by the + NotFound view (and other exception views). -- cgit v1.2.3