Skip to content

Commit

Permalink
Test if correct statuses are returned if we allow acces index or not
Browse files Browse the repository at this point in the history
Related: aio-libs#921
  • Loading branch information
trimailov committed Jul 23, 2016
1 parent 6db10c5 commit b76bd59
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
9 changes: 4 additions & 5 deletions aiohttp/web_urldispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
11 changes: 7 additions & 4 deletions tests/test_web_urldispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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()

Expand Down

0 comments on commit b76bd59

Please sign in to comment.