From 3c806390e6efa8d1938cfbe7494940e1a0ed83b3 Mon Sep 17 00:00:00 2001 From: Mike Cronce Date: Mon, 23 May 2022 23:48:38 -0400 Subject: [PATCH] actix-web-httpauth/src/headers/www_authenticate/challenge/mod.rs: impl Challenge for &str --- actix-web-httpauth/CHANGES.md | 1 + actix-web-httpauth/Cargo.toml | 2 ++ .../headers/www_authenticate/challenge/mod.rs | 24 +++++++++++++++++++ 3 files changed, 27 insertions(+) diff --git a/actix-web-httpauth/CHANGES.md b/actix-web-httpauth/CHANGES.md index e4c280f260..2d10c0a5ba 100644 --- a/actix-web-httpauth/CHANGES.md +++ b/actix-web-httpauth/CHANGES.md @@ -1,6 +1,7 @@ # Changes ## Unreleased - 2021-xx-xx +- impl Challenge for &str ## 0.6.0 - 2022-03-01 diff --git a/actix-web-httpauth/Cargo.toml b/actix-web-httpauth/Cargo.toml index cebb395bf2..0967691f1f 100644 --- a/actix-web-httpauth/Cargo.toml +++ b/actix-web-httpauth/Cargo.toml @@ -30,3 +30,5 @@ pin-project-lite = "0.2.7" [dev-dependencies] actix-cors = "0.6.0" actix-web = { version = "4", default_features = false, features = ["macros"] } +proptest = "1.0.0" +test-strategy = "0.2.0" diff --git a/actix-web-httpauth/src/headers/www_authenticate/challenge/mod.rs b/actix-web-httpauth/src/headers/www_authenticate/challenge/mod.rs index 15f87c379a..1beade69bb 100644 --- a/actix-web-httpauth/src/headers/www_authenticate/challenge/mod.rs +++ b/actix-web-httpauth/src/headers/www_authenticate/challenge/mod.rs @@ -10,3 +10,27 @@ pub trait Challenge: TryIntoHeaderValue + Debug + Display + Clone + Send + Sync /// Converts the challenge into a bytes suitable for HTTP transmission. fn to_bytes(&self) -> Bytes; } + +/// This is particularly useful for writing constructs such as +/// `AuthenticationError::new("Authentication required")` +impl Challenge for &'static str { + fn to_bytes(&self) -> Bytes { + (*self).into() + } +} + +#[cfg(test)] +mod tests { + use test_strategy::proptest; + + use super::*; + + #[proptest] + fn roundtrip_static_str(input: Box) { + // This will leak, but it's probably fine in the context of a test. Fixable by adding: + // unsafe { Box::from_raw(s as *const str as *mut str); } + let s: &'static str = Box::leak(input); + let bytes = s.to_bytes(); + assert_eq!(s.as_bytes(), bytes); + } +}