Skip to content

Commit 0c2fa7b

Browse files
committed
client: add default user agent
1 parent 1641e22 commit 0c2fa7b

11 files changed

+45
-1
lines changed

src/client/mod.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,29 @@
11
//! Process HTTP connections on the client.
22
33
use async_std::io::{self, Read, Write};
4+
use http_types::headers::USER_AGENT;
45
use http_types::{Request, Response};
6+
use lazy_static::lazy_static;
57

68
mod decode;
79
mod encode;
810

911
pub use decode::decode;
1012
pub use encode::Encoder;
1113

14+
lazy_static! {
15+
static ref DEFAULT_USER_AGENT: String = format!("http-rs-h1/{}", env!("CARGO_PKG_VERSION"));
16+
}
17+
1218
/// Opens an HTTP/1.1 connection to a remote host.
13-
pub async fn connect<RW>(mut stream: RW, req: Request) -> http_types::Result<Response>
19+
pub async fn connect<RW>(mut stream: RW, mut req: Request) -> http_types::Result<Response>
1420
where
1521
RW: Read + Write + Send + Sync + Unpin + 'static,
1622
{
23+
if let None = req.header(USER_AGENT) {
24+
req.insert_header(USER_AGENT, DEFAULT_USER_AGENT.as_str());
25+
}
26+
1727
let mut req = Encoder::encode(req).await?;
1828
log::trace!("> {:?}", &req);
1929

tests/client.rs

+17
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,23 @@ mod common;
77

88
use common::TestCase;
99

10+
#[async_std::test]
11+
async fn test_default_user_agent() {
12+
let case = TestCase::new_client(
13+
"fixtures/request-user-agent.txt",
14+
"fixtures/response-user-agent.txt",
15+
)
16+
.await;
17+
18+
let url = Url::parse("http://localhost:8080").unwrap();
19+
let req = Request::new(Method::Get, url);
20+
21+
let res = client::connect(case.clone(), req).await.unwrap();
22+
assert_eq!(res.status(), StatusCode::Ok);
23+
24+
case.assert().await;
25+
}
26+
1027
#[async_std::test]
1128
async fn test_encode_request_add_date() {
1229
let case = TestCase::new_client(

tests/common/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ impl TestCase {
103103

104104
// munge actual and expected so that we don't rely on dates matching exactly
105105
munge_date(&mut actual, &mut expected);
106+
let expected = expected.replace("{VERSION}", env!("CARGO_PKG_VERSION"));
106107
pretty_assertions::assert_eq!(actual, expected);
107108
}
108109
}

tests/fixtures/request-add-date.txt

+1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ POST / HTTP/1.1
22
host: localhost:8080
33
content-length: 5
44
content-type: text/plain;charset=utf-8
5+
user-agent: http-rs-h1/{VERSION}
56

67
hello

tests/fixtures/request-chunked-echo.txt

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ host: example.com
33
user-agent: curl/7.54.0
44
content-type: text/plain
55
transfer-encoding: chunked
6+
user-agent: http-rs-h1/{VERSION}
67

78
7
89
Mozilla

tests/fixtures/request-unexpected-eof.txt

+1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ POST / HTTP/1.1
22
host: example.com
33
content-type: text/plain
44
content-length: 11
5+
user-agent: http-rs-h1/{VERSION}
56

67
aaaaabbbbb

tests/fixtures/request-user-agent.txt

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
GET / HTTP/1.1
2+
host: localhost:8080
3+
content-length: 0
4+
user-agent: http-rs-h1/{VERSION}
5+

tests/fixtures/request-with-connect.txt

+1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ CONNECT example.com:443 HTTP/1.1
22
host: example.com
33
proxy-connection: keep-alive
44
content-length: 0
5+
user-agent: http-rs-h1/{VERSION}
56

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
GET /path?query HTTP/1.1
22
host: example.com
33
content-length: 0
4+
user-agent: http-rs-h1/{VERSION}
45

tests/fixtures/request-with-host.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
GET /pub/WWW/TheProject.html HTTP/1.1
22
Host: www.w3.org
33
Content-Length: 0
4+
user-agent: http-rs-h1/{VERSION}
45

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
HTTP/1.1 200 OK
2+
content-length: 0
3+
date: {DATE}
4+
content-type: text/plain;charset=utf-8
5+

0 commit comments

Comments
 (0)