-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Route tracing #2167
Open
the10thWiz
wants to merge
7
commits into
rwf2:master
Choose a base branch
from
the10thWiz:route-tracing
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+332
−15
Open
Route tracing #2167
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9aa14d0
Add support for checking which route routed a request
the10thWiz 78aecfd
Documentation and minor fixes
the10thWiz 2bfdb53
Fix formatting
the10thWiz c410871
Fix typos and minor mistakes
the10thWiz ca8e409
Rename methods to make them easier to read
the10thWiz bd1d0f5
Implement tracing through the list of routes
the10thWiz 09ebdbd
Fix examples to use new names
the10thWiz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -180,6 +180,134 @@ macro_rules! pub_response_impl { | |
self._into_msgpack() $(.$suffix)? | ||
} | ||
|
||
/// Checks if a response was generted by a specific route type. This only returns true if the route | ||
/// actually generated the response, and a catcher was _not_ run. See [`was_attempted_by`] to | ||
/// check if a route was attempted, but may not have generated the response | ||
/// | ||
/// # Example | ||
/// | ||
/// ```rust | ||
/// # use rocket::{get, routes}; | ||
/// #[get("/")] | ||
/// fn index() -> &'static str { "Hello World" } | ||
#[doc = $doc_prelude] | ||
/// # Client::_test_with(|r| r.mount("/", routes![index]), |_, _, response| { | ||
/// let response: LocalResponse = response; | ||
/// assert!(response.was_routed_by::<index>()); | ||
/// # }); | ||
/// ``` | ||
/// | ||
/// # Other Route types | ||
/// | ||
/// [`FileServer`](crate::fs::FileServer) implementes `RouteType`, so a route that should | ||
/// return a static file can be checked against it. Libraries which provide custom Routes should | ||
/// implement `RouteType`, see [`RouteType`](crate::route::RouteType) for more information. | ||
pub fn was_routed_by<T: crate::route::RouteType>(&self) -> bool { | ||
// If this request was caught, the route in `.route()` did NOT generate this response. | ||
if self._request().catcher().is_some() { | ||
false | ||
} else if let Some(route_type) = self._request().route().map(|r| r.route_type).flatten() { | ||
route_type == std::any::TypeId::of::<T>() | ||
} else { | ||
false | ||
} | ||
} | ||
|
||
/// Checks if a request was routed to a specific route type. This will return true for routes | ||
/// that were attempted, _but not actually called_. This enables a test to verify that a route | ||
/// was attempted, even if another route actually generated the response, e.g. an | ||
/// authenticated route will typically defer to an error catcher if the request does not have | ||
/// the proper authentication. This makes it possible to verify that a request was routed to | ||
/// the authentication route, even if the response was eventaully generated by another route or | ||
/// a catcher. | ||
/// | ||
/// # Example | ||
/// | ||
// WARNING: this doc-test is NOT run, because cargo test --doc does not run doc-tests for items | ||
// only available during tests. | ||
/// ```rust | ||
/// # use rocket::{get, routes, async_trait, request::{Request, Outcome, FromRequest}}; | ||
/// # struct WillFail {} | ||
/// # #[async_trait] | ||
/// # impl<'r> FromRequest<'r> for WillFail { | ||
/// # type Error = (); | ||
/// # async fn from_request(request: &'r Request<'_>) -> Outcome<Self, Self::Error> { | ||
/// # Outcome::Forward(()) | ||
/// # } | ||
/// # } | ||
/// #[get("/", rank = 2)] | ||
/// fn index1(guard: WillFail) -> &'static str { "Hello World" } | ||
/// #[get("/")] | ||
/// fn index2() -> &'static str { "Hello World" } | ||
#[doc = $doc_prelude] | ||
/// # Client::_test_with(|r| r.mount("/", routes![index1, index2]), |_, _, response| { | ||
/// let response: LocalResponse = response; | ||
/// assert!(response.was_attempted_by::<index1>()); | ||
/// assert!(response.was_attempted_by::<index2>()); | ||
/// assert!(response.was_routed_by::<index2>()); | ||
/// # }); | ||
/// ``` | ||
/// | ||
/// # Other Route types | ||
/// | ||
/// [`FileServer`](crate::fs::FileServer) implementes `RouteType`, so a route that should | ||
/// return a static file can be checked against it. Libraries which provide custom Routes should | ||
/// implement `RouteType`, see [`RouteType`](crate::route::RouteType) for more information. | ||
/// | ||
/// # Note | ||
/// | ||
/// This method is marked as `cfg(test)`, and is therefore only available in unit and | ||
/// integration tests. This is because the list of routes attempted is only collected in these | ||
/// testing environments, to minimize performance impacts during normal operation. | ||
#[cfg(test)] | ||
Comment on lines
+259
to
+262
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is unfortunately not the way |
||
pub fn was_attempted_by<T: crate::route::RouteType>(&self) -> bool { | ||
self._request().route_path(|path| path.iter().any(|r| | ||
r.route_type == Some(std::any::TypeId::of::<T>()) | ||
)) | ||
} | ||
|
||
/// Checks if a route was caught by a specific route type | ||
/// | ||
/// # Example | ||
/// | ||
/// ```rust | ||
/// # use rocket::{catch, catchers}; | ||
/// #[catch(404)] | ||
/// fn default_404() -> &'static str { "Hello World" } | ||
#[doc = $doc_prelude] | ||
/// # Client::_test_with(|r| r.register("/", catchers![default_404]), |_, _, response| { | ||
/// let response: LocalResponse = response; | ||
/// assert!(response.was_caught_by::<default_404>()); | ||
/// # }); | ||
/// ``` | ||
/// | ||
/// # Rocket's default catcher | ||
/// | ||
/// The default catcher has a `CatcherType` of [`DefaultCatcher`](crate::catcher::DefaultCatcher) | ||
pub fn was_caught_by<T: crate::catcher::CatcherType>(&self) -> bool { | ||
if let Some(catcher_type) = self._request().catcher().map(|r| r.catcher_type).flatten() { | ||
catcher_type == std::any::TypeId::of::<T>() | ||
} else { | ||
false | ||
} | ||
} | ||
|
||
/// Checks if a route was caught by a catcher | ||
/// | ||
/// # Example | ||
/// | ||
/// ```rust | ||
/// # use rocket::get; | ||
#[doc = $doc_prelude] | ||
/// # Client::_test(|_, _, response| { | ||
/// let response: LocalResponse = response; | ||
/// assert!(response.was_caught()) | ||
/// # }); | ||
/// ``` | ||
pub fn was_caught(&self) -> bool { | ||
self._request().catcher().is_some() | ||
} | ||
|
||
#[cfg(test)] | ||
#[allow(dead_code)] | ||
fn _ensure_impls_exist() { | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very cool! I had to read about TypeId.