51 lines
2.0 KiB
Python
51 lines
2.0 KiB
Python
import unittest
|
|
from adlist.cache import cache_headers
|
|
from adlist.validation import convert_host_line, validate_ip, validate_and_normalize_url
|
|
|
|
|
|
class ValidationAndCacheTests(unittest.TestCase):
|
|
def test_hosts_format(self):
|
|
self.assertEqual(convert_host_line('0.0.0.0 ads.example.com', '127.0.0.1'), '127.0.0.1 ads.example.com')
|
|
|
|
def test_adguard_format(self):
|
|
self.assertEqual(convert_host_line('||tracking.example.com^', '0.0.0.0'), '0.0.0.0 tracking.example.com')
|
|
|
|
def test_dnsmasq_format(self):
|
|
self.assertEqual(convert_host_line('address=/block.example.com/0.0.0.0', '0.0.0.0'), '0.0.0.0 block.example.com')
|
|
|
|
def test_plain_domains(self):
|
|
self.assertEqual(convert_host_line('tracking.example.com', '::'), ':: tracking.example.com')
|
|
|
|
def test_comments_ignored(self):
|
|
self.assertIsNone(convert_host_line('# comment', '0.0.0.0'))
|
|
|
|
def test_malformed_domains_ignored(self):
|
|
self.assertIsNone(convert_host_line('a<b.invalid', '0.0.0.0'))
|
|
|
|
def test_ipv6_target(self):
|
|
self.assertEqual(validate_ip('2001:db8::1'), '2001:db8::1')
|
|
|
|
def test_invalid_target(self):
|
|
with self.assertRaises(ValueError): validate_ip('999.999.999.999')
|
|
|
|
def test_nested_percent_escapes_preserved(self):
|
|
nested='https://example.com/a%2Fb?q=a%26b'
|
|
self.assertEqual(validate_and_normalize_url(nested), nested)
|
|
|
|
def test_https_default(self):
|
|
self.assertEqual(validate_and_normalize_url('example.com/list'), 'https://example.com/list')
|
|
|
|
def test_cache_directives(self):
|
|
headers=cache_headers('f'*64)
|
|
self.assertIn('s-maxage=604800', headers['Cache-Control'])
|
|
self.assertIn('stale-if-error=604800', headers['Cache-Control'])
|
|
self.assertEqual(headers['ETag'],'"'+'f'*64+'"')
|
|
|
|
def test_debug_no_store(self):
|
|
headers=cache_headers('f'*64,debug_mode=True)
|
|
self.assertEqual(headers['Cache-Control'], 'private, no-store')
|
|
self.assertNotIn('ETag',headers)
|
|
|
|
|
|
if __name__ == '__main__': unittest.main()
|