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

Allow route based function middlewares #600

Merged
merged 2 commits into from
Jun 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 3 additions & 3 deletions src/route.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,11 @@ impl<'a, State: 'static> Route<'a, State> {
/// Apply the given middleware to the current route.
pub fn middleware<M>(&mut self, middleware: M) -> &mut Self
where
M: Middleware<State> + Debug,
M: Middleware<State>,
{
log::trace!(
"Adding middleware {:?} to route {:?}",
middleware,
"Adding middleware {} to route {:?}",
middleware.name(),
self.path
);
self.middleware.push(Arc::new(middleware));
Expand Down
84 changes: 84 additions & 0 deletions tests/function_middleware.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
use test_utils::BoxFuture;
use tide::http::{self, url::Url, Method};

mod test_utils;

fn auth_middleware<'a>(
request: tide::Request<()>,
next: tide::Next<'a, ()>,
) -> BoxFuture<'a, tide::Result> {
Box::pin(async {
let authenticated = match request.header("X-Auth") {
Some(header) => header == "secret_key",
None => false,
};

if authenticated {
next.run(request).await
} else {
Ok(tide::Response::new(tide::StatusCode::Unauthorized))
}
})
}

async fn echo_path<State>(req: tide::Request<State>) -> tide::Result<String> {
Ok(req.url().path().to_string())
}

#[async_std::test]
async fn route_middleware() {
let mut app = tide::new();
app.at("/protected")
.middleware(auth_middleware)
.get(echo_path);
app.at("/unprotected").get(echo_path);

// Protected
let req = http::Request::new(
Method::Get,
Url::parse("http://localhost/protected").unwrap(),
);
let res: http::Response = app.respond(req).await.unwrap();
assert_eq!(res.status(), tide::StatusCode::Unauthorized);

let mut req = http::Request::new(
Method::Get,
Url::parse("http://localhost/protected").unwrap(),
);
req.insert_header("X-Auth", "secret_key");
let res: http::Response = app.respond(req).await.unwrap();
assert_eq!(res.status(), tide::StatusCode::Ok);

// Unprotected
let req = http::Request::new(
Method::Get,
Url::parse("http://localhost/unprotected").unwrap(),
);
let res: http::Response = app.respond(req).await.unwrap();
assert_eq!(res.status(), tide::StatusCode::Ok);

let mut req = http::Request::new(
Method::Get,
Url::parse("http://localhost/unprotected").unwrap(),
);
req.insert_header("X-Auth", "secret_key");
let res: http::Response = app.respond(req).await.unwrap();
assert_eq!(res.status(), tide::StatusCode::Ok);
}

#[async_std::test]
async fn app_middleware() {
let mut app = tide::new();
app.middleware(auth_middleware);
app.at("/foo").get(echo_path);

// Foo
let req = http::Request::new(Method::Get, Url::parse("http://localhost/foo").unwrap());
let res: http::Response = app.respond(req).await.unwrap();
assert_eq!(res.status(), tide::StatusCode::Unauthorized);

let mut req = http::Request::new(Method::Get, Url::parse("http://localhost/foo").unwrap());
req.insert_header("X-Auth", "secret_key");
let res: http::Response = app.respond(req).await.unwrap();
assert_eq!(res.status(), tide::StatusCode::Ok);
}