#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, serde::Serialize)]
pub enum RequestStage {
Request,
Response,
}
impl RequestStage {
#[must_use]
pub fn as_cdp_str(&self) -> &'static str {
match self {
Self::Request => "Request",
Self::Response => "Response",
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, serde::Serialize)]
pub enum ResourceType {
Document,
Stylesheet,
Image,
Media,
Font,
Script,
TextTrack,
XHR,
Fetch,
EventSource,
WebSocket,
Manifest,
SignedExchange,
Ping,
CSPViolationReport,
Preflight,
Other,
}
impl ResourceType {
#[must_use]
pub fn as_cdp_str(&self) -> &'static str {
match self {
Self::Document => "Document",
Self::Stylesheet => "Stylesheet",
Self::Image => "Image",
Self::Media => "Media",
Self::Font => "Font",
Self::Script => "Script",
Self::TextTrack => "TextTrack",
Self::XHR => "XHR",
Self::Fetch => "Fetch",
Self::EventSource => "EventSource",
Self::WebSocket => "WebSocket",
Self::Manifest => "Manifest",
Self::SignedExchange => "SignedExchange",
Self::Ping => "Ping",
Self::CSPViolationReport => "CSPViolationReport",
Self::Preflight => "Preflight",
Self::Other => "Other",
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, serde::Serialize)]
pub enum AbortReason {
Failed,
Aborted,
TimedOut,
AccessDenied,
ConnectionClosed,
ConnectionReset,
ConnectionRefused,
ConnectionAborted,
ConnectionFailed,
NameNotResolved,
InternetDisconnected,
AddressUnreachable,
BlockedByClient,
BlockedByResponse,
}
impl AbortReason {
#[must_use]
pub fn as_cdp_str(&self) -> &'static str {
match self {
Self::Failed => "Failed",
Self::Aborted => "Aborted",
Self::TimedOut => "TimedOut",
Self::AccessDenied => "AccessDenied",
Self::ConnectionClosed => "ConnectionClosed",
Self::ConnectionReset => "ConnectionReset",
Self::ConnectionRefused => "ConnectionRefused",
Self::ConnectionAborted => "ConnectionAborted",
Self::ConnectionFailed => "ConnectionFailed",
Self::NameNotResolved => "NameNotResolved",
Self::InternetDisconnected => "InternetDisconnected",
Self::AddressUnreachable => "AddressUnreachable",
Self::BlockedByClient => "BlockedByClient",
Self::BlockedByResponse => "BlockedByResponse",
}
}
}
impl std::fmt::Display for AbortReason {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.as_cdp_str())
}
}
#[derive(Debug, Clone)]
pub struct RequestInfo {
pub url: String,
pub method: String,
pub headers: Vec<(String, String)>,
pub post_data: Option<Vec<u8>>,
pub resource_type: ResourceType,
}
#[derive(Debug, Clone)]
pub struct ResponseInfo {
pub status: u16,
pub status_text: String,
pub headers: Vec<(String, String)>,
}
#[derive(Debug, Clone, Default)]
pub struct ResponseOverrides {
pub status: Option<u16>,
pub phrase: Option<String>,
pub headers: Option<Vec<(String, String)>>,
}
#[derive(Debug, Clone, Default)]
pub struct RequestOverrides {
pub url: Option<String>,
pub method: Option<String>,
pub headers: Option<Vec<(String, String)>>,
pub post_data: Option<Vec<u8>>,
}
#[cfg(test)]
#[allow(clippy::panic, clippy::unwrap_used)]
mod tests {
use super::*;
#[test]
fn enum_cdp_strings_snapshot() {
let pairs = serde_json::json!({
"RequestStage": [
["Request", RequestStage::Request.as_cdp_str()],
["Response", RequestStage::Response.as_cdp_str()],
],
"ResourceType": [
["Document", ResourceType::Document.as_cdp_str()],
["Stylesheet", ResourceType::Stylesheet.as_cdp_str()],
["Image", ResourceType::Image.as_cdp_str()],
["Media", ResourceType::Media.as_cdp_str()],
["Font", ResourceType::Font.as_cdp_str()],
["Script", ResourceType::Script.as_cdp_str()],
["TextTrack", ResourceType::TextTrack.as_cdp_str()],
["XHR", ResourceType::XHR.as_cdp_str()],
["Fetch", ResourceType::Fetch.as_cdp_str()],
["EventSource", ResourceType::EventSource.as_cdp_str()],
["WebSocket", ResourceType::WebSocket.as_cdp_str()],
["Manifest", ResourceType::Manifest.as_cdp_str()],
["SignedExchange", ResourceType::SignedExchange.as_cdp_str()],
["Ping", ResourceType::Ping.as_cdp_str()],
["CSPViolationReport", ResourceType::CSPViolationReport.as_cdp_str()],
["Preflight", ResourceType::Preflight.as_cdp_str()],
["Other", ResourceType::Other.as_cdp_str()],
],
"AbortReason": [
["Failed", AbortReason::Failed.as_cdp_str()],
["Aborted", AbortReason::Aborted.as_cdp_str()],
["TimedOut", AbortReason::TimedOut.as_cdp_str()],
["AccessDenied", AbortReason::AccessDenied.as_cdp_str()],
["ConnectionClosed", AbortReason::ConnectionClosed.as_cdp_str()],
["ConnectionReset", AbortReason::ConnectionReset.as_cdp_str()],
["ConnectionRefused", AbortReason::ConnectionRefused.as_cdp_str()],
["ConnectionAborted", AbortReason::ConnectionAborted.as_cdp_str()],
["ConnectionFailed", AbortReason::ConnectionFailed.as_cdp_str()],
["NameNotResolved", AbortReason::NameNotResolved.as_cdp_str()],
["InternetDisconnected", AbortReason::InternetDisconnected.as_cdp_str()],
["AddressUnreachable", AbortReason::AddressUnreachable.as_cdp_str()],
["BlockedByClient", AbortReason::BlockedByClient.as_cdp_str()],
["BlockedByResponse", AbortReason::BlockedByResponse.as_cdp_str()],
],
});
insta::assert_yaml_snapshot!("enum_cdp_strings", pairs);
}
#[test]
fn abort_reason_display_matches_cdp_string() {
for reason in [
AbortReason::Failed,
AbortReason::BlockedByClient,
AbortReason::NameNotResolved,
] {
assert_eq!(reason.to_string(), reason.as_cdp_str());
}
}
}