Add tests for hello_alt_methods example.

This commit is contained in:
Robert 2016-12-29 20:06:42 -05:00 committed by Sergio Benitez
parent e230ce9b95
commit e4006c56c8
3 changed files with 30 additions and 0 deletions

View File

@ -7,3 +7,6 @@ workspace = "../../"
[dependencies] [dependencies]
rocket = { path = "../../lib" } rocket = { path = "../../lib" }
rocket_codegen = { path = "../../codegen" } rocket_codegen = { path = "../../codegen" }
[dev-dependencies]
rocket = { path = "../../lib", features = ["testing"] }

View File

@ -7,6 +7,8 @@ use std::io;
use rocket::response::NamedFile; use rocket::response::NamedFile;
#[cfg(test)] mod tests;
#[get("/")] #[get("/")]
fn index() -> io::Result<NamedFile> { fn index() -> io::Result<NamedFile> {
NamedFile::open("static/index.html") NamedFile::open("static/index.html")

View File

@ -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);
}