diff --git a/examples/hello_alt_methods/Cargo.toml b/examples/hello_alt_methods/Cargo.toml index 43746f1e..61fb6a6a 100644 --- a/examples/hello_alt_methods/Cargo.toml +++ b/examples/hello_alt_methods/Cargo.toml @@ -7,3 +7,6 @@ workspace = "../../" [dependencies] rocket = { path = "../../lib" } rocket_codegen = { path = "../../codegen" } + +[dev-dependencies] +rocket = { path = "../../lib", features = ["testing"] } diff --git a/examples/hello_alt_methods/src/main.rs b/examples/hello_alt_methods/src/main.rs index aeca2246..fbc8a7b1 100644 --- a/examples/hello_alt_methods/src/main.rs +++ b/examples/hello_alt_methods/src/main.rs @@ -7,6 +7,8 @@ use std::io; use rocket::response::NamedFile; +#[cfg(test)] mod tests; + #[get("/")] fn index() -> io::Result { NamedFile::open("static/index.html") diff --git a/examples/hello_alt_methods/src/tests.rs b/examples/hello_alt_methods/src/tests.rs new file mode 100644 index 00000000..1703900f --- /dev/null +++ b/examples/hello_alt_methods/src/tests.rs @@ -0,0 +1,25 @@ +use super::rocket; +use rocket::testing::MockRequest; +use rocket::http::Method; +use rocket::http::Status; + +fn test(method: Method, status: Status, body_prefix: Option<&str>) { + let rocket = rocket::ignite() + .mount("/", routes![super::index, super::put]); + + let mut req = MockRequest::new(method, "/"); + let mut response = req.dispatch_with(&rocket); + + assert_eq!(response.status(), status); + if let Some(expected_body_string) = body_prefix { + let body_str = response.body().and_then(|body| body.into_string()).unwrap(); + assert!(body_str.starts_with(expected_body_string)); + } +} + +#[test] +fn hello_world_alt_methods() { + test(Method::Get, Status::Ok, Some("")); + test(Method::Put, Status::Ok, Some("Hello, PUT request!")); + test(Method::Post, Status::NotFound, None); +}