diff --git a/Cargo.toml b/Cargo.toml index b4fbd628..33d11189 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -67,7 +67,9 @@ hex-literal = "0.4" wasm-bindgen-test = "0.3.19" [target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies] -hyper = { version = "0.14", default-features = false, features = ["server"] } +hyper = { version = "1", default-features = false, features = ["server"] } +hyper-util = { version = "0.1", features = ["full"] } +http-body-util = "0.1" tokio = { version = "1.0", features = ["full"] } tokio-stream = { version = "0.1", features = ["net"] } diff --git a/src/transports/http.rs b/src/transports/http.rs index 252538e2..4af6479b 100644 --- a/src/transports/http.rs +++ b/src/transports/http.rs @@ -193,6 +193,9 @@ fn id_of_output(output: &Output) -> Result { mod tests { use super::*; use crate::Error::Rpc; + use bytes::Bytes; + use http_body_util::BodyExt; + use http_body_util::Full; use jsonrpc_core::ErrorCode; use std::net::TcpListener; @@ -200,37 +203,42 @@ mod tests { Some(TcpListener::bind(("127.0.0.1", 0)).ok()?.local_addr().ok()?.port()) } - async fn server(req: hyper::Request) -> hyper::Result> { - use hyper::body::HttpBody; - + async fn server(req: hyper::Request) -> hyper::Result>> { let expected = r#"{"jsonrpc":"2.0","method":"eth_getAccounts","params":[],"id":0}"#; let response = r#"{"jsonrpc":"2.0","id":0,"result":"x"}"#; assert_eq!(req.method(), &hyper::Method::POST); assert_eq!(req.uri().path(), "/"); let mut content: Vec = vec![]; - let mut body = req.into_body(); - while let Some(Ok(chunk)) = body.data().await { - content.extend(&*chunk); - } + let body = req.into_body(); + let body = body.collect().await?.to_bytes().to_vec(); + content.extend(body); + assert_eq!(std::str::from_utf8(&*content), Ok(expected)); - Ok(hyper::Response::new(response.into())) + Ok(hyper::Response::new(Full::new(Bytes::from(response)))) } #[tokio::test] async fn should_make_a_request() { - use hyper::service::{make_service_fn, service_fn}; - + use hyper::service::service_fn; + use hyper_util::rt::TokioExecutor; + use hyper_util::rt::TokioIo; + use hyper_util::server::conn::auto; + use tokio::net::TcpListener; // given let addr = format!("127.0.0.1:{}", get_available_port().unwrap()); - // start server - let service = make_service_fn(|_| async { Ok::<_, hyper::Error>(service_fn(server)) }); - let server = hyper::Server::bind(&addr.parse().unwrap()).serve(service); let addr_clone = addr.clone(); + // start server + let listener = TcpListener::bind(addr.clone()).await.unwrap(); + let (stream, _) = listener.accept().await.unwrap(); + let io = TokioIo::new(stream); tokio::spawn(async move { println!("Listening on http://{}", addr_clone); - server.await.unwrap(); + auto::Builder::new(TokioExecutor::new()) + .serve_connection(io, service_fn(server)) + .await + .unwrap(); }); // when @@ -245,9 +253,14 @@ mod tests { #[tokio::test] async fn catch_generic_json_error_for_batched_request() { - use hyper::service::{make_service_fn, service_fn}; - - async fn handler(_req: hyper::Request) -> hyper::Result> { + use http_body_util::Full; + use hyper::service::service_fn; + use hyper_util::rt::TokioExecutor; + use hyper_util::rt::TokioIo; + use hyper_util::server::conn::auto; + use tokio::net::TcpListener; + + async fn handler(_req: hyper::Request) -> hyper::Result>> { let response = r#"{ "jsonrpc":"2.0", "error":{ @@ -256,18 +269,22 @@ mod tests { }, "id":null }"#; - Ok(hyper::Response::::new(response.into())) + Ok(hyper::Response::new(Full::new(Bytes::from(response)))) } - // given + // Given let addr = format!("127.0.0.1:{}", get_available_port().unwrap()); - // start server - let service = make_service_fn(|_| async { Ok::<_, hyper::Error>(service_fn(handler)) }); - let server = hyper::Server::bind(&addr.parse().unwrap()).serve(service); let addr_clone = addr.clone(); + // start server + let listener = TcpListener::bind(addr.clone()).await.unwrap(); + let (stream, _) = listener.accept().await.unwrap(); + let io = TokioIo::new(stream); tokio::spawn(async move { println!("Listening on http://{}", addr_clone); - server.await.unwrap(); + auto::Builder::new(TokioExecutor::new()) + .serve_connection(io, service_fn(handler)) + .await + .unwrap(); }); // when