Skip to content

Commit 759078a

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 11ac468 commit 759078a

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
@@ -82,7 +82,7 @@ pub struct Client {
8282
/// A `ClientBuilder` can be used to create a `Client` with custom configuration.
8383
#[must_use]
8484
pub struct ClientBuilder {
85-
config: Config,
85+
pub(crate) config: Config,
8686
}
8787

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

152-
struct Config {
152+
pub(crate) struct Config {
153153
// NOTE: When adding a new field, update `fmt::Debug for ClientBuilder`
154154
accepts: Accepts,
155-
headers: HeaderMap,
155+
pub(crate) headers: HeaderMap,
156156
#[cfg(feature = "__tls")]
157157
hostname_verification: bool,
158158
#[cfg(feature = "__tls")]
@@ -2416,6 +2416,11 @@ impl Client {
24162416
ClientBuilder::new()
24172417
}
24182418

2419+
/// Get the headers this `Client` will add to all its `Request`s.
2420+
pub fn headers(&self) -> &crate::header::HeaderMap {
2421+
&self.inner.as_ref().headers
2422+
}
2423+
24192424
/// Convenience method to make a `GET` request to a URL.
24202425
///
24212426
/// # Errors

src/blocking/client.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1226,6 +1226,11 @@ impl Client {
12261226
ClientBuilder::new()
12271227
}
12281228

1229+
/// Get the headers this `Client` will add to all its `Request`s.
1230+
pub fn headers(&self) -> &crate::header::HeaderMap {
1231+
&self.inner.headers
1232+
}
1233+
12291234
/// Convenience method to make a `GET` request to a URL.
12301235
///
12311236
/// # Errors
@@ -1329,6 +1334,7 @@ impl fmt::Debug for ClientBuilder {
13291334
#[derive(Clone)]
13301335
struct ClientHandle {
13311336
timeout: Timeout,
1337+
headers: crate::header::HeaderMap,
13321338
inner: Arc<InnerClientHandle>,
13331339
}
13341340

@@ -1359,6 +1365,7 @@ impl Drop for InnerClientHandle {
13591365
impl ClientHandle {
13601366
fn new(builder: ClientBuilder) -> crate::Result<ClientHandle> {
13611367
let timeout = builder.timeout;
1368+
let headers = builder.inner.config.headers.clone();
13621369
let builder = builder.inner;
13631370
let (tx, rx) = mpsc::unbounded_channel::<(async_impl::Request, OneshotResponse)>();
13641371
let (spawn_tx, spawn_rx) = oneshot::channel::<crate::Result<()>>();
@@ -1427,6 +1434,7 @@ impl ClientHandle {
14271434

14281435
Ok(ClientHandle {
14291436
timeout,
1437+
headers,
14301438
inner: inner_handle,
14311439
})
14321440
}

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)