feat: Add optional Request::basic_auth
utility function
This function adds a dependency on `base64` and is thus gated behind the (enabled by default) `basic_auth` feature.
This commit is contained in:
parent
bd726c7d4c
commit
de16d9698d
3 changed files with 29 additions and 1 deletions
|
@ -4,10 +4,12 @@ version = "0.1.0"
|
||||||
authors = ["Vincent Ambo <mail@tazj.in>"]
|
authors = ["Vincent Ambo <mail@tazj.in>"]
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
default = [ "json" ]
|
default = [ "json", "basic_auth" ]
|
||||||
json = [ "serde", "serde_json"]
|
json = [ "serde", "serde_json"]
|
||||||
|
basic_auth = [ "base64" ]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
curl = "0.4"
|
curl = "0.4"
|
||||||
serde = { version = "1.0", optional = true }
|
serde = { version = "1.0", optional = true }
|
||||||
serde_json = { version = "1.0", optional = true }
|
serde_json = { version = "1.0", optional = true }
|
||||||
|
base64 = { version = "0.10", optional = true }
|
||||||
|
|
10
src/lib.rs
10
src/lib.rs
|
@ -42,6 +42,7 @@ extern crate curl;
|
||||||
|
|
||||||
#[cfg(feature = "json")] extern crate serde;
|
#[cfg(feature = "json")] extern crate serde;
|
||||||
#[cfg(feature = "json")] extern crate serde_json;
|
#[cfg(feature = "json")] extern crate serde_json;
|
||||||
|
#[cfg(feature = "basic_auth")] extern crate base64;
|
||||||
|
|
||||||
use curl::easy::{Easy, Form, List, ReadError};
|
use curl::easy::{Easy, Form, List, ReadError};
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
@ -136,6 +137,15 @@ impl <'a> Request<'a> {
|
||||||
Ok(self)
|
Ok(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "basic_auth")]
|
||||||
|
/// Set the `Authorization` header to a basic authentication value
|
||||||
|
/// from the supplied username and password.
|
||||||
|
pub fn basic_auth(mut self, username: &str, password: &str) -> Result<Self, curl::Error> {
|
||||||
|
let auth = base64::encode(format!("{}:{}", username, password).as_bytes());
|
||||||
|
self.headers.append(&format!("Authorization: Basic {}", auth))?;
|
||||||
|
Ok(self)
|
||||||
|
}
|
||||||
|
|
||||||
/// Add a byte-array body to a request using the specified
|
/// Add a byte-array body to a request using the specified
|
||||||
/// `Content-Type`.
|
/// `Content-Type`.
|
||||||
pub fn body(mut self, content_type: &'a str, data: &'a [u8]) -> Self {
|
pub fn body(mut self, content_type: &'a str, data: &'a [u8]) -> Self {
|
||||||
|
|
16
src/tests.rs
16
src/tests.rs
|
@ -88,3 +88,19 @@ fn test_http_post_json() {
|
||||||
"Content-Type should be `application/json`",
|
"Content-Type should be `application/json`",
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Tests for different authentication methods that are supported
|
||||||
|
// out-of-the-box:
|
||||||
|
|
||||||
|
#[cfg(feature = "basic_auth")] #[test]
|
||||||
|
fn test_basic_auth() {
|
||||||
|
let request = Request::new(
|
||||||
|
Method::Get, "https://httpbin.org/basic-auth/alan_watts/oneness"
|
||||||
|
);
|
||||||
|
|
||||||
|
let response = request
|
||||||
|
.basic_auth("alan_watts", "oneness").expect("failed to set auth header")
|
||||||
|
.send().expect("failed to send request");
|
||||||
|
|
||||||
|
assert!(response.is_success(), "authorized request should succeed");
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue