async def test_forwarded_relaxed_ok(aiohttp_client): async def handler(request): assert request.host == 'example.com' assert request.scheme == 'https' assert request.secure assert request.remote == '10.10.10.10' return web.Response() app = web.Application() app.router.add_get('/', handler) await _setup(app, ForwardedRelaxed()) cl = await aiohttp_client(app) hdr_val = '; '.join(['for=10.10.10.10', 'proto=https', 'host=example.com']) resp = await cl.get('/', headers={'Forwarded': hdr_val}) assert resp.status == 200
async def test_forwarded_relaxed_no_host(aiohttp_client): async def handler(request): url = cl.make_url('/') host = url.host + ':' + str(url.port) assert request.host == host assert request.scheme == 'https' assert request.secure assert request.remote == '10.10.10.10' return web.Response() app = web.Application() app.router.add_get('/', handler) await _setup(app, ForwardedRelaxed()) cl = await aiohttp_client(app) hdr_val = '; '.join(['for=10.10.10.10', 'proto=https']) resp = await cl.get('/', headers={'Forwarded': hdr_val}) assert resp.status == 200
async def app_factory(*args) -> web.Application: # pylint: disable=unused-argument app = web.Application(middlewares=[ ForwardedRelaxed().middleware, XForwardedRelaxed().middleware, request_context_middleware, ]) app.update( # pylint: disable=no-member subapps={}, client_session=None, # populated via signal scheduler=None, # populated via signal redis=None, # populated via signal db=None, # populated via signal REDIS_URL=settings.REDIS_URL, DATABASE_URL=settings.DATABASE_URL, SLACK_INVITE_TOKEN=settings.SLACK_INVITE_TOKEN, SLACK_TOKEN=settings.SLACK_TOKEN, ) app.cleanup_ctx.extend([ apscheduler_ctx, client_session, redis_pool, postgresql_pool, slack_client, background_jobs, ]) app.router.add_get("/", index) app["subapps"]["website"] = await website.app_factory() app.add_subapp("/web", app["subapps"]["website"]) app["subapps"]["sirbot"] = await sirbot.app_factory() app.add_subapp("/bot", app["subapps"]["sirbot"]) return app
async def app_factory() -> web.Application: app = web.Application(middlewares=[ ForwardedRelaxed().middleware, XForwardedRelaxed().middleware, request_context_middleware, ]) app.update( # pylint: disable=no-member client_session=None, # populated via signal scheduler=None, # populated via signal redis=None, # populated via signal db=None, # populated via signal REDIS_URL=settings.REDIS_URL, POSTGRESQL_DSN=settings.POSTGRESQL_DSN, ) app.cleanup_ctx.extend( [apscheduler, client_session, redis_pool, postgresql_pool]) app.router.add_get("/", index) app["website_app"] = await website.app_factory() app.add_subapp("/web", app["website_app"]) return app