diff options
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/test_exceptions.py | 13 | ||||
| -rw-r--r-- | tests/test_redirect.py | 68 |
2 files changed, 80 insertions, 1 deletions
diff --git a/tests/test_exceptions.py b/tests/test_exceptions.py index becf107..3e3dc9f 100644 --- a/tests/test_exceptions.py +++ b/tests/test_exceptions.py | |||
| @@ -1,5 +1,5 @@ | |||
| 1 | import adblock | 1 | import adblock |
| 2 | 2 | import pytest | |
| 3 | 3 | ||
| 4 | def test_correct_baseclasses(): | 4 | def test_correct_baseclasses(): |
| 5 | assert issubclass(adblock.AdblockException, Exception) | 5 | assert issubclass(adblock.AdblockException, Exception) |
| @@ -9,3 +9,14 @@ def test_correct_baseclasses(): | |||
| 9 | assert issubclass(adblock.OptimizedFilterExistence, adblock.BlockerException) | 9 | assert issubclass(adblock.OptimizedFilterExistence, adblock.BlockerException) |
| 10 | assert issubclass(adblock.BadFilterAddUnsupported, adblock.BlockerException) | 10 | assert issubclass(adblock.BadFilterAddUnsupported, adblock.BlockerException) |
| 11 | assert issubclass(adblock.FilterExists, adblock.BlockerException) | 11 | assert issubclass(adblock.FilterExists, adblock.BlockerException) |
| 12 | assert issubclass(adblock.AddResourceError, adblock.BlockerException) | ||
| 13 | |||
| 14 | |||
| 15 | def test_add_resource_error(): | ||
| 16 | filter_set = adblock.FilterSet() | ||
| 17 | engine = adblock.Engine(filter_set=filter_set) | ||
| 18 | |||
| 19 | with pytest.raises(adblock.AddResourceError) as exc: | ||
| 20 | engine.add_resource(name='aa', content_type="image/jpeg", content="111") | ||
| 21 | |||
| 22 | assert "invalid base64 content" in str(exc.value) | ||
diff --git a/tests/test_redirect.py b/tests/test_redirect.py new file mode 100644 index 0000000..a99a728 --- /dev/null +++ b/tests/test_redirect.py | |||
| @@ -0,0 +1,68 @@ | |||
| 1 | import adblock | ||
| 2 | |||
| 3 | |||
| 4 | def test_redirect_worked_as_excepted_with_include_redirect_urls(): | ||
| 5 | # https://github.com/brave/adblock-rust/blob/b7f29af8c0a0d000201d8d769b6a0b25a9dd4e89/src/blocker.rs#L1242 | ||
| 6 | filter_set = adblock.FilterSet() | ||
| 7 | filter_set.add_filter_list("||foo.com$important,redirect-url=http://xyz.com", include_redirect_urls=True) | ||
| 8 | |||
| 9 | engine = adblock.Engine(filter_set=filter_set) | ||
| 10 | |||
| 11 | res = engine.check_network_urls("https://foo.com", "https://foo.com", "script") | ||
| 12 | assert res.matched is True | ||
| 13 | assert res.important is True | ||
| 14 | assert res.redirect_type == "url" | ||
| 15 | assert res.redirect == "http://xyz.com" | ||
| 16 | |||
| 17 | |||
| 18 | def test_redirect_url_is_not_recognized_without_include_redirect_urls(): | ||
| 19 | # https://github.com/brave/adblock-rust/blob/b7f29af8c0a0d000201d8d769b6a0b25a9dd4e89/src/blocker.rs#L1267 | ||
| 20 | filter_set2 = adblock.FilterSet() | ||
| 21 | filter_set2.add_filter_list("||foo.com$important,redirect-url=http://xyz.com", include_redirect_urls=False) | ||
| 22 | |||
| 23 | engine2 = adblock.Engine(filter_set=filter_set2) | ||
| 24 | |||
| 25 | res = engine2.check_network_urls("https://foo.com", "https://foo.com", "script") | ||
| 26 | assert res.matched is False | ||
| 27 | assert res.redirect is None | ||
| 28 | assert res.redirect_type is None | ||
| 29 | |||
| 30 | |||
| 31 | def test_redirect_url_exception(): | ||
| 32 | # https://github.com/brave/adblock-rust/blob/b7f29af8c0a0d000201d8d769b6a0b25a9dd4e89/src/blocker.rs#L1314 | ||
| 33 | filter_set = adblock.FilterSet(debug=True) | ||
| 34 | filter_set.add_filter_list( | ||
| 35 | """ | ||
| 36 | ||imdb-video.media-imdb.com$media,redirect-url=http://xyz.com | ||
| 37 | @@||imdb-video.media-imdb.com^$domain=imdb.com | ||
| 38 | """, | ||
| 39 | include_redirect_urls=True | ||
| 40 | ) | ||
| 41 | |||
| 42 | engine2 = adblock.Engine(filter_set=filter_set, optimize=False) | ||
| 43 | |||
| 44 | res = engine2.check_network_urls( | ||
| 45 | "https://imdb-video.media-imdb.com/kBOeI88k1o23eNAi", "https://www.imdb.com/video/13", "media" | ||
| 46 | ) | ||
| 47 | assert res.matched is False | ||
| 48 | assert res.redirect == "http://xyz.com" | ||
| 49 | assert res.redirect_type == "url" | ||
| 50 | assert res.exception == "@@||imdb-video.media-imdb.com^$domain=imdb.com" | ||
| 51 | |||
| 52 | |||
| 53 | def test_redirect_with_custom_resource(): | ||
| 54 | filters = adblock.FilterSet() | ||
| 55 | filters.add_filter_list("-advertisement-$redirect=test\n") | ||
| 56 | |||
| 57 | engine = adblock.Engine(filter_set=filters) | ||
| 58 | engine.add_resource(name="test", content_type="application/javascript", content="YWxlcnQoMSk=") | ||
| 59 | |||
| 60 | result = engine.check_network_urls( | ||
| 61 | url="http://example.com/-advertisement-icon.", source_url="example.com", request_type="image", | ||
| 62 | ) | ||
| 63 | |||
| 64 | assert result.matched | ||
| 65 | assert not result.exception | ||
| 66 | assert not result.important | ||
| 67 | assert result.redirect == "data:application/javascript;base64,YWxlcnQoMSk=" | ||
| 68 | |||
