Complete worked example
python
from bootstrap_lab import django_setup
django_setup(__name__)
from django.http import JsonResponse
from django.test import Client, override_settings
from django.urls import path
def health(request):
return JsonResponse({"host": request.get_host()})
urlpatterns = [path("health/", health)]
def inspect_host(host):
with override_settings(DEBUG=False, ALLOWED_HOSTS=["testserver"]):
return Client().get("/health/", HTTP_HOST=host).status_code- Reasoning
- The worked view calls request.get_host, so the configured ALLOWED_HOSTS rule is actually exercised. The test client supplies one controlled Host header, and DEBUG stays false.