Skip to content

Commit

Permalink
Add test for Error::HttpStatus variant formatting
Browse files Browse the repository at this point in the history
This change adds a test checking that we format the Error::HttpStatus
variant as expected.
  • Loading branch information
d-e-s-o committed Jun 5, 2023
1 parent 4e5cc22 commit 11c7c5b
Showing 1 changed file with 27 additions and 2 deletions.
29 changes: 27 additions & 2 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
Expand All @@ -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")]
Expand All @@ -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"
);
}
}

0 comments on commit 11c7c5b

Please sign in to comment.