Update to hyper 0.13

This commit is contained in:
Gleb Pomykalov
2019-12-11 03:24:05 +03:00
committed by Sean McArthur
parent db2de90e42
commit 0f32c4a01a
23 changed files with 469 additions and 219 deletions

View File

@@ -82,12 +82,12 @@ fn test_get() {
#[test]
fn test_post() {
let server = server::http(move |mut req| {
let server = server::http(move |req| {
async move {
assert_eq!(req.method(), "POST");
assert_eq!(req.headers()["content-length"], "5");
let data = req.body_mut().next().await.unwrap().unwrap();
let data = hyper::body::to_bytes(req.into_body()).await.unwrap();
assert_eq!(&*data, b"Hello");
http::Response::default()
@@ -107,7 +107,7 @@ fn test_post() {
#[test]
fn test_post_form() {
let server = server::http(move |mut req| {
let server = server::http(move |req| {
async move {
assert_eq!(req.method(), "POST");
assert_eq!(req.headers()["content-length"], "24");
@@ -116,7 +116,7 @@ fn test_post_form() {
"application/x-www-form-urlencoded"
);
let data = req.body_mut().next().await.unwrap().unwrap();
let data = hyper::body::to_bytes(req.into_body()).await.unwrap();
assert_eq!(&*data, b"hello=world&sean=monstar");
http::Response::default()
@@ -287,3 +287,17 @@ fn test_appended_headers_not_overwritten() {
assert_eq!(res.url().as_str(), &url);
assert_eq!(res.status(), reqwest::StatusCode::OK);
}
#[test]
#[should_panic]
fn test_blocking_inside_a_runtime() {
let server = server::http(move |_req| async { http::Response::new("Hello".into()) });
let url = format!("http://{}/text", server.addr());
let mut rt = tokio::runtime::Builder::new().build().expect("new rt");
rt.block_on(async move {
let _should_panic = reqwest::blocking::get(&url);
});
}

View File

@@ -1,4 +1,5 @@
mod support;
use futures_util::stream::StreamExt;
use support::*;
use reqwest::Client;

View File

@@ -1,4 +1,5 @@
mod support;
use futures_util::stream::StreamExt;
use support::*;
#[tokio::test]

View File

@@ -1,4 +1,5 @@
mod support;
use futures_util::stream::StreamExt;
use support::*;
#[tokio::test]

View File

@@ -8,6 +8,7 @@ use std::time::Duration;
use tokio::sync::oneshot;
pub use http::Response;
use tokio::runtime;
pub struct Server {
addr: net::SocketAddr,
@@ -40,41 +41,52 @@ where
F: Fn(http::Request<hyper::Body>) -> Fut + Clone + Send + 'static,
Fut: Future<Output = http::Response<hyper::Body>> + Send + 'static,
{
let srv = hyper::Server::bind(&([127, 0, 0, 1], 0).into()).serve(
hyper::service::make_service_fn(move |_| {
let func = func.clone();
async move {
Ok::<_, Infallible>(hyper::service::service_fn(move |req| {
let fut = func(req);
async move { Ok::<_, Infallible>(fut.await) }
}))
}
}),
);
//Spawn new runtime in thread to prevent reactor execution context conflict
thread::spawn(move || {
let mut rt = runtime::Builder::new()
.basic_scheduler()
.enable_all()
.build()
.expect("new rt");
let srv = rt.block_on(async move {
hyper::Server::bind(&([127, 0, 0, 1], 0).into()).serve(hyper::service::make_service_fn(
move |_| {
let func = func.clone();
async move {
Ok::<_, Infallible>(hyper::service::service_fn(move |req| {
let fut = func(req);
async move { Ok::<_, Infallible>(fut.await) }
}))
}
},
))
});
let addr = srv.local_addr();
let (shutdown_tx, shutdown_rx) = oneshot::channel();
let srv = srv.with_graceful_shutdown(async move {
let _ = shutdown_rx.await;
});
let addr = srv.local_addr();
let (shutdown_tx, shutdown_rx) = oneshot::channel();
let srv = srv.with_graceful_shutdown(async move {
let _ = shutdown_rx.await;
});
let (panic_tx, panic_rx) = std_mpsc::channel();
let tname = format!(
"test({})-support-server",
thread::current().name().unwrap_or("<unknown>")
);
thread::Builder::new()
.name(tname)
.spawn(move || {
let mut rt = tokio::runtime::current_thread::Runtime::new().expect("rt new");
rt.block_on(srv).unwrap();
let _ = panic_tx.send(());
})
.expect("thread spawn");
let (panic_tx, panic_rx) = std_mpsc::channel();
let tname = format!(
"test({})-support-server",
thread::current().name().unwrap_or("<unknown>")
);
thread::Builder::new()
.name(tname)
.spawn(move || {
rt.block_on(srv).unwrap();
let _ = panic_tx.send(());
})
.expect("thread spawn");
Server {
addr,
panic_rx,
shutdown_tx: Some(shutdown_tx),
}
Server {
addr,
panic_rx,
shutdown_tx: Some(shutdown_tx),
}
})
.join()
.unwrap()
}

View File

@@ -1,7 +1,7 @@
mod support;
use support::*;
use std::time::{Duration, Instant};
use std::time::Duration;
#[tokio::test]
async fn request_timeout() {
@@ -10,7 +10,7 @@ async fn request_timeout() {
let server = server::http(move |_req| {
async {
// delay returning the response
tokio::timer::delay(Instant::now() + Duration::from_secs(2)).await;
tokio::time::delay_for(Duration::from_secs(2)).await;
http::Response::default()
}
});
@@ -38,7 +38,7 @@ async fn response_timeout() {
async {
// immediate response, but delayed body
let body = hyper::Body::wrap_stream(futures_util::stream::once(async {
tokio::timer::delay(Instant::now() + Duration::from_secs(2)).await;
tokio::time::delay_for(Duration::from_secs(2)).await;
Ok::<_, std::convert::Infallible>("Hello")
}));
@@ -77,7 +77,7 @@ fn timeout_closes_connection() {
let server = server::http(move |_req| {
async {
// delay returning the response
tokio::timer::delay(Instant::now() + Duration::from_secs(2)).await;
tokio::time::delay_for(Duration::from_secs(2)).await;
http::Response::default()
}
});
@@ -106,7 +106,7 @@ fn write_timeout_large_body() {
let server = server::http(move |_req| {
async {
// delay returning the response
tokio::timer::delay(Instant::now() + Duration::from_secs(2)).await;
tokio::time::delay_for(Duration::from_secs(2)).await;
http::Response::default()
}
});