mirror of https://github.com/rwf2/Rocket.git
Add tests for optional_result example.
This commit is contained in:
parent
55a2535896
commit
ab94e344b4
|
@ -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"] }
|
||||||
|
|
|
@ -3,6 +3,9 @@
|
||||||
|
|
||||||
extern crate rocket;
|
extern crate rocket;
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests;
|
||||||
|
|
||||||
#[get("/users/<name>")]
|
#[get("/users/<name>")]
|
||||||
fn user(name: &str) -> Option<&'static str> {
|
fn user(name: &str) -> Option<&'static str> {
|
||||||
if name == "Sergio" {
|
if name == "Sergio" {
|
||||||
|
|
|
@ -0,0 +1,24 @@
|
||||||
|
use super::rocket;
|
||||||
|
use rocket::testing::MockRequest;
|
||||||
|
use rocket::http::{Method, Status};
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_200() {
|
||||||
|
let rocket = rocket::ignite().mount("/", routes![super::user]);
|
||||||
|
let mut request = MockRequest::new(Method::Get, "/users/Sergio");
|
||||||
|
let mut response = request.dispatch_with(&rocket);
|
||||||
|
|
||||||
|
assert_eq!(response.status(), Status::Ok);
|
||||||
|
assert_eq!(response.body().and_then(|b| b.into_string()),
|
||||||
|
Some("Hello, Sergio!".to_string()));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_404() {
|
||||||
|
let rocket = rocket::ignite().mount("/", routes![super::user]);
|
||||||
|
let mut request = MockRequest::new(Method::Get, "/users/unknown");
|
||||||
|
let response = request.dispatch_with(&rocket);
|
||||||
|
|
||||||
|
// Only test the status because the body is the default 404.
|
||||||
|
assert_eq!(response.status(), Status::NotFound);
|
||||||
|
}
|
Loading…
Reference in New Issue