You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

65 lines
1.8 KiB

  1. from django.test import Client, TestCase
  2. from hc.api.models import Check, Ping
  3. class PingTestCase(TestCase):
  4. def test_it_works(self):
  5. check = Check()
  6. check.save()
  7. r = self.client.get("/ping/%s/" % check.code)
  8. assert r.status_code == 200
  9. same_check = Check.objects.get(code=check.code)
  10. assert same_check.status == "up"
  11. pings = list(Ping.objects.all())
  12. assert pings[0].scheme == "http"
  13. def test_post_works(self):
  14. check = Check()
  15. check.save()
  16. csrf_client = Client(enforce_csrf_checks=True)
  17. r = csrf_client.post("/ping/%s/" % check.code)
  18. assert r.status_code == 200
  19. def test_head_works(self):
  20. check = Check()
  21. check.save()
  22. csrf_client = Client(enforce_csrf_checks=True)
  23. r = csrf_client.head("/ping/%s/" % check.code)
  24. assert r.status_code == 200
  25. assert Ping.objects.count() == 1
  26. def test_it_handles_bad_uuid(self):
  27. r = self.client.get("/ping/not-uuid/")
  28. assert r.status_code == 400
  29. def test_it_handles_120_char_ua(self):
  30. ua = ("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) "
  31. "AppleWebKit/537.36 (KHTML, like Gecko) "
  32. "Chrome/44.0.2403.89 Safari/537.36")
  33. check = Check()
  34. check.save()
  35. r = self.client.get("/ping/%s/" % check.code, HTTP_USER_AGENT=ua)
  36. assert r.status_code == 200
  37. pings = list(Ping.objects.all())
  38. assert pings[0].ua == ua
  39. def test_it_truncates_long_ua(self):
  40. ua = "01234567890" * 30
  41. check = Check()
  42. check.save()
  43. r = self.client.get("/ping/%s/" % check.code, HTTP_USER_AGENT=ua)
  44. assert r.status_code == 200
  45. pings = list(Ping.objects.all())
  46. assert len(pings[0].ua) == 200
  47. assert ua.startswith(pings[0].ua)