mirror of
https://github.com/actix/actix-web.git
synced 2024-12-28 02:50:39 +00:00
Merge pull request #274 from mockersf/user-agent
add default value for header User-Agent in requests
This commit is contained in:
commit
67ee24f9a0
3 changed files with 40 additions and 1 deletions
|
@ -17,6 +17,7 @@
|
|||
|
||||
* Use `&mut self` instead of `&self` for Middleware trait
|
||||
|
||||
* Added header `User-Agent: Actix-web/<current_version>` to default headers when building a request
|
||||
|
||||
### Removed
|
||||
|
||||
|
|
|
@ -506,7 +506,7 @@ impl ClientRequestBuilder {
|
|||
}
|
||||
|
||||
/// Do not add default request headers.
|
||||
/// By default `Accept-Encoding` header is set.
|
||||
/// By default `Accept-Encoding` and `User-Agent` headers are set.
|
||||
pub fn no_default_headers(&mut self) -> &mut Self {
|
||||
self.default_headers = false;
|
||||
self
|
||||
|
@ -608,6 +608,18 @@ impl ClientRequestBuilder {
|
|||
} else {
|
||||
self.header(header::ACCEPT_ENCODING, "gzip, deflate");
|
||||
}
|
||||
|
||||
let contains = if let Some(parts) = parts(&mut self.request, &self.err) {
|
||||
parts.headers.contains_key(header::USER_AGENT)
|
||||
} else {
|
||||
true
|
||||
};
|
||||
if !contains {
|
||||
self.header(
|
||||
header::USER_AGENT,
|
||||
concat!("Actix-web/", env!("CARGO_PKG_VERSION")),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
let mut request = self.request.take().expect("cannot reuse request builder");
|
||||
|
|
|
@ -432,3 +432,29 @@ fn test_client_cookie_handling() {
|
|||
let c2 = response.cookie("cookie2").expect("Missing cookie2");
|
||||
assert_eq!(c2, &cookie2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_default_headers() {
|
||||
let srv = test::TestServer::new(|app| app.handler(|_| HttpResponse::Ok().body(STR)));
|
||||
|
||||
let request = srv.get().finish().unwrap();
|
||||
let repr = format!("{:?}", request);
|
||||
assert!(repr.contains("\"accept-encoding\": \"gzip, deflate\""));
|
||||
assert!(repr.contains(concat!(
|
||||
"\"user-agent\": \"Actix-web/",
|
||||
env!("CARGO_PKG_VERSION"),
|
||||
"\""
|
||||
)));
|
||||
|
||||
let request_override = srv.get()
|
||||
.header("User-Agent", "test")
|
||||
.finish()
|
||||
.unwrap();
|
||||
let repr_override = format!("{:?}", request_override);
|
||||
assert!(repr_override.contains("\"user-agent\": \"test\""));
|
||||
assert!(!repr_override.contains(concat!(
|
||||
"\"user-agent\": \"Actix-web/",
|
||||
env!("CARGO_PKG_VERSION"),
|
||||
"\""
|
||||
)));
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue