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

update hyper to version 1.0 #733

Merged
merged 2 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
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
6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ once_cell = { version = "1.8.0", optional = true }
## HTTP
base64 = { version = "0.22", optional = true }
bytes = { version = "1.0", optional = true }
reqwest = { version = "0.11", optional = true, default-features = false, features = ["json"] }
reqwest = { version = "0.12", optional = true, default-features = false, features = ["json"] }
headers = { version = "0.4", optional = true }
## WS
# async-native-tls = { git = "https://github.com/async-email/async-native-tls.git", rev = "b5b5562d6cea77f913d4cbe448058c031833bf17", optional = true, default-features = false }
Expand Down Expand Up @@ -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"] }

Expand Down
64 changes: 41 additions & 23 deletions src/transports/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,44 +193,52 @@ fn id_of_output(output: &Output) -> Result<RequestId> {
mod tests {
use super::*;
use crate::Error::Rpc;
use bytes::Bytes;
use http_body_util::{BodyExt, Full};
use jsonrpc_core::ErrorCode;
use std::net::TcpListener;

fn get_available_port() -> Option<u16> {
Some(TcpListener::bind(("127.0.0.1", 0)).ok()?.local_addr().ok()?.port())
}

async fn server(req: hyper::Request<hyper::Body>) -> hyper::Result<hyper::Response<hyper::Body>> {
use hyper::body::HttpBody;

async fn server(req: hyper::Request<hyper::body::Incoming>) -> hyper::Result<hyper::Response<Full<Bytes>>> {
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<u8> = 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, TokioIo},
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();
tokio::spawn(async move {
println!("Listening on http://{}", addr_clone);
server.await.unwrap();
let (stream, _) = listener.accept().await.unwrap();
let io = TokioIo::new(stream);
auto::Builder::new(TokioExecutor::new())
.serve_connection(io, service_fn(server))
.await
.unwrap();
});

// when
Expand All @@ -245,9 +253,15 @@ 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::Body>) -> hyper::Result<hyper::Response<hyper::Body>> {
use http_body_util::Full;
use hyper::service::service_fn;
use hyper_util::{
rt::{TokioExecutor, TokioIo},
server::conn::auto,
};
use tokio::net::TcpListener;

async fn handler(_req: hyper::Request<hyper::body::Incoming>) -> hyper::Result<hyper::Response<Full<Bytes>>> {
let response = r#"{
"jsonrpc":"2.0",
"error":{
Expand All @@ -256,18 +270,22 @@ mod tests {
},
"id":null
}"#;
Ok(hyper::Response::<hyper::Body>::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();
tokio::spawn(async move {
println!("Listening on http://{}", addr_clone);
server.await.unwrap();
let (stream, _) = listener.accept().await.unwrap();
let io = TokioIo::new(stream);
auto::Builder::new(TokioExecutor::new())
.serve_connection(io, service_fn(handler))
.await
.unwrap();
});

// when
Expand Down
Loading