Skip to content

Commit

Permalink
Clear interactions when setting mode
Browse files Browse the repository at this point in the history
The previous commit cleared interactions every time a new interaction
was recorded. That meant that only a single interaction could ever be
saved.

Here we clear interactions once when the mode is set. This'll happen
after interactions have been loaded, since `VCRMiddleware` is
instantiated using `TryFrom<PathBuf>` which loads the old interactions.
  • Loading branch information
niclas-ahden committed Mar 14, 2024
1 parent 4f04c70 commit 7e4edcd
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ pub type VCRError = &'static str;
impl VCRMiddleware {
/// Adjust mode in the middleware and return it
pub fn with_mode(mut self, mode: VCRMode) -> Self {
if mode == VCRMode::Record {
let mut cassette = self.storage.lock().unwrap();
cassette.http_interactions.clear();
}
self.mode = mode;
self
}
Expand Down Expand Up @@ -377,7 +381,6 @@ impl VCRMiddleware {

fn record(&self, request: vcr_cassette::Request, response: vcr_cassette::Response) {
let mut cassette = self.storage.lock().unwrap();
cassette.http_interactions.clear();
cassette
.http_interactions
.push(vcr_cassette::HttpInteraction {
Expand Down Expand Up @@ -482,7 +485,7 @@ impl Drop for VCRMiddleware {
}
}

/// Load VCR cassette for filesystem
/// Load VCR cassette from filesystem
//
/// For simplicity, support JSON format only for now
impl TryFrom<PathBuf> for VCRMiddleware {
Expand Down

0 comments on commit 7e4edcd

Please sign in to comment.