Replies: 2 comments 1 reply
-
Hi. I use the testcontainers crate in order to create a database for tests. This creates a clean instance for every test. In this file at the bottom you can see how to setup the database with the pre-existing SurrealDB module. Hope that helps. Mike |
Beta Was this translation helpful? Give feedback.
-
To answer your question about multiple request guards, one of the things i have done in the past is this: /// Wrapper for a request guard to make it forward on error instead of failing.
///
/// [`Deref`] has been implemented so use should be transparent.
pub struct Forwarding<T: for<'a> FromRequest<'a>>(T);
#[rocket::async_trait]
impl<T: for<'a> FromRequest<'a>, 'r> FromRequest<'r> for Forwarding<T> {
type Error = Status;
async fn from_request(req: &'r Request<'_>) -> Outcome<Self, Self::Error> {
match T::from_request(req).await {
Outcome::Success(s) => Outcome::Success(Forwarding(s)),
Outcome::Forward(f) => Outcome::Forward(f),
Outcome::Error((f, _)) => Outcome::Forward(f),
}
}
}
impl<T: for<'a> FromRequest<'a>> Deref for Forwarding<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.0
}
} The usage of this is then: #[get("/thing/<user>")]
fn do_thing(user: Forwarding<SomeOtherGuard>) -> &'static str {
[...] This also prevents you having to repeat work in multiple guards, since each wrapper guard has access to the successful response from the guard it is wrapping. This guard is obviously very simple, but you could easily modify it to do additional checks and logic before returning its reponse |
Beta Was this translation helpful? Give feedback.
-
Hi,
I am working on an Open Source platform called Meet-OS to allow people to organize events (aka meetups). I am using Rocket and I'd love to get your feedback and suggestions for improvements.
The source code is here and there is a development server that one can try with without local installation.
I am sure there are plenty of places where the Rocket-related code can be improved, but there are two specific areas that I'd love to get suggestions or even Pull-Requests.
One of them is access the database directly from the tests. So far I could not figure out how to access the database directly from the tests and so the test helper functions that create the fixtures use the Rocket routes that makes the test slower than they could be.
The other one is the use of Multiple guards for checking if the current visitor is logged in and if she is the administrator of the site. I wonder what would be the idiomatic way to do that.
Beta Was this translation helpful? Give feedback.
All reactions