Rocket - Redirect to another page



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());
}