Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,10 @@ impl Agent {
body: SendBody,
) -> Result<Response<Body>, Error> {
let (parts, _) = request.into_parts();
let request = http::Request::from_parts(parts, body);
let mut request = http::Request::from_parts(parts, body);

// Clone here is cheap, since it's all Arcs.
request.extensions_mut().insert(AgentInstance(self.clone()));

let next = MiddlewareNext::new(self);
next.handle(request)
Expand Down Expand Up @@ -360,6 +363,9 @@ impl Agent {
}
}

#[derive(Clone)]
pub(crate) struct AgentInstance(pub(crate) Agent);

impl From<Config> for Agent {
fn from(value: Config) -> Self {
Agent::new_with_config(value)
Expand Down
60 changes: 60 additions & 0 deletions src/request_ext.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::agent::AgentInstance;
use crate::config::typestate::RequestExtScope;
use crate::config::{Config, ConfigBuilder, RequestLevelConfig};
use crate::typestate::HttpCrateScope;
use crate::{http, Agent, AsSendBody, Body, Error};
use std::ops::Deref;
use ureq_proto::http::{Request, Response};
Expand Down Expand Up @@ -90,6 +92,16 @@ where
/// .run();
/// ```
fn with_agent<'a>(self, agent: impl Into<AgentRef<'a>>) -> WithAgent<'a, S>;

/// Returns a [`ConfigBuilder`] for configuring the request.
///
/// This is only to be used from within [`Middleware`](crate::middleware::Middleware).
///
/// Returns `None` if not called from within a [`Middleware`](crate::middleware::Middleware).
///
/// Any usage beyond from inside a middleware, synchronously with handling the request, is
/// incorrect usage and might break without notice.
fn middleware_config(self) -> Option<ConfigBuilder<HttpCrateScope<S>>>;
}

/// Wrapper struct that holds a [`Request`] associated with an [`Agent`].
Expand Down Expand Up @@ -149,6 +161,11 @@ impl<S: AsSendBody> RequestExt<S> for http::Request<S> {
request: self,
}
}

fn middleware_config(self) -> Option<ConfigBuilder<HttpCrateScope<S>>> {
let instance = self.extensions().get::<AgentInstance>()?.clone();
Some(instance.0.configure_request(self))
}
}

impl From<Agent> for AgentRef<'static> {
Expand Down Expand Up @@ -178,6 +195,8 @@ impl Deref for AgentRef<'_> {
mod tests {
use super::*;
use crate::config::RequestLevelConfig;
use crate::middleware::MiddlewareNext;
use crate::SendBody;
use std::time::Duration;

#[test]
Expand Down Expand Up @@ -315,4 +334,45 @@ mod tests {
Some(Duration::from_secs(60))
);
}

#[test]
fn middleware_config_none() {
let request = http::Request::builder()
.method(http::Method::GET)
.uri("http://foo.bar")
.body(())
.unwrap();

assert!(request.middleware_config().is_none());
}

#[test]
fn middleware_config() {
fn my_middleware(
req: Request<SendBody>,
next: MiddlewareNext,
) -> Result<Response<Body>, crate::Error> {
let config = req.middleware_config().unwrap();
let req = config.build();
let mut ret = next.handle(req)?;
ret.extensions_mut().insert("worked".to_string());
Ok(ret)
}

let agent = Agent::config_builder()
.middleware(my_middleware)
.build()
.new_agent();

let request = http::Request::builder()
.method(http::Method::GET)
.uri("http://httpbin.org/get")
.body(())
.unwrap();

let request = request.with_agent(&agent);

let ret = request.run().unwrap();
assert_eq!(ret.extensions().get::<String>().unwrap(), "worked");
}
}