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
11 changes: 8 additions & 3 deletions src/async_impl/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ pub struct Client {
/// A `ClientBuilder` can be used to create a `Client` with custom configuration.
#[must_use]
pub struct ClientBuilder {
config: Config,
pub(crate) config: Config,
}

enum HttpVersionPref {
Expand Down Expand Up @@ -117,10 +117,10 @@ impl Service<hyper::Request<crate::async_impl::body::Body>> for HyperService {
}
}

struct Config {
pub(crate) struct Config {
// NOTE: When adding a new field, update `fmt::Debug for ClientBuilder`
accepts: Accepts,
headers: HeaderMap,
pub(crate) headers: HeaderMap,
#[cfg(feature = "__tls")]
hostname_verification: bool,
#[cfg(feature = "__tls")]
Expand Down Expand Up @@ -2379,6 +2379,11 @@ impl Client {
ClientBuilder::new()
}

/// Get the headers this `Client` will add to all its `Request`s.
pub fn headers(&self) -> &crate::header::HeaderMap {
&self.inner.as_ref().headers
}

/// Convenience method to make a `GET` request to a URL.
///
/// # Errors
Expand Down
8 changes: 8 additions & 0 deletions src/blocking/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1226,6 +1226,11 @@ impl Client {
ClientBuilder::new()
}

/// Get the headers this `Client` will add to all its `Request`s.
pub fn headers(&self) -> &crate::header::HeaderMap {
&self.inner.headers
}

/// Convenience method to make a `GET` request to a URL.
///
/// # Errors
Expand Down Expand Up @@ -1329,6 +1334,7 @@ impl fmt::Debug for ClientBuilder {
#[derive(Clone)]
struct ClientHandle {
timeout: Timeout,
headers: crate::header::HeaderMap,
inner: Arc<InnerClientHandle>,
}

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

Ok(ClientHandle {
timeout,
headers,
inner: inner_handle,
})
}
Expand Down
12 changes: 12 additions & 0 deletions tests/blocking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,8 @@ fn test_default_headers() {
let url = format!("http://{}/1", server.addr());
let res = client.get(&url).send().unwrap();

assert_eq!(client.headers()["reqwest-test"], "orly");

assert_eq!(res.url().as_str(), &url);
assert_eq!(res.status(), reqwest::StatusCode::OK);
}
Expand Down Expand Up @@ -250,6 +252,8 @@ fn test_override_default_headers() {
.send()
.unwrap();

assert_eq!(client.headers()[&http::header::AUTHORIZATION], "iamatoken");

assert_eq!(res.url().as_str(), &url);
assert_eq!(res.status(), reqwest::StatusCode::OK);
}
Expand All @@ -275,6 +279,10 @@ fn test_appended_headers_not_overwritten() {
.send()
.unwrap();

let mut accepts = client.headers().get_all("accept").into_iter();
assert_eq!(accepts.next().unwrap(), "*/*");
assert_eq!(accepts.next(), None);

assert_eq!(res.url().as_str(), &url);
assert_eq!(res.status(), reqwest::StatusCode::OK);

Expand All @@ -298,6 +306,10 @@ fn test_appended_headers_not_overwritten() {
.send()
.unwrap();

let mut accepts = client.headers().get_all("accept").into_iter();
assert_eq!(accepts.next().unwrap(), "text/html");
assert_eq!(accepts.next(), None);

assert_eq!(res.url().as_str(), &url);
assert_eq!(res.status(), reqwest::StatusCode::OK);
}
Expand Down
24 changes: 10 additions & 14 deletions tests/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,11 @@ async fn auto_headers() {
});

let url = format!("http://{}/1", server.addr());
let res = reqwest::Client::builder()
.no_proxy()
.build()
.unwrap()
.get(&url)
.send()
.await
.unwrap();
let client = reqwest::Client::builder().no_proxy().build().unwrap();
let res = client.get(&url).send().await.unwrap();

assert_eq!(client.headers()["accept"], "*/*");
assert_eq!(client.headers().get("user-agent"), None);

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

let url = format!("http://{}/ua", server.addr());
let res = reqwest::Client::builder()
let client = reqwest::Client::builder()
.user_agent("reqwest-test-agent")
.build()
.expect("client builder")
.get(&url)
.send()
.await
.expect("request");
.expect("client builder");
let res = client.get(&url).send().await.expect("request");

assert_eq!(client.headers()["user-agent"], "reqwest-test-agent");

assert_eq!(res.status(), reqwest::StatusCode::OK);
}
Expand Down