diff --git a/actix-web-httpauth/CHANGES.md b/actix-web-httpauth/CHANGES.md index 74e300883..bdc639fdf 100644 --- a/actix-web-httpauth/CHANGES.md +++ b/actix-web-httpauth/CHANGES.md @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [unreleased] - Update the `base64` dependency to 0.12 + - AuthenticationError's status code is preserved when converting to a ResponseError ## [0.4.1] - 2020-02-19 - Move repository to actix-extras diff --git a/actix-web-httpauth/src/extractors/errors.rs b/actix-web-httpauth/src/extractors/errors.rs index c136d6617..94233a1c5 100644 --- a/actix-web-httpauth/src/extractors/errors.rs +++ b/actix-web-httpauth/src/extractors/errors.rs @@ -57,4 +57,26 @@ impl ResponseError for AuthenticationError { .set(WwwAuthenticate(self.challenge.clone())) .finish() } + + fn status_code(&self) -> StatusCode { + self.status_code + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::headers::www_authenticate::basic::Basic; + use actix_web::Error; + + #[test] + fn test_status_code_is_preserved_across_error_conversions() { + let ae: AuthenticationError = AuthenticationError::new(Basic::default()); + let expected = ae.status_code; + + // Converting the AuthenticationError into a ResponseError should preserve the status code. + let e = Error::from(ae); + let re = e.as_response_error(); + assert_eq!(expected, re.status_code()); + } }