diff --git a/aiohttp/web_urldispatcher.py b/aiohttp/web_urldispatcher.py index e7361463012..d867dba440c 100644 --- a/aiohttp/web_urldispatcher.py +++ b/aiohttp/web_urldispatcher.py @@ -569,6 +569,10 @@ def handle(self, request): request.logger.exception(error) raise HTTPNotFound() from error + # Make sure that filepath is a file + if not filepath.is_file(): + raise HTTPNotFound() + st = filepath.stat() modsince = request.if_modified_since diff --git a/tests/test_web_urldispatcher.py b/tests/test_web_urldispatcher.py new file mode 100644 index 00000000000..87ca5335ea8 --- /dev/null +++ b/tests/test_web_urldispatcher.py @@ -0,0 +1,46 @@ +import pytest +import os +import shutil +import tempfile + + +@pytest.fixture(scope='function') +def tmp_dir_path(request): + """ + Give a path for a temporary directory + The directory is destroyed at the end of the test. + """ + # Temporary directory. + tmp_dir = tempfile.mkdtemp() + + def teardown(): + # Delete the whole directory: + shutil.rmtree(tmp_dir) + + request.addfinalizer(teardown) + return tmp_dir + + +@pytest.mark.run_loop +def test_access_root_of_static_handler(tmp_dir_path, create_app_and_client): + """ + Tests the operation of static file server. + Try to access the root of static file server, and make + sure that a proper not found error is returned. + """ + # Put a file inside tmp_dir_path: + my_file_path = os.path.join(tmp_dir_path, 'my_file') + with open(my_file_path, 'w') as fw: + fw.write('hello') + + app, client = yield from create_app_and_client() + + # Register global static route: + app.router.add_static('/', tmp_dir_path) + + # Request the root of the static directory. + # Expect an 404 error page. + r = (yield from client.get('/')) + assert r.status == 404 + # data = (yield from r.read()) + yield from r.release()