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