From d5d72816c0b6dcbbd88ccc7521668fb721f7355d Mon Sep 17 00:00:00 2001 From: Fangdun Tsai Date: Sun, 17 Dec 2023 19:48:12 +0800 Subject: [PATCH] fix(tests): remove redundant tests --- viz-core/tests/handler.rs | 113 ++++---------------------------------- 1 file changed, 10 insertions(+), 103 deletions(-) diff --git a/viz-core/tests/handler.rs b/viz-core/tests/handler.rs index 90d25006..1976d5c1 100644 --- a/viz-core/tests/handler.rs +++ b/viz-core/tests/handler.rs @@ -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: H, - f: F, - _marker: PhantomData R>, - } - - impl Clone for CatchError { - fn clone(&self) -> Self { - Self { - h: self.h.clone(), - f: self.f.clone(), - _marker: PhantomData, - } - } - } - - impl CatchError { - #[inline] - pub(crate) fn new(h: H, f: F) -> Self { - Self { - h, - f, - _marker: PhantomData, - } - } - } - - #[async_trait] - impl Handler for CatchError - where - I: Send + 'static, - H: Handler> + Clone, - O: IntoResponse + Send, - E: std::error::Error + Send + 'static, - F: Handler + Clone, - R: IntoResponse + 'static, - { - type Output = Result; - - 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::()?).await.into_response()), - } - } - } - trait HandlerPlus: Handler { fn catch_error2(self, f: F) -> CatchError where @@ -153,52 +106,34 @@ async fn handler() -> Result<()> { res } - async fn a(_req: Request) -> Result { - // Err(CustomError::NotFound.into()) - + async fn a(_: Request) -> Result { + Err(CustomError::NotFound)?; Err(CustomError2::NotFound)?; - // Err(CustomError::NotFound)?; Ok(().into_response()) - - // Err(CustomError2::NotFound.into()) } - async fn b(_req: Request) -> Result { - // Err("hello error".into()) - + async fn b(_: Request) -> Result { Err(MyString("hello error".to_string()))?; Ok(().into_response()) } - async fn c(_req: Request) -> Result { - // 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 { 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 { + async fn e(_: Request) -> Result { Ok("hello") } - async fn f(_req: Request) -> Result { + async fn f(_: Request) -> Result { Ok("world") } - async fn g(_req: Request) -> Result> { + async fn g(_: Request) -> Result> { Ok(vec![144, 233]) } - // async fn h() -> Vec { - // vec![144, 233] - // } async fn h() -> Result> { Err(CustomError::NotFound)?; Ok(vec![144, 233]) @@ -215,14 +150,10 @@ async fn handler() -> Result<()> { async fn l(a: MyU8, b: MyU8, _: MyString) -> Result { Ok(vec![0, a.0, b.0].into_response()) } - async fn m(_a: MyU8, _b: MyU8, _c: MyString) -> Result { - // Err(CustomError::NotFound)?; - // Ok(().into_response()) + async fn m(_: MyU8, _: MyU8, _: MyString) -> Result { CustomError::NotFound.into() } - // dbg!(FnExt::call(&h, Request::new(Body::empty())).await); - #[derive(Clone)] struct MyBefore { name: String, @@ -242,15 +173,6 @@ async fn handler() -> Result<()> { name: String, } - // #[async_trait] - // impl Handler for MyAfter { - // type Output = O; - - // async fn call(&self, o: O) -> Self::Output { - // dbg!(&self.name); - // o - // } - // } #[async_trait] impl Handler> for MyAfter { type Output = Result; @@ -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()) @@ -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)