codewars / runner

Issue tracker for Code Runner
34 stars 8 forks source link

Add rocket for Rust #292

Open andria-dev opened 10 months ago

andria-dev commented 10 months ago

Package Information

Code Example

In this example, we create a simple web server that serves Hello, world! when visiting the / route. In the test section, we launch and track the Rocket server with a blocking client, and we test that it is a valid Rocket instance with expect. Once that's ready, we do a get request to the hello (/) route and check that the status is correct and the response is the correct value.

#[macro_use] extern crate rocket;

#[get("/")]
fn hello() -> &'static str {
    "Hello, world!"
}

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

#[cfg(test)]
mod test {
    use super::rocket;
    use rocket::local::blocking::Client;
    use rocket::http::Status;

    #[test]
    fn hello_world() {
        let client = Client::tracked(rocket()).expect("valid rocket instance");
        let mut response = client.get(uri!(super::hello)).dispatch();
        assert_eq!(response.status(), Status::Ok);
        assert_eq!(response.into_string().unwrap(), "Hello, world!");
    }
}

An Aside

Rocket also has four other related packages: rocket_db_pools, rocket_dyn_templates, rocket_sync_db_pools, and rocket_ws. I believe that rocket_ws and rocket_dyn_templates could be very useful, but just the main rocket package would probably be a good start for now.

I would recommend also taking a look at Rocket's feature flags. At least json should be turned on.


:+1: reaction might help to get this request prioritized.