mirror of https://github.com/rwf2/Rocket.git
Add tests for manual_routes example.
This commit is contained in:
parent
fc89d0e96c
commit
1683102e74
|
@ -5,3 +5,6 @@ workspace = "../../"
|
|||
|
||||
[dependencies]
|
||||
rocket = { path = "../../lib" }
|
||||
|
||||
[dev-dependencies]
|
||||
rocket = { path = "../../lib", features = ["testing"] }
|
||||
|
|
|
@ -1,5 +1,8 @@
|
|||
extern crate rocket;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
||||
use std::io;
|
||||
use std::fs::File;
|
||||
|
||||
|
@ -7,6 +10,7 @@ use rocket::{Request, Route, Data, Catcher, Error};
|
|||
use rocket::http::Status;
|
||||
use rocket::request::FromParam;
|
||||
use rocket::response::{self, Responder};
|
||||
use rocket::response::status::Custom;
|
||||
use rocket::handler::Outcome;
|
||||
use rocket::http::Method::*;
|
||||
|
||||
|
@ -23,7 +27,10 @@ fn name<'a>(req: &'a Request, _: Data) -> Outcome<'a> {
|
|||
}
|
||||
|
||||
fn echo_url(req: &Request, _: Data) -> Outcome<'static> {
|
||||
let param = req.uri().as_str().split_at(6).1;
|
||||
let param = req.uri()
|
||||
.as_str()
|
||||
.split_at(6)
|
||||
.1;
|
||||
Outcome::of(String::from_param(param).unwrap())
|
||||
}
|
||||
|
||||
|
@ -52,10 +59,10 @@ fn get_upload(_: &Request, _: Data) -> Outcome<'static> {
|
|||
}
|
||||
|
||||
fn not_found_handler<'r>(_: Error, req: &'r Request) -> response::Result<'r> {
|
||||
format!("Couldn't find: {}", req.uri()).respond()
|
||||
Custom(Status::NotFound, format!("Couldn't find: {}", req.uri())).respond()
|
||||
}
|
||||
|
||||
fn main() {
|
||||
fn rocket() -> rocket::Rocket {
|
||||
let always_forward = Route::ranked(1, Get, "/", forward);
|
||||
let hello = Route::ranked(2, Get, "/", hi);
|
||||
|
||||
|
@ -72,5 +79,8 @@ fn main() {
|
|||
.mount("/hello", vec![name.clone()])
|
||||
.mount("/hi", vec![name])
|
||||
.catch(vec![not_found_catcher])
|
||||
.launch();
|
||||
}
|
||||
|
||||
fn main() {
|
||||
rocket().launch();
|
||||
}
|
||||
|
|
|
@ -0,0 +1,63 @@
|
|||
use super::*;
|
||||
use rocket::testing::MockRequest;
|
||||
use rocket::http::{ContentType, Status};
|
||||
use rocket::http::Method::*;
|
||||
|
||||
fn test(uri: &str, content_type: ContentType, status: Status, body: String) {
|
||||
let rocket = rocket();
|
||||
let mut request = MockRequest::new(Get, uri).header(content_type);
|
||||
let mut response = request.dispatch_with(&rocket);
|
||||
|
||||
assert_eq!(response.status(), status);
|
||||
assert_eq!(response.body().and_then(|b| b.into_string()), Some(body));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_forward() {
|
||||
test("/", ContentType::Plain, Status::Ok, "Hello!".to_string());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_name() {
|
||||
for &name in &[("John"), ("Mike"), ("Angela")] {
|
||||
let uri = format!("/hello/{}", name);
|
||||
test(&uri, ContentType::Plain, Status::Ok, name.to_string());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_echo() {
|
||||
let echo = "echo text";
|
||||
let uri = format!("/echo:echo text");
|
||||
test(&uri, ContentType::Plain, Status::Ok, echo.to_string());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_upload() {
|
||||
let expected_body = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, \
|
||||
sed do eiusmod tempor incididunt ut labore et dolore \
|
||||
magna aliqua";
|
||||
let rocket = rocket();
|
||||
let mut request = MockRequest::new(Post, "/upload")
|
||||
.header(ContentType::Plain)
|
||||
.body(expected_body);
|
||||
let response = request.dispatch_with(&rocket);
|
||||
|
||||
assert_eq!(response.status(), Status::Ok);
|
||||
|
||||
let mut request = MockRequest::new(Get, "/upload");
|
||||
let mut response = request.dispatch_with(&rocket);
|
||||
|
||||
let expected = expected_body.to_string();
|
||||
assert_eq!(response.status(), Status::Ok);
|
||||
assert_eq!(response.body().and_then(|b| b.into_string()), Some(expected));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_not_found() {
|
||||
let uri = "/wrong_address";
|
||||
test(uri,
|
||||
ContentType::Plain,
|
||||
Status::NotFound,
|
||||
format!("Couldn't find: {}", uri));
|
||||
}
|
Loading…
Reference in New Issue