1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
//! # Network Interception and Monitoring
//!
//! This module provides comprehensive network capabilities including request
//! interception, response mocking, event monitoring, and HAR recording/replay.
//!
//! ## Features
//!
//! - **Request Interception**: Intercept and modify outgoing requests
//! - **Response Mocking**: Return custom responses without hitting the server
//! - **Request Blocking**: Block requests matching specific patterns
//! - **Network Events**: Monitor requests, responses, and failures
//! - **HAR Recording**: Record network traffic for debugging
//! - **HAR Replay**: Replay recorded traffic for testing
//! - **WebSocket Monitoring**: Track WebSocket connections and messages
//!
//! ## Mock API Responses
//!
//! Mock API endpoints to test UI without a backend:
//!
//! ```ignore
//! use viewpoint_core::Browser;
//!
//! # async fn example() -> Result<(), viewpoint_core::CoreError> {
//! # let browser = Browser::launch().headless(true).launch().await?;
//! # let context = browser.new_context().await?;
//! # let page = context.new_page().await?;
//! // Mock a REST API endpoint with JSON response
//! page.route("**/api/users", |route| async move {
//! route.fulfill()
//! .status(200)
//! .content_type("application/json")
//! .body(r#"{"users": [{"id": 1, "name": "Alice"}, {"id": 2, "name": "Bob"}]}"#)
//! .fulfill()
//! .await
//! }).await?;
//!
//! // Mock different responses based on request method
//! page.route("**/api/items", |route| async move {
//! let method = route.request().method();
//! match method.as_str() {
//! "GET" => {
//! route.fulfill()
//! .status(200)
//! .content_type("application/json")
//! .body(r#"[{"id": 1, "name": "Item 1"}]"#)
//! .fulfill()
//! .await
//! }
//! "POST" => {
//! route.fulfill()
//! .status(201)
//! .content_type("application/json")
//! .body(r#"{"id": 2, "name": "New Item", "created": true}"#)
//! .fulfill()
//! .await
//! }
//! "DELETE" => {
//! route.fulfill()
//! .status(204)
//! .fulfill()
//! .await
//! }
//! _ => route.continue_route().continue_route().await
//! }
//! }).await?;
//!
//! // Mock error responses
//! page.route("**/api/error", |route| async move {
//! route.fulfill()
//! .status(500)
//! .content_type("application/json")
//! .body(r#"{"error": "Internal Server Error", "code": "SERVER_ERROR"}"#)
//! .fulfill()
//! .await
//! }).await?;
//!
//! // Mock 404 Not Found
//! page.route("**/api/not-found", |route| async move {
//! route.fulfill()
//! .status(404)
//! .content_type("application/json")
//! .body(r#"{"error": "Resource not found"}"#)
//! .fulfill()
//! .await
//! }).await?;
//!
//! // Mock delayed response for loading state testing
//! page.route("**/api/slow", |route| async move {
//! tokio::time::sleep(std::time::Duration::from_secs(2)).await;
//! route.fulfill()
//! .status(200)
//! .content_type("application/json")
//! .body(r#"{"data": "loaded"}"#)
//! .fulfill()
//! .await
//! }).await?;
//! # Ok(())
//! # }
//! ```
//!
//! ## Request Interception
//!
//! Use [`Route`] to intercept and handle network requests:
//!
//! ```ignore
//! use viewpoint_core::Browser;
//!
//! # async fn example() -> Result<(), viewpoint_core::CoreError> {
//! # let browser = Browser::launch().headless(true).launch().await?;
//! # let context = browser.new_context().await?;
//! # let page = context.new_page().await?;
//! // Block images for faster tests
//! page.route("**/*.{png,jpg,jpeg,gif,webp}", |route| async move {
//! route.abort().await
//! }).await?;
//!
//! // Add authentication header to all API requests
//! page.route("**/api/**", |route| async move {
//! route.continue_route()
//! .header("Authorization", "Bearer test-token-123")
//! .continue_route()
//! .await
//! }).await?;
//!
//! // Modify POST body
//! page.route("**/api/submit", |route| async move {
//! route.continue_route()
//! .post_data(r#"{"modified": true, "test": true}"#)
//! .continue_route()
//! .await
//! }).await?;
//!
//! // Intercept and inspect request before continuing
//! page.route("**/api/log", |route| async move {
//! let request = route.request();
//! println!("Request URL: {}", request.url());
//! println!("Request method: {}", request.method());
//! if let Some(body) = request.post_data() {
//! println!("Request body: {}", body);
//! }
//! route.continue_route().continue_route().await
//! }).await?;
//! # Ok(())
//! # }
//! ```
//!
//! ## URL Patterns
//!
//! Routes support glob patterns for matching URLs:
//!
//! - `**` - Match any path segments
//! - `*` - Match any characters except `/`
//! - `?` - Match a single character
//!
//! ```ignore
//! use viewpoint_core::{Browser, Route};
//! # async fn example() -> Result<(), viewpoint_core::CoreError> {
//! # let browser = Browser::launch().headless(true).launch().await?;
//! # let context = browser.new_context().await?;
//! # let page = context.new_page().await?;
//! // Match all API endpoints
//! page.route("**/api/**", |route| async move {
//! route.continue_route().continue_route().await
//! }).await?;
//!
//! // Match specific file types
//! page.route("**/*.{js,css}", |route| async move {
//! route.continue_route().continue_route().await
//! }).await?;
//!
//! // Match exact URL
//! page.route("https://example.com/login", |route| async move {
//! route.continue_route().continue_route().await
//! }).await?;
//! # Ok(())
//! # }
//! ```
//!
//! ## Network Events
//!
//! Monitor network activity with event listeners:
//!
//! ```ignore
//! use viewpoint_core::Browser;
//!
//! # async fn example() -> Result<(), viewpoint_core::CoreError> {
//! # let browser = Browser::launch().headless(true).launch().await?;
//! # let context = browser.new_context().await?;
//! # let page = context.new_page().await?;
//! // Wait for a specific request
//! let request = page.wait_for_request("**/api/data")
//! .wait()
//! .await?;
//! println!("Request URL: {}", request.url());
//!
//! // Wait for a specific response
//! let response = page.wait_for_response("**/api/data")
//! .wait()
//! .await?;
//! println!("Response status: {}", response.status());
//! # Ok(())
//! # }
//! ```
//!
//! ## HAR Recording
//!
//! Record network traffic for debugging or test fixtures:
//!
//! ```ignore
//! use viewpoint_core::Browser;
//!
//! # async fn example() -> Result<(), viewpoint_core::CoreError> {
//! # let browser = Browser::launch().headless(true).launch().await?;
//! # let context = browser.new_context().await?;
//! // Start HAR recording
//! context.route_from_har()
//! .record_path("recording.har")
//! .build()
//! .await?;
//!
//! // ... navigate and interact ...
//!
//! // HAR is automatically saved when context closes
//! # Ok(())
//! # }
//! ```
//!
//! ## HAR Replay
//!
//! Replay recorded traffic for deterministic tests:
//!
//! ```ignore
//! use viewpoint_core::Browser;
//!
//! # async fn example() -> Result<(), viewpoint_core::CoreError> {
//! # let browser = Browser::launch().headless(true).launch().await?;
//! # let context = browser.new_context().await?;
//! // Replay from HAR file
//! context.route_from_har()
//! .path("recording.har")
//! .build()
//! .await?;
//!
//! // Now requests will be served from the HAR file
//! let page = context.new_page().await?;
//! page.goto("https://example.com").goto().await?;
//! # Ok(())
//! # }
//! ```
//!
//! ## WebSocket Monitoring
//!
//! Monitor WebSocket connections:
//!
//! ```ignore
//! use viewpoint_core::Browser;
//!
//! # async fn example() -> Result<(), viewpoint_core::CoreError> {
//! # let browser = Browser::launch().headless(true).launch().await?;
//! # let context = browser.new_context().await?;
//! # let page = context.new_page().await?;
//! // Listen for WebSocket connections
//! page.on_websocket(|ws| async move {
//! println!("WebSocket connected: {}", ws.url());
//!
//! // Listen for messages
//! ws.on_frame(|frame| async move {
//! println!("Frame: {:?}", frame.payload());
//! Ok(())
//! }).await;
//!
//! Ok(())
//! }).await;
//! # Ok(())
//! # }
//! ```
pub
pub use ;
pub use RouteHandlerRegistry;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
// Re-export CDP types that are used directly
pub use HeaderEntry;