http-client-vcr 1.1.0

Record http request and responses for testing
Documentation
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
use crate::cassette::Cassette;
use crate::filter::FilterChain;
use crate::serializable::{SerializableRequest, SerializableResponse};
use http_client::Error;
use std::path::PathBuf;

/// Utility function to apply filters to a cassette file and save the filtered version
/// This is useful for batch processing cassette files without creating a VcrClient
pub async fn filter_cassette_file<P: Into<PathBuf>>(
    cassette_path: P,
    filter_chain: FilterChain,
) -> Result<(), Error> {
    let path = cassette_path.into();

    // Load the cassette
    let mut cassette = Cassette::load_from_file(path.clone()).await?;

    // Apply filters to all interactions
    for interaction in &mut cassette.interactions {
        filter_chain.filter_request(&mut interaction.request);
        filter_chain.filter_response(&mut interaction.response);
    }

    // Save the filtered cassette
    cassette.save_to_file().await?;

    log::debug!(
        "Applied filters to {} interactions in {path:?}",
        cassette.interactions.len()
    );
    Ok(())
}

/// Apply a filter function to all requests in a cassette file
/// This allows for custom mutation logic beyond the standard filter chains
pub async fn mutate_all_requests<P, F>(cassette_path: P, mut mutator: F) -> Result<(), Error>
where
    P: Into<PathBuf>,
    F: FnMut(&mut SerializableRequest),
{
    let path = cassette_path.into();
    let mut cassette = Cassette::load_from_file(path.clone()).await?;

    for interaction in &mut cassette.interactions {
        mutator(&mut interaction.request);
    }

    cassette.save_to_file().await?;
    log::debug!(
        "Applied custom mutations to {} requests in {path:?}",
        cassette.interactions.len()
    );
    Ok(())
}

/// Apply a filter function to all responses in a cassette file
pub async fn mutate_all_responses<P, F>(cassette_path: P, mut mutator: F) -> Result<(), Error>
where
    P: Into<PathBuf>,
    F: FnMut(&mut SerializableResponse),
{
    let path = cassette_path.into();
    let mut cassette = Cassette::load_from_file(path.clone()).await?;

    for interaction in &mut cassette.interactions {
        mutator(&mut interaction.response);
    }

    cassette.save_to_file().await?;
    log::debug!(
        "Applied custom mutations to {} responses in {path:?}",
        cassette.interactions.len()
    );
    Ok(())
}

/// Apply mutation functions to both requests and responses in a cassette file
pub async fn mutate_all_interactions<P, RF, ResF>(
    cassette_path: P,
    mut request_mutator: RF,
    mut response_mutator: ResF,
) -> Result<(), Error>
where
    P: Into<PathBuf>,
    RF: FnMut(&mut SerializableRequest),
    ResF: FnMut(&mut SerializableResponse),
{
    let path = cassette_path.into();
    let mut cassette = Cassette::load_from_file(path.clone()).await?;

    for interaction in &mut cassette.interactions {
        request_mutator(&mut interaction.request);
        response_mutator(&mut interaction.response);
    }

    cassette.save_to_file().await?;
    log::debug!(
        "Applied custom mutations to {} interactions in {path:?}",
        cassette.interactions.len()
    );
    Ok(())
}

/// Helper to remove all sensitive form data from requests using smart detection
pub async fn strip_all_credentials_from_requests<P: Into<PathBuf>>(
    cassette_path: P,
) -> Result<(), Error> {
    mutate_all_requests(cassette_path, |request| {
        if let Some(body) = &mut request.body {
            // Check if this looks like form data
            if body.contains('=') && (body.contains('&') || !body.contains(' ')) {
                let filtered = crate::form_data::filter_form_data(body, "[REMOVED]");
                *body = filtered;
            }
        }
    })
    .await
}

/// Helper to remove all cookie headers from requests and set-cookie from responses
pub async fn strip_all_cookies<P: Into<PathBuf>>(cassette_path: P) -> Result<(), Error> {
    mutate_all_interactions(
        cassette_path,
        |request| {
            request.headers.remove("cookie");
            request.headers.remove("Cookie");
        },
        |response| {
            response.headers.remove("set-cookie");
            response.headers.remove("Set-Cookie");
        },
    )
    .await
}

/// Replace specific field values in all form data requests
pub async fn replace_form_field_in_all_requests<P: Into<PathBuf>>(
    cassette_path: P,
    field_name: &str,
    replacement_value: &str,
) -> Result<(), Error> {
    let field = field_name.to_string();
    let replacement = replacement_value.to_string();

    mutate_all_requests(cassette_path, move |request| {
        if let Some(body) = &mut request.body {
            if body.contains('=') && (body.contains('&') || !body.contains(' ')) {
                let mut params = crate::form_data::parse_form_data(body);
                if params.contains_key(&field) {
                    params.insert(field.clone(), replacement.clone());
                    *body = crate::form_data::encode_form_data(&params);
                }
            }
        }
    })
    .await
}

/// Remove specific header from all requests
pub async fn remove_header_from_all_requests<P: Into<PathBuf>>(
    cassette_path: P,
    header_name: &str,
) -> Result<(), Error> {
    let header = header_name.to_string();

    mutate_all_requests(cassette_path, move |request| {
        request.headers.remove(&header);
        // Also try lowercase version
        request.headers.remove(&header.to_lowercase());
    })
    .await
}

/// Replace specific header value in all requests
pub async fn replace_header_in_all_requests<P: Into<PathBuf>>(
    cassette_path: P,
    header_name: &str,
    replacement_value: &str,
) -> Result<(), Error> {
    let header = header_name.to_string();
    let replacement = replacement_value.to_string();

    mutate_all_requests(cassette_path, move |request| {
        if request.headers.contains_key(&header) {
            request
                .headers
                .insert(header.clone(), vec![replacement.clone()]);
        }
        // Also check lowercase version
        let header_lower = header.to_lowercase();
        if request.headers.contains_key(&header_lower) {
            request
                .headers
                .insert(header_lower, vec![replacement.clone()]);
        }
    })
    .await
}

/// Scrub URLs by removing or replacing query parameters
pub async fn scrub_urls_in_all_requests<P: Into<PathBuf>, F>(
    cassette_path: P,
    mut url_mutator: F,
) -> Result<(), Error>
where
    F: FnMut(&str) -> String,
{
    mutate_all_requests(cassette_path, move |request| {
        request.url = url_mutator(&request.url);
    })
    .await
}

/// Helper to replace all instances of a specific username across all requests
pub async fn replace_username_in_all_requests<P: Into<PathBuf>>(
    cassette_path: P,
    new_username: &str,
) -> Result<(), Error> {
    let replacement = new_username.to_string();

    mutate_all_requests(cassette_path, move |request| {
        // Handle form data
        if let Some(body) = &mut request.body {
            if body.contains('=') && (body.contains('&') || !body.contains(' ')) {
                let mut params = crate::form_data::parse_form_data(body);

                // Look for common username fields
                let username_fields = ["username", "user", "username_or_email", "email", "login"];
                for field in &username_fields {
                    if params.contains_key(*field) {
                        params.insert(field.to_string(), replacement.clone());
                    }
                }

                *body = crate::form_data::encode_form_data(&params);
            }
        }

        // Handle basic auth in headers
        if let Some(auth_headers) = request.headers.get_mut("authorization") {
            for auth_header in auth_headers.iter_mut() {
                if auth_header.starts_with("Basic ") {
                    // For basic auth, we'd need to decode, replace username, re-encode
                    // For now, just replace the whole thing
                    *auth_header = "[FILTERED_BASIC_AUTH]".to_string();
                }
            }
        }
    })
    .await
}

/// One-stop function to sanitize an entire cassette for sharing/testing
pub async fn sanitize_cassette_for_sharing<P: Into<PathBuf>>(
    cassette_path: P,
) -> Result<(), Error> {
    let path = cassette_path.into();

    log::debug!("🧹 Sanitizing cassette for sharing: {path:?}");

    // First analyze what we're dealing with
    let analysis = analyze_cassette_file(&path).await?;
    analysis.print_report();

    log::debug!("\n🔧 Applying sanitization...");

    // Apply comprehensive cleaning
    mutate_all_interactions(
        &path,
        |request| {
            // Clean headers
            request.headers.remove("authorization");
            request.headers.remove("Authorization");

            // Clean form data
            if let Some(body) = &mut request.body {
                if body.contains('=') && (body.contains('&') || !body.contains(' ')) {
                    *body = crate::form_data::filter_form_data(body, "[SANITIZED]");
                }
            }

            // Clean URLs of sensitive query params
            if let Ok(mut url) = url::Url::parse(&request.url) {
                let sensitive_params = ["api_key", "access_token", "key"];
                let query_pairs: Vec<(String, String)> = url
                    .query_pairs()
                    .filter(|(key, _)| !sensitive_params.contains(&key.as_ref()))
                    .map(|(k, v)| (k.to_string(), v.to_string()))
                    .collect();

                url.query_pairs_mut().clear();
                for (key, value) in query_pairs {
                    url.query_pairs_mut().append_pair(&key, &value);
                }

                request.url = url.to_string();
            }
        },
        |response| {
            // Clean response headers

            // Clean sensitive data from response bodies
            if let Some(body) = &mut response.body {
                // Simple replacements for common sensitive patterns
                *body = body.replace(r#""sessionid":"[^"]*""#, r#""sessionid":"[SANITIZED]""#);
            }
        },
    )
    .await?;

    log::debug!("✅ Cassette sanitized successfully!");
    log::debug!("🔒 All credentials, session data, and sensitive headers have been removed");

    Ok(())
}

/// Analyze a cassette file for sensitive data without modifying it
/// This helps identify what needs to be filtered
pub async fn analyze_cassette_file<P: Into<PathBuf>>(
    cassette_path: P,
) -> Result<CassetteAnalysis, Error> {
    let path = cassette_path.into();
    let cassette = Cassette::load_from_file(path.clone()).await?;

    let mut analysis = CassetteAnalysis {
        file_path: path,
        total_interactions: cassette.interactions.len(),
        requests_with_form_data: Vec::new(),
        requests_with_credentials: Vec::new(),
        sensitive_headers: Vec::new(),
    };

    for (i, interaction) in cassette.interactions.iter().enumerate() {
        // Analyze request body for form data
        if let Some(body) = &interaction.request.body {
            if body.contains('=') && (body.contains('&') || !body.contains(' ')) {
                let form_analysis = crate::form_data::analyze_form_data(body);
                if !form_analysis.credential_fields.is_empty() {
                    analysis.requests_with_form_data.push(i);
                    analysis
                        .requests_with_credentials
                        .push((i, form_analysis.credential_fields));
                }
            }
        }

        // Analyze headers for sensitive data
        for (header_name, header_values) in &interaction.request.headers {
            let header_lower = header_name.to_lowercase();
            if header_lower.contains("cookie")
                || header_lower.contains("authorization")
                || header_lower.contains("token")
            {
                analysis
                    .sensitive_headers
                    .push((i, header_name.clone(), header_values.clone()));
            }
        }

        // Also check response headers
        for (header_name, header_values) in &interaction.response.headers {
            let header_lower = header_name.to_lowercase();
            if header_lower.contains("set-cookie")
                || header_lower.contains("authorization")
                || header_lower.contains("token")
            {
                analysis.sensitive_headers.push((
                    i,
                    format!("response-{header_name}"),
                    header_values.clone(),
                ));
            }
        }
    }

    Ok(analysis)
}

/// Replace the password in all requests with a test password
/// This is useful when you want to use a known test password for replay
pub async fn set_test_password_in_cassette<P: Into<PathBuf>>(
    cassette_path: P,
    test_password: &str,
) -> Result<(), Error> {
    let path = cassette_path.into();
    let password = test_password.to_string();

    log::debug!("🔑 Setting test password in cassette: {path:?}");

    mutate_all_requests(&path, move |request| {
        if let Some(body) = &mut request.body {
            if body.contains('=') && (body.contains('&') || !body.contains(' ')) {
                let mut params = crate::form_data::parse_form_data(body);

                if params.contains_key("password") {
                    params.insert("password".to_string(), password.clone());
                    *body = crate::form_data::encode_form_data(&params);
                }
            }
        }
    })
    .await?;

    log::debug!("✅ Test password set in cassette");
    Ok(())
}

/// Get the username from a cassette (useful for test setup)
/// Returns the first username found in form data
pub async fn extract_username_from_cassette<P: Into<PathBuf>>(
    cassette_path: P,
) -> Result<Option<String>, Error> {
    let path = cassette_path.into();
    let cassette = Cassette::load_from_file(path).await?;

    for interaction in &cassette.interactions {
        if let Some(body) = &interaction.request.body {
            if body.contains('=') && (body.contains('&') || !body.contains(' ')) {
                let params = crate::form_data::parse_form_data(body);

                // Look for common username fields
                let username_fields = ["username", "username_or_email", "user", "email"];
                for field in &username_fields {
                    if let Some(username) = params.get(*field) {
                        // Skip filtered values
                        if !username.starts_with("[FILTERED") && !username.starts_with("[SANITIZED")
                        {
                            return Ok(Some(username.clone()));
                        }
                    }
                }
            }
        }
    }

    Ok(None)
}

#[derive(Debug)]
pub struct CassetteAnalysis {
    pub file_path: PathBuf,
    pub total_interactions: usize,
    pub requests_with_form_data: Vec<usize>,
    pub requests_with_credentials: Vec<(usize, Vec<(String, String)>)>,
    pub sensitive_headers: Vec<(usize, String, Vec<String>)>,
}

impl CassetteAnalysis {
    /// Print a detailed analysis report
    pub fn print_report(&self) {
        log::debug!("📊 Cassette Analysis Report");
        log::debug!("=====================================");
        log::debug!("File: {:?}", self.file_path);
        log::debug!("Total interactions: {}", self.total_interactions);
        log::debug!("");

        if !self.requests_with_form_data.is_empty() {
            log::debug!(
                "🔍 Interactions with form data: {}",
                self.requests_with_form_data.len()
            );
            for idx in &self.requests_with_form_data {
                log::debug!("  - Interaction #{idx}");
            }
            log::debug!("");
        }

        if !self.requests_with_credentials.is_empty() {
            log::debug!(
                "🔐 Interactions containing credentials: {}",
                self.requests_with_credentials.len()
            );
            for (idx, credentials) in &self.requests_with_credentials {
                log::debug!(
                    "  - Interaction #{}: {} credential fields",
                    idx,
                    credentials.len()
                );
                for (key, value) in credentials {
                    let preview = if value.len() > 20 {
                        format!("{}...", &value[..20])
                    } else {
                        value.clone()
                    };
                    log::debug!("    * {key}: {preview}");
                }
            }
            log::debug!("");
        }

        if !self.sensitive_headers.is_empty() {
            log::debug!(
                "🏷️  Interactions with sensitive headers: {}",
                self.sensitive_headers.len()
            );
            for (idx, header_name, header_values) in &self.sensitive_headers {
                log::debug!("  - Interaction #{idx}: {header_name} header");
                for value in header_values {
                    let preview = if value.len() > 50 {
                        format!("{}...", &value[..50])
                    } else {
                        value.clone()
                    };
                    log::debug!("    * {preview}");
                }
            }
            log::debug!("");
        }

        log::debug!("💡 Recommendations:");
        if !self.requests_with_credentials.is_empty() {
            log::debug!(
                "  - Use SmartFormFilter to automatically detect and filter form credentials"
            );
        }
        if !self.sensitive_headers.is_empty() {
            log::debug!("  - Use HeaderFilter to filter sensitive headers like cookies and tokens");
        }
        if self.requests_with_form_data.is_empty() && self.sensitive_headers.is_empty() {
            log::debug!("  - No obvious sensitive data detected, but consider reviewing manually");
        }
    }
}