Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

client: add default user agent #140

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"));
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unsure what to call it, async-h1 seems wrong when used by e.g. surf, but also maybe surf should override it anyways. Unsure.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 for surf overriding or appending. why not async-h1, though, since that's the name of the crate? Then it could be something like surf/2.0.0 (async-h1/6.0.0)

}

/// 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