|
@ -1,6 +1,7 @@ |
|
|
from datetime import timedelta as td |
|
|
from datetime import timedelta as td |
|
|
|
|
|
|
|
|
from django.test import Client |
|
|
from django.test import Client |
|
|
|
|
|
from django.test.utils import override_settings |
|
|
from django.utils.timezone import now |
|
|
from django.utils.timezone import now |
|
|
from hc.api.models import Check, Flip, Ping |
|
|
from hc.api.models import Check, Flip, Ping |
|
|
from hc.test import BaseTestCase |
|
|
from hc.test import BaseTestCase |
|
@ -10,9 +11,10 @@ class PingTestCase(BaseTestCase): |
|
|
def setUp(self): |
|
|
def setUp(self): |
|
|
super().setUp() |
|
|
super().setUp() |
|
|
self.check = Check.objects.create(project=self.project) |
|
|
self.check = Check.objects.create(project=self.project) |
|
|
|
|
|
self.url = "/ping/%s/" % self.check.code |
|
|
|
|
|
|
|
|
def test_it_works(self): |
|
|
def test_it_works(self): |
|
|
r = self.client.get("/ping/%s/" % self.check.code) |
|
|
|
|
|
|
|
|
r = self.client.get(self.url) |
|
|
self.assertEqual(r.status_code, 200) |
|
|
self.assertEqual(r.status_code, 200) |
|
|
|
|
|
|
|
|
self.check.refresh_from_db() |
|
|
self.check.refresh_from_db() |
|
@ -28,7 +30,7 @@ class PingTestCase(BaseTestCase): |
|
|
self.check.status = "paused" |
|
|
self.check.status = "paused" |
|
|
self.check.save() |
|
|
self.check.save() |
|
|
|
|
|
|
|
|
r = self.client.get("/ping/%s/" % self.check.code) |
|
|
|
|
|
|
|
|
r = self.client.get(self.url) |
|
|
self.assertEqual(r.status_code, 200) |
|
|
self.assertEqual(r.status_code, 200) |
|
|
|
|
|
|
|
|
self.check.refresh_from_db() |
|
|
self.check.refresh_from_db() |
|
@ -38,7 +40,7 @@ class PingTestCase(BaseTestCase): |
|
|
self.check.last_start = now() |
|
|
self.check.last_start = now() |
|
|
self.check.save() |
|
|
self.check.save() |
|
|
|
|
|
|
|
|
r = self.client.get("/ping/%s/" % self.check.code) |
|
|
|
|
|
|
|
|
r = self.client.get(self.url) |
|
|
self.assertEqual(r.status_code, 200) |
|
|
self.assertEqual(r.status_code, 200) |
|
|
|
|
|
|
|
|
self.check.refresh_from_db() |
|
|
self.check.refresh_from_db() |
|
@ -46,9 +48,7 @@ class PingTestCase(BaseTestCase): |
|
|
|
|
|
|
|
|
def test_post_works(self): |
|
|
def test_post_works(self): |
|
|
csrf_client = Client(enforce_csrf_checks=True) |
|
|
csrf_client = Client(enforce_csrf_checks=True) |
|
|
r = csrf_client.post( |
|
|
|
|
|
"/ping/%s/" % self.check.code, "hello world", content_type="text/plain" |
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
r = csrf_client.post(self.url, "hello world", content_type="text/plain") |
|
|
self.assertEqual(r.status_code, 200) |
|
|
self.assertEqual(r.status_code, 200) |
|
|
|
|
|
|
|
|
ping = Ping.objects.latest("id") |
|
|
ping = Ping.objects.latest("id") |
|
@ -57,7 +57,7 @@ class PingTestCase(BaseTestCase): |
|
|
|
|
|
|
|
|
def test_head_works(self): |
|
|
def test_head_works(self): |
|
|
csrf_client = Client(enforce_csrf_checks=True) |
|
|
csrf_client = Client(enforce_csrf_checks=True) |
|
|
r = csrf_client.head("/ping/%s/" % self.check.code) |
|
|
|
|
|
|
|
|
r = csrf_client.head(self.url) |
|
|
self.assertEqual(r.status_code, 200) |
|
|
self.assertEqual(r.status_code, 200) |
|
|
self.assertEqual(Ping.objects.count(), 1) |
|
|
self.assertEqual(Ping.objects.count(), 1) |
|
|
|
|
|
|
|
@ -81,7 +81,7 @@ class PingTestCase(BaseTestCase): |
|
|
"Chrome/44.0.2403.89 Safari/537.36" |
|
|
"Chrome/44.0.2403.89 Safari/537.36" |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
r = self.client.get("/ping/%s/" % self.check.code, HTTP_USER_AGENT=ua) |
|
|
|
|
|
|
|
|
r = self.client.get(self.url, HTTP_USER_AGENT=ua) |
|
|
self.assertEqual(r.status_code, 200) |
|
|
self.assertEqual(r.status_code, 200) |
|
|
|
|
|
|
|
|
ping = Ping.objects.latest("id") |
|
|
ping = Ping.objects.latest("id") |
|
@ -90,7 +90,7 @@ class PingTestCase(BaseTestCase): |
|
|
def test_it_truncates_long_ua(self): |
|
|
def test_it_truncates_long_ua(self): |
|
|
ua = "01234567890" * 30 |
|
|
ua = "01234567890" * 30 |
|
|
|
|
|
|
|
|
r = self.client.get("/ping/%s/" % self.check.code, HTTP_USER_AGENT=ua) |
|
|
|
|
|
|
|
|
r = self.client.get(self.url, HTTP_USER_AGENT=ua) |
|
|
self.assertEqual(r.status_code, 200) |
|
|
self.assertEqual(r.status_code, 200) |
|
|
|
|
|
|
|
|
ping = Ping.objects.latest("id") |
|
|
ping = Ping.objects.latest("id") |
|
@ -99,38 +99,30 @@ class PingTestCase(BaseTestCase): |
|
|
|
|
|
|
|
|
def test_it_reads_forwarded_ip(self): |
|
|
def test_it_reads_forwarded_ip(self): |
|
|
ip = "1.1.1.1" |
|
|
ip = "1.1.1.1" |
|
|
r = self.client.get("/ping/%s/" % self.check.code, HTTP_X_FORWARDED_FOR=ip) |
|
|
|
|
|
|
|
|
r = self.client.get(self.url, HTTP_X_FORWARDED_FOR=ip) |
|
|
ping = Ping.objects.latest("id") |
|
|
ping = Ping.objects.latest("id") |
|
|
self.assertEqual(r.status_code, 200) |
|
|
self.assertEqual(r.status_code, 200) |
|
|
self.assertEqual(ping.remote_addr, "1.1.1.1") |
|
|
self.assertEqual(ping.remote_addr, "1.1.1.1") |
|
|
|
|
|
|
|
|
ip = "1.1.1.1, 2.2.2.2" |
|
|
ip = "1.1.1.1, 2.2.2.2" |
|
|
r = self.client.get( |
|
|
|
|
|
"/ping/%s/" % self.check.code, |
|
|
|
|
|
HTTP_X_FORWARDED_FOR=ip, |
|
|
|
|
|
REMOTE_ADDR="3.3.3.3", |
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
r = self.client.get(self.url, HTTP_X_FORWARDED_FOR=ip, REMOTE_ADDR="3.3.3.3",) |
|
|
ping = Ping.objects.latest("id") |
|
|
ping = Ping.objects.latest("id") |
|
|
self.assertEqual(r.status_code, 200) |
|
|
self.assertEqual(r.status_code, 200) |
|
|
self.assertEqual(ping.remote_addr, "1.1.1.1") |
|
|
self.assertEqual(ping.remote_addr, "1.1.1.1") |
|
|
|
|
|
|
|
|
def test_it_reads_forwarded_protocol(self): |
|
|
def test_it_reads_forwarded_protocol(self): |
|
|
r = self.client.get( |
|
|
|
|
|
"/ping/%s/" % self.check.code, HTTP_X_FORWARDED_PROTO="https" |
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
r = self.client.get(self.url, HTTP_X_FORWARDED_PROTO="https") |
|
|
ping = Ping.objects.latest("id") |
|
|
ping = Ping.objects.latest("id") |
|
|
self.assertEqual(r.status_code, 200) |
|
|
self.assertEqual(r.status_code, 200) |
|
|
self.assertEqual(ping.scheme, "https") |
|
|
self.assertEqual(ping.scheme, "https") |
|
|
|
|
|
|
|
|
def test_it_never_caches(self): |
|
|
def test_it_never_caches(self): |
|
|
r = self.client.get("/ping/%s/" % self.check.code) |
|
|
|
|
|
|
|
|
r = self.client.get(self.url) |
|
|
assert "no-cache" in r.get("Cache-Control") |
|
|
assert "no-cache" in r.get("Cache-Control") |
|
|
|
|
|
|
|
|
def test_it_updates_confirmation_flag(self): |
|
|
def test_it_updates_confirmation_flag(self): |
|
|
payload = "Please Confirm ..." |
|
|
payload = "Please Confirm ..." |
|
|
r = self.client.post( |
|
|
|
|
|
"/ping/%s/" % self.check.code, data=payload, content_type="text/plain" |
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
r = self.client.post(self.url, data=payload, content_type="text/plain") |
|
|
self.assertEqual(r.status_code, 200) |
|
|
self.assertEqual(r.status_code, 200) |
|
|
|
|
|
|
|
|
self.check.refresh_from_db() |
|
|
self.check.refresh_from_db() |
|
@ -181,7 +173,7 @@ class PingTestCase(BaseTestCase): |
|
|
self.check.last_start = now() - td(seconds=10) |
|
|
self.check.last_start = now() - td(seconds=10) |
|
|
self.check.save() |
|
|
self.check.save() |
|
|
|
|
|
|
|
|
r = self.client.get("/ping/%s/" % self.check.code) |
|
|
|
|
|
|
|
|
r = self.client.get(self.url) |
|
|
self.assertEqual(r.status_code, 200) |
|
|
self.assertEqual(r.status_code, 200) |
|
|
|
|
|
|
|
|
self.check.refresh_from_db() |
|
|
self.check.refresh_from_db() |
|
@ -191,7 +183,7 @@ class PingTestCase(BaseTestCase): |
|
|
self.check.methods = "POST" |
|
|
self.check.methods = "POST" |
|
|
self.check.save() |
|
|
self.check.save() |
|
|
|
|
|
|
|
|
r = self.client.get("/ping/%s/" % self.check.code) |
|
|
|
|
|
|
|
|
r = self.client.get(self.url) |
|
|
self.assertEqual(r.status_code, 200) |
|
|
self.assertEqual(r.status_code, 200) |
|
|
|
|
|
|
|
|
self.check.refresh_from_db() |
|
|
self.check.refresh_from_db() |
|
@ -201,3 +193,18 @@ class PingTestCase(BaseTestCase): |
|
|
ping = Ping.objects.latest("id") |
|
|
ping = Ping.objects.latest("id") |
|
|
self.assertEqual(ping.scheme, "http") |
|
|
self.assertEqual(ping.scheme, "http") |
|
|
self.assertEqual(ping.kind, "ign") |
|
|
self.assertEqual(ping.kind, "ign") |
|
|
|
|
|
|
|
|
|
|
|
@override_settings(PING_BODY_LIMIT=5) |
|
|
|
|
|
def test_it_chops_long_body(self): |
|
|
|
|
|
self.client.post(self.url, "hello world", content_type="text/plain") |
|
|
|
|
|
|
|
|
|
|
|
ping = Ping.objects.latest("id") |
|
|
|
|
|
self.assertEqual(ping.method, "POST") |
|
|
|
|
|
self.assertEqual(ping.body, "hello") |
|
|
|
|
|
|
|
|
|
|
|
@override_settings(PING_BODY_LIMIT=None) |
|
|
|
|
|
def test_it_allows_unlimited_body(self): |
|
|
|
|
|
self.client.post(self.url, "A" * 20000, content_type="text/plain") |
|
|
|
|
|
|
|
|
|
|
|
ping = Ping.objects.latest("id") |
|
|
|
|
|
self.assertEqual(len(ping.body), 20000) |