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.

82 lines
2.7 KiB

  1. from django.test import Client, TestCase
  2. from hc.api.models import Check, Ping
  3. class PingTestCase(TestCase):
  4. def setUp(self):
  5. super(PingTestCase, self).setUp()
  6. self.check = Check.objects.create()
  7. def test_it_works(self):
  8. r = self.client.get("/ping/%s/" % self.check.code)
  9. assert r.status_code == 200
  10. same_check = Check.objects.get(code=self.check.code)
  11. assert same_check.status == "up"
  12. ping = Ping.objects.latest("id")
  13. assert ping.scheme == "http"
  14. def test_post_works(self):
  15. csrf_client = Client(enforce_csrf_checks=True)
  16. r = csrf_client.post("/ping/%s/" % self.check.code)
  17. assert r.status_code == 200
  18. def test_head_works(self):
  19. csrf_client = Client(enforce_csrf_checks=True)
  20. r = csrf_client.head("/ping/%s/" % self.check.code)
  21. assert r.status_code == 200
  22. assert Ping.objects.count() == 1
  23. def test_it_handles_bad_uuid(self):
  24. r = self.client.get("/ping/not-uuid/")
  25. assert r.status_code == 400
  26. def test_it_handles_120_char_ua(self):
  27. ua = ("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) "
  28. "AppleWebKit/537.36 (KHTML, like Gecko) "
  29. "Chrome/44.0.2403.89 Safari/537.36")
  30. r = self.client.get("/ping/%s/" % self.check.code, HTTP_USER_AGENT=ua)
  31. assert r.status_code == 200
  32. ping = Ping.objects.latest("id")
  33. assert ping.ua == ua
  34. def test_it_truncates_long_ua(self):
  35. ua = "01234567890" * 30
  36. r = self.client.get("/ping/%s/" % self.check.code, HTTP_USER_AGENT=ua)
  37. assert r.status_code == 200
  38. ping = Ping.objects.latest("id")
  39. assert len(ping.ua) == 200
  40. assert ua.startswith(ping.ua)
  41. def test_it_reads_forwarded_ip(self):
  42. ip = "1.1.1.1"
  43. r = self.client.get("/ping/%s/" % self.check.code,
  44. HTTP_X_FORWARDED_FOR=ip)
  45. ping = Ping.objects.latest("id")
  46. assert r.status_code == 200
  47. assert ping.remote_addr == "1.1.1.1"
  48. ip = "1.1.1.1, 2.2.2.2"
  49. r = self.client.get("/ping/%s/" % self.check.code,
  50. HTTP_X_FORWARDED_FOR=ip, REMOTE_ADDR="3.3.3.3")
  51. ping = Ping.objects.latest("id")
  52. assert r.status_code == 200
  53. assert ping.remote_addr == "1.1.1.1"
  54. def test_it_reads_forwarded_protocol(self):
  55. r = self.client.get("/ping/%s/" % self.check.code,
  56. HTTP_X_FORWARDED_PROTO="https")
  57. ping = Ping.objects.latest("id")
  58. assert r.status_code == 200
  59. assert ping.scheme == "https"
  60. def test_it_never_caches(self):
  61. r = self.client.get("/ping/%s/" % self.check.code)
  62. assert "no-cache" in r.get("Cache-Control")