Skip to content
This repository has been archived by the owner on Dec 14, 2021. It is now read-only.

Commit

Permalink
allow for custom header auth
Browse files Browse the repository at this point in the history
  • Loading branch information
SebRollen committed Nov 4, 2021
1 parent 9f90060 commit c61f940
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::error::{Error, Result};
use crate::pagination::{PaginatedRequest, PaginationState, PaginationType};
use crate::request::{Request, RequestBuilderExt};
use futures::prelude::*;
use reqwest::header::{HeaderMap, HeaderValue};
use reqwest::Client as ReqwestClient;
use std::borrow::Cow;
use std::sync::Arc;
Expand All @@ -11,6 +12,7 @@ enum Authentication<'a> {
Bearer(Cow<'a, str>),
Basic(Cow<'a, str>, Cow<'a, str>),
Query(Vec<(Cow<'a, str>, Cow<'a, str>)>),
Header(HeaderMap<HeaderValue>),
}

/// The main client used for making requests.
Expand Down Expand Up @@ -58,6 +60,19 @@ impl<'a> Client<'a> {
self
}

/// Enable custom header authentication for the client
pub fn header_auth(mut self, pairs: Vec<(&'static str, &'static str)>) -> Self {
let mut map = HeaderMap::new();
for (k, v) in pairs {
map.insert(
k,
HeaderValue::from_str(v).expect("Unable to parse header value"),
);
}
self.auth = Some(Authentication::Header(map));
self
}

fn format_request<R: Request>(&'a self, request: &R) -> Result<reqwest::Request> {
let endpoint = request.endpoint();
let endpoint = endpoint.trim_matches('/');
Expand All @@ -74,6 +89,7 @@ impl<'a> Client<'a> {
Some(Authentication::Bearer(token)) => req.bearer_auth(token),
Some(Authentication::Basic(user, pass)) => req.basic_auth(user, Some(pass)),
Some(Authentication::Query(pairs)) => req.query(&pairs),
Some(Authentication::Header(pairs)) => req.headers(pairs.clone()),
};
req.build().map_err(From::from)
}
Expand Down
33 changes: 33 additions & 0 deletions tests/integration/authentication/header.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use rest_client::{Client, EmptyResponse, Request};
use std::borrow::Cow;
use wiremock::matchers::{header, method, path};
use wiremock::{Mock, MockServer, ResponseTemplate};

struct EmptyHello;

impl Request for EmptyHello {
type Body = ();
type Response = EmptyResponse;

fn endpoint(&self) -> Cow<str> {
"/hello".into()
}
}

#[tokio::test]
async fn header_auth() {
let server = MockServer::start().await;
let uri = server.uri();
let auth = vec![("key", "k"), ("secret", "s")];
let client = Client::new(&uri).header_auth(auth);

Mock::given(method("GET"))
.and(path("/hello"))
.and(header("key", "k"))
.and(header("secret", "s"))
.respond_with(ResponseTemplate::new(200))
.mount(&server)
.await;

client.send(&EmptyHello).await.unwrap();
}
1 change: 1 addition & 0 deletions tests/integration/authentication/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod basic;
mod bearer;
mod header;
mod query;

0 comments on commit c61f940

Please sign in to comment.