commit 71210038c16fefbf741d573a29334db08271b4b6
parent 64c1fa2c91b55a35eceeffab6e0715374a5a7231
Author: dm <dm@work-pc>
Date: Fri, 18 Feb 2022 11:05:22 +0800
refactor: AddResourceException inheritance relationship && RustBlockerResult implementations
Diffstat:
4 files changed, 58 insertions(+), 30 deletions(-)
diff --git a/adblock/__init__.py b/adblock/__init__.py
@@ -11,7 +11,9 @@ from adblock.adblock import (
OptimizedFilterExistence,
BadFilterAddUnsupported,
FilterExists,
- AddResourceError,
+ AddResourceException,
+ InvalidUtf8ContentError,
+ InvalidBase64ContentError,
)
@@ -27,5 +29,7 @@ __all__ = (
"OptimizedFilterExistence",
"BadFilterAddUnsupported",
"FilterExists",
- "AddResourceError",
+ "AddResourceException",
+ "InvalidUtf8ContentError",
+ "InvalidBase64ContentError",
)
diff --git a/adblock/adblock.pyi b/adblock/adblock.pyi
@@ -21,7 +21,13 @@ class BadFilterAddUnsupported(BlockerException):
class FilterExists(BlockerException):
pass
-class AddResourceError(BlockerException):
+class AddResourceException(AdblockException):
+ pass
+
+class InvalidUtf8ContentError(AddResourceException):
+ pass
+
+class InvalidBase64ContentError(AddResourceException):
pass
class BlockerResult:
diff --git a/src/lib.rs b/src/lib.rs
@@ -58,7 +58,18 @@ fn adblock(py: Python<'_>, m: &PyModule) -> PyResult<()> {
py.get_type::<BadFilterAddUnsupported>(),
)?;
m.add("FilterExists", py.get_type::<FilterExists>())?;
- m.add("AddResourceError", py.get_type::<AddResourceError>())?;
+ m.add(
+ "AddResourceException",
+ py.get_type::<AddResourceException>(),
+ )?;
+ m.add(
+ "InvalidBase64ContentError",
+ py.get_type::<InvalidBase64ContentError>(),
+ )?;
+ m.add(
+ "InvalidUtf8ContentError",
+ py.get_type::<InvalidUtf8ContentError>(),
+ )?;
Ok(())
}
@@ -113,21 +124,14 @@ pub struct BlockerResult {
impl From<RustBlockerResult> for BlockerResult {
fn from(br: RustBlockerResult) -> Self {
- let mut redirect: Option<String> = None;
- let mut redirect_type: Option<String> = None;
- if br.redirect.is_some() {
- let resource = br.redirect.unwrap();
- redirect = Option::from(match resource {
- Redirection::Resource(resource) => {
- redirect_type = Some("resource".to_string());
- resource
- }
- Redirection::Url(url) => {
- redirect_type = Some("url".to_string());
- url
- }
- });
- }
+ let (redirect, redirect_type) = if let Some(resource) = br.redirect {
+ match resource {
+ Redirection::Resource(resource) => (Some(resource), Some("resource".to_string())),
+ Redirection::Url(url) => (Some(url), Some("url".to_string())),
+ }
+ } else {
+ (None, None)
+ };
Self {
matched: br.matched,
@@ -135,8 +139,8 @@ impl From<RustBlockerResult> for BlockerResult {
exception: br.exception,
filter: br.filter,
error: br.error,
- redirect_type: redirect_type,
- redirect: redirect,
+ redirect_type,
+ redirect,
}
}
}
@@ -189,12 +193,14 @@ impl Display for BlockerError {
create_exception!(adblock, AdblockException, PyException);
create_exception!(adblock, BlockerException, AdblockException);
+create_exception!(adblock, AddResourceException, AdblockException);
+create_exception!(adblock, InvalidBase64ContentError, AddResourceException);
+create_exception!(adblock, InvalidUtf8ContentError, AddResourceException);
create_exception!(adblock, SerializationError, BlockerException);
create_exception!(adblock, DeserializationError, BlockerException);
create_exception!(adblock, OptimizedFilterExistence, BlockerException);
create_exception!(adblock, BadFilterAddUnsupported, BlockerException);
create_exception!(adblock, FilterExists, BlockerException);
-create_exception!(adblock, AddResourceError, BlockerException);
impl From<BlockerError> for PyErr {
fn from(err: BlockerError) -> Self {
@@ -517,12 +523,12 @@ impl Engine {
match result {
Ok(_) => Ok(()),
Err(err) => match err {
- RustAddResourceError::InvalidBase64Content => Err(AddResourceError::new_err(
- "invalid base64 content".to_string(),
+ RustAddResourceError::InvalidBase64Content => Err(
+ InvalidBase64ContentError::new_err("invalid base64 content".to_string()),
+ ),
+ RustAddResourceError::InvalidUtf8Content => Err(InvalidUtf8ContentError::new_err(
+ "invalid utf content".to_string(),
)),
- RustAddResourceError::InvalidUtf8Content => {
- Err(AddResourceError::new_err("invalid utf content".to_string()))
- }
},
}
}
diff --git a/tests/test_exceptions.py b/tests/test_exceptions.py
@@ -5,19 +5,31 @@ import pytest
def test_correct_baseclasses():
assert issubclass(adblock.AdblockException, Exception)
assert issubclass(adblock.BlockerException, adblock.AdblockException)
+ assert issubclass(adblock.AddResourceException, adblock.AdblockException)
+ assert issubclass(adblock.InvalidUtf8ContentError, adblock.AddResourceException)
+ assert issubclass(adblock.InvalidBase64ContentError, adblock.AddResourceException)
assert issubclass(adblock.SerializationError, adblock.BlockerException)
assert issubclass(adblock.DeserializationError, adblock.BlockerException)
assert issubclass(adblock.OptimizedFilterExistence, adblock.BlockerException)
assert issubclass(adblock.BadFilterAddUnsupported, adblock.BlockerException)
assert issubclass(adblock.FilterExists, adblock.BlockerException)
- assert issubclass(adblock.AddResourceError, adblock.BlockerException)
def test_add_resource_error():
filter_set = adblock.FilterSet()
engine = adblock.Engine(filter_set=filter_set)
- with pytest.raises(adblock.AddResourceError) as exc:
+ with pytest.raises(adblock.InvalidBase64ContentError) as exc:
engine.add_resource(name="aa", content_type="image/jpeg", content="111")
-
assert "invalid base64 content" in str(exc.value)
+
+ with pytest.raises(adblock.InvalidUtf8ContentError) as exc:
+ # // Ensure any text contents are also valid utf8
+ # MimeType::ApplicationJavascript | MimeType::TextPlain | MimeType::TextHtml => {
+ # let _ = String::from_utf8(decoded)?;
+ # }
+ # xOO6ww== => base64.b64encode('你好'.encode('gbk'))
+ engine.add_resource(
+ name="aa", content_type="application/javascript", content="xOO6ww=="
+ )
+ assert "invalid utf content" in str(exc.value)