playwright_rs/protocol/
har_options.rs1use serde::Serialize;
8use serde_json::Value;
9
10#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
14#[serde(rename_all = "lowercase")]
15#[non_exhaustive]
16pub enum HarContent {
17 Omit,
19 Embed,
21 Attach,
23}
24
25#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
29#[serde(rename_all = "lowercase")]
30#[non_exhaustive]
31pub enum HarMode {
32 Full,
34 Minimal,
36}
37
38#[derive(Debug, Clone, Default)]
53#[non_exhaustive]
54pub struct StartHarOptions {
55 pub content: Option<HarContent>,
58 pub mode: Option<HarMode>,
60 pub url_filter: Option<String>,
62 pub resources_dir: Option<String>,
64}
65
66impl StartHarOptions {
67 pub fn content(mut self, content: HarContent) -> Self {
69 self.content = Some(content);
70 self
71 }
72 pub fn mode(mut self, mode: HarMode) -> Self {
74 self.mode = Some(mode);
75 self
76 }
77 pub fn url_filter(mut self, url_filter: impl Into<String>) -> Self {
79 self.url_filter = Some(url_filter.into());
80 self
81 }
82 pub fn resources_dir(mut self, resources_dir: impl Into<String>) -> Self {
84 self.resources_dir = Some(resources_dir.into());
85 self
86 }
87
88 pub(crate) fn to_record_har_json(&self, path: &str) -> Value {
95 let is_zip = path.ends_with(".zip");
96 let content = self.content.unwrap_or(if is_zip {
97 HarContent::Attach
98 } else {
99 HarContent::Embed
100 });
101 let mode = self.mode.unwrap_or(HarMode::Full);
102
103 let mut o = serde_json::json!({});
104 o["content"] = serde_json::to_value(content).expect("serialize HarContent cannot fail");
105 o["mode"] = serde_json::to_value(mode).expect("serialize HarMode cannot fail");
106 if let Some(glob) = &self.url_filter {
107 o["urlGlob"] = serde_json::json!(glob);
108 }
109 if let Some(dir) = &self.resources_dir {
110 o["resourcesDir"] = serde_json::json!(dir);
111 }
112 o
113 }
114}
115
116#[cfg(test)]
117mod tests {
118 use super::*;
119
120 #[test]
121 fn test_start_har_options_zip_defaults_to_attach() {
122 let json = StartHarOptions::default().to_record_har_json("run.har.zip");
123 assert_eq!(json["content"], "attach");
124 assert_eq!(json["mode"], "full");
125 assert!(json.get("harPath").is_none());
127 }
128
129 #[test]
130 fn test_start_har_options_plain_defaults_to_embed() {
131 let json = StartHarOptions::default().to_record_har_json("run.har");
132 assert_eq!(json["content"], "embed");
133 }
134
135 #[test]
136 fn test_start_har_options_setters() {
137 let opts = StartHarOptions::default()
138 .content(HarContent::Omit)
139 .mode(HarMode::Minimal)
140 .url_filter("**/api/**");
141 let json = opts.to_record_har_json("run.har");
142 assert_eq!(json["content"], "omit");
143 assert_eq!(json["mode"], "minimal");
144 assert_eq!(json["urlGlob"], "**/api/**");
145 assert!(json.get("resourcesDir").is_none());
146 }
147
148 #[test]
149 fn test_start_har_options_resources_dir_setter() {
150 let json = StartHarOptions::default()
151 .resources_dir("/tmp/har-resources")
152 .to_record_har_json("run.har");
153 assert_eq!(json["resourcesDir"], "/tmp/har-resources");
154 }
155}