Aiohttp: broken interface normalize_path_middleware

Created on 27 Mar 2019  路  8Comments  路  Source: aio-libs/aiohttp

Long story short

middleware normalize_path_middleware is not working properly

Expected behaviour

I appeal to /paymethods, there is an endpoint /paymethod

/paymethod -> 200
/paymethods -> 301 -> /paymethod

settings: normalize_path_middleware(remove_slash=True, append_slash=False)

if switch off
/paymethod -> 200
/paymethods -> 404

I think the problem is in the last if merge_slashes and remove_slash


            if merge_slashes:
                paths_to_check.append(re.sub('//+', '/', path))
            if append_slash and not request.path.endswith('/'):
                paths_to_check.append(path + '/')
            if remove_slash and request.path.endswith('/'):
                paths_to_check.append(path[:-1])
            if merge_slashes and append_slash:
                paths_to_check.append(
                    re.sub('//+', '/', path + '/'))
            if merge_slashes and remove_slash:
                merged_slashes = re.sub('//+', '/', path)
                paths_to_check.append(merged_slashes[:-1])

aiohttp == 3.5.4

Your environment

server

bug outdated

Most helpful comment

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.url.path == '/paymethod'

All 8 comments

GitMate.io thinks the contributor most likely able to help you is @asvetlov.

Possibly related issues are https://github.com/aio-libs/aiohttp/issues/3086 (add_static breaks normalize_path_middleware), https://github.com/aio-libs/aiohttp/issues/1178 (Websocket interface change break existing code), https://github.com/aio-libs/aiohttp/issues/1292 (broken interface for response.url), https://github.com/aio-libs/aiohttp/issues/1583 (normalize_path_middleware tests failures), and https://github.com/aio-libs/aiohttp/issues/445 (Is break in finally intentional?).

if we change last condition
if merge_slashes and remove_slash:
on
if merge_slashes and remove_slash and path.endswith('/'):
all will be correct.

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

    async def paymethods(request):
        raise web.HTTPPermanentRedirect('/paymethod')

    app = web.Application()
    app.router.add_route('GET', '/paymethods', paymethods)
    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 == 200
    assert resp.url.path == '/paymethod'

@vir-mir, this test case passed. How is it different from your's case?

@kxepal Why did the final URL in the test change?

you send /paymethods
assert resp.url.path == '/paymethod'

@vir-mir Because of redirect? Eventually, it causes new request to new location.

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.url.path == '/paymethod'

So no redirect involved. That's make a sense. Thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

AtomsForPeace picture AtomsForPeace  路  5Comments

zhmiao picture zhmiao  路  3Comments

deckar01 picture deckar01  路  4Comments

JulienPalard picture JulienPalard  路  3Comments

rckclmbr picture rckclmbr  路  5Comments