Skip to content

Commit 19b8587

Browse files
authored
fix(test): Update IP extraction for ASGI tests (#1200)
1 parent a6e1fae commit 19b8587

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

sentry_sdk/integrations/asgi.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,13 +227,17 @@ def _get_query(self, scope):
227227

228228
def _get_ip(self, scope):
229229
# type: (Any) -> str
230+
"""
231+
Extract IP Address from the ASGI scope based on request headers with fallback to scope client.
232+
"""
233+
headers = self._get_headers(scope)
230234
try:
231-
return scope["headers"]["x_forwarded_for"].split(",")[0].strip()
235+
return headers["x-forwarded-for"].split(",")[0].strip()
232236
except (KeyError, IndexError):
233237
pass
234238

235239
try:
236-
return scope["headers"]["x_real_ip"]
240+
return headers["x-real-ip"]
237241
except KeyError:
238242
pass
239243

tests/integrations/asgi/test_asgi.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ def test_x_forwarded_for(sentry_init, app, capture_events):
258258
events = capture_events()
259259

260260
client = TestClient(app)
261-
response = client.get("/", headers={"X-Forwarded-For": "testproxy"})
261+
response = client.get("/sync-message", headers={"X-Forwarded-For": "testproxy"})
262262

263263
assert response.status_code == 200
264264

@@ -272,7 +272,7 @@ def test_x_forwarded_for_multiple_entries(sentry_init, app, capture_events):
272272

273273
client = TestClient(app)
274274
response = client.get(
275-
"/", headers={"X-Forwarded-For": "testproxy1,testproxy2,testproxy3"}
275+
"/sync-message", headers={"X-Forwarded-For": "testproxy1,testproxy2,testproxy3"}
276276
)
277277

278278
assert response.status_code == 200
@@ -286,7 +286,7 @@ def test_x_real_ip(sentry_init, app, capture_events):
286286
events = capture_events()
287287

288288
client = TestClient(app)
289-
response = client.get("/", headers={"X-Real-IP": "1.2.3.4"})
289+
response = client.get("/sync-message", headers={"X-Real-IP": "1.2.3.4"})
290290

291291
assert response.status_code == 200
292292

0 commit comments

Comments
 (0)