- Redirect
- uri
- Status
- SeeOther
- TODO
Rocket - Redirect to another page
- TODO: Implement the same in the separate files case.
- TODO: Redirect using other Status code
- TODO: Redirect to an external URL
- TODO: Optional redirction? (e.g. after successful login we redirect, but if it fails we would like to show the login page)
- TODO: Dynamic redirect. (e.g. after successful login we go to the page where the user really wanted to go)
examples/rocket/redirect-to-fixed-url/src/main.rs
#[macro_use] extern crate rocket; use rocket::response::Redirect; #[get("/")] fn index() -> &'static str { "Hello, world!" } #[get("/page")] fn page() -> &'static str { "A page" } #[get("/other")] fn other() -> Redirect { Redirect::to(uri!(page)) } #[launch] fn rocket() -> _ { rocket::build().mount("/", routes![index, page, other]) } #[cfg(test)] mod tests;
examples/rocket/redirect-to-fixed-url/src/tests.rs
use rocket::http::Status; use rocket::local::blocking::Client; #[test] fn hello_world() { 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("Hello, world!".into())); let response = client.get("/other").dispatch(); assert_eq!(response.status(), Status::SeeOther); assert_eq!(response.headers().get_one("Location").unwrap(), "/page"); assert_eq!(response.into_string(), None); //dbg!(response.headers()); }