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

Real-world test case for message parsing #196

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ _fetcher-rusttls-tokio = ["fetcher", "chromiumoxide_fetcher/_rustls-tokio"]
_fetcher-native-async-std = ["fetcher", "chromiumoxide_fetcher/_native-async-std"]
_fetcher-native-tokio = ["fetcher", "chromiumoxide_fetcher/_native-tokio"]

debug-raw-ws-messages = []

[[example]]
name = "wiki-tokio"
required-features = ["tokio-runtime"]
Expand Down
148 changes: 139 additions & 9 deletions src/conn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,22 +136,152 @@ impl<T: EventMessage + Unpin> Stream for Connection<T> {
}

// read from the ws
match ready!(pin.ws.poll_next_unpin(cx)) {
Some(Ok(msg)) => match serde_json::from_slice::<Message<T>>(&msg.into_data()) {
let msg = match ready!(pin.ws.poll_next_unpin(cx)) {
Some(Ok(msg)) => msg,
Some(Err(err)) => return Poll::Ready(Some(Err(CdpError::Ws(err)))),
None => {
// ws connection closed
return Poll::Ready(None);
}
};

tracing::trace!(target: "chromiumoxide::conn::raw_ws", ?msg, "Got raw WS message");

#[cfg(feature = "debug-raw-ws-messages")]
let msg_for_debug = msg.clone();

Poll::Ready(Some(
match serde_json::from_slice::<Message<T>>(&msg.into_data()) {
Ok(msg) => {
tracing::trace!("Received {:?}", msg);
Poll::Ready(Some(Ok(msg)))
Ok(msg)
}
Err(err) => {
#[cfg(feature = "debug-raw-ws-messages")]
tracing::debug!(target: "chromiumoxide::conn::raw_ws::parse_errors", msg = ?msg_for_debug, "Failed to parse raw WS message");

tracing::error!("Failed to deserialize WS response {}", err);
Poll::Ready(Some(Err(err.into())))
Err(err.into())
}
},
Some(Err(err)) => Poll::Ready(Some(Err(CdpError::Ws(err)))),
None => {
// ws connection closed
Poll::Ready(None)
}
))
}
}

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

const SAMPLES: &[&str] = &[r#"{
"method": "Network.responseReceivedExtraInfo",
"params": {
"requestId": "...",
"blockedCookies": [
{
"blockedReasons": [
"SameSiteUnspecifiedTreatedAsLax"
],
"cookieLine": "...=5; expires=Mon, 18 Dec 2023 01:25:44 GMT; Max-Age=3600; Path=/",
"cookie": {
"name": "...",
"value": "5",
"domain": "...",
"path": "/",
"expires": 1702862744.118586,
"size": 21,
"httpOnly": false,
"secure": false,
"session": false,
"priority": "Medium",
"sameParty": false,
"sourceScheme": "Secure",
"sourcePort": 443
}
},
{
"blockedReasons": [
"SameSiteUnspecifiedTreatedAsLax"
],
"cookieLine": "...=\"\"; expires=Thu, 01 Jan 1970 00:00:00 GMT; Max-Age=0; Path=/",
"cookie": {
"name": "...",
"value": "\"\"",
"domain": "...",
"path": "/",
"expires": null,
"size": 20,
"httpOnly": false,
"secure": false,
"session": false,
"priority": "Medium",
"sameParty": false,
"sourceScheme": "Secure",
"sourcePort": 443
}
},
{
"blockedReasons": [
"SameSiteUnspecifiedTreatedAsLax"
],
"cookieLine": "...=\"\"; expires=Thu, 01 Jan 1970 00:00:00 GMT; Max-Age=0; Path=/",
"cookie": {
"name": "...",
"value": "\"\"",
"domain": "...",
"path": "/",
"expires": null,
"size": 11,
"httpOnly": false,
"secure": false,
"session": false,
"priority": "Medium",
"sameParty": false,
"sourceScheme": "Secure",
"sourcePort": 443
}
},
{
"blockedReasons": [
"SameSiteUnspecifiedTreatedAsLax"
],
"cookieLine": "...=\"\"; expires=Thu, 01 Jan 1970 00:00:00 GMT; Max-Age=0; Path=/",
"cookie": {
"name": "...",
"value": "\"\"",
"domain": "...",
"path": "/",
"expires": null,
"size": 12,
"httpOnly": false,
"secure": false,
"session": false,
"priority": "Medium",
"sameParty": false,
"sourceScheme": "Secure",
"sourcePort": 443
}
}
],
"headers": {
"server": "cloudflare",
"more": "redacted"
},
"resourceIPAddressSpace": "Public",
"statusCode": 200,
"cookiePartitionKey": "https://...",
"cookiePartitionKeyOpaque": false
},
"sessionId": "2B3589AF08CFD7B319297F7D3E57FAEF"
}"#];

#[test]
fn parses_without_error() {
for sample in SAMPLES {
let _message: Message<chromiumoxide_cdp::cdp::CdpEventMessage> =
match serde_json::from_str(sample) {
Ok(val) => val,
Err(error) => panic!("Failed to parse the message:\n{error}\n{sample}",),
};
}
}
}
Loading