|
| 1 | +import pathlib |
| 2 | +import sys |
| 3 | +import unittest |
| 4 | + |
| 5 | + |
| 6 | +ROOT = pathlib.Path(__file__).resolve().parents[1] |
| 7 | +SRC = ROOT / "src" |
| 8 | +if str(SRC) not in sys.path: |
| 9 | + sys.path.insert(0, str(SRC)) |
| 10 | + |
| 11 | +from core.constants import CACHE_TTL_STATIC_LONG |
| 12 | +from proxy.proxy_support import ( |
| 13 | + ResponseCache, |
| 14 | + has_unsupported_transfer_encoding, |
| 15 | + host_matches_rules, |
| 16 | + is_ip_literal, |
| 17 | + load_host_rules, |
| 18 | + parse_content_length, |
| 19 | +) |
| 20 | + |
| 21 | + |
| 22 | +class ProxySupportTests(unittest.TestCase): |
| 23 | + def test_is_ip_literal_handles_ipv4_ipv6_and_hostnames(self): |
| 24 | + self.assertTrue(is_ip_literal("127.0.0.1")) |
| 25 | + self.assertTrue(is_ip_literal("[2001:db8::1]")) |
| 26 | + self.assertFalse(is_ip_literal("example.com")) |
| 27 | + |
| 28 | + def test_load_host_rules_and_match_suffixes(self): |
| 29 | + rules = load_host_rules(["Example.com", ".example.org", "api.example.net."]) |
| 30 | + self.assertTrue(host_matches_rules("example.com", rules)) |
| 31 | + self.assertTrue(host_matches_rules("sub.example.org", rules)) |
| 32 | + self.assertTrue(host_matches_rules("api.example.net", rules)) |
| 33 | + self.assertFalse(host_matches_rules("example.org", rules)) |
| 34 | + self.assertFalse(host_matches_rules("other.test", rules)) |
| 35 | + |
| 36 | + def test_parse_content_length_uses_exact_header_name(self): |
| 37 | + headers = ( |
| 38 | + b"HTTP/1.1 200 OK\r\n" |
| 39 | + b"X-Content-Length: 999\r\n" |
| 40 | + b"Content-Length: 42\r\n\r\n" |
| 41 | + ) |
| 42 | + self.assertEqual(parse_content_length(headers), 42) |
| 43 | + self.assertEqual(parse_content_length(b"HTTP/1.1 200 OK\r\n\r\n"), 0) |
| 44 | + |
| 45 | + def test_has_unsupported_transfer_encoding(self): |
| 46 | + self.assertFalse( |
| 47 | + has_unsupported_transfer_encoding( |
| 48 | + b"Transfer-Encoding: identity\r\n\r\n" |
| 49 | + ) |
| 50 | + ) |
| 51 | + self.assertTrue( |
| 52 | + has_unsupported_transfer_encoding( |
| 53 | + b"Transfer-Encoding: chunked\r\n\r\n" |
| 54 | + ) |
| 55 | + ) |
| 56 | + self.assertTrue( |
| 57 | + has_unsupported_transfer_encoding( |
| 58 | + b"Transfer-Encoding: gzip, chunked\r\n\r\n" |
| 59 | + ) |
| 60 | + ) |
| 61 | + |
| 62 | + |
| 63 | +class ResponseCacheParseTtlTests(unittest.TestCase): |
| 64 | + def test_static_asset_uses_long_ttl(self): |
| 65 | + raw = ( |
| 66 | + b"HTTP/1.1 200 OK\r\n" |
| 67 | + b"Content-Type: image/png\r\n\r\n" |
| 68 | + b"body" |
| 69 | + ) |
| 70 | + self.assertEqual( |
| 71 | + ResponseCache.parse_ttl(raw, "https://example.com/logo.png"), |
| 72 | + CACHE_TTL_STATIC_LONG, |
| 73 | + ) |
| 74 | + |
| 75 | + def test_private_no_store_and_set_cookie_disable_caching(self): |
| 76 | + private_resp = ( |
| 77 | + b"HTTP/1.1 200 OK\r\n" |
| 78 | + b"Cache-Control: private, max-age=600\r\n\r\n" |
| 79 | + b"body" |
| 80 | + ) |
| 81 | + no_store_resp = ( |
| 82 | + b"HTTP/1.1 200 OK\r\n" |
| 83 | + b"Cache-Control: no-store\r\n\r\n" |
| 84 | + b"body" |
| 85 | + ) |
| 86 | + cookie_resp = ( |
| 87 | + b"HTTP/1.1 200 OK\r\n" |
| 88 | + b"Set-Cookie: sid=1\r\n" |
| 89 | + b"Content-Type: image/png\r\n\r\n" |
| 90 | + b"body" |
| 91 | + ) |
| 92 | + |
| 93 | + self.assertEqual(ResponseCache.parse_ttl(private_resp, "https://example.com/app.js"), 0) |
| 94 | + self.assertEqual(ResponseCache.parse_ttl(no_store_resp, "https://example.com/app.js"), 0) |
| 95 | + self.assertEqual(ResponseCache.parse_ttl(cookie_resp, "https://example.com/logo.png"), 0) |
| 96 | + |
| 97 | + |
| 98 | +if __name__ == "__main__": |
| 99 | + unittest.main() |
0 commit comments