tomaka / rouille

Web framework in Rust
Apache License 2.0
1.12k stars 106 forks source link

Unit testing without running the server #282

Open szabgab opened 9 months ago

szabgab commented 9 months ago

I am aware of #106 but what I am interested is a way to run tests without running a server.

Rocket has something like this:

#[cfg(test)]
mod test {
    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.into_string(), Some("Hello, world!".into()));
    }
}

I have not seen if this possible with Rouille.

I tried to write something and got this:

#[macro_use]
extern crate rouille;

use rouille::{Server, Request, Response};

fn handle(req: &Request) -> Response {
    router!(req,
        (GET) (/) => {
            rouille::Response::html("Hello <b>world!</b>")
        },
        _ => rouille::Response::html("This page does <b>not</b> exist.").with_status_code(404)
    )
}

fn main() {
    let host = "localhost";
    let port = "8000";

    println!("Now listening on {host}:{port}");

    let server = Server::new(format!("{host}:{port}"), move |req| { handle(req) }).expect("Err");
    server.run()
}

#[test]
fn hello_world() {
    let server = Server::new(format!("localhost:8000"), move |req| { handle(req) }).expect("Err");
    let req = Request::fake_http(
        "GET",
        "/",
        vec![("Content-Type".to_owned(), "text/plain".to_owned())],
        b"test".to_vec());
    //server.server.incoming_requests(req);   
}

but I still don't have the response....