diff --git a/src/client/mod.rs b/src/client/mod.rs index 3734e02..7392f82 100644 --- a/src/client/mod.rs +++ b/src/client/mod.rs @@ -1,7 +1,9 @@ //! Process HTTP connections on the client. use async_std::io::{self, Read, Write}; +use http_types::headers::USER_AGENT; use http_types::{Request, Response}; +use lazy_static::lazy_static; mod decode; mod encode; @@ -9,11 +11,19 @@ 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 +