mirror of https://github.com/rwf2/Rocket.git
Add tests for 'raw_upload' example.
This commit is contained in:
parent
732cb26eb5
commit
6a7903a220
|
@ -3,14 +3,14 @@
|
||||||
|
|
||||||
extern crate rocket;
|
extern crate rocket;
|
||||||
|
|
||||||
use std::io;
|
#[cfg(test)] mod tests;
|
||||||
|
|
||||||
|
use std::io;
|
||||||
use rocket::Data;
|
use rocket::Data;
|
||||||
use rocket::response::content::Plain;
|
|
||||||
|
|
||||||
#[post("/upload", format = "text/plain", data = "<data>")]
|
#[post("/upload", format = "text/plain", data = "<data>")]
|
||||||
fn upload(data: Data) -> io::Result<Plain<String>> {
|
fn upload(data: Data) -> io::Result<String> {
|
||||||
data.stream_to_file("/tmp/upload.txt").map(|n| Plain(n.to_string()))
|
data.stream_to_file("/tmp/upload.txt").map(|n| n.to_string())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/")]
|
#[get("/")]
|
||||||
|
@ -18,6 +18,10 @@ fn index() -> &'static str {
|
||||||
"Upload your text files by POSTing them to /upload."
|
"Upload your text files by POSTing them to /upload."
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn rocket() -> rocket::Rocket {
|
||||||
rocket::ignite().mount("/", routes![index, upload]).launch();
|
rocket::ignite().mount("/", routes![index, upload])
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
rocket().launch();
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,36 @@
|
||||||
|
use rocket::testing::MockRequest;
|
||||||
|
use rocket::http::{Status, ContentType};
|
||||||
|
use rocket::http::Method::*;
|
||||||
|
|
||||||
|
use std::io::Read;
|
||||||
|
use std::fs::File;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_index() {
|
||||||
|
let rocket = super::rocket();
|
||||||
|
let mut req = MockRequest::new(Get, "/");
|
||||||
|
let mut res = req.dispatch_with(&rocket);
|
||||||
|
|
||||||
|
assert_eq!(res.body_string(), Some(super::index().to_string()));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_raw_upload() {
|
||||||
|
const UPLOAD_CONTENTS: &str = "Hey! I'm going to be uploaded. :D Yay!";
|
||||||
|
|
||||||
|
let rocket = super::rocket();
|
||||||
|
let mut req = MockRequest::new(Post, "/upload")
|
||||||
|
.header(ContentType::Plain)
|
||||||
|
.body(UPLOAD_CONTENTS);
|
||||||
|
|
||||||
|
// Do the upload. Make sure we get the expected results.
|
||||||
|
let mut res = req.dispatch_with(&rocket);
|
||||||
|
assert_eq!(res.status(), Status::Ok);
|
||||||
|
assert_eq!(res.body_string(), Some(UPLOAD_CONTENTS.len().to_string()));
|
||||||
|
|
||||||
|
// Ensure we find the body in the /tmp/upload.txt file.
|
||||||
|
let mut file_contents = String::new();
|
||||||
|
let mut file = File::open("/tmp/upload.txt").expect("open upload.txt file");
|
||||||
|
file.read_to_string(&mut file_contents).expect("read upload.txt");
|
||||||
|
assert_eq!(&file_contents, UPLOAD_CONTENTS);
|
||||||
|
}
|
Loading…
Reference in New Issue