Skip to content

Sub app middlewares are called before main app,  #1853

Closed
@Anvil

Description

Long story short

According to http://aiohttp.readthedocs.io/en/stable/web.html#nested-applications,
[I]f URL is '/admin/something' middlewares from [main application] are applied first and [sub application] middlewares are the next in the call chain.

It turns out it's actually the other way.

Steps to reproduce

Run this code saved as foo.py:

#!/usr/bin/python3 -tt

import aiohttp
import aiohttp.web

async def middleware1(app, next_handler):

    async def handler(request):
        print("middleware1")
        return await next_handler(request)

    return handler
    
async def middleware2(app, next_handler):

    async def handler(request):
        print("middleware2")
        return await next_handler(request)

    return handler

async def middleware3(app, next_handler):

    async def handler(request):
        print("middleware3")
        return await next_handler(request)

    return handler

app = aiohttp.web.Application(
    middlewares=[middleware1])

subapp1 = aiohttp.web.Application(middlewares=[middleware2])
app.add_subapp("/foo", subapp1)

subapp2 = aiohttp.web.Application(middlewares=[middleware3])
subapp1.add_subapp("/foo/bar", subapp2)

aiohttp.web.run_app(app, host='127.0.0.1', port=4096)

And the run wget http://127.0.0.1:4096/foo/bar.

Current Result:

$ python3 foo.py
======== Running on http://127.0.0.1:4096 ========
(Press CTRL+C to quit)
middleware3
middleware2
middleware1

Expected output:

$ python3 foo.py
======== Running on http://127.0.0.1:4096 ========
(Press CTRL+C to quit)
middleware1
middleware2
middleware3

Your environment

Ubuntu Xenial (python 3.5.2) with aiohttp 2.0.7

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions