Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement reset_middleware in Server #855

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
15 changes: 15 additions & 0 deletions tests/route_middleware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A test should be added to ensure that previously defined routes on the app (i.e. those before line 136) still use the initially set middleware.