Skip to content

Commit

Permalink
client: add default user agent
Browse files Browse the repository at this point in the history
  • Loading branch information
Fishrock123 committed Sep 14, 2020
1 parent 1641e22 commit 0c2fa7b
Show file tree
Hide file tree
Showing 11 changed files with 45 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/client/mod.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
//! 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;

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<RW>(mut stream: RW, req: Request) -> http_types::Result<Response>
pub async fn connect<RW>(mut stream: RW, mut req: Request) -> http_types::Result<Response>
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);

Expand Down
17 changes: 17 additions & 0 deletions tests/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
1 change: 1 addition & 0 deletions tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/request-add-date.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions tests/fixtures/request-chunked-echo.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/request-unexpected-eof.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 5 additions & 0 deletions tests/fixtures/request-user-agent.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
GET / HTTP/1.1
host: localhost:8080
content-length: 0
user-agent: http-rs-h1/{VERSION}

1 change: 1 addition & 0 deletions tests/fixtures/request-with-connect.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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}

1 change: 1 addition & 0 deletions tests/fixtures/request-with-fragment.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
GET /path?query HTTP/1.1
host: example.com
content-length: 0
user-agent: http-rs-h1/{VERSION}

1 change: 1 addition & 0 deletions tests/fixtures/request-with-host.txt
Original file line number Diff line number Diff line change
@@ -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}

5 changes: 5 additions & 0 deletions tests/fixtures/response-user-agent.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
HTTP/1.1 200 OK
content-length: 0
date: {DATE}
content-type: text/plain;charset=utf-8

0 comments on commit 0c2fa7b

Please sign in to comment.