Skip to content

Commit

Permalink
Merge pull request #803 from realcr/master
Browse files Browse the repository at this point in the history
Fixed bug in serving static directory
  • Loading branch information
asvetlov committed Feb 27, 2016
2 parents 0259c09 + c93da59 commit 3b4eef8
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
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()

0 comments on commit 3b4eef8

Please sign in to comment.