2017-04-14 21:39:17 +00:00
|
|
|
use super::{rocket, index};
|
2017-06-06 20:41:04 +00:00
|
|
|
use rocket::local::Client;
|
2017-04-14 21:39:17 +00:00
|
|
|
use rocket::http::{Status, ContentType};
|
|
|
|
|
|
|
|
fn extract_id(from: &str) -> Option<String> {
|
|
|
|
from.rfind('/').map(|i| &from[(i + 1)..]).map(|s| s.trim_right().to_string())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn check_index() {
|
2017-06-06 20:41:04 +00:00
|
|
|
let client = Client::new(rocket()).unwrap();
|
2017-04-14 21:39:17 +00:00
|
|
|
|
|
|
|
// Ensure the index returns what we expect.
|
2017-06-06 20:41:04 +00:00
|
|
|
let mut response = client.get("/").dispatch();
|
2017-04-14 21:39:17 +00:00
|
|
|
assert_eq!(response.status(), Status::Ok);
|
|
|
|
assert_eq!(response.content_type(), Some(ContentType::Plain));
|
|
|
|
assert_eq!(response.body_string(), Some(index().into()))
|
|
|
|
}
|
|
|
|
|
2017-06-06 20:41:04 +00:00
|
|
|
fn upload_paste(client: &Client, body: &str) -> String {
|
|
|
|
let mut response = client.post("/").body(body).dispatch();
|
2017-04-14 21:39:17 +00:00
|
|
|
assert_eq!(response.status(), Status::Ok);
|
|
|
|
assert_eq!(response.content_type(), Some(ContentType::Plain));
|
|
|
|
extract_id(&response.body_string().unwrap()).unwrap()
|
|
|
|
}
|
|
|
|
|
2017-06-06 20:41:04 +00:00
|
|
|
fn download_paste(client: &Client, id: &str) -> String {
|
|
|
|
let mut response = client.get(format!("/{}", id)).dispatch();
|
2017-04-14 21:39:17 +00:00
|
|
|
assert_eq!(response.status(), Status::Ok);
|
|
|
|
response.body_string().unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn pasting() {
|
2017-06-06 20:41:04 +00:00
|
|
|
let client = Client::new(rocket()).unwrap();
|
2017-04-14 21:39:17 +00:00
|
|
|
|
|
|
|
// Do a trivial upload, just to make sure it works.
|
|
|
|
let body_1 = "Hello, world!";
|
2017-06-06 20:41:04 +00:00
|
|
|
let id_1 = upload_paste(&client, body_1);
|
|
|
|
assert_eq!(download_paste(&client, &id_1), body_1);
|
2017-04-14 21:39:17 +00:00
|
|
|
|
|
|
|
// Make sure we can keep getting that paste.
|
2017-06-06 20:41:04 +00:00
|
|
|
assert_eq!(download_paste(&client, &id_1), body_1);
|
|
|
|
assert_eq!(download_paste(&client, &id_1), body_1);
|
|
|
|
assert_eq!(download_paste(&client, &id_1), body_1);
|
2017-04-14 21:39:17 +00:00
|
|
|
|
|
|
|
// Upload some unicode.
|
|
|
|
let body_2 = "こんにちは";
|
2017-06-06 20:41:04 +00:00
|
|
|
let id_2 = upload_paste(&client, body_2);
|
|
|
|
assert_eq!(download_paste(&client, &id_2), body_2);
|
2017-04-14 21:39:17 +00:00
|
|
|
|
|
|
|
// Make sure we can get both pastes.
|
2017-06-06 20:41:04 +00:00
|
|
|
assert_eq!(download_paste(&client, &id_1), body_1);
|
|
|
|
assert_eq!(download_paste(&client, &id_2), body_2);
|
|
|
|
assert_eq!(download_paste(&client, &id_1), body_1);
|
|
|
|
assert_eq!(download_paste(&client, &id_2), body_2);
|
2017-04-14 21:39:17 +00:00
|
|
|
|
|
|
|
// Now a longer upload.
|
|
|
|
let body_3 = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed
|
|
|
|
do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim
|
|
|
|
ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut
|
|
|
|
aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit
|
|
|
|
in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
|
|
|
|
Excepteur sint occaecat cupidatat non proident, sunt in culpa qui
|
|
|
|
officia deserunt mollit anim id est laborum.";
|
2017-06-06 20:41:04 +00:00
|
|
|
let id_3 = upload_paste(&client, body_3);
|
|
|
|
assert_eq!(download_paste(&client, &id_3), body_3);
|
|
|
|
assert_eq!(download_paste(&client, &id_1), body_1);
|
|
|
|
assert_eq!(download_paste(&client, &id_2), body_2);
|
2017-04-14 21:39:17 +00:00
|
|
|
}
|