From 86be54df71d8b4b97a7c569d6274e6fe4bf8aae8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Mockers?= Date: Sun, 3 Jun 2018 15:48:00 +0200 Subject: [PATCH 1/3] add default value for header User-Agent in requests --- src/client/request.rs | 11 ++++++++++- tests/test_client.rs | 18 ++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/client/request.rs b/src/client/request.rs index 4fef3e5d0..d3f6ebdb6 100644 --- a/src/client/request.rs +++ b/src/client/request.rs @@ -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,15 @@ 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, "Actix-web"); + } } let mut request = self.request.take().expect("cannot reuse request builder"); diff --git a/tests/test_client.rs b/tests/test_client.rs index 9b9fb0e82..cd47583c6 100644 --- a/tests/test_client.rs +++ b/tests/test_client.rs @@ -432,3 +432,21 @@ 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("\"user-agent\": \"Actix-web\"")); + + 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("\"user-agent\": \"Actix-web\"")); +} From ca3fb11f8b0fddaeb817ab0e7fa0b52f0f0af50f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Mockers?= Date: Mon, 4 Jun 2018 08:15:04 +0200 Subject: [PATCH 2/3] add actix-web version in header --- src/client/request.rs | 5 ++++- tests/test_client.rs | 12 ++++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/client/request.rs b/src/client/request.rs index d3f6ebdb6..bb338482b 100644 --- a/src/client/request.rs +++ b/src/client/request.rs @@ -615,7 +615,10 @@ impl ClientRequestBuilder { true }; if !contains { - self.header(header::USER_AGENT, "Actix-web"); + self.header( + header::USER_AGENT, + concat!("Actix-web/", env!("CARGO_PKG_VERSION")), + ); } } diff --git a/tests/test_client.rs b/tests/test_client.rs index cd47583c6..3128bb942 100644 --- a/tests/test_client.rs +++ b/tests/test_client.rs @@ -440,7 +440,11 @@ fn test_default_headers() { let request = srv.get().finish().unwrap(); let repr = format!("{:?}", request); assert!(repr.contains("\"accept-encoding\": \"gzip, deflate\"")); - assert!(repr.contains("\"user-agent\": \"Actix-web\"")); + assert!(repr.contains(concat!( + "\"user-agent\": \"Actix-web/", + env!("CARGO_PKG_VERSION"), + "\"" + ))); let request_override = srv.get() .header("User-Agent", "test") @@ -448,5 +452,9 @@ fn test_default_headers() { .unwrap(); let repr_override = format!("{:?}", request_override); assert!(repr_override.contains("\"user-agent\": \"test\"")); - assert!(!repr_override.contains("\"user-agent\": \"Actix-web\"")); + assert!(!repr_override.contains(concat!( + "\"user-agent\": \"Actix-web/", + env!("CARGO_PKG_VERSION"), + "\"" + ))); } From b07c50860a33eb1eced1c35faf535c1325ad37d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Mockers?= Date: Mon, 4 Jun 2018 22:34:07 +0200 Subject: [PATCH 3/3] update changelog --- CHANGES.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGES.md b/CHANGES.md index 367354a9b..7de9470c6 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -17,6 +17,7 @@ * Use `&mut self` instead of `&self` for Middleware trait +* Added header `User-Agent: Actix-web/` to default headers when building a request ### Removed