Skip to content

Commit

Permalink
Iterate on tests
Browse files Browse the repository at this point in the history
  • Loading branch information
howardjohn committed Nov 28, 2023
1 parent 35250d0 commit 15eb88a
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::convert::Infallible;

use bytes::Bytes;
use http::{Request, Response, StatusCode};
use http_body_util::BodyExt;
use hyper_1::server::conn::http1;
use tokio::net::TcpListener;
Expand All @@ -10,15 +9,15 @@ use crate::*;

#[tokio::test]
async fn tower_service_03_service_to_hyper_1_service() {
async fn handle<B>(req: Request<B>) -> Result<Response<hyper_014::Body>, Infallible>
async fn handle<B>(req: http_02::Request<B>) -> Result<http_02::Response<hyper_014::Body>, Infallible>
where
B: http_body_04::Body,
{
let bytes = hyper_014::body::to_bytes(req)
.await
.unwrap_or_else(|_| panic!());
assert_eq!(bytes, "in");
Ok(Response::new(hyper_014::Body::from("out")))
Ok(http_02::Response::new(hyper_014::Body::from("out")))
}

let svc = tower::service_fn(handle);
Expand All @@ -42,34 +41,34 @@ async fn tower_service_03_service_to_hyper_1_service() {
let client = hyper_014::Client::builder().build_http();
let mut res = client
.request(
Request::builder()
http_02::Request::builder()
.uri(format!("http://{addr}"))
.body(hyper_014::Body::from("in"))
.unwrap(),
)
.await
.unwrap();

assert_eq!(res.status(), StatusCode::OK);
assert_eq!(res.status(), http_02::StatusCode::OK);

let bytes = hyper_014::body::to_bytes(&mut res).await.unwrap();
assert_eq!(bytes, "out");
}

#[tokio::test]
async fn hyper_1_service_to_tower_service_03_service() {
async fn handle<B>(req: Request<B>) -> Result<Response<http_body_util::Full<Bytes>>, Infallible>
async fn handle<B>(req: http_1::Request<B>) -> Result<http_1::Response<http_body_util::Full<Bytes>>, Infallible>
where
B: http_body_1::Body,
{
let collected = req.into_body().collect().await.unwrap_or_else(|_| panic!());
assert_eq!(collected.to_bytes(), "in");

Ok(Response::new(http_body_util::Full::new(Bytes::from("out"))))
Ok(http_1::Response::new(http_body_util::Full::new(Bytes::from("out"))))
}

let svc = hyper_1::service::service_fn(handle);
let svc = Hyper1HttpServiceAsTowerService03HttpService::new(svc);
let svc = crate::Hyper1HttpServiceAsTowerService03HttpService::new(svc);

let tcp_listener = std::net::TcpListener::bind("0.0.0.0:0").unwrap();
let addr = tcp_listener.local_addr().unwrap();
Expand All @@ -84,15 +83,15 @@ async fn hyper_1_service_to_tower_service_03_service() {
let client = hyper_014::Client::builder().build_http();
let mut res = client
.request(
Request::builder()
http_02::Request::builder()
.uri(format!("http://{addr}"))
.body(hyper_014::Body::from("in"))
.unwrap(),
)
.await
.unwrap();

assert_eq!(res.status(), StatusCode::OK);
assert_eq!(res.status(), http_02::StatusCode::OK);

let bytes = hyper_014::body::to_bytes(&mut res).await.unwrap();
assert_eq!(bytes, "out");
Expand Down

0 comments on commit 15eb88a

Please sign in to comment.