- TODO
Rocket - Two applications in separate files
- We created a separate file with its own routes
- We then mounted it under a path called /blog
- We provide a function called routes listing all the routes in this applcation and use that in the mount.
Limitation of this solution:
- in the blog_test we need to use super::super::rocket() instead of super::rocket().
- in the blog_test we need to access /blog that mean we need to know where it will be mounted.
examples/rocket/separate-files/src/main.rs
#[macro_use] extern crate rocket; pub(crate) mod blog; #[get("/")] fn index() -> &'static str { "Main page" } #[launch] fn rocket() -> _ { rocket::build() .mount("/", routes![index]) .mount("/blog", blog::routes()) } #[cfg(test)] mod tests;
examples/rocket/separate-files/src/tests.rs
use rocket::http::Status; use rocket::local::blocking::Client; #[test] fn check_main() { let client = Client::tracked(super::rocket()).unwrap(); let response = client.get("/").dispatch(); assert_eq!(response.status(), Status::Ok); assert_eq!( response.headers().get_one("Content-Type").unwrap(), "text/plain; charset=utf-8" ); assert_eq!(response.into_string(), Some("Main page".into())); }
examples/rocket/separate-files/src/blog.rs
use rocket::Route; pub fn routes() -> Vec<Route> { routes![index] } #[get("/")] pub fn index() -> &'static str { "Blog" } #[cfg(test)] mod blog_tests;
examples/rocket/separate-files/src/blog/blog_tests.rs
use rocket::http::Status; use rocket::local::blocking::Client; #[test] fn check_blog() { let client = Client::tracked(super::super::rocket()).unwrap(); let response = client.get("/blog").dispatch(); assert_eq!(response.status(), Status::Ok); assert_eq!( response.headers().get_one("Content-Type").unwrap(), "text/plain; charset=utf-8" ); assert_eq!(response.into_string(), Some("Blog".into())); }