Skip to content

Commit

Permalink
add header if no Accept is set
Browse files Browse the repository at this point in the history
  • Loading branch information
seanmonstar committed Dec 12, 2016
1 parent a317339 commit 559ae80
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ authors = ["Sean McArthur <[email protected]>"]
license = "MIT/Apache-2.0"

[dependencies]
hyper = { version = "0.9" , default-features = false }
hyper = { version = "0.9.14" , default-features = false }
log = "0.3"
native-tls = "0.1"
serde = "0.8"
Expand Down
6 changes: 5 additions & 1 deletion src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::io::{self, Read};
use std::sync::{Arc, Mutex};

use hyper::client::IntoUrl;
use hyper::header::{Headers, ContentType, Location, Referer, UserAgent};
use hyper::header::{Headers, ContentType, Location, Referer, UserAgent, Accept};
use hyper::method::Method;
use hyper::status::StatusCode;
use hyper::version::HttpVersion;
Expand Down Expand Up @@ -197,6 +197,10 @@ impl RequestBuilder {
self.headers.set(UserAgent(DEFAULT_USER_AGENT.to_owned()));
}

if !self.headers.has::<Accept>() {
self.headers.set(Accept::star());
}

let client = self.client;
let mut method = self.method;
let mut url = try!(self.url);
Expand Down
35 changes: 35 additions & 0 deletions tests/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ fn test_get() {
GET /1 HTTP/1.1\r\n\
Host: $HOST\r\n\
User-Agent: $USERAGENT\r\n\
Accept: */*\r\n\
\r\n\
",
response: b"\
Expand Down Expand Up @@ -43,6 +44,7 @@ fn test_redirect_301_and_302_and_303_changes_post_to_get() {
POST /{} HTTP/1.1\r\n\
Host: $HOST\r\n\
User-Agent: $USERAGENT\r\n\
Accept: */*\r\n\
Content-Length: 0\r\n\
\r\n\
", code),
Expand All @@ -59,6 +61,7 @@ fn test_redirect_301_and_302_and_303_changes_post_to_get() {
GET /dst HTTP/1.1\r\n\
Host: $HOST\r\n\
User-Agent: $USERAGENT\r\n\
Accept: */*\r\n\
Referer: http://$HOST/{}\r\n\
\r\n\
", code),
Expand Down Expand Up @@ -88,6 +91,7 @@ fn test_redirect_307_and_308_tries_to_post_again() {
POST /{} HTTP/1.1\r\n\
Host: $HOST\r\n\
User-Agent: $USERAGENT\r\n\
Accept: */*\r\n\
Content-Length: 5\r\n\
\r\n\
Hello\
Expand All @@ -105,6 +109,7 @@ fn test_redirect_307_and_308_tries_to_post_again() {
POST /dst HTTP/1.1\r\n\
Host: $HOST\r\n\
User-Agent: $USERAGENT\r\n\
Accept: */*\r\n\
Referer: http://$HOST/{}\r\n\
Content-Length: 5\r\n\
\r\n\
Expand Down Expand Up @@ -137,6 +142,7 @@ fn test_redirect_307_does_not_try_if_reader_cannot_reset() {
POST /{} HTTP/1.1\r\n\
Host: $HOST\r\n\
User-Agent: $USERAGENT\r\n\
Accept: */*\r\n\
Transfer-Encoding: chunked\r\n\
\r\n\
5\r\n\
Expand Down Expand Up @@ -168,6 +174,7 @@ fn test_redirect_policy_can_return_errors() {
GET /loop HTTP/1.1\r\n\
Host: $HOST\r\n\
User-Agent: $USERAGENT\r\n\
Accept: */*\r\n\
\r\n\
",
response: b"\
Expand All @@ -193,6 +200,7 @@ fn test_redirect_policy_can_stop_redirects_without_an_error() {
GET /no-redirect HTTP/1.1\r\n\
Host: $HOST\r\n\
User-Agent: $USERAGENT\r\n\
Accept: */*\r\n\
\r\n\
",
response: b"\
Expand All @@ -213,3 +221,30 @@ fn test_redirect_policy_can_stop_redirects_without_an_error() {
assert_eq!(res.status(), &reqwest::StatusCode::Found);
assert_eq!(res.headers().get(), Some(&reqwest::header::Server("test-dont".to_string())));
}

#[test]
fn test_accept_header_is_not_changed_if_set() {
let server = server! {
request: b"\
GET /accept HTTP/1.1\r\n\
Host: $HOST\r\n\
Accept: application/json\r\n\
User-Agent: $USERAGENT\r\n\
\r\n\
",
response: b"\
HTTP/1.1 200 OK\r\n\
Server: test-accept\r\n\
Content-Length: 0\r\n\
\r\n\
"
};
let mut client = reqwest::Client::new().unwrap();

let res = client.get(&format!("http://{}/accept", server.addr()))
.header(reqwest::header::Accept::json())
.send()
.unwrap();

assert_eq!(res.status(), &reqwest::StatusCode::Ok);
}

0 comments on commit 559ae80

Please sign in to comment.