Skip to content

Commit

Permalink
Use new quality-of-life improvements from vcr-cassette
Browse files Browse the repository at this point in the history
This exposes the ability to provide "matchers" for request bodies, and
specify your JSON response bodies as JSON, new features recently added
to `vcr-cassette`.
  • Loading branch information
mpalmer committed Jun 7, 2024
1 parent 6733d07 commit ebeb688
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 26 deletions.
5 changes: 3 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ name = "rvcr"
[features]
default = []
compress = ["dep:zip"]

json = ["vcr-cassette/json"]
matching = ["vcr-cassette/matching"]
regex = ["vcr-cassette/regex"]

[dependencies]
anyhow = "^1"
Expand All @@ -46,3 +48,5 @@ tracing-subscriber = { version = "0.3", features = ["registry", "env-filter"] }
tracing-test = { version = "0.2.4", features = ["no-env-filter"] }
url = "2.4"

[patch.crates-io]
vcr-cassette = { git = "https://github.com/mpalmer/vcr-cassette.git", branch = "full-body-workout" }
49 changes: 26 additions & 23 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use std::{fs, path::PathBuf, str::FromStr, sync::Mutex};

use base64::{engine::general_purpose, Engine};
use reqwest_middleware::Middleware;
use vcr_cassette::{HttpInteraction, RecorderId};
use vcr_cassette::{Body, HttpInteraction, RecorderId};

pub const VERSION: &str = env!("CARGO_PKG_VERSION");

Expand Down Expand Up @@ -166,7 +166,7 @@ impl VCRMiddleware {
Err(e) => {
tracing::debug!("Can not deserialize utf-8 string: {e:?}");
let base64_str = general_purpose::STANDARD_NO_PAD.encode(body_bytes);
vcr_cassette::Body {
vcr_cassette::Body::EncodedString {
string: base64_str,
encoding: Some(BASE64.to_string()),
}
Expand Down Expand Up @@ -323,9 +323,9 @@ impl VCRMiddleware {
diff.push_str(" Body differs:\n");
diff.push_str(&format!(
" recorded: \"{}\"\n",
interaction.request.body.string
interaction.request.body
));
diff.push_str(&format!(" got: \"{}\"\n", req.body.string));
diff.push_str(&format!(" got: \"{}\"\n", req.body));
}
diff.push('\n');
}
Expand Down Expand Up @@ -353,25 +353,28 @@ impl VCRMiddleware {
);
let builder = builder.version(http_version);

match response.body.encoding {
None => {
if !response.body.string.is_empty() {
reqwest::Response::from(builder.body(response.body.string).unwrap())
} else {
reqwest::Response::from(builder.body("".as_bytes()).unwrap())
}
}
Some(encoding) => {
if encoding == "base64" {
let decoded = general_purpose::STANDARD_NO_PAD
.decode(encoding)
.expect("Invalid response body base64 can not be decoded");
reqwest::Response::from(builder.body(decoded).unwrap())
} else {
// FIXME: support more encodings
panic!("Unsupported encoding: {encoding}");
}
}
match response.body {
Body::String(s) => reqwest::Response::from(builder.body(s.clone()).unwrap()),
Body::EncodedString { encoding, string } => match encoding.as_deref() {
Some("base64") => reqwest::Response::from(
builder
.body(
general_purpose::STANDARD_NO_PAD
.decode(string)
.expect("invalid response body, base64 decoding failed"),
)
.unwrap(),
),
Some(enc) => panic!("Unsupported encoding: {enc}"),
None => reqwest::Response::from(builder.body(string.clone()).unwrap()),
},
#[cfg(feature = "json")]
Body::Json(j) => reqwest::Response::from(
builder
.body(serde_json::to_string(&j).expect("invalid JSON response body"))
.unwrap(),
),
b => panic!("Unsupported response body specification: {b:?}"),
}
}

Expand Down

0 comments on commit ebeb688

Please sign in to comment.