From 300bb766b8dcd2b3fa84251dcaf443ef675045c7 Mon Sep 17 00:00:00 2001 From: yugo-horie Date: Tue, 19 Oct 2021 11:35:01 +0900 Subject: [PATCH 1/2] Implement reset_middleware in Server --- src/server.rs | 8 ++++++++ tests/route_middleware.rs | 15 +++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/src/server.rs b/src/server.rs index 1e6f8c1ab..356b7320f 100644 --- a/src/server.rs +++ b/src/server.rs @@ -188,6 +188,14 @@ where self } + /// Reset the middleware chain for the current server, if any. + pub fn reset_middleware(&mut self) -> &mut Self { + let m = Arc::get_mut(&mut self.middleware) + .expect("Registering middleware is not possible after the Server has started"); + m.clear(); + self + } + /// Asynchronously serve the app with the supplied listener. /// /// This is a shorthand for calling `Server::bind`, logging the `ListenInfo` diff --git a/tests/route_middleware.rs b/tests/route_middleware.rs index ba89a15d8..4caa2805a 100644 --- a/tests/route_middleware.rs +++ b/tests/route_middleware.rs @@ -128,3 +128,18 @@ async fn subroute_not_nested() -> tide::Result<()> { assert_eq!(res["x-child"], "child"); Ok(()) } + +#[async_std::test] +async fn app_middleware_reset() -> tide::Result<()> { + let mut app = tide::new(); + app.with(TestMiddleware::with_header_name("X-Root", "root")); + app.reset_middleware(); + app.at("/foo") + .with(TestMiddleware::with_header_name("X-Foo", "foo")) + .get(echo_path); + + let res = app.get("/foo").await?; + assert!(res.header("X-Root").is_none()); + assert_eq!(res["X-Foo"], "foo"); + Ok(()) +} From 36280c85fdbb2009b491e88385bcc4b4d3eca480 Mon Sep 17 00:00:00 2001 From: u5surf Date: Sat, 27 Nov 2021 07:58:29 +0900 Subject: [PATCH 2/2] Fix test to ensure previously defined route --- tests/route_middleware.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/tests/route_middleware.rs b/tests/route_middleware.rs index 4caa2805a..47a063101 100644 --- a/tests/route_middleware.rs +++ b/tests/route_middleware.rs @@ -132,7 +132,9 @@ async fn subroute_not_nested() -> tide::Result<()> { #[async_std::test] async fn app_middleware_reset() -> tide::Result<()> { let mut app = tide::new(); - app.with(TestMiddleware::with_header_name("X-Root", "root")); + app.at("/") + .with(TestMiddleware::with_header_name("X-Root", "root")) + .get(echo_path); app.reset_middleware(); app.at("/foo") .with(TestMiddleware::with_header_name("X-Foo", "foo")) @@ -141,5 +143,9 @@ async fn app_middleware_reset() -> tide::Result<()> { let res = app.get("/foo").await?; assert!(res.header("X-Root").is_none()); assert_eq!(res["X-Foo"], "foo"); + + let res = app.get("/").await?; + assert!(res.header("X-Foo").is_none()); + assert_eq!(res["X-Root"], "root"); Ok(()) }