Add HTTP Upgrade support to Response. (#1376)

This commit is contained in:
Luqman Aden
2022-07-28 13:18:18 -07:00
committed by GitHub
parent e9ba0a9dc7
commit 61474f422c
5 changed files with 161 additions and 42 deletions

51
tests/upgrade.rs Normal file
View File

@@ -0,0 +1,51 @@
#![cfg(not(target_arch = "wasm32"))]
mod support;
use support::*;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
#[tokio::test]
async fn http_upgrade() {
let server = server::http(move |req| {
assert_eq!(req.method(), "GET");
assert_eq!(req.headers()["connection"], "upgrade");
assert_eq!(req.headers()["upgrade"], "foobar");
tokio::spawn(async move {
let mut upgraded = 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()
.status(http::StatusCode::SWITCHING_PROTOCOLS)
.header(http::header::CONNECTION, "upgrade")
.header(http::header::UPGRADE, "foobar")
.body(hyper::Body::empty())
.unwrap()
}
});
let res = reqwest::Client::builder()
.build()
.unwrap()
.get(format!("http://{}", server.addr()))
.header(http::header::CONNECTION, "upgrade")
.header(http::header::UPGRADE, "foobar")
.send()
.await
.unwrap();
assert_eq!(res.status(), http::StatusCode::SWITCHING_PROTOCOLS);
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");
}