Rocket Userid in path



examples/rocket/userid-in-path/src/main.rs
#[macro_use]
extern crate rocket;

#[cfg(test)]
mod tests;

use rocket::response::content;

use rocket::request::FromParam;
#[derive(Debug)]
struct User {
    uid: usize,
}

impl<'r> FromParam<'r> for User {
    type Error = &'r str;

    fn from_param(param: &'r str) -> Result<Self, Self::Error> {
        rocket::info!("from_param: {:?}", param);
        match param.parse::<usize>() {
            Ok(uid) => {
                if uid < 10000 {
                    Ok(Self { uid })
                } else {
                    Err("bad uid")
                }
            }
            Err(_) => Err("not a usize"),
        }
    }
}

#[get("/")]
fn index() -> content::RawHtml<String> {
    let html = String::from(
        r#"
    <a href="/user/42">id 42</a><br>
    <a href="/user/10001">id 10001</a> (not in database)<br>
    <a href="/user/text">text</a><br>
    "#,
    );
    content::RawHtml(html)
}

#[get("/user/<user>")]
fn user(user: User) -> content::RawHtml<String> {
    rocket::info!("slug: {:?}", user);

    let html = format!("uid: {}", user.uid);

    content::RawHtml(html)
}

#[launch]
fn rocket() -> _ {
    rocket::build().mount("/", routes![index, user])
}

examples/rocket/userid-in-path/src/tests.rs
use rocket::http::Status;
use rocket::local::blocking::Client;

#[test]
fn check_index() {
    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/html; charset=utf-8"
    );
    let html = response.into_string().unwrap();
    assert!(html.contains(r#"<a href="/blog/main">main</a><br>"#));
    assert!(html.contains(r#"<a href="/blog/missing">missing</a><br>"#));
}

#[test]
fn check_main() {
    let client = Client::tracked(super::rocket()).unwrap();
    let response = client.get("/blog/main").dispatch();

    assert_eq!(response.status(), Status::Ok);
    assert_eq!(
        response.headers().get_one("Content-Type").unwrap(),
        "text/html; charset=utf-8"
    );
    let html = response.into_string().unwrap();
    //println!("{html}");
    assert!(html.contains(r#"content: Main page"#));
}

#[test]
fn check_about() {
    let client = Client::tracked(super::rocket()).unwrap();
    let response = client.get("/blog/about").dispatch();

    assert_eq!(response.status(), Status::Ok);
    assert_eq!(
        response.headers().get_one("Content-Type").unwrap(),
        "text/html; charset=utf-8"
    );
    let html = response.into_string().unwrap();
    assert!(html.contains(r#"content: About page"#));
}

#[test]
fn check_missing_page() {
    let client = Client::tracked(super::rocket()).unwrap();
    let response = client.get("/blog/other").dispatch();

    assert_eq!(response.status(), Status::UnprocessableEntity);
    assert_eq!(
        response.headers().get_one("Content-Type").unwrap(),
        "text/html; charset=utf-8"
    );
    let html = response.into_string().unwrap();
    println!("{html}");
    assert!(html.contains(r#"<h1>422: Unprocessable Entity</h1>"#));
}