Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit testing without running the server #282

Open
szabgab opened this issue Jan 1, 2024 · 0 comments
Open

Unit testing without running the server #282

szabgab opened this issue Jan 1, 2024 · 0 comments

Comments

@szabgab
Copy link

szabgab commented Jan 1, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant