mirror of
https://github.com/home-assistant/core.git
synced 2025-12-24 12:59:34 +00:00
Add current request context to get_url helper (#38602)
This commit is contained in:
33
tests/components/http/test_request_context.py
Normal file
33
tests/components/http/test_request_context.py
Normal file
@@ -0,0 +1,33 @@
|
||||
"""Test request context middleware."""
|
||||
from contextvars import ContextVar
|
||||
|
||||
from aiohttp import web
|
||||
|
||||
from homeassistant.components.http.request_context import setup_request_context
|
||||
|
||||
|
||||
async def test_request_context_middleware(aiohttp_client):
|
||||
"""Test that request context is set from middleware."""
|
||||
context = ContextVar("request", default=None)
|
||||
app = web.Application()
|
||||
|
||||
async def mock_handler(request):
|
||||
"""Return the real IP as text."""
|
||||
request_context = context.get()
|
||||
assert request_context
|
||||
assert request_context == request
|
||||
|
||||
return web.Response(text="hi!")
|
||||
|
||||
app.router.add_get("/", mock_handler)
|
||||
setup_request_context(app, context)
|
||||
mock_api_client = await aiohttp_client(app)
|
||||
|
||||
resp = await mock_api_client.get("/")
|
||||
assert resp.status == 200
|
||||
|
||||
text = await resp.text()
|
||||
assert text == "hi!"
|
||||
|
||||
# We are outside of the context here, should be None
|
||||
assert context.get() is None
|
||||
Reference in New Issue
Block a user