summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--adblock/__init__.py8
-rw-r--r--adblock/adblock.pyi8
-rw-r--r--src/lib.rs54
-rw-r--r--tests/test_exceptions.py18
4 files changed, 58 insertions, 30 deletions
diff --git a/adblock/__init__.py b/adblock/__init__.py
index 4ba4c4b..b231aeb 100644
--- a/adblock/__init__.py
+++ b/adblock/__init__.py
@@ -11,7 +11,9 @@ from adblock.adblock import (
11 OptimizedFilterExistence, 11 OptimizedFilterExistence,
12 BadFilterAddUnsupported, 12 BadFilterAddUnsupported,
13 FilterExists, 13 FilterExists,
14 AddResourceError, 14 AddResourceException,
15 InvalidUtf8ContentError,
16 InvalidBase64ContentError,
15) 17)
16 18
17 19
@@ -27,5 +29,7 @@ __all__ = (
27 "OptimizedFilterExistence", 29 "OptimizedFilterExistence",
28 "BadFilterAddUnsupported", 30 "BadFilterAddUnsupported",
29 "FilterExists", 31 "FilterExists",
30 "AddResourceError", 32 "AddResourceException",
33 "InvalidUtf8ContentError",
34 "InvalidBase64ContentError",
31) 35)
diff --git a/adblock/adblock.pyi b/adblock/adblock.pyi
index 4ed340c..9fae1da 100644
--- a/adblock/adblock.pyi
+++ b/adblock/adblock.pyi
@@ -21,7 +21,13 @@ class BadFilterAddUnsupported(BlockerException):
21class FilterExists(BlockerException): 21class FilterExists(BlockerException):
22 pass 22 pass
23 23
24class AddResourceError(BlockerException): 24class AddResourceException(AdblockException):
25 pass
26
27class InvalidUtf8ContentError(AddResourceException):
28 pass
29
30class InvalidBase64ContentError(AddResourceException):
25 pass 31 pass
26 32
27class BlockerResult: 33class BlockerResult:
diff --git a/src/lib.rs b/src/lib.rs
index 5fba7dd..9117254 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -58,7 +58,18 @@ fn adblock(py: Python<'_>, m: &PyModule) -> PyResult<()> {
58 py.get_type::<BadFilterAddUnsupported>(), 58 py.get_type::<BadFilterAddUnsupported>(),
59 )?; 59 )?;
60 m.add("FilterExists", py.get_type::<FilterExists>())?; 60 m.add("FilterExists", py.get_type::<FilterExists>())?;
61 m.add("AddResourceError", py.get_type::<AddResourceError>())?; 61 m.add(
62 "AddResourceException",
63 py.get_type::<AddResourceException>(),
64 )?;
65 m.add(
66 "InvalidBase64ContentError",
67 py.get_type::<InvalidBase64ContentError>(),
68 )?;
69 m.add(
70 "InvalidUtf8ContentError",
71 py.get_type::<InvalidUtf8ContentError>(),
72 )?;
62 Ok(()) 73 Ok(())
63} 74}
64 75
@@ -113,21 +124,14 @@ pub struct BlockerResult {
113 124
114impl From<RustBlockerResult> for BlockerResult { 125impl From<RustBlockerResult> for BlockerResult {
115 fn from(br: RustBlockerResult) -> Self { 126 fn from(br: RustBlockerResult) -> Self {
116 let mut redirect: Option<String> = None; 127 let (redirect, redirect_type) = if let Some(resource) = br.redirect {
117 let mut redirect_type: Option<String> = None; 128 match resource {
118 if br.redirect.is_some() { 129 Redirection::Resource(resource) => (Some(resource), Some("resource".to_string())),
119 let resource = br.redirect.unwrap(); 130 Redirection::Url(url) => (Some(url), Some("url".to_string())),
120 redirect = Option::from(match resource { 131 }
121 Redirection::Resource(resource) => { 132 } else {
122 redirect_type = Some("resource".to_string()); 133 (None, None)
123 resource 134 };
124 }
125 Redirection::Url(url) => {
126 redirect_type = Some("url".to_string());
127 url
128 }
129 });
130 }
131 135
132 Self { 136 Self {
133 matched: br.matched, 137 matched: br.matched,
@@ -135,8 +139,8 @@ impl From<RustBlockerResult> for BlockerResult {
135 exception: br.exception, 139 exception: br.exception,
136 filter: br.filter, 140 filter: br.filter,
137 error: br.error, 141 error: br.error,
138 redirect_type: redirect_type, 142 redirect_type,
139 redirect: redirect, 143 redirect,
140 } 144 }
141 } 145 }
142} 146}
@@ -189,12 +193,14 @@ impl Display for BlockerError {
189 193
190create_exception!(adblock, AdblockException, PyException); 194create_exception!(adblock, AdblockException, PyException);
191create_exception!(adblock, BlockerException, AdblockException); 195create_exception!(adblock, BlockerException, AdblockException);
196create_exception!(adblock, AddResourceException, AdblockException);
197create_exception!(adblock, InvalidBase64ContentError, AddResourceException);
198create_exception!(adblock, InvalidUtf8ContentError, AddResourceException);
192create_exception!(adblock, SerializationError, BlockerException); 199create_exception!(adblock, SerializationError, BlockerException);
193create_exception!(adblock, DeserializationError, BlockerException); 200create_exception!(adblock, DeserializationError, BlockerException);
194create_exception!(adblock, OptimizedFilterExistence, BlockerException); 201create_exception!(adblock, OptimizedFilterExistence, BlockerException);
195create_exception!(adblock, BadFilterAddUnsupported, BlockerException); 202create_exception!(adblock, BadFilterAddUnsupported, BlockerException);
196create_exception!(adblock, FilterExists, BlockerException); 203create_exception!(adblock, FilterExists, BlockerException);
197create_exception!(adblock, AddResourceError, BlockerException);
198 204
199impl From<BlockerError> for PyErr { 205impl From<BlockerError> for PyErr {
200 fn from(err: BlockerError) -> Self { 206 fn from(err: BlockerError) -> Self {
@@ -517,12 +523,12 @@ impl Engine {
517 match result { 523 match result {
518 Ok(_) => Ok(()), 524 Ok(_) => Ok(()),
519 Err(err) => match err { 525 Err(err) => match err {
520 RustAddResourceError::InvalidBase64Content => Err(AddResourceError::new_err( 526 RustAddResourceError::InvalidBase64Content => Err(
521 "invalid base64 content".to_string(), 527 InvalidBase64ContentError::new_err("invalid base64 content".to_string()),
528 ),
529 RustAddResourceError::InvalidUtf8Content => Err(InvalidUtf8ContentError::new_err(
530 "invalid utf content".to_string(),
522 )), 531 )),
523 RustAddResourceError::InvalidUtf8Content => {
524 Err(AddResourceError::new_err("invalid utf content".to_string()))
525 }
526 }, 532 },
527 } 533 }
528 } 534 }
diff --git a/tests/test_exceptions.py b/tests/test_exceptions.py
index 2efe2b2..e1c357c 100644
--- a/tests/test_exceptions.py
+++ b/tests/test_exceptions.py
@@ -5,19 +5,31 @@ import pytest
5def test_correct_baseclasses(): 5def test_correct_baseclasses():
6 assert issubclass(adblock.AdblockException, Exception) 6 assert issubclass(adblock.AdblockException, Exception)
7 assert issubclass(adblock.BlockerException, adblock.AdblockException) 7 assert issubclass(adblock.BlockerException, adblock.AdblockException)
8 assert issubclass(adblock.AddResourceException, adblock.AdblockException)
9 assert issubclass(adblock.InvalidUtf8ContentError, adblock.AddResourceException)
10 assert issubclass(adblock.InvalidBase64ContentError, adblock.AddResourceException)
8 assert issubclass(adblock.SerializationError, adblock.BlockerException) 11 assert issubclass(adblock.SerializationError, adblock.BlockerException)
9 assert issubclass(adblock.DeserializationError, adblock.BlockerException) 12 assert issubclass(adblock.DeserializationError, adblock.BlockerException)
10 assert issubclass(adblock.OptimizedFilterExistence, adblock.BlockerException) 13 assert issubclass(adblock.OptimizedFilterExistence, adblock.BlockerException)
11 assert issubclass(adblock.BadFilterAddUnsupported, adblock.BlockerException) 14 assert issubclass(adblock.BadFilterAddUnsupported, adblock.BlockerException)
12 assert issubclass(adblock.FilterExists, adblock.BlockerException) 15 assert issubclass(adblock.FilterExists, adblock.BlockerException)
13 assert issubclass(adblock.AddResourceError, adblock.BlockerException)
14 16
15 17
16def test_add_resource_error(): 18def test_add_resource_error():
17 filter_set = adblock.FilterSet() 19 filter_set = adblock.FilterSet()
18 engine = adblock.Engine(filter_set=filter_set) 20 engine = adblock.Engine(filter_set=filter_set)
19 21
20 with pytest.raises(adblock.AddResourceError) as exc: 22 with pytest.raises(adblock.InvalidBase64ContentError) as exc:
21 engine.add_resource(name="aa", content_type="image/jpeg", content="111") 23 engine.add_resource(name="aa", content_type="image/jpeg", content="111")
22
23 assert "invalid base64 content" in str(exc.value) 24 assert "invalid base64 content" in str(exc.value)
25
26 with pytest.raises(adblock.InvalidUtf8ContentError) as exc:
27 # // Ensure any text contents are also valid utf8
28 # MimeType::ApplicationJavascript | MimeType::TextPlain | MimeType::TextHtml => {
29 # let _ = String::from_utf8(decoded)?;
30 # }
31 # xOO6ww== => base64.b64encode('你好'.encode('gbk'))
32 engine.add_resource(
33 name="aa", content_type="application/javascript", content="xOO6ww=="
34 )
35 assert "invalid utf content" in str(exc.value)