mirror of
https://github.com/actix/actix-extras.git
synced 2024-11-28 01:32:57 +01:00
Session wide headers, basic and bearer auth
This commit is contained in:
parent
3b897da8e2
commit
ea4d98d669
@ -6,6 +6,10 @@
|
|||||||
|
|
||||||
* Re-export `actix_http::client::Connector`
|
* Re-export `actix_http::client::Connector`
|
||||||
|
|
||||||
|
* Session wide headers
|
||||||
|
|
||||||
|
* Session wide basic and bearer auth
|
||||||
|
|
||||||
|
|
||||||
## [0.1.0-alpha.1] - 2019-03-28
|
## [0.1.0-alpha.1] - 2019-03-28
|
||||||
|
|
||||||
|
@ -3,13 +3,11 @@ use std::fmt;
|
|||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|
||||||
use actix_http::client::{ConnectError, Connection, Connector};
|
use actix_http::client::{ConnectError, Connection, Connector};
|
||||||
use actix_http::http::{
|
use actix_http::http::{header, HeaderMap, HeaderName, HttpTryFrom, Uri};
|
||||||
header::IntoHeaderValue, HeaderMap, HeaderName, HttpTryFrom, Uri,
|
|
||||||
};
|
|
||||||
use actix_service::Service;
|
use actix_service::Service;
|
||||||
|
|
||||||
use crate::connect::{Connect, ConnectorWrapper};
|
use crate::connect::{Connect, ConnectorWrapper};
|
||||||
use crate::Client;
|
use crate::{Client, ClientConfig};
|
||||||
|
|
||||||
/// An HTTP Client builder
|
/// An HTTP Client builder
|
||||||
///
|
///
|
||||||
@ -77,7 +75,7 @@ impl ClientBuilder {
|
|||||||
where
|
where
|
||||||
HeaderName: HttpTryFrom<K>,
|
HeaderName: HttpTryFrom<K>,
|
||||||
<HeaderName as HttpTryFrom<K>>::Error: fmt::Debug,
|
<HeaderName as HttpTryFrom<K>>::Error: fmt::Debug,
|
||||||
V: IntoHeaderValue,
|
V: header::IntoHeaderValue,
|
||||||
V::Error: fmt::Debug,
|
V::Error: fmt::Debug,
|
||||||
{
|
{
|
||||||
match HeaderName::try_from(key) {
|
match HeaderName::try_from(key) {
|
||||||
@ -92,10 +90,85 @@ impl ClientBuilder {
|
|||||||
self
|
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.
|
/// Finish build process and create `Client` instance.
|
||||||
pub fn finish(self) -> Client {
|
pub fn finish(self) -> Client {
|
||||||
Client {
|
Client {
|
||||||
connector: self.connector,
|
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"
|
||||||
|
);
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -25,7 +25,7 @@ use std::rc::Rc;
|
|||||||
|
|
||||||
pub use actix_http::{client::Connector, http};
|
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;
|
use actix_http::RequestHead;
|
||||||
|
|
||||||
mod builder;
|
mod builder;
|
||||||
@ -68,6 +68,11 @@ use self::connect::{Connect, ConnectorWrapper};
|
|||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct Client {
|
pub struct Client {
|
||||||
pub(crate) connector: Rc<RefCell<dyn Connect>>,
|
pub(crate) connector: Rc<RefCell<dyn Connect>>,
|
||||||
|
pub(crate) config: Rc<ClientConfig>,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) struct ClientConfig {
|
||||||
|
pub(crate) headers: HeaderMap,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Client {
|
impl Default for Client {
|
||||||
@ -76,6 +81,9 @@ impl Default for Client {
|
|||||||
connector: Rc::new(RefCell::new(ConnectorWrapper(
|
connector: Rc::new(RefCell::new(ConnectorWrapper(
|
||||||
Connector::new().service(),
|
Connector::new().service(),
|
||||||
))),
|
))),
|
||||||
|
config: Rc::new(ClientConfig {
|
||||||
|
headers: HeaderMap::new(),
|
||||||
|
}),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -96,7 +104,12 @@ impl Client {
|
|||||||
where
|
where
|
||||||
Uri: HttpTryFrom<U>,
|
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`
|
/// Create `ClientRequest` from `RequestHead`
|
||||||
@ -107,13 +120,10 @@ impl Client {
|
|||||||
where
|
where
|
||||||
Uri: HttpTryFrom<U>,
|
Uri: HttpTryFrom<U>,
|
||||||
{
|
{
|
||||||
let mut req =
|
let mut req = self.request(head.method.clone(), url);
|
||||||
ClientRequest::new(head.method.clone(), url, self.connector.clone());
|
|
||||||
|
|
||||||
for (key, value) in &head.headers {
|
for (key, value) in &head.headers {
|
||||||
req.head.headers.insert(key.clone(), value.clone());
|
req.head.headers.insert(key.clone(), value.clone());
|
||||||
}
|
}
|
||||||
|
|
||||||
req
|
req
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -121,55 +131,60 @@ impl Client {
|
|||||||
where
|
where
|
||||||
Uri: HttpTryFrom<U>,
|
Uri: HttpTryFrom<U>,
|
||||||
{
|
{
|
||||||
ClientRequest::new(Method::GET, url, self.connector.clone())
|
self.request(Method::GET, url)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn head<U>(&self, url: U) -> ClientRequest
|
pub fn head<U>(&self, url: U) -> ClientRequest
|
||||||
where
|
where
|
||||||
Uri: HttpTryFrom<U>,
|
Uri: HttpTryFrom<U>,
|
||||||
{
|
{
|
||||||
ClientRequest::new(Method::HEAD, url, self.connector.clone())
|
self.request(Method::HEAD, url)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn put<U>(&self, url: U) -> ClientRequest
|
pub fn put<U>(&self, url: U) -> ClientRequest
|
||||||
where
|
where
|
||||||
Uri: HttpTryFrom<U>,
|
Uri: HttpTryFrom<U>,
|
||||||
{
|
{
|
||||||
ClientRequest::new(Method::PUT, url, self.connector.clone())
|
self.request(Method::PUT, url)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn post<U>(&self, url: U) -> ClientRequest
|
pub fn post<U>(&self, url: U) -> ClientRequest
|
||||||
where
|
where
|
||||||
Uri: HttpTryFrom<U>,
|
Uri: HttpTryFrom<U>,
|
||||||
{
|
{
|
||||||
ClientRequest::new(Method::POST, url, self.connector.clone())
|
self.request(Method::POST, url)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn patch<U>(&self, url: U) -> ClientRequest
|
pub fn patch<U>(&self, url: U) -> ClientRequest
|
||||||
where
|
where
|
||||||
Uri: HttpTryFrom<U>,
|
Uri: HttpTryFrom<U>,
|
||||||
{
|
{
|
||||||
ClientRequest::new(Method::PATCH, url, self.connector.clone())
|
self.request(Method::PATCH, url)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn delete<U>(&self, url: U) -> ClientRequest
|
pub fn delete<U>(&self, url: U) -> ClientRequest
|
||||||
where
|
where
|
||||||
Uri: HttpTryFrom<U>,
|
Uri: HttpTryFrom<U>,
|
||||||
{
|
{
|
||||||
ClientRequest::new(Method::DELETE, url, self.connector.clone())
|
self.request(Method::DELETE, url)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn options<U>(&self, url: U) -> ClientRequest
|
pub fn options<U>(&self, url: U) -> ClientRequest
|
||||||
where
|
where
|
||||||
Uri: HttpTryFrom<U>,
|
Uri: HttpTryFrom<U>,
|
||||||
{
|
{
|
||||||
ClientRequest::new(Method::OPTIONS, url, self.connector.clone())
|
self.request(Method::OPTIONS, url)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn ws<U>(&self, url: U) -> WebsocketsRequest
|
pub fn ws<U>(&self, url: U) -> WebsocketsRequest
|
||||||
where
|
where
|
||||||
Uri: HttpTryFrom<U>,
|
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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -241,10 +241,9 @@ impl ClientRequest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Set HTTP basic authorization header
|
/// 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
|
where
|
||||||
U: fmt::Display,
|
U: fmt::Display,
|
||||||
P: fmt::Display,
|
|
||||||
{
|
{
|
||||||
let auth = match password {
|
let auth = match password {
|
||||||
Some(password) => format!("{}:{}", username, password),
|
Some(password) => format!("{}:{}", username, password),
|
||||||
@ -552,3 +551,108 @@ impl fmt::Debug for ClientRequest {
|
|||||||
Ok(())
|
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"
|
||||||
|
);
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -8,6 +8,25 @@ use cookie::{Cookie, CookieJar};
|
|||||||
|
|
||||||
use crate::ClientResponse;
|
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
|
/// Test `ClientResponse` builder
|
||||||
pub struct TestResponse {
|
pub struct TestResponse {
|
||||||
head: ResponseHead,
|
head: ResponseHead,
|
||||||
|
@ -23,7 +23,7 @@ use crate::response::ClientResponse;
|
|||||||
|
|
||||||
/// `WebSocket` connection
|
/// `WebSocket` connection
|
||||||
pub struct WebsocketsRequest {
|
pub struct WebsocketsRequest {
|
||||||
head: RequestHead,
|
pub(crate) head: RequestHead,
|
||||||
err: Option<HttpError>,
|
err: Option<HttpError>,
|
||||||
origin: Option<HeaderValue>,
|
origin: Option<HeaderValue>,
|
||||||
protocols: Option<String>,
|
protocols: Option<String>,
|
||||||
|
Loading…
Reference in New Issue
Block a user