basic auth password is not Option

This commit is contained in:
Rob Ede 2021-03-07 11:32:53 +00:00
parent e2b83e3082
commit 3b642c8545
No known key found for this signature in database
GPG Key ID: 97C636207D3EF933
3 changed files with 14 additions and 12 deletions

View File

@ -8,6 +8,7 @@
### Changed
* Feature `cookies` is now optional and enabled by default. [#1981]
* `ClientBuilder::connector` method would take `actix_http::client::Connector<T, U>` type. [#2008]
* Basic auth password now takes blank passwords as an empty string instead of Option. [#2050]
### Removed
* `ClientBuilder::default` function [#2008]
@ -17,6 +18,7 @@
[#1981]: https://github.com/actix/actix-web/pull/1981
[#2008]: https://github.com/actix/actix-web/pull/2008
[#2024]: https://github.com/actix/actix-web/pull/2024
[#2050]: https://github.com/actix/actix-web/pull/2050
## 3.0.0-beta.2 - 2021-02-10
### Added

View File

@ -252,16 +252,16 @@ impl ClientRequest {
self.insert_header((header::CONTENT_LENGTH, buf.format(len)))
}
/// Set HTTP basic authorization header
/// Set HTTP basic authorization header.
///
/// If no password is needed, just provide an empty string.
pub fn basic_auth(
self,
username: impl fmt::Display,
password: Option<impl fmt::Display>,
password: impl fmt::Display,
) -> Self {
let auth = match password {
Some(password) => format!("{}:{}", username, password),
None => format!("{}:", username),
};
let auth = format!("{}:{}", username, password);
self.insert_header((
header::AUTHORIZATION,
format!("Basic {}", base64::encode(&auth)),
@ -269,8 +269,8 @@ impl ClientRequest {
}
/// Set HTTP bearer authentication header
pub fn insert_auth(self, token: impl fmt::Display) -> Self {
self.append_header((header::AUTHORIZATION, format!("Bearer {}", token)))
pub fn bearer_auth(self, token: impl fmt::Display) -> Self {
self.insert_header((header::AUTHORIZATION, format!("Bearer {}", token)))
}
/// Set a cookie
@ -644,7 +644,7 @@ mod tests {
async fn client_basic_auth() {
let req = Client::new()
.get("/")
.basic_auth("username", Some("password"));
.basic_auth("username", "password");
assert_eq!(
req.head
.headers
@ -655,7 +655,7 @@ mod tests {
"Basic dXNlcm5hbWU6cGFzc3dvcmQ="
);
let req = Client::new().get("/").basic_auth("username", None);
let req = Client::new().get("/").basic_auth("username", "");
assert_eq!(
req.head
.headers

View File

@ -829,7 +829,7 @@ async fn client_basic_auth() {
.unwrap()
.to_str()
.unwrap()
== "Basic dXNlcm5hbWU6cGFzc3dvcmQ="
== format!("Basic {}", base64::encode("username:password"))
{
HttpResponse::Ok()
} else {
@ -840,7 +840,7 @@ async fn client_basic_auth() {
});
// set authorization header to Basic <base64 encoded username:password>
let request = srv.get("/").basic_auth("username", Some("password"));
let request = srv.get("/").basic_auth("username", "password");
let response = request.send().await.unwrap();
assert!(response.status().is_success());
}