Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CdpEvent::InvalidParams variant #207

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion chromiumoxide_cdp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ tempfile = "3.10.1"
chromiumoxide_pdl = { path = "../chromiumoxide_pdl", version = "0.7" }
chromiumoxide_types = { path = "../chromiumoxide_types", version = "0.7" }
serde = { version = "1", features = ["derive"] }
serde_json = "1"
serde_json = { version = "1", features = ["raw_value"] }
11 changes: 10 additions & 1 deletion chromiumoxide_cdp/src/cdp.rs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion chromiumoxide_cdp/tests/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ fn generated_code_is_fresh() {

let tmp = tempfile::tempdir().unwrap();
Generator::default()
.out_dir(&tmp.path())
.out_dir(tmp.path())
.experimental(env::var("CDP_NO_EXPERIMENTAL").is_err())
.deprecated(env::var("CDP_DEPRECATED").is_ok())
.compile_pdls(&[js_proto, browser_proto])
Expand Down
22 changes: 14 additions & 8 deletions chromiumoxide_pdl/src/build/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,11 @@ impl<'a> EventBuilder<'a> {

let deserialize_from = if event.needs_box {
quote! {
#ty_qualifier::IDENTIFIER =>CdpEvent::#var_ident(Box::new(map.next_value::<#ty_qualifier>()?)),
#ty_qualifier::IDENTIFIER => serde_json::from_str(value.get()).map(Box::new).map(CdpEvent::#var_ident).unwrap_or_else(|_| CdpEvent::InvalidParams(serde_json::from_str::<serde_json::Value>(value.get()).unwrap())),
}
} else {
quote! {
#ty_qualifier::IDENTIFIER =>CdpEvent::#var_ident(map.next_value::<#ty_qualifier>()?),
#ty_qualifier::IDENTIFIER => serde_json::from_str(value.get()).map(CdpEvent::#var_ident).unwrap_or_else(|_| CdpEvent::InvalidParams(serde_json::from_str::<serde_json::Value>(value.get()).unwrap())),
}
};

Expand Down Expand Up @@ -182,7 +182,10 @@ impl<'a> EventBuilder<'a> {
#[derive(Debug, Clone, PartialEq)]
pub enum CdpEvent {
#variants_stream
Other(serde_json::Value)
/// A known method with invalid params
InvalidParams(serde_json::Value),
/// Unknown method
Other(serde_json::Value),
}

impl CdpEvent {
Expand All @@ -195,14 +198,16 @@ impl<'a> EventBuilder<'a> {
pub fn into_json(self) -> serde_json::Result<serde_json::Value> {
match self {
#(CdpEvent::#var_idents(inner) => serde_json::to_value(inner),)*
CdpEvent::Other(val) => Ok(val)
CdpEvent::InvalidParams(val) => Ok(val),
CdpEvent::Other(val) => Ok(val),
}
}

pub fn into_event(self) -> ::std::result::Result<Box<dyn super::Event>, serde_json::Value> {
match self {
#event_as_boxed_results
CdpEvent::Other(other) => Err(other)
CdpEvent::InvalidParams(other) => Err(other),
CdpEvent::Other(other) => Err(other),
}
}

Expand Down Expand Up @@ -289,10 +294,10 @@ impl<'a> EventBuilder<'a> {
if params.is_some() {
return Err(de::Error::duplicate_field("params"));
}
params = Some(match method.as_ref().ok_or_else(|| de::Error::missing_field("params"))
?.as_str() {
let value = map.next_value::<&serde_json::value::RawValue>()?;
params = Some(match method.as_ref().ok_or_else(|| de::Error::missing_field("method"))?.as_str() {
#deserialize_from_method
_=>CdpEvent::Other(map.next_value::<serde_json::Value>()?)
_ => CdpEvent::Other(serde_json::from_str::<serde_json::Value>(value.get()).unwrap())
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These unwrap() should never panic, because RawValue can only be deserialized from a valid JSON, and Value can be serialized from any valid JSON.

});
}
}
Expand Down Expand Up @@ -340,6 +345,7 @@ impl<'a> EventBuilder<'a> {
{
match $ev {
#consume_event_macro_exprs
CdpEvent::InvalidParams(json) => {$custom(json);}
CdpEvent::Other(json) => {$custom(json);}
}
}
Expand Down
77 changes: 77 additions & 0 deletions src/handler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -754,3 +754,80 @@ pub(crate) enum HandlerMessage {
AddEventListener(EventListenerRequest),
CloseBrowser(OneshotSender<Result<CloseReturns>>),
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_invalid_cdp_event() {
// expires is invalid
let json = serde_json::json!({
"method": "Network.responseReceivedExtraInfo",
"params": {
"requestId": "requestId",
"blockedCookies": [
{
"blockedReason": ["SecureOnly"],
"cookieLine": "cookieLine",
"cookie": {
"name": "name",
"value": "value",
"domain": "example.com",
"path": "/",
"expires": null, // invalid here
"size": 10,
"httpOnly": false,
"secure": false,
"session": false,
"priority": "Medium",
"sameParty": false,
"sourceScheme": "Secure",
"sourcePort": 443
}
}
],
"headers": {
"key": "value"
},
"resourceIPAddressSpace": "Public",
"statusCode": 200,
},
"sessionId": "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
})
.to_string();
let message: CdpEventMessage = serde_json::from_str(&json).unwrap();
assert_eq!(
message.params,
CdpEvent::InvalidParams(serde_json::json!({
"requestId": "requestId",
"blockedCookies": [
{
"blockedReason": ["SecureOnly"],
"cookieLine": "cookieLine",
"cookie": {
"name": "name",
"value": "value",
"domain": "example.com",
"path": "/",
"expires": null, // invalid here
"size": 10,
"httpOnly": false,
"secure": false,
"session": false,
"priority": "Medium",
"sameParty": false,
"sourceScheme": "Secure",
"sourcePort": 443
}
}
],
"headers": {
"key": "value"
},
"resourceIPAddressSpace": "Public",
"statusCode": 200,
}))
);
}
}
Loading