Skip to content

Commit

Permalink
fix(tests): remove redundant tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fundon committed Dec 17, 2023
1 parent eea4090 commit d5d7281
Showing 1 changed file with 10 additions and 103 deletions.
113 changes: 10 additions & 103 deletions viz-core/tests/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,58 +4,11 @@
#![allow(clippy::wildcard_imports)]

use http_body_util::Full;
use std::marker::PhantomData;
use viz_core::handler::CatchError;
use viz_core::*;

#[tokio::test]
async fn handler() -> Result<()> {
struct CatchError<H, F, R, E> {
h: H,
f: F,
_marker: PhantomData<fn(E) -> R>,
}

impl<H: Clone, F: Clone, R, E> Clone for CatchError<H, F, R, E> {
fn clone(&self) -> Self {
Self {
h: self.h.clone(),
f: self.f.clone(),
_marker: PhantomData,
}
}
}

impl<H, F, R, E> CatchError<H, F, R, E> {
#[inline]
pub(crate) fn new(h: H, f: F) -> Self {
Self {
h,
f,
_marker: PhantomData,
}
}
}

#[async_trait]
impl<H, F, I, O, R, E> Handler<I> for CatchError<H, F, R, E>
where
I: Send + 'static,
H: Handler<I, Output = Result<O>> + Clone,
O: IntoResponse + Send,
E: std::error::Error + Send + 'static,
F: Handler<E, Output = R> + Clone,
R: IntoResponse + 'static,
{
type Output = Result<Response>;

async fn call(&self, i: I) -> Self::Output {
match self.h.call(i).await {
Ok(r) => Ok(r.into_response()),
Err(e) => Ok(self.f.call(e.downcast::<E>()?).await.into_response()),
}
}
}

trait HandlerPlus<I>: Handler<I> {
fn catch_error2<F, E, R>(self, f: F) -> CatchError<Self, F, E, R>
where
Expand Down Expand Up @@ -153,52 +106,34 @@ async fn handler() -> Result<()> {
res
}

async fn a(_req: Request) -> Result<Response> {
// Err(CustomError::NotFound.into())

async fn a(_: Request) -> Result<Response> {
Err(CustomError::NotFound)?;
Err(CustomError2::NotFound)?;
// Err(CustomError::NotFound)?;
Ok(().into_response())

// Err(CustomError2::NotFound.into())
}
async fn b(_req: Request) -> Result<Response> {
// Err("hello error".into())

async fn b(_: Request) -> Result<Response> {
Err(MyString("hello error".to_string()))?;
Ok(().into_response())
}
async fn c(_req: Request) -> Result<Response> {
// Ok(Response::new("hello".into()))
// Err(std::io::Error::new(std::io::ErrorKind::AlreadyExists, "file read failed").into())

// Err(std::io::Error::new(
// std::io::ErrorKind::AlreadyExists,
// "file read failed",
// ))?;
// Ok(().into_response())

async fn c(_: Request) -> Result<Response> {
Err((
std::io::Error::from(std::io::ErrorKind::AlreadyExists),
(StatusCode::INTERNAL_SERVER_ERROR, "file read failed"),
)
.into())
}
async fn d(_req: Request) -> Result<&'static str> {
async fn d(_: Request) -> Result<&'static str> {
Ok("hello")
}
async fn e(_req: Request) -> Result<impl IntoResponse> {
async fn e(_: Request) -> Result<impl IntoResponse> {
Ok("hello")
}
async fn f(_req: Request) -> Result<impl IntoResponse> {
async fn f(_: Request) -> Result<impl IntoResponse> {
Ok("world")
}
async fn g(_req: Request) -> Result<Vec<u8>> {
async fn g(_: Request) -> Result<Vec<u8>> {
Ok(vec![144, 233])
}
// async fn h() -> Vec<u8> {
// vec![144, 233]
// }
async fn h() -> Result<Vec<u8>> {
Err(CustomError::NotFound)?;
Ok(vec![144, 233])
Expand All @@ -215,14 +150,10 @@ async fn handler() -> Result<()> {
async fn l(a: MyU8, b: MyU8, _: MyString) -> Result<Response> {
Ok(vec![0, a.0, b.0].into_response())
}
async fn m(_a: MyU8, _b: MyU8, _c: MyString) -> Result<Response> {
// Err(CustomError::NotFound)?;
// Ok(().into_response())
async fn m(_: MyU8, _: MyU8, _: MyString) -> Result<Response> {
CustomError::NotFound.into()
}

// dbg!(FnExt::call(&h, Request::new(Body::empty())).await);

#[derive(Clone)]
struct MyBefore {
name: String,
Expand All @@ -242,15 +173,6 @@ async fn handler() -> Result<()> {
name: String,
}

// #[async_trait]
// impl<O: Send + 'static> Handler<O> for MyAfter {
// type Output = O;

// async fn call(&self, o: O) -> Self::Output {
// dbg!(&self.name);
// o
// }
// }
#[async_trait]
impl<O: Send + 'static> Handler<Result<O>> for MyAfter {
type Output = Result<O>;
Expand Down Expand Up @@ -323,7 +245,6 @@ async fn handler() -> Result<()> {
name: String::new(),
};

// let rha = Responder::new(a);
let rha = aa
.map_into_response()
.around(th.clone())
Expand Down Expand Up @@ -371,20 +292,6 @@ async fn handler() -> Result<()> {
.is_ok());

assert!(rhb.call(Request::default()).await.is_err());
// dbg!(rhc.call(Request::default()).await);
// dbg!(rhd.call(Request::default()).await);
// dbg!(rhe.call(Request::default()).await);
// dbg!(rhf.call(Request::default()).await);
// dbg!(rhg.call(Request::default()).await);
// dbg!(rhh.call(Request::default()).await);
// dbg!(Handler::call(&rhh, Request::default()).await);
// dbg!(rhi.call(Request::default()).await);
// dbg!(rhj.call(Request::default()).await);

// dbg!(Handler::call(&rhk, Request::default()).await);
// dbg!(Handler::call(&rhl, Request::default()).await);
// dbg!(Handler::call(&rhm, Request::default()).await);
// dbg!(Handler::call(&rhi, Request::default()).await);

let brha: BoxHandler = rha.boxed();
let brhb: BoxHandler = Box::new(rhb)
Expand Down

0 comments on commit d5d7281

Please sign in to comment.