diff options
author | Vika <vika@fireburn.ru> | 2022-02-21 21:46:25 +0300 |
---|---|---|
committer | Vika <vika@fireburn.ru> | 2022-02-21 22:17:30 +0300 |
commit | a6b0c696bb28ab991b5cd71369f868c49773abd2 (patch) | |
tree | 72e8f63d735770d2203e42096fbd968e3412aa09 | |
parent | eb6d5015eec34a1a65fb2d4d54b5201d4aef2728 (diff) | |
download | kittybox-a6b0c696bb28ab991b5cd71369f868c49773abd2.tar.zst |
Make the HTTP client a generic
This adds the ability to use mocks that don't actually touch the network and alternative transports such as using OpenSSL instead of rustls (but rustls is still superior).
-rw-r--r-- | src/indieauth.rs | 12 |
1 files changed, 5 insertions, 7 deletions
diff --git a/src/indieauth.rs b/src/indieauth.rs index 8f3ef8f..bcc8bbe 100644 --- a/src/indieauth.rs +++ b/src/indieauth.rs @@ -90,10 +90,8 @@ impl User { } } -// TODO: consider making this a generic -type HttpClient = hyper::Client<hyper_rustls::HttpsConnector<hyper::client::HttpConnector<hyper::client::connect::dns::GaiResolver>>, hyper::Body>; - -pub fn require_token(token_endpoint: String, http: HttpClient) -> impl Filter<Extract = (User,), Error = Rejection> { +pub fn require_token<T>(token_endpoint: String, http: hyper::Client<T, hyper::Body>) -> impl Filter<Extract = (User,), Error = Rejection> + Clone +where T: hyper::client::connect::Connect + Clone + Send + Sync + 'static { // It might be OK to panic here, because we're still inside the initialisation sequence for now. // Proper error handling on the top of this should be used though. let token_endpoint_uri = hyper::Uri::try_from(&token_endpoint) @@ -101,7 +99,6 @@ pub fn require_token(token_endpoint: String, http: HttpClient) -> impl Filter<Ex warp::any() .map(move || token_endpoint_uri.clone()) .and(warp::any().map(move || http.clone())) - .and_then(|token_endpoint, http: HttpClient, token| async move { .and(warp::header::<String>("Authorization").recover(|err: Rejection| async move { if err.find::<MissingHeader>().is_some() { Err(IndieAuthError { @@ -113,6 +110,7 @@ pub fn require_token(token_endpoint: String, http: HttpClient) -> impl Filter<Ex Err(err) } }).unify()) + .and_then(|token_endpoint, http: hyper::Client<T, hyper::Body>, token| async move { let request = hyper::Request::builder() .method(hyper::Method::GET) .uri(token_endpoint) @@ -197,7 +195,7 @@ pub fn require_token(token_endpoint: String, http: HttpClient) -> impl Filter<Ex #[cfg(test)] mod tests { - use super::{HttpClient, User, IndieAuthError, require_token}; + use super::{User, IndieAuthError, require_token}; use httpmock::prelude::*; #[test] @@ -212,7 +210,7 @@ mod tests { assert!(!user.check_scope("delete")); } - fn get_http_client() -> HttpClient { + fn get_http_client() -> hyper::Client<impl hyper::client::connect::Connect + Clone + Send + Sync + 'static, hyper::Body> { let builder = hyper::Client::builder(); let https = hyper_rustls::HttpsConnectorBuilder::new() .with_webpki_roots() |