Skip to content

Commit

Permalink
remove dependency on hyper
Browse files Browse the repository at this point in the history
  • Loading branch information
davidpdrsn committed Mar 24, 2023
1 parent 6c76166 commit 35c9aed
Show file tree
Hide file tree
Showing 21 changed files with 222 additions and 79 deletions.
2 changes: 1 addition & 1 deletion tower-http/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ bytes = "1"
flate2 = "1.0"
brotli = "3"
futures = "0.3"
hyper = { version = "0.14", features = ["full"] }
once_cell = "1"
tokio = { version = "1", features = ["full"] }
tower = { version = "0.4.10", features = ["buffer", "util", "retry", "make", "timeout"] }
tracing-subscriber = "0.3"
uuid = { version = "1.0", features = ["v4"] }
serde_json = "1.0"
sync_wrapper = "0.1.1"
zstd = "0.11"

[features]
Expand Down
2 changes: 1 addition & 1 deletion tower-http/src/add_extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,8 @@ where
mod tests {
#[allow(unused_imports)]
use super::*;
use crate::test_helpers::Body;
use http::Response;
use hyper::Body;
use std::{convert::Infallible, sync::Arc};
use tower::{service_fn, ServiceBuilder, ServiceExt};

Expand Down
9 changes: 4 additions & 5 deletions tower-http/src/auth/add_authorization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,12 +184,11 @@ where

#[cfg(test)]
mod tests {
use crate::validate_request::ValidateRequestHeaderLayer;

#[allow(unused_imports)]
use super::*;
use crate::test_helpers::Body;
use crate::validate_request::ValidateRequestHeaderLayer;
use http::{Response, StatusCode};
use hyper::Body;
use std::convert::Infallible;
use tower::{BoxError, Service, ServiceBuilder, ServiceExt};

#[tokio::test]
Expand Down Expand Up @@ -242,7 +241,7 @@ mod tests {
let auth = request.headers().get(http::header::AUTHORIZATION).unwrap();
assert!(auth.is_sensitive());

Ok::<_, hyper::Error>(Response::new(Body::empty()))
Ok::<_, Infallible>(Response::new(Body::empty()))
});

let mut client = AddAuthorization::bearer(svc, "foo").as_sensitive(true);
Expand Down
2 changes: 1 addition & 1 deletion tower-http/src/auth/async_require_authorization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,9 +307,9 @@ where
mod tests {
#[allow(unused_imports)]
use super::*;
use crate::test_helpers::Body;
use futures_util::future::BoxFuture;
use http::{header, StatusCode};
use hyper::Body;
use tower::{BoxError, ServiceBuilder, ServiceExt};

#[derive(Clone, Copy)]
Expand Down
2 changes: 1 addition & 1 deletion tower-http/src/auth/require_authorization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,8 @@ mod tests {

#[allow(unused_imports)]
use super::*;
use crate::test_helpers::Body;
use http::header;
use hyper::Body;
use tower::{BoxError, ServiceBuilder, ServiceExt};
use tower_service::Service;

Expand Down
7 changes: 4 additions & 3 deletions tower-http/src/catch_panic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,8 @@ mod tests {
#![allow(unreachable_code)]

use super::*;
use hyper::{Body, Response};
use crate::test_helpers::Body;
use http::Response;
use std::convert::Infallible;
use tower::{ServiceBuilder, ServiceExt};

Expand All @@ -377,7 +378,7 @@ mod tests {
let res = svc.oneshot(req).await.unwrap();

assert_eq!(res.status(), StatusCode::INTERNAL_SERVER_ERROR);
let body = hyper::body::to_bytes(res).await.unwrap();
let body = crate::test_helpers::to_bytes(res).await.unwrap();
assert_eq!(&body[..], b"Service panicked");
}

Expand All @@ -395,7 +396,7 @@ mod tests {
let res = svc.oneshot(req).await.unwrap();

assert_eq!(res.status(), StatusCode::INTERNAL_SERVER_ERROR);
let body = hyper::body::to_bytes(res).await.unwrap();
let body = crate::test_helpers::to_bytes(res).await.unwrap();
assert_eq!(&body[..], b"Service panicked");
}
}
2 changes: 1 addition & 1 deletion tower-http/src/classify/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ impl fmt::Display for ServerErrorsFailureClass {
mod usable_for_retries {
#[allow(unused_imports)]
use super::*;
use hyper::{Request, Response};
use http::{Request, Response};
use tower::retry::Policy;

trait IsRetryable {
Expand Down
4 changes: 2 additions & 2 deletions tower-http/src/compression/layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,9 @@ impl CompressionLayer {
#[cfg(test)]
mod tests {
use super::*;
use crate::test_helpers::Body;
use http::{header::ACCEPT_ENCODING, Request, Response};
use http_body::Body as _;
use hyper::Body;
use tokio::fs::File;
// for Body::data
use bytes::{Bytes, BytesMut};
Expand All @@ -139,7 +139,7 @@ mod tests {
// Convert the file into a `Stream`.
let stream = ReaderStream::new(file);
// Convert the `Stream` into a `Body`.
let body = Body::wrap_stream(stream);
let body = Body::from_stream(stream);
// Create response.
Ok(Response::new(body))
}
Expand Down
26 changes: 8 additions & 18 deletions tower-http/src/compression/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,17 +87,19 @@ mod tests {
use crate::compression::predicate::SizeAbove;

use super::*;
use crate::test_helpers::Body;
use async_compression::tokio::write::{BrotliDecoder, BrotliEncoder};
use bytes::BytesMut;
use flate2::read::GzDecoder;
use http::header::{ACCEPT_ENCODING, CONTENT_ENCODING, CONTENT_TYPE};
use http::{Request, Response};
use http_body::Body as _;
use hyper::{Body, Error, Request, Response, Server};
use std::convert::Infallible;
use std::io::Read;
use std::sync::{Arc, RwLock};
use std::{io::Read, net::SocketAddr};
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio_util::io::StreamReader;
use tower::{make::Shared, service_fn, Service, ServiceExt};
use tower::{service_fn, Service, ServiceExt};

// Compression filter allows every other request to be compressed
#[derive(Clone)]
Expand Down Expand Up @@ -171,18 +173,6 @@ mod tests {
assert_eq!(decompressed, "Hello, World!");
}

#[allow(dead_code)]
async fn is_compatible_with_hyper() {
let svc = service_fn(handle);
let svc = Compression::new(svc);

let make_service = Shared::new(svc);

let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
let server = Server::bind(&addr).serve(make_service);
server.await.unwrap();
}

#[tokio::test]
async fn no_recompress() {
const DATA: &str = "Hello, World! I'm already compressed with br!";
Expand Down Expand Up @@ -247,7 +237,7 @@ mod tests {
assert_eq!(data, DATA.as_bytes());
}

async fn handle(_req: Request<Body>) -> Result<Response<Body>, Error> {
async fn handle(_req: Request<Body>) -> Result<Response<Body>, Infallible> {
Ok(Response::new(Body::from("Hello, World!")))
}

Expand Down Expand Up @@ -317,7 +307,7 @@ mod tests {

#[tokio::test]
async fn doesnt_compress_images() {
async fn handle(_req: Request<Body>) -> Result<Response<Body>, Error> {
async fn handle(_req: Request<Body>) -> Result<Response<Body>, Infallible> {
let mut res = Response::new(Body::from(
"a".repeat((SizeAbove::DEFAULT_MIN_SIZE * 2) as usize),
));
Expand All @@ -342,7 +332,7 @@ mod tests {

#[tokio::test]
async fn does_compress_svg() {
async fn handle(_req: Request<Body>) -> Result<Response<Body>, Error> {
async fn handle(_req: Request<Body>) -> Result<Response<Body>, Infallible> {
let mut res = Response::new(Body::from(
"a".repeat((SizeAbove::DEFAULT_MIN_SIZE * 2) as usize),
));
Expand Down
17 changes: 5 additions & 12 deletions tower-http/src/decompression/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,15 @@ pub use self::request::service::RequestDecompression;

#[cfg(test)]
mod tests {
use std::convert::Infallible;

use super::*;
use crate::compression::Compression;
use crate::test_helpers::Body;
use bytes::BytesMut;
use http::Request;
use http::Response;
use http_body::Body as _;
use hyper::{Body, Client, Error, Request};
use tower::{service_fn, Service, ServiceExt};

#[tokio::test]
Expand All @@ -145,17 +148,7 @@ mod tests {
assert_eq!(decompressed_data, "Hello, World!");
}

async fn handle(_req: Request<Body>) -> Result<Response<Body>, Error> {
async fn handle(_req: Request<Body>) -> Result<Response<Body>, Infallible> {
Ok(Response::new(Body::from("Hello, World!")))
}

#[allow(dead_code)]
async fn is_compatible_with_hyper() {
let mut client = Decompression::new(Client::new());

let req = Request::new(Body::empty());

let _: Response<DecompressionBody<Body>> =
client.ready().await.unwrap().call(req).await.unwrap();
}
}
27 changes: 7 additions & 20 deletions tower-http/src/decompression/request/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@ pub(super) mod service;
mod tests {
use super::service::RequestDecompression;
use crate::decompression::DecompressionBody;
use crate::test_helpers::Body;
use bytes::BytesMut;
use flate2::{write::GzEncoder, Compression};
use http::{header, Response, StatusCode};
use http::{header, Request, Response, StatusCode};
use http_body::Body as _;
use hyper::{Body, Error, Request, Server};
use std::io::Write;
use std::net::SocketAddr;
use tower::{make::Shared, service_fn, Service, ServiceExt};
use std::{convert::Infallible, io::Write};
use tower::{service_fn, Service, ServiceExt};

#[tokio::test]
async fn decompress_accepted_encoding() {
Expand Down Expand Up @@ -48,7 +47,7 @@ mod tests {

async fn assert_request_is_decompressed(
req: Request<DecompressionBody<Body>>,
) -> Result<Response<Body>, Error> {
) -> Result<Response<Body>, Infallible> {
let (parts, mut body) = req.into_parts();
let body = read_body(&mut body).await;

Expand All @@ -60,7 +59,7 @@ mod tests {

async fn assert_request_is_passed_through(
req: Request<DecompressionBody<Body>>,
) -> Result<Response<Body>, Error> {
) -> Result<Response<Body>, Infallible> {
let (parts, mut body) = req.into_parts();
let body = read_body(&mut body).await;

Expand All @@ -72,7 +71,7 @@ mod tests {

async fn should_not_be_called(
_: Request<DecompressionBody<Body>>,
) -> Result<Response<Body>, Error> {
) -> Result<Response<Body>, Infallible> {
panic!("Inner service should not be called");
}

Expand All @@ -94,16 +93,4 @@ mod tests {
}
data.freeze().to_vec()
}

#[allow(dead_code)]
async fn is_compatible_with_hyper() {
let svc = service_fn(assert_request_is_decompressed);
let svc = RequestDecompression::new(svc);

let make_service = Shared::new(svc);

let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
let server = Server::bind(&addr).serve(make_service);
server.await.unwrap();
}
}
3 changes: 2 additions & 1 deletion tower-http/src/follow_redirect/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,8 @@ fn resolve_uri(relative: &str, base: &Uri) -> Option<Uri> {
#[cfg(test)]
mod tests {
use super::{policy::*, *};
use hyper::{header::LOCATION, Body};
use crate::test_helpers::Body;
use http::header::LOCATION;
use std::convert::Infallible;
use tower::{ServiceBuilder, ServiceExt};

Expand Down
3 changes: 3 additions & 0 deletions tower-http/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,9 @@
#[macro_use]
pub(crate) mod macros;

#[cfg(test)]
mod test_helpers;

#[cfg(feature = "auth")]
pub mod auth;

Expand Down
4 changes: 2 additions & 2 deletions tower-http/src/metrics/in_flight_requests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,8 +297,8 @@ where
mod tests {
#[allow(unused_imports)]
use super::*;
use crate::test_helpers::Body;
use http::Request;
use hyper::Body;
use tower::{BoxError, ServiceBuilder};

#[tokio::test]
Expand All @@ -325,7 +325,7 @@ mod tests {
assert_eq!(counter.get(), 1);

let body = response.into_body();
hyper::body::to_bytes(body).await.unwrap();
crate::test_helpers::to_bytes(body).await.unwrap();
assert_eq!(counter.get(), 0);
}

Expand Down
3 changes: 2 additions & 1 deletion tower-http/src/request_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -481,8 +481,9 @@ impl MakeRequestId for MakeRequestUuid {

#[cfg(test)]
mod tests {
use crate::test_helpers::Body;
use crate::ServiceBuilderExt as _;
use hyper::{Body, Response};
use http::Response;
use std::{
convert::Infallible,
sync::{
Expand Down
6 changes: 3 additions & 3 deletions tower-http/src/services/fs/serve_dir/tests.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use crate::services::{ServeDir, ServeFile};
use crate::test_helpers::{to_bytes, Body};
use brotli::BrotliDecompress;
use bytes::Bytes;
use flate2::bufread::{DeflateDecoder, GzDecoder};
use http::header::ALLOW;
use http::{header, Method, Response};
use http::{Request, StatusCode};
use http_body::Body as HttpBody;
use hyper::Body;
use std::convert::Infallible;
use std::io::{self, Read};
use tower::{service_fn, ServiceExt};
Expand Down Expand Up @@ -404,7 +404,7 @@ where
B: HttpBody<Data = bytes::Bytes> + Unpin,
B::Error: std::fmt::Debug,
{
let bytes = hyper::body::to_bytes(body).await.unwrap();
let bytes = to_bytes(body).await.unwrap();
String::from_utf8(bytes.to_vec()).unwrap()
}

Expand Down Expand Up @@ -474,7 +474,7 @@ async fn read_partial_in_bounds() {
)));
assert_eq!(res.headers()["content-type"], "text/markdown");

let body = hyper::body::to_bytes(res.into_body()).await.ok().unwrap();
let body = to_bytes(res.into_body()).await.ok().unwrap();
let source = Bytes::from(file_contents[bytes_start_incl..=bytes_end_incl].to_vec());
assert_eq!(body, source);
}
Expand Down
2 changes: 1 addition & 1 deletion tower-http/src/services/fs/serve_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,14 @@ where
#[cfg(test)]
mod tests {
use crate::services::ServeFile;
use crate::test_helpers::Body;
use brotli::BrotliDecompress;
use flate2::bufread::DeflateDecoder;
use flate2::bufread::GzDecoder;
use http::header;
use http::Method;
use http::{Request, StatusCode};
use http_body::Body as _;
use hyper::Body;
use mime::Mime;
use std::io::Read;
use std::str::FromStr;
Expand Down
Loading

0 comments on commit 35c9aed

Please sign in to comment.