Skip to content

Unit testing without running the server #282

@szabgab

Description

@szabgab

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....

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions