From 11c7c5bcea09862c47d147c5da2d039d61c04f0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20M=C3=BCller?= Date: Mon, 5 Jun 2023 07:59:32 -0700 Subject: [PATCH] Add test for Error::HttpStatus variant formatting This change adds a test checking that we format the Error::HttpStatus variant as expected. --- src/error.rs | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/src/error.rs b/src/error.rs index a7d1a216..b89046fe 100644 --- a/src/error.rs +++ b/src/error.rs @@ -49,7 +49,7 @@ impl Display for HttpBody { fn fmt(&self, fmt: &mut Formatter<'_>) -> FmtResult { match from_utf8(&self.0) { Ok(s) => fmt.write_str(s)?, - Err(b) => write!(fmt, "{b:?}")?, + Err(..) => write!(fmt, "{:?}", &self.0)?, } Ok(()) } @@ -68,7 +68,7 @@ pub enum Error { ), /// We encountered an HTTP status code that either represents a /// failure or is not supported. - #[error("encountered an unexpected HTTP status: {0}")] + #[error("encountered an unexpected HTTP status: {0}: {1}")] HttpStatus(HttpStatusCode, #[source] HttpBody), /// A JSON conversion error. #[error("a JSON conversion failed")] @@ -95,3 +95,28 @@ pub enum Error { WebSocketError, ), } + + +#[cfg(test)] +mod tests { + use super::*; + + + /// Check that we can serialize a [`Side`] object. + #[test] + fn formatting() { + let body = HttpBody(vec![0, 159, 146, 150]); + let err = Error::HttpStatus(HttpStatusCode::NOT_FOUND, body); + assert_eq!( + format!("{err}"), + "encountered an unexpected HTTP status: 404 Not Found: [0, 159, 146, 150]" + ); + + let body = HttpBody("invalid".to_string().as_bytes().to_vec()); + let err = Error::HttpStatus(HttpStatusCode::NOT_FOUND, body); + assert_eq!( + format!("{err}"), + "encountered an unexpected HTTP status: 404 Not Found: invalid" + ); + } +}