Skip to content

Commit e20aee3

Browse files
committed
test(django): Custom 404 handler and request context
1 parent 52fd228 commit e20aee3

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

tests/integrations/django/myapp/urls.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,4 @@
3232
]
3333

3434
handler500 = views.handler500
35+
handler404 = views.handler404

tests/integrations/django/myapp/views.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from django.contrib.auth import login
22
from django.contrib.auth.models import User
3-
from django.http import HttpResponse, HttpResponseServerError
3+
from django.http import HttpResponse, HttpResponseServerError, HttpResponseNotFound
44
from django.views.generic import ListView
55

66
import sentry_sdk
@@ -37,3 +37,8 @@ def head(self, *args, **kwargs):
3737
def post_echo(request):
3838
sentry_sdk.capture_message("hi")
3939
return HttpResponse(request.body)
40+
41+
42+
def handler404(*args, **kwargs):
43+
sentry_sdk.capture_message("not found", level="error")
44+
return HttpResponseNotFound("404")

tests/integrations/django/test_basic.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,23 @@ def test_user_captured(sentry_init, client, capture_events):
101101
}
102102

103103

104-
def test_404(sentry_init, client):
105-
sentry_init(integrations=[DjangoIntegration()], send_default_pii=True)
106-
content, status, headers = client.get("/404")
107-
assert status.lower() == "404 not found"
104+
def test_custom_error_handler_request_context(sentry_init, client, capture_events):
105+
sentry_init(integrations=[DjangoIntegration()])
106+
events = capture_events()
107+
content, status, headers = client.post("/404")
108+
assert status == "404 Not Found"
109+
110+
event, = events
111+
112+
assert event["message"] == "not found"
113+
assert event["level"] == "error"
114+
assert event["request"] == {
115+
"env": {"SERVER_NAME": "localhost", "SERVER_PORT": "80"},
116+
"headers": {"Content-Length": "0", "Content-Type": "", "Host": "localhost"},
117+
"method": "POST",
118+
"query_string": "",
119+
"url": "http://localhost/404",
120+
}
108121

109122

110123
def test_500(sentry_init, client):

0 commit comments

Comments
 (0)