Skip to content

Commit

Permalink
chore: remove is_valid_route function (#204)
Browse files Browse the repository at this point in the history
  • Loading branch information
elcharitas authored Oct 22, 2024
1 parent 39e1c2d commit 1291baa
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 37 deletions.
27 changes: 0 additions & 27 deletions crates/shared/src/server/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,19 +326,6 @@ impl NgynContext {
}
}

impl NgynContext {
/// Checks if the context has a valid route.
/// A valid route is when the route information and the params are set.
/// This is great for differentiating known routes from unknown routes.
///
/// ### Returns
///
/// `true` if the context has a valid route, `false` otherwise.
pub fn is_valid_route(&self) -> bool {
self.params.is_some()
}
}

impl NgynContext {
/// Creates a new `NgynContext` from the given request.
///
Expand Down Expand Up @@ -647,20 +634,6 @@ mod tests {
assert!(!context.has("age"));
}

#[test]
fn test_is_valid_route() {
let request = Request::new(Vec::new());
let mut context = NgynContext::from_request(request);
context.set("name", "John".to_string());

assert!(!context.is_valid_route());

let params = vec![("param1".to_string(), "value1".to_string())];
context.params = Some(params);

assert!(context.is_valid_route());
}

#[test]
fn test_with() {
let mut request = Request::new(Vec::new());
Expand Down
18 changes: 8 additions & 10 deletions examples/weather_app/src/middlewares/notfound_middleware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,14 @@ pub struct NotFoundMiddleware;

impl NgynMiddleware for NotFoundMiddleware {
async fn handle(&self, cx: &mut NgynContext, res: &mut NgynResponse) {
if cx.is_valid_route() {
return;
if cx.params().is_none() {
let body = json!({
"error": {
"status": 404, // this will be interpreted by the ResponseInterpreter, and set as the status code
"message": "Route not found",
}
});
*res.body_mut() = body.to_bytes().into();
}
*res.body_mut() = json!({
"error": {
"status": 404, // this will be interpreted by the ResponseInterpreter, and set as the status code
"message": "Route not found",
}
})
.to_bytes()
.into();
}
}

0 comments on commit 1291baa

Please sign in to comment.