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

test(upgrade): Add http2 upgrade test #384

Merged
merged 1 commit into from
Jan 28, 2025
Merged
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
51 changes: 51 additions & 0 deletions tests/upgrade.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![cfg(not(target_arch = "wasm32"))]
mod support;
use http::Method;
use support::server;
use tokio::io::{AsyncReadExt, AsyncWriteExt};

Expand Down Expand Up @@ -49,3 +50,53 @@ async fn http_upgrade() {
upgraded.read_to_end(&mut buf).await.unwrap();
assert_eq!(buf, b"bar=foo");
}

#[tokio::test]
async fn http2_upgrade() {
let server = server::http_with_config(
move |req| {
assert_eq!(req.method(), http::Method::CONNECT);
assert_eq!(req.version(), http::Version::HTTP_2);

tokio::spawn(async move {
let mut upgraded =
hyper_util::rt::TokioIo::new(hyper::upgrade::on(req).await.unwrap());

let mut buf = vec![0; 7];
upgraded.read_exact(&mut buf).await.unwrap();
assert_eq!(buf, b"foo=bar");

upgraded.write_all(b"bar=foo").await.unwrap();
});

async {
http::Response::builder()
.body(rquest::Body::default())
.unwrap()
}
},
|builder| {
let mut http2 = builder.http2();
http2.enable_connect_protocol();
},
);

let res = rquest::Client::builder()
.http2_only()
.build()
.unwrap()
.request(Method::CONNECT, format!("http://{}", server.addr()))
.send()
.await
.unwrap();

assert_eq!(res.status(), http::StatusCode::OK);
assert_eq!(res.version(), http::Version::HTTP_2);
let mut upgraded = res.upgrade().await.unwrap();

upgraded.write_all(b"foo=bar").await.unwrap();

let mut buf = vec![];
upgraded.read_to_end(&mut buf).await.unwrap();
assert_eq!(buf, b"bar=foo");
}
Loading