1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-06-11 09:49:29 +00:00

Session wide headers, basic and bearer auth

This commit is contained in:
Nikolay Kim 2019-03-28 21:48:35 -07:00
parent 3b897da8e2
commit ea4d98d669
6 changed files with 237 additions and 22 deletions

View file

@ -6,6 +6,10 @@
* Re-export `actix_http::client::Connector`
* Session wide headers
* Session wide basic and bearer auth
## [0.1.0-alpha.1] - 2019-03-28

View file

@ -3,13 +3,11 @@ use std::fmt;
use std::rc::Rc;
use actix_http::client::{ConnectError, Connection, Connector};
use actix_http::http::{
header::IntoHeaderValue, HeaderMap, HeaderName, HttpTryFrom, Uri,
};
use actix_http::http::{header, HeaderMap, HeaderName, HttpTryFrom, Uri};
use actix_service::Service;
use crate::connect::{Connect, ConnectorWrapper};
use crate::Client;
use crate::{Client, ClientConfig};
/// An HTTP Client builder
///
@ -77,7 +75,7 @@ impl ClientBuilder {
where
HeaderName: HttpTryFrom<K>,
<HeaderName as HttpTryFrom<K>>::Error: fmt::Debug,
V: IntoHeaderValue,
V: header::IntoHeaderValue,
V::Error: fmt::Debug,
{
match HeaderName::try_from(key) {
@ -92,10 +90,85 @@ impl ClientBuilder {
self
}
/// Set client wide HTTP basic authorization header
pub fn basic_auth<U>(self, username: U, password: Option<&str>) -> Self
where
U: fmt::Display,
{
let auth = match password {
Some(password) => format!("{}:{}", username, password),
None => format!("{}", username),
};
self.header(
header::AUTHORIZATION,
format!("Basic {}", base64::encode(&auth)),
)
}
/// Set client wide HTTP bearer authentication header
pub fn bearer_auth<T>(self, token: T) -> Self
where
T: fmt::Display,
{
self.header(header::AUTHORIZATION, format!("Bearer {}", token))
}
/// Finish build process and create `Client` instance.
pub fn finish(self) -> Client {
Client {
connector: self.connector,
config: Rc::new(ClientConfig {
headers: self.headers,
}),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::test;
#[test]
fn client_basic_auth() {
test::run_on(|| {
let client = ClientBuilder::new().basic_auth("username", Some("password"));
assert_eq!(
client
.headers
.get(header::AUTHORIZATION)
.unwrap()
.to_str()
.unwrap(),
"Basic dXNlcm5hbWU6cGFzc3dvcmQ="
);
let client = ClientBuilder::new().basic_auth("username", None);
assert_eq!(
client
.headers
.get(header::AUTHORIZATION)
.unwrap()
.to_str()
.unwrap(),
"Basic dXNlcm5hbWU="
);
});
}
#[test]
fn client_bearer_auth() {
test::run_on(|| {
let client = ClientBuilder::new().bearer_auth("someS3cr3tAutht0k3n");
assert_eq!(
client
.headers
.get(header::AUTHORIZATION)
.unwrap()
.to_str()
.unwrap(),
"Bearer someS3cr3tAutht0k3n"
);
})
}
}

View file

@ -25,7 +25,7 @@ use std::rc::Rc;
pub use actix_http::{client::Connector, http};
use actix_http::http::{HttpTryFrom, Method, Uri};
use actix_http::http::{HeaderMap, HttpTryFrom, Method, Uri};
use actix_http::RequestHead;
mod builder;
@ -68,6 +68,11 @@ use self::connect::{Connect, ConnectorWrapper};
#[derive(Clone)]
pub struct Client {
pub(crate) connector: Rc<RefCell<dyn Connect>>,
pub(crate) config: Rc<ClientConfig>,
}
pub(crate) struct ClientConfig {
pub(crate) headers: HeaderMap,
}
impl Default for Client {
@ -76,6 +81,9 @@ impl Default for Client {
connector: Rc::new(RefCell::new(ConnectorWrapper(
Connector::new().service(),
))),
config: Rc::new(ClientConfig {
headers: HeaderMap::new(),
}),
}
}
}
@ -96,7 +104,12 @@ impl Client {
where
Uri: HttpTryFrom<U>,
{
ClientRequest::new(method, url, self.connector.clone())
let mut req = ClientRequest::new(method, url, self.connector.clone());
for (key, value) in &self.config.headers {
req.head.headers.insert(key.clone(), value.clone());
}
req
}
/// Create `ClientRequest` from `RequestHead`
@ -107,13 +120,10 @@ impl Client {
where
Uri: HttpTryFrom<U>,
{
let mut req =
ClientRequest::new(head.method.clone(), url, self.connector.clone());
let mut req = self.request(head.method.clone(), url);
for (key, value) in &head.headers {
req.head.headers.insert(key.clone(), value.clone());
}
req
}
@ -121,55 +131,60 @@ impl Client {
where
Uri: HttpTryFrom<U>,
{
ClientRequest::new(Method::GET, url, self.connector.clone())
self.request(Method::GET, url)
}
pub fn head<U>(&self, url: U) -> ClientRequest
where
Uri: HttpTryFrom<U>,
{
ClientRequest::new(Method::HEAD, url, self.connector.clone())
self.request(Method::HEAD, url)
}
pub fn put<U>(&self, url: U) -> ClientRequest
where
Uri: HttpTryFrom<U>,
{
ClientRequest::new(Method::PUT, url, self.connector.clone())
self.request(Method::PUT, url)
}
pub fn post<U>(&self, url: U) -> ClientRequest
where
Uri: HttpTryFrom<U>,
{
ClientRequest::new(Method::POST, url, self.connector.clone())
self.request(Method::POST, url)
}
pub fn patch<U>(&self, url: U) -> ClientRequest
where
Uri: HttpTryFrom<U>,
{
ClientRequest::new(Method::PATCH, url, self.connector.clone())
self.request(Method::PATCH, url)
}
pub fn delete<U>(&self, url: U) -> ClientRequest
where
Uri: HttpTryFrom<U>,
{
ClientRequest::new(Method::DELETE, url, self.connector.clone())
self.request(Method::DELETE, url)
}
pub fn options<U>(&self, url: U) -> ClientRequest
where
Uri: HttpTryFrom<U>,
{
ClientRequest::new(Method::OPTIONS, url, self.connector.clone())
self.request(Method::OPTIONS, url)
}
pub fn ws<U>(&self, url: U) -> WebsocketsRequest
where
Uri: HttpTryFrom<U>,
{
WebsocketsRequest::new(url, self.connector.clone())
let mut req = WebsocketsRequest::new(url, self.connector.clone());
for (key, value) in &self.config.headers {
req.head.headers.insert(key.clone(), value.clone());
}
req
}
}

View file

@ -241,10 +241,9 @@ impl ClientRequest {
}
/// Set HTTP basic authorization header
pub fn basic_auth<U, P>(self, username: U, password: Option<P>) -> Self
pub fn basic_auth<U>(self, username: U, password: Option<&str>) -> Self
where
U: fmt::Display,
P: fmt::Display,
{
let auth = match password {
Some(password) => format!("{}:{}", username, password),
@ -552,3 +551,108 @@ impl fmt::Debug for ClientRequest {
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{test, Client};
#[test]
fn test_debug() {
test::run_on(|| {
let request = Client::new().get("/").header("x-test", "111");
let repr = format!("{:?}", request);
assert!(repr.contains("ClientRequest"));
assert!(repr.contains("x-test"));
})
}
#[test]
fn test_client_header() {
test::run_on(|| {
let req = Client::build()
.header(header::CONTENT_TYPE, "111")
.finish()
.get("/");
assert_eq!(
req.head
.headers
.get(header::CONTENT_TYPE)
.unwrap()
.to_str()
.unwrap(),
"111"
);
})
}
#[test]
fn test_client_header_override() {
test::run_on(|| {
let req = Client::build()
.header(header::CONTENT_TYPE, "111")
.finish()
.get("/")
.set_header(header::CONTENT_TYPE, "222");
assert_eq!(
req.head
.headers
.get(header::CONTENT_TYPE)
.unwrap()
.to_str()
.unwrap(),
"222"
);
})
}
#[test]
fn client_basic_auth() {
test::run_on(|| {
let client = Client::new()
.get("/")
.basic_auth("username", Some("password"));
assert_eq!(
client
.head
.headers
.get(header::AUTHORIZATION)
.unwrap()
.to_str()
.unwrap(),
"Basic dXNlcm5hbWU6cGFzc3dvcmQ="
);
let client = Client::new().get("/").basic_auth("username", None);
assert_eq!(
client
.head
.headers
.get(header::AUTHORIZATION)
.unwrap()
.to_str()
.unwrap(),
"Basic dXNlcm5hbWU="
);
});
}
#[test]
fn client_bearer_auth() {
test::run_on(|| {
let client = Client::new().get("/").bearer_auth("someS3cr3tAutht0k3n");
assert_eq!(
client
.head
.headers
.get(header::AUTHORIZATION)
.unwrap()
.to_str()
.unwrap(),
"Bearer someS3cr3tAutht0k3n"
);
})
}
}

View file

@ -8,6 +8,25 @@ use cookie::{Cookie, CookieJar};
use crate::ClientResponse;
#[cfg(test)]
thread_local! {
static RT: std::cell::RefCell<actix_rt::Runtime> = {
std::cell::RefCell::new(actix_rt::Runtime::new().unwrap())
};
}
#[cfg(test)]
pub fn run_on<F, R>(f: F) -> R
where
F: Fn() -> R,
{
RT.with(move |rt| {
rt.borrow_mut()
.block_on(futures::future::lazy(|| Ok::<_, ()>(f())))
})
.unwrap()
}
/// Test `ClientResponse` builder
pub struct TestResponse {
head: ResponseHead,

View file

@ -23,7 +23,7 @@ use crate::response::ClientResponse;
/// `WebSocket` connection
pub struct WebsocketsRequest {
head: RequestHead,
pub(crate) head: RequestHead,
err: Option<HttpError>,
origin: Option<HeaderValue>,
protocols: Option<String>,