pub struct ResponseSnapshot {
pub status: u16,
pub headers: Vec<(String, String)>,
pub body: String,
pub body_json: Option<Value>,
}Expand description
A serializable snapshot of an HTTP response for fixture-based testing.
Snapshots capture status code, selected headers, and body content, enabling API contract verification by comparing responses against stored fixtures.
§Usage
let response = client.get("/api/users").send();
let snapshot = ResponseSnapshot::from_test_response(&response);
// First run: save the snapshot
snapshot.save("tests/snapshots/get_users.json").unwrap();
// Subsequent runs: compare against saved snapshot
let expected = ResponseSnapshot::load("tests/snapshots/get_users.json").unwrap();
assert_eq!(snapshot, expected, "{}", snapshot.diff(&expected));Fields§
§status: u16HTTP status code.
headers: Vec<(String, String)>Selected response headers (name, value) — sorted for determinism.
body: StringResponse body as a string.
body_json: Option<Value>If the body is valid JSON, the parsed value for structural comparison.
Implementations§
Source§impl ResponseSnapshot
impl ResponseSnapshot
Sourcepub fn from_test_response(resp: &TestResponse) -> Self
pub fn from_test_response(resp: &TestResponse) -> Self
Create a snapshot from a TestResponse.
Captures the status code, all response headers, and body text. If the body is valid JSON, it’s also parsed for structural comparison.
Sourcepub fn from_test_response_with_headers(
resp: &TestResponse,
header_names: &[&str],
) -> Self
pub fn from_test_response_with_headers( resp: &TestResponse, header_names: &[&str], ) -> Self
Create a snapshot with only specific headers (for ignoring dynamic headers).
Sourcepub fn mask_fields(self, paths: &[&str], placeholder: &str) -> Self
pub fn mask_fields(self, paths: &[&str], placeholder: &str) -> Self
Mask dynamic fields in the JSON body (replace with a placeholder).
This is useful for fields like timestamps, UUIDs, or auto-increment IDs that change between test runs.
paths are dot-separated JSON paths, e.g. ["id", "created_at", "items.0.id"].
Sourcepub fn diff(&self, other: &Self) -> String
pub fn diff(&self, other: &Self) -> String
Compare two snapshots and return a human-readable diff.
Sourcepub fn matches_ignoring_headers(&self, other: &Self, ignore: &[&str]) -> bool
pub fn matches_ignoring_headers(&self, other: &Self, ignore: &[&str]) -> bool
Check if two snapshots match, optionally ignoring specific headers.
Trait Implementations§
Source§impl Clone for ResponseSnapshot
impl Clone for ResponseSnapshot
Source§fn clone(&self) -> ResponseSnapshot
fn clone(&self) -> ResponseSnapshot
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more