Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

impl Challenge for &'static str #248

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions actix-web-httpauth/CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Changes

## Unreleased - 2021-xx-xx
- impl Challenge for &str
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- impl Challenge for &str
- impl `Challenge` for `&str`



## 0.6.0 - 2022-03-01
Expand Down
2 changes: 2 additions & 0 deletions actix-web-httpauth/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
24 changes: 24 additions & 0 deletions actix-web-httpauth/src/headers/www_authenticate/challenge/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<str>) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if that conversion really needs to use such expensive testing, isn't it enough to add a basic test? We should be cautious to add an additional test framework just for one test.

// 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); }
Copy link
Member

@ibraheemdev ibraheemdev May 24, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unleaking is UB, especially through a shared reference. This should use Box::into_raw and Box::from_raw if memory is to be cleaned up.

let s: &'static str = Box::leak(input);
let bytes = s.to_bytes();
assert_eq!(s.as_bytes(), bytes);
}
}