Skip to content

Commit 28f24bb

Browse files
committed
Expose the default headers used by clients
This is convenient in debug and testing scenario for code using reqwest. In particular for introspecting to know what will be sent without introducing a MITM.
1 parent 6358cef commit 28f24bb

File tree

4 files changed

+38
-17
lines changed

4 files changed

+38
-17
lines changed

src/async_impl/client.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ pub struct Client {
8383
/// A `ClientBuilder` can be used to create a `Client` with custom configuration.
8484
#[must_use]
8585
pub struct ClientBuilder {
86-
config: Config,
86+
pub(crate) config: Config,
8787
}
8888

8989
enum HttpVersionPref {
@@ -150,10 +150,10 @@ impl Service<hyper::Request<crate::async_impl::body::Body>> for HyperService {
150150
}
151151
}
152152

153-
struct Config {
153+
pub(crate) struct Config {
154154
// NOTE: When adding a new field, update `fmt::Debug for ClientBuilder`
155155
accepts: Accepts,
156-
headers: HeaderMap,
156+
pub(crate) headers: HeaderMap,
157157
#[cfg(feature = "__tls")]
158158
hostname_verification: bool,
159159
#[cfg(feature = "__tls")]
@@ -2355,6 +2355,11 @@ impl Client {
23552355
ClientBuilder::new()
23562356
}
23572357

2358+
/// Get the headers this `Client` will add to all its `Request`s.
2359+
pub fn headers(&self) -> &crate::header::HeaderMap {
2360+
&self.inner.as_ref().headers
2361+
}
2362+
23582363
/// Convenience method to make a `GET` request to a URL.
23592364
///
23602365
/// # Errors

src/blocking/client.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1199,6 +1199,11 @@ impl Client {
11991199
ClientBuilder::new()
12001200
}
12011201

1202+
/// Get the headers this `Client` will add to all its `Request`s.
1203+
pub fn headers(&self) -> &crate::header::HeaderMap {
1204+
&self.inner.headers
1205+
}
1206+
12021207
/// Convenience method to make a `GET` request to a URL.
12031208
///
12041209
/// # Errors
@@ -1302,6 +1307,7 @@ impl fmt::Debug for ClientBuilder {
13021307
#[derive(Clone)]
13031308
struct ClientHandle {
13041309
timeout: Timeout,
1310+
headers: crate::header::HeaderMap,
13051311
inner: Arc<InnerClientHandle>,
13061312
}
13071313

@@ -1332,6 +1338,7 @@ impl Drop for InnerClientHandle {
13321338
impl ClientHandle {
13331339
fn new(builder: ClientBuilder) -> crate::Result<ClientHandle> {
13341340
let timeout = builder.timeout;
1341+
let headers = builder.inner.config.headers.clone();
13351342
let builder = builder.inner;
13361343
let (tx, rx) = mpsc::unbounded_channel::<(async_impl::Request, OneshotResponse)>();
13371344
let (spawn_tx, spawn_rx) = oneshot::channel::<crate::Result<()>>();
@@ -1400,6 +1407,7 @@ impl ClientHandle {
14001407

14011408
Ok(ClientHandle {
14021409
timeout,
1410+
headers,
14031411
inner: inner_handle,
14041412
})
14051413
}

tests/blocking.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,8 @@ fn test_default_headers() {
216216
let url = format!("http://{}/1", server.addr());
217217
let res = client.get(&url).send().unwrap();
218218

219+
assert_eq!(client.headers()["reqwest-test"], "orly");
220+
219221
assert_eq!(res.url().as_str(), &url);
220222
assert_eq!(res.status(), reqwest::StatusCode::OK);
221223
}
@@ -250,6 +252,8 @@ fn test_override_default_headers() {
250252
.send()
251253
.unwrap();
252254

255+
assert_eq!(client.headers()[&http::header::AUTHORIZATION], "iamatoken");
256+
253257
assert_eq!(res.url().as_str(), &url);
254258
assert_eq!(res.status(), reqwest::StatusCode::OK);
255259
}
@@ -275,6 +279,10 @@ fn test_appended_headers_not_overwritten() {
275279
.send()
276280
.unwrap();
277281

282+
let mut accepts = client.headers().get_all("accept").into_iter();
283+
assert_eq!(accepts.next().unwrap(), "*/*");
284+
assert_eq!(accepts.next(), None);
285+
278286
assert_eq!(res.url().as_str(), &url);
279287
assert_eq!(res.status(), reqwest::StatusCode::OK);
280288

@@ -298,6 +306,10 @@ fn test_appended_headers_not_overwritten() {
298306
.send()
299307
.unwrap();
300308

309+
let mut accepts = client.headers().get_all("accept").into_iter();
310+
assert_eq!(accepts.next().unwrap(), "text/html");
311+
assert_eq!(accepts.next(), None);
312+
301313
assert_eq!(res.url().as_str(), &url);
302314
assert_eq!(res.status(), reqwest::StatusCode::OK);
303315
}

tests/client.rs

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,11 @@ async fn auto_headers() {
4747
});
4848

4949
let url = format!("http://{}/1", server.addr());
50-
let res = reqwest::Client::builder()
51-
.no_proxy()
52-
.build()
53-
.unwrap()
54-
.get(&url)
55-
.send()
56-
.await
57-
.unwrap();
50+
let client = reqwest::Client::builder().no_proxy().build().unwrap();
51+
let res = client.get(&url).send().await.unwrap();
52+
53+
assert_eq!(client.headers()["accept"], "*/*");
54+
assert_eq!(client.headers().get("user-agent"), None);
5855

5956
assert_eq!(res.url().as_str(), &url);
6057
assert_eq!(res.status(), reqwest::StatusCode::OK);
@@ -93,14 +90,13 @@ async fn user_agent() {
9390
});
9491

9592
let url = format!("http://{}/ua", server.addr());
96-
let res = reqwest::Client::builder()
93+
let client = reqwest::Client::builder()
9794
.user_agent("reqwest-test-agent")
9895
.build()
99-
.expect("client builder")
100-
.get(&url)
101-
.send()
102-
.await
103-
.expect("request");
96+
.expect("client builder");
97+
let res = client.get(&url).send().await.expect("request");
98+
99+
assert_eq!(client.headers()["user-agent"], "reqwest-test-agent");
104100

105101
assert_eq!(res.status(), reqwest::StatusCode::OK);
106102
}

0 commit comments

Comments
 (0)