From b30a2d5d156ca8e125c1617b5228718252282d03 Mon Sep 17 00:00:00 2001 From: Jeremiah Senkpiel Date: Mon, 14 Sep 2020 15:57:58 -0700 Subject: [PATCH] client: add default user agent --- Cargo.toml | 2 ++ src/client/mod.rs | 15 ++++++++++++++- tests/client.rs | 17 +++++++++++++++++ tests/common/mod.rs | 1 + tests/fixtures/request-add-date.txt | 1 + tests/fixtures/request-chunked-echo.txt | 1 + tests/fixtures/request-unexpected-eof.txt | 1 + tests/fixtures/request-user-agent.txt | 5 +++++ tests/fixtures/request-with-connect.txt | 1 + tests/fixtures/request-with-fragment.txt | 1 + tests/fixtures/request-with-host.txt | 1 + tests/fixtures/response-user-agent.txt | 5 +++++ 12 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 tests/fixtures/request-user-agent.txt create mode 100644 tests/fixtures/response-user-agent.txt diff --git a/Cargo.toml b/Cargo.toml index bd9beb6..29b8d3a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,6 +11,8 @@ authors = ["Yoshua Wuyts "] readme = "README.md" edition = "2018" +[features] + [dependencies] httparse = "1.3.3" async-std = { version = "1.6.0", features = ["unstable"] } diff --git a/src/client/mod.rs b/src/client/mod.rs index 3734e02..b73afef 100644 --- a/src/client/mod.rs +++ b/src/client/mod.rs @@ -2,6 +2,8 @@ use async_std::io::{self, Read, Write}; use http_types::{Request, Response}; +use http_types::headers::USER_AGENT; +use lazy_static::lazy_static; mod decode; mod encode; @@ -9,11 +11,22 @@ mod encode; pub use decode::decode; pub use encode::Encoder; +lazy_static! { + static ref DEFAULT_USER_AGENT: String = format!( + "http-rs-h1/{}", + env!("CARGO_PKG_VERSION") + ); +} + /// Opens an HTTP/1.1 connection to a remote host. -pub async fn connect(mut stream: RW, req: Request) -> http_types::Result +pub async fn connect(mut stream: RW, mut req: Request) -> http_types::Result where RW: Read + Write + Send + Sync + Unpin + 'static, { + if let None = req.header(USER_AGENT) { + req.insert_header(USER_AGENT, DEFAULT_USER_AGENT.as_str()); + } + let mut req = Encoder::encode(req).await?; log::trace!("> {:?}", &req); diff --git a/tests/client.rs b/tests/client.rs index 675dd16..7440f56 100644 --- a/tests/client.rs +++ b/tests/client.rs @@ -7,6 +7,23 @@ mod common; use common::TestCase; +#[async_std::test] +async fn test_default_user_agent() { + let case = TestCase::new_client( + "fixtures/request-user-agent.txt", + "fixtures/response-user-agent.txt", + ) + .await; + + let url = Url::parse("http://localhost:8080").unwrap(); + let req = Request::new(Method::Get, url); + + let res = client::connect(case.clone(), req).await.unwrap(); + assert_eq!(res.status(), StatusCode::Ok); + + case.assert().await; +} + #[async_std::test] async fn test_encode_request_add_date() { let case = TestCase::new_client( diff --git a/tests/common/mod.rs b/tests/common/mod.rs index 400b13a..f6ede4e 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -103,6 +103,7 @@ impl TestCase { // munge actual and expected so that we don't rely on dates matching exactly munge_date(&mut actual, &mut expected); + let expected = expected.replace("{VERSION}", env!("CARGO_PKG_VERSION")); pretty_assertions::assert_eq!(actual, expected); } } diff --git a/tests/fixtures/request-add-date.txt b/tests/fixtures/request-add-date.txt index 9a0fb6c..a41ee90 100644 --- a/tests/fixtures/request-add-date.txt +++ b/tests/fixtures/request-add-date.txt @@ -2,5 +2,6 @@ POST / HTTP/1.1 host: localhost:8080 content-length: 5 content-type: text/plain;charset=utf-8 +user-agent: http-rs-h1/{VERSION} hello \ No newline at end of file diff --git a/tests/fixtures/request-chunked-echo.txt b/tests/fixtures/request-chunked-echo.txt index dac19a0..7c9d665 100644 --- a/tests/fixtures/request-chunked-echo.txt +++ b/tests/fixtures/request-chunked-echo.txt @@ -3,6 +3,7 @@ host: example.com user-agent: curl/7.54.0 content-type: text/plain transfer-encoding: chunked +user-agent: http-rs-h1/{VERSION} 7 Mozilla diff --git a/tests/fixtures/request-unexpected-eof.txt b/tests/fixtures/request-unexpected-eof.txt index 6feaf13..30fc21a 100644 --- a/tests/fixtures/request-unexpected-eof.txt +++ b/tests/fixtures/request-unexpected-eof.txt @@ -2,5 +2,6 @@ POST / HTTP/1.1 host: example.com content-type: text/plain content-length: 11 +user-agent: http-rs-h1/{VERSION} aaaaabbbbb \ No newline at end of file diff --git a/tests/fixtures/request-user-agent.txt b/tests/fixtures/request-user-agent.txt new file mode 100644 index 0000000..3649f16 --- /dev/null +++ b/tests/fixtures/request-user-agent.txt @@ -0,0 +1,5 @@ +GET / HTTP/1.1 +host: localhost:8080 +content-length: 0 +user-agent: http-rs-h1/{VERSION} + diff --git a/tests/fixtures/request-with-connect.txt b/tests/fixtures/request-with-connect.txt index 3cb337e..34ee287 100644 --- a/tests/fixtures/request-with-connect.txt +++ b/tests/fixtures/request-with-connect.txt @@ -2,4 +2,5 @@ CONNECT example.com:443 HTTP/1.1 host: example.com proxy-connection: keep-alive content-length: 0 +user-agent: http-rs-h1/{VERSION} diff --git a/tests/fixtures/request-with-fragment.txt b/tests/fixtures/request-with-fragment.txt index da73f24..58d1198 100644 --- a/tests/fixtures/request-with-fragment.txt +++ b/tests/fixtures/request-with-fragment.txt @@ -1,4 +1,5 @@ GET /path?query HTTP/1.1 host: example.com content-length: 0 +user-agent: http-rs-h1/{VERSION} diff --git a/tests/fixtures/request-with-host.txt b/tests/fixtures/request-with-host.txt index ede81f3..2fb2698 100644 --- a/tests/fixtures/request-with-host.txt +++ b/tests/fixtures/request-with-host.txt @@ -1,4 +1,5 @@ GET /pub/WWW/TheProject.html HTTP/1.1 Host: www.w3.org Content-Length: 0 +user-agent: http-rs-h1/{VERSION} diff --git a/tests/fixtures/response-user-agent.txt b/tests/fixtures/response-user-agent.txt new file mode 100644 index 0000000..0dc66f4 --- /dev/null +++ b/tests/fixtures/response-user-agent.txt @@ -0,0 +1,5 @@ +HTTP/1.1 200 OK +content-length: 0 +date: {DATE} +content-type: text/plain;charset=utf-8 +