Skip to content

Commit

Permalink
Fix normalize_path_middleware behavior for patch without slash (3669) (
Browse files Browse the repository at this point in the history
  • Loading branch information
bigbag authored and kxepal committed Mar 27, 2019
1 parent 2bf371e commit 8260eda
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGES/3669.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix ``web_middlewares.normalize_path_middleware`` behavior for patch without slash.
2 changes: 1 addition & 1 deletion aiohttp/web_middlewares.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ async def impl(request: Request, handler: _Handler) -> StreamResponse:
if merge_slashes and append_slash:
paths_to_check.append(
re.sub('//+', '/', path + '/'))
if merge_slashes and remove_slash:
if merge_slashes and remove_slash and path.endswith('/'):
merged_slashes = re.sub('//+', '/', path)
paths_to_check.append(merged_slashes[:-1])

Expand Down
23 changes: 22 additions & 1 deletion tests/test_web_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,9 @@ async def test_add_trailing_when_necessary(
('/resource2?p1=1&p2=2', 404),
('/resource2/?p1=1&p2=2', 200),
('/resource2/a/b%2Fc', 404),
('/resource2/a/b%2Fc/', 200)
('/resource2/a/b%2Fc/', 200),
('/resource12', 404),
('/resource12345', 404)
])
async def test_remove_trailing_when_necessary(self, path,
status, cli) -> None:
Expand Down Expand Up @@ -276,6 +278,25 @@ async def test_cannot_remove_and_add_slash(self) -> None:
web.normalize_path_middleware(append_slash=True, remove_slash=True)


async def test_bug_3669(aiohttp_client):
async def paymethod(request):
return web.Response(text="OK")

app = web.Application()
app.router.add_route('GET', '/paymethod', paymethod)
app.middlewares.append(
web.normalize_path_middleware(append_slash=False, remove_slash=True)
)

client = await aiohttp_client(
app, server_kwargs={'skip_url_asserts': True}
)

resp = await client.get('/paymethods')
assert resp.status == 404
assert resp.url.path != '/paymethod'


async def test_old_style_middleware(loop, aiohttp_client) -> None:
async def handler(request):
return web.Response(body=b'OK')
Expand Down

0 comments on commit 8260eda

Please sign in to comment.