Skip to content

Commit b76bd59

Browse files
committed
Test if correct statuses are returned if we allow acces index or not
Related: aio-libs#921
1 parent 6db10c5 commit b76bd59

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

aiohttp/web_urldispatcher.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -497,17 +497,16 @@ def handle(self, request):
497497
# on opening a dir, load it's contents if allowed
498498
if filepath.is_dir():
499499
if self._show_index:
500-
return Response(text=self._dir_index_html(filepath))
500+
ret = Response(text=self._dir_index_html(filepath))
501501
else:
502502
raise HTTPForbidden()
503-
504-
# on opening a file, load it's contents if they exist
505-
if filepath.is_file():
503+
elif filepath.is_file():
506504
ret = yield from self._file_sender.send(request, filepath)
507-
return ret
508505
else:
509506
raise HTTPNotFound
510507

508+
return ret
509+
511510
def _dir_index_html(self, filepath):
512511
"returns directory's index as html"
513512
assert filepath.is_dir()

tests/test_web_urldispatcher.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,15 @@ def teardown():
2929
return tmp_dir
3030

3131

32+
@pytest.mark.parametrize("show_index,status", [(False, 403), (True, 200)])
3233
@pytest.mark.run_loop
33-
def test_access_root_of_static_handler(tmp_dir_path, create_app_and_client):
34+
def test_access_root_of_static_handler(tmp_dir_path, create_app_and_client,
35+
show_index, status):
3436
"""
3537
Tests the operation of static file server.
3638
Try to access the root of static file server, and make
37-
sure that a `HTTP 403 - Forbidden` is returned.
39+
sure that correct HTTP statuses are returned depending if we directory
40+
index should be shown or not.
3841
"""
3942
# Put a file inside tmp_dir_path:
4043
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):
4447
app, client = yield from create_app_and_client()
4548

4649
# Register global static route:
47-
app.router.add_static('/', tmp_dir_path)
50+
app.router.add_static('/', tmp_dir_path, show_index=show_index)
4851

4952
# Request the root of the static directory.
5053
# Expect an 403 error page.
5154
r = yield from client.get('/')
52-
assert r.status == 403
55+
assert r.status == status
5356
# data = (yield from r.read())
5457
yield from r.release()
5558

0 commit comments

Comments
 (0)