forked from home-assistant/core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__init__.py
36 lines (24 loc) · 928 Bytes
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
"""Test utilities."""
from collections.abc import Awaitable, Callable
from aiohttp.web import Application, Request, StreamResponse, middleware
def mock_real_ip(app: Application) -> Callable[[str], None]:
"""Inject middleware to mock real IP.
Returns a function to set the real IP.
"""
ip_to_mock: str | None = None
def set_ip_to_mock(value: str):
nonlocal ip_to_mock
ip_to_mock = value
@middleware
async def mock_real_ip(
request: Request, handler: Callable[[Request], Awaitable[StreamResponse]]
) -> StreamResponse:
"""Mock Real IP middleware."""
nonlocal ip_to_mock
request = request.clone(remote=ip_to_mock)
return await handler(request)
async def real_ip_startup(app):
"""Startup of real ip."""
app.middlewares.insert(0, mock_real_ip)
app.on_startup.append(real_ip_startup)
return set_ip_to_mock