mirror of
https://github.com/home-assistant/core.git
synced 2025-12-24 21:06:19 +00:00
Black
This commit is contained in:
@@ -15,94 +15,86 @@ async def mock_handler(request):
|
||||
async def test_ignore_x_forwarded_for(aiohttp_client):
|
||||
"""Test that we get the IP from the transport."""
|
||||
app = web.Application()
|
||||
app.router.add_get('/', mock_handler)
|
||||
app.router.add_get("/", mock_handler)
|
||||
setup_real_ip(app, False, [])
|
||||
|
||||
mock_api_client = await aiohttp_client(app)
|
||||
|
||||
resp = await mock_api_client.get('/', headers={
|
||||
X_FORWARDED_FOR: '255.255.255.255'
|
||||
})
|
||||
resp = await mock_api_client.get("/", headers={X_FORWARDED_FOR: "255.255.255.255"})
|
||||
assert resp.status == 200
|
||||
text = await resp.text()
|
||||
assert text != '255.255.255.255'
|
||||
assert text != "255.255.255.255"
|
||||
|
||||
|
||||
async def test_use_x_forwarded_for_without_trusted_proxy(aiohttp_client):
|
||||
"""Test that we get the IP from the transport."""
|
||||
app = web.Application()
|
||||
app.router.add_get('/', mock_handler)
|
||||
app.router.add_get("/", mock_handler)
|
||||
setup_real_ip(app, True, [])
|
||||
|
||||
mock_api_client = await aiohttp_client(app)
|
||||
|
||||
resp = await mock_api_client.get('/', headers={
|
||||
X_FORWARDED_FOR: '255.255.255.255'
|
||||
})
|
||||
resp = await mock_api_client.get("/", headers={X_FORWARDED_FOR: "255.255.255.255"})
|
||||
assert resp.status == 200
|
||||
text = await resp.text()
|
||||
assert text != '255.255.255.255'
|
||||
assert text != "255.255.255.255"
|
||||
|
||||
|
||||
async def test_use_x_forwarded_for_with_trusted_proxy(aiohttp_client):
|
||||
"""Test that we get the IP from the transport."""
|
||||
app = web.Application()
|
||||
app.router.add_get('/', mock_handler)
|
||||
setup_real_ip(app, True, [ip_network('127.0.0.1')])
|
||||
app.router.add_get("/", mock_handler)
|
||||
setup_real_ip(app, True, [ip_network("127.0.0.1")])
|
||||
|
||||
mock_api_client = await aiohttp_client(app)
|
||||
|
||||
resp = await mock_api_client.get('/', headers={
|
||||
X_FORWARDED_FOR: '255.255.255.255'
|
||||
})
|
||||
resp = await mock_api_client.get("/", headers={X_FORWARDED_FOR: "255.255.255.255"})
|
||||
assert resp.status == 200
|
||||
text = await resp.text()
|
||||
assert text == '255.255.255.255'
|
||||
assert text == "255.255.255.255"
|
||||
|
||||
|
||||
async def test_use_x_forwarded_for_with_untrusted_proxy(aiohttp_client):
|
||||
"""Test that we get the IP from the transport."""
|
||||
app = web.Application()
|
||||
app.router.add_get('/', mock_handler)
|
||||
setup_real_ip(app, True, [ip_network('1.1.1.1')])
|
||||
app.router.add_get("/", mock_handler)
|
||||
setup_real_ip(app, True, [ip_network("1.1.1.1")])
|
||||
|
||||
mock_api_client = await aiohttp_client(app)
|
||||
|
||||
resp = await mock_api_client.get('/', headers={
|
||||
X_FORWARDED_FOR: '255.255.255.255'
|
||||
})
|
||||
resp = await mock_api_client.get("/", headers={X_FORWARDED_FOR: "255.255.255.255"})
|
||||
assert resp.status == 200
|
||||
text = await resp.text()
|
||||
assert text != '255.255.255.255'
|
||||
assert text != "255.255.255.255"
|
||||
|
||||
|
||||
async def test_use_x_forwarded_for_with_spoofed_header(aiohttp_client):
|
||||
"""Test that we get the IP from the transport."""
|
||||
app = web.Application()
|
||||
app.router.add_get('/', mock_handler)
|
||||
setup_real_ip(app, True, [ip_network('127.0.0.1')])
|
||||
app.router.add_get("/", mock_handler)
|
||||
setup_real_ip(app, True, [ip_network("127.0.0.1")])
|
||||
|
||||
mock_api_client = await aiohttp_client(app)
|
||||
|
||||
resp = await mock_api_client.get('/', headers={
|
||||
X_FORWARDED_FOR: '222.222.222.222, 255.255.255.255'
|
||||
})
|
||||
resp = await mock_api_client.get(
|
||||
"/", headers={X_FORWARDED_FOR: "222.222.222.222, 255.255.255.255"}
|
||||
)
|
||||
assert resp.status == 200
|
||||
text = await resp.text()
|
||||
assert text == '255.255.255.255'
|
||||
assert text == "255.255.255.255"
|
||||
|
||||
|
||||
async def test_use_x_forwarded_for_with_nonsense_header(aiohttp_client):
|
||||
"""Test that we get the IP from the transport."""
|
||||
app = web.Application()
|
||||
app.router.add_get('/', mock_handler)
|
||||
setup_real_ip(app, True, [ip_network('127.0.0.1')])
|
||||
app.router.add_get("/", mock_handler)
|
||||
setup_real_ip(app, True, [ip_network("127.0.0.1")])
|
||||
|
||||
mock_api_client = await aiohttp_client(app)
|
||||
|
||||
resp = await mock_api_client.get('/', headers={
|
||||
X_FORWARDED_FOR: 'This value is invalid'
|
||||
})
|
||||
resp = await mock_api_client.get(
|
||||
"/", headers={X_FORWARDED_FOR: "This value is invalid"}
|
||||
)
|
||||
assert resp.status == 200
|
||||
text = await resp.text()
|
||||
assert text == '127.0.0.1'
|
||||
assert text == "127.0.0.1"
|
||||
|
||||
Reference in New Issue
Block a user