Skip to content

Commit 71c7a46

Browse files
☔ Make sure Django's behavior around "ALLOWED_HOSTS" is proven
1 parent cb1c60e commit 71c7a46

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

instrumentation/opentelemetry-instrumentation-django/tests/test_middleware.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,20 @@ def test_templated_route_get(self):
234234
self.assertEqual(span.attributes["http.scheme"], "http")
235235
self.assertEqual(span.attributes["http.status_code"], 200)
236236

237+
def test_when_middleware_triggers_host_check_then_raises_exception(self):
238+
def return_host(request):
239+
# "HttpRequest.get_host" checks for allowed hosts and raises
240+
# `django.core.DisallowedHost` if it isn't allowed.
241+
return request.get_host()
242+
243+
with patch.object(
244+
_DjangoMiddleware, "process_request", side_effect=return_host
245+
) as process_mock:
246+
response = Client().get("/route/2020/template/")
247+
248+
assert response.status_code == 400
249+
process_mock.assert_called_once()
250+
237251
def test_traced_get(self):
238252
Client().get("/traced/")
239253

0 commit comments

Comments
 (0)