Add HTTP Upgrade support to Response. (#1376)
This commit is contained in:
51
tests/upgrade.rs
Normal file
51
tests/upgrade.rs
Normal 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");
|
||||
}
|
||||
Reference in New Issue
Block a user