From b76bd5996ca35d262e5a766efa0a0abba9d78d2d Mon Sep 17 00:00:00 2001 From: Justas Trimailovas Date: Sat, 23 Jul 2016 15:46:45 +0200 Subject: [PATCH] Test if correct statuses are returned if we allow acces index or not Related: #921 --- aiohttp/web_urldispatcher.py | 9 ++++----- tests/test_web_urldispatcher.py | 11 +++++++---- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/aiohttp/web_urldispatcher.py b/aiohttp/web_urldispatcher.py index 4689cdbcea3..c4b3a32e92d 100644 --- a/aiohttp/web_urldispatcher.py +++ b/aiohttp/web_urldispatcher.py @@ -497,17 +497,16 @@ def handle(self, request): # on opening a dir, load it's contents if allowed if filepath.is_dir(): if self._show_index: - return Response(text=self._dir_index_html(filepath)) + ret = Response(text=self._dir_index_html(filepath)) else: raise HTTPForbidden() - - # on opening a file, load it's contents if they exist - if filepath.is_file(): + elif filepath.is_file(): ret = yield from self._file_sender.send(request, filepath) - return ret else: raise HTTPNotFound + return ret + def _dir_index_html(self, filepath): "returns directory's index as html" assert filepath.is_dir() diff --git a/tests/test_web_urldispatcher.py b/tests/test_web_urldispatcher.py index 1dad198f5c8..6145acc5d45 100644 --- a/tests/test_web_urldispatcher.py +++ b/tests/test_web_urldispatcher.py @@ -29,12 +29,15 @@ def teardown(): return tmp_dir +@pytest.mark.parametrize("show_index,status", [(False, 403), (True, 200)]) @pytest.mark.run_loop -def test_access_root_of_static_handler(tmp_dir_path, create_app_and_client): +def test_access_root_of_static_handler(tmp_dir_path, create_app_and_client, + show_index, status): """ Tests the operation of static file server. Try to access the root of static file server, and make - sure that a `HTTP 403 - Forbidden` is returned. + sure that correct HTTP statuses are returned depending if we directory + index should be shown or not. """ # Put a file inside tmp_dir_path: my_file_path = os.path.join(tmp_dir_path, 'my_file') @@ -44,12 +47,12 @@ def test_access_root_of_static_handler(tmp_dir_path, create_app_and_client): app, client = yield from create_app_and_client() # Register global static route: - app.router.add_static('/', tmp_dir_path) + app.router.add_static('/', tmp_dir_path, show_index=show_index) # Request the root of the static directory. # Expect an 403 error page. r = yield from client.get('/') - assert r.status == 403 + assert r.status == status # data = (yield from r.read()) yield from r.release()