Browse Source

Fix ping handler to return "not found" in 404 body

pull/563/head
Pēteris Caune 3 years ago
parent
commit
027920ef2b
No known key found for this signature in database GPG Key ID: E28D7679E9A9EDE2
3 changed files with 9 additions and 2 deletions
  1. +1
    -0
      hc/api/tests/test_ping.py
  2. +1
    -0
      hc/api/tests/test_ping_by_slug.py
  3. +7
    -2
      hc/api/views.py

+ 1
- 0
hc/api/tests/test_ping.py View File

@ -76,6 +76,7 @@ class PingTestCase(BaseTestCase):
def test_it_handles_missing_check(self):
r = self.client.get("/ping/07c2f548-9850-4b27-af5d-6c9dc157ec02/")
self.assertEqual(r.status_code, 404)
self.assertEqual(r.content.decode(), "not found")
def test_it_handles_120_char_ua(self):
ua = (


+ 1
- 0
hc/api/tests/test_ping_by_slug.py View File

@ -35,6 +35,7 @@ class PingBySlugTestCase(BaseTestCase):
def test_it_handles_missing_check(self):
r = self.client.get(f"/ping/{self.project.ping_key}/bar")
self.assertEqual(r.status_code, 404)
self.assertEqual(r.content.decode(), "not found")
def test_it_never_caches(self):
r = self.client.get(self.url)


+ 7
- 2
hc/api/views.py View File

@ -33,7 +33,10 @@ class BadChannelException(Exception):
@never_cache
def ping(request, code, check=None, action="success", exitstatus=None):
if check is None:
check = get_object_or_404(Check, code=code)
try:
check = Check.objects.get(code=code)
except Check.DoesNotExist:
return HttpResponseNotFound("not found")
if exitstatus is not None and exitstatus > 255:
return HttpResponseBadRequest("invalid url format")
@ -62,7 +65,9 @@ def ping(request, code, check=None, action="success", exitstatus=None):
@csrf_exempt
def ping_by_slug(request, ping_key, slug, action="success", exitstatus=None):
try:
check = get_object_or_404(Check, slug=slug, project__ping_key=ping_key)
check = Check.objects.get(slug=slug, project__ping_key=ping_key)
except Check.DoesNotExist:
return HttpResponseNotFound("not found")
except Check.MultipleObjectsReturned:
return HttpResponse("ambiguous slug", status=409)


Loading…
Cancel
Save