Skip to content

Commit 0c2c1d9

Browse files
committed
Add test for x-forwarded-port header
1 parent 6396563 commit 0c2c1d9

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

tests/test_reverse_proxy.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,3 +282,31 @@ async def test_nginx_headers_behavior(scope_overrides, headers, expected_forward
282282
assert f"{key}={expected_value}" in forwarded, (
283283
f"Expected {key}={expected_value} in {forwarded}"
284284
)
285+
286+
287+
@pytest.mark.parametrize("legacy_headers", [False, True])
288+
@pytest.mark.asyncio
289+
async def test_x_forwarded_port_in_forwarded_header(legacy_headers):
290+
"""Test that x-forwarded-port is included in the Forwarded header."""
291+
headers = [
292+
(b"host", b"localhost:8000"),
293+
(b"user-agent", b"test-agent"),
294+
(b"x-forwarded-port", b"443"),
295+
(b"x-forwarded-proto", b"https"),
296+
(b"x-forwarded-host", b"api.example.com"),
297+
]
298+
request = create_request(headers=headers)
299+
handler = ReverseProxyHandler(
300+
upstream="http://upstream-api.com", legacy_forwarded_headers=legacy_headers
301+
)
302+
result_headers = handler._prepare_headers(request)
303+
304+
# Check that the Forwarded header includes the port
305+
forwarded = result_headers["Forwarded"]
306+
assert "host=api.example.com:443" in forwarded, (
307+
f"Expected host=api.example.com:443 in {forwarded}"
308+
)
309+
assert "proto=https" in forwarded
310+
311+
# Check that the x-forwarded-port header is preserved
312+
assert result_headers["X-Forwarded-Port"] == "443"

0 commit comments

Comments
 (0)