Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed bug in serving static directory #803

Merged
merged 6 commits into from
Feb 27, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions aiohttp/web_urldispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
46 changes: 46 additions & 0 deletions tests/test_web_urldispatcher.py
Original file line number Diff line number Diff line change
@@ -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()