mirror of https://github.com/rwf2/Rocket.git
Add tests for hello_alt_methods example.
This commit is contained in:
parent
e230ce9b95
commit
e4006c56c8
|
@ -7,3 +7,6 @@ workspace = "../../"
|
|||
[dependencies]
|
||||
rocket = { path = "../../lib" }
|
||||
rocket_codegen = { path = "../../codegen" }
|
||||
|
||||
[dev-dependencies]
|
||||
rocket = { path = "../../lib", features = ["testing"] }
|
||||
|
|
|
@ -7,6 +7,8 @@ use std::io;
|
|||
|
||||
use rocket::response::NamedFile;
|
||||
|
||||
#[cfg(test)] mod tests;
|
||||
|
||||
#[get("/")]
|
||||
fn index() -> io::Result<NamedFile> {
|
||||
NamedFile::open("static/index.html")
|
||||
|
|
|
@ -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("<!DOCTYPE html>"));
|
||||
test(Method::Put, Status::Ok, Some("Hello, PUT request!"));
|
||||
test(Method::Post, Status::NotFound, None);
|
||||
}
|
Loading…
Reference in New Issue