Skip to content

Commit f3e4fd2

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 f186803 commit f3e4fd2

File tree

4 files changed

+39
-9
lines changed

4 files changed

+39
-9
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")]
@@ -2357,6 +2357,11 @@ impl Client {
23572357
ClientBuilder::new()
23582358
}
23592359

2360+
/// Get the headers this `Client` will add to all its `Request`s.
2361+
pub fn headers(&self) -> &crate::header::HeaderMap {
2362+
&self.inner.as_ref().headers
2363+
}
2364+
23602365
/// Convenience method to make a `GET` request to a URL.
23612366
///
23622367
/// # 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: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,18 @@ async fn auto_headers() {
4747
});
4848

4949
let url = format!("http://{}/1", server.addr());
50-
let res = reqwest::Client::builder()
50+
let client = reqwest::Client::builder()
5151
.no_proxy()
5252
.build()
53-
.unwrap()
54-
.get(&url)
53+
.unwrap();
54+
let res = client.get(&url)
5555
.send()
5656
.await
5757
.unwrap();
5858

59+
assert_eq!(client.headers()["accept"], "*/*");
60+
assert_eq!(client.headers().get("user-agent"), None);
61+
5962
assert_eq!(res.url().as_str(), &url);
6063
assert_eq!(res.status(), reqwest::StatusCode::OK);
6164
assert_eq!(res.remote_addr(), Some(server.addr()));
@@ -93,15 +96,17 @@ async fn user_agent() {
9396
});
9497

9598
let url = format!("http://{}/ua", server.addr());
96-
let res = reqwest::Client::builder()
99+
let client = reqwest::Client::builder()
97100
.user_agent("reqwest-test-agent")
98101
.build()
99-
.expect("client builder")
100-
.get(&url)
102+
.expect("client builder");
103+
let res = client.get(&url)
101104
.send()
102105
.await
103106
.expect("request");
104107

108+
assert_eq!(client.headers()["user-agent"], "reqwest-test-agent");
109+
105110
assert_eq!(res.status(), reqwest::StatusCode::OK);
106111
}
107112

0 commit comments

Comments
 (0)