Skip to content

Commit

Permalink
Don't print non-utf8 bodies
Browse files Browse the repository at this point in the history
A lot of our services take in multipart data or binary data in the HTTP
body and when a wiremock predicate fails megabytes of binary data get
printed out to the console. Here I address this by not printing out a
string if it isn't valid utf8 and instead printing out the body length.

Another alternative may be to put in an upper limit to how large a body
will be printed and if it exceeds that size maybe saving the request to
a file for later analysis. But this change was so simple I figured I'd
open the PR first to start the discussion.
  • Loading branch information
xd009642 committed Aug 4, 2023
1 parent 443a585 commit 1d4d604
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ impl fmt::Display for Request {
let values = values.join(",");
writeln!(f, "{}: {}", name, values)?;
}
writeln!(f, "{}", String::from_utf8_lossy(&self.body))
if let Ok(body) = String::from_utf8(self.body.clone()) {
writeln!(f, "{}", body)
} else {
writeln!(f, "Body size is {} bytes", &self.body.len())
}
}
}

Expand Down

0 comments on commit 1d4d604

Please sign in to comment.