From e75b13a52c4af5a81ef27efdb9e10401dfc98909 Mon Sep 17 00:00:00 2001 From: xd009642 Date: Fri, 4 Aug 2023 12:16:05 +0100 Subject: [PATCH] Don't print non-utf8 bodies 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. --- src/request.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/request.rs b/src/request.rs index cb2e163..f4b84da 100644 --- a/src/request.rs +++ b/src/request.rs @@ -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) { + writeln!(f, "{}", body) + } else { + writeln!(f, "Body size is {} bytes", self.body.len()) + } } }