From 5608cb0cc81c0846f9c2c1d915d2db07ad4b8575 Mon Sep 17 00:00:00 2001 From: Tanner Donovan Date: Fri, 6 Jan 2017 14:40:15 -0800 Subject: [PATCH] add tests for examples/pastebin --- examples/pastebin/.gitignore | 1 + examples/pastebin/Cargo.toml | 3 ++ examples/pastebin/src/main.rs | 2 + examples/pastebin/src/tests/file.txt | 1 + examples/pastebin/src/tests/mod.rs | 69 +++++++++++++++++++++++++++ examples/pastebin/src/tests/usage.txt | 14 ++++++ 6 files changed, 90 insertions(+) create mode 100644 examples/pastebin/.gitignore create mode 100644 examples/pastebin/src/tests/file.txt create mode 100644 examples/pastebin/src/tests/mod.rs create mode 100644 examples/pastebin/src/tests/usage.txt diff --git a/examples/pastebin/.gitignore b/examples/pastebin/.gitignore new file mode 100644 index 00000000..b724e4db --- /dev/null +++ b/examples/pastebin/.gitignore @@ -0,0 +1 @@ +/upload/ \ No newline at end of file diff --git a/examples/pastebin/Cargo.toml b/examples/pastebin/Cargo.toml index fa299dde..37c2d451 100644 --- a/examples/pastebin/Cargo.toml +++ b/examples/pastebin/Cargo.toml @@ -8,3 +8,6 @@ workspace = "../../" rocket = { path = "../../lib" } rocket_codegen = { path = "../../codegen" } rand = "0.3" + +[dev-dependencies] +rocket = { path = "../../lib", features = ["testing"] } diff --git a/examples/pastebin/src/main.rs b/examples/pastebin/src/main.rs index 77bbc9eb..e9644185 100644 --- a/examples/pastebin/src/main.rs +++ b/examples/pastebin/src/main.rs @@ -4,6 +4,8 @@ extern crate rocket; extern crate rand; +#[cfg(test)] mod tests; + mod paste_id; use std::io; diff --git a/examples/pastebin/src/tests/file.txt b/examples/pastebin/src/tests/file.txt new file mode 100644 index 00000000..8effb3e8 --- /dev/null +++ b/examples/pastebin/src/tests/file.txt @@ -0,0 +1 @@ +"Hello, world!" diff --git a/examples/pastebin/src/tests/mod.rs b/examples/pastebin/src/tests/mod.rs new file mode 100644 index 00000000..395d7587 --- /dev/null +++ b/examples/pastebin/src/tests/mod.rs @@ -0,0 +1,69 @@ +use std::io::prelude::*; +use std::fs::File; +use std::path::Path; + +use super::rocket; +use rocket::testing::MockRequest; +use rocket::http::{Method, Status}; + +#[test] +fn index() { + let rocket = rocket::ignite().mount("/", routes![super::index]); + let mut req = MockRequest::new(Method::Get, "/"); + let mut response = req.dispatch_with(&rocket); + + assert_eq!(response.status(), Status::Ok); + + let body_str = response.body().and_then(|body| body.into_string()); + assert_eq!(body_str, Some(include_str!("usage.txt").to_string())); +} + +#[test] +fn upload() { + let rocket = rocket::ignite().mount("/", routes![super::upload]); + let mut req = MockRequest::new(Method::Post, "/") + .body(include_str!("file.txt").to_string()); + let mut response = req.dispatch_with(&rocket); + + assert_eq!(response.status(), Status::Ok); + + match response.body().and_then(|body| body.into_string()) { + Some(url) => { + match url.trim().split("/").last() { + Some(paste_id) => { + let filename = format!("upload/{}", paste_id); + assert!(Path::new(&filename).exists()); + }, + _ => unreachable!(), + } + }, + _ => unreachable!(), + } +} + +#[test] +fn retrieve_file_found() { + let id = super::PasteID::new(super::ID_LENGTH); + let filename = format!("upload/{}", id); + + let mut file = File::create(&filename).expect("failed to create file"); + file.write_all(b"Hello from Rocket!").expect("failed to write file"); + + let rocket = rocket::ignite().mount("/", routes![super::retrieve]); + let mut req = MockRequest::new(Method::Get, format!("/{}", id)); + let mut response = req.dispatch_with(&rocket); + + assert_eq!(response.status(), Status::Ok); + + let body_str = response.body().and_then(|body| body.into_string()); + assert_eq!(body_str, Some("Hello from Rocket!".to_string())); +} + +#[test] +fn retrieve_file_not_found() { + let rocket = rocket::ignite().mount("/", routes![super::retrieve]); + let mut req = MockRequest::new(Method::Get, format!("/{}", "file-not-found")); + let response = req.dispatch_with(&rocket); + + assert_eq!(response.status(), Status::NotFound); +} diff --git a/examples/pastebin/src/tests/usage.txt b/examples/pastebin/src/tests/usage.txt new file mode 100644 index 00000000..f443bec6 --- /dev/null +++ b/examples/pastebin/src/tests/usage.txt @@ -0,0 +1,14 @@ + + USAGE + + POST / + + accepts raw data in the body of the request and responds with a URL of + a page containing the body's content + + EXMAPLE: curl --data-binary @file.txt http://localhost:8000 + + GET / + + retrieves the content for the paste with id `` + \ No newline at end of file