2017-02-18 07:01:12 +00:00
|
|
|
use std::fs::File;
|
|
|
|
use std::io::Read;
|
|
|
|
|
2017-06-06 20:41:04 +00:00
|
|
|
use rocket::local::Client;
|
2017-02-18 07:01:12 +00:00
|
|
|
use rocket::http::Status;
|
|
|
|
|
|
|
|
use super::rocket;
|
|
|
|
|
2019-08-14 18:29:55 +00:00
|
|
|
async fn test_query_file<T> (path: &str, file: T, status: Status)
|
2017-02-18 07:01:12 +00:00
|
|
|
where T: Into<Option<&'static str>>
|
|
|
|
{
|
2020-06-14 15:57:53 +00:00
|
|
|
let client = Client::new(rocket()).await.unwrap();
|
2019-08-24 17:27:10 +00:00
|
|
|
let mut response = client.get(path).dispatch().await;
|
2017-02-18 07:01:12 +00:00
|
|
|
assert_eq!(response.status(), status);
|
|
|
|
|
2019-08-14 18:29:55 +00:00
|
|
|
let body_data = response.body_bytes().await;
|
2017-02-18 07:01:12 +00:00
|
|
|
if let Some(filename) = file.into() {
|
|
|
|
let expected_data = read_file_content(filename);
|
|
|
|
assert!(body_data.map_or(false, |s| s == expected_data));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn read_file_content(path: &str) -> Vec<u8> {
|
2018-08-13 09:13:55 +00:00
|
|
|
let mut fp = File::open(&path).expect(&format!("Can't open {}", path));
|
2017-02-18 07:01:12 +00:00
|
|
|
let mut file_content = vec![];
|
|
|
|
|
|
|
|
fp.read_to_end(&mut file_content).expect(&format!("Reading {} failed.", path));
|
|
|
|
file_content
|
|
|
|
}
|
|
|
|
|
2019-08-24 17:27:10 +00:00
|
|
|
#[rocket::async_test]
|
|
|
|
async fn test_index_html() {
|
|
|
|
test_query_file("/", "static/index.html", Status::Ok).await;
|
|
|
|
test_query_file("/?v=1", "static/index.html", Status::Ok).await;
|
|
|
|
test_query_file("/?this=should&be=ignored", "static/index.html", Status::Ok).await;
|
2017-02-18 07:01:12 +00:00
|
|
|
}
|
|
|
|
|
2019-08-24 17:27:10 +00:00
|
|
|
#[rocket::async_test]
|
|
|
|
async fn test_hidden_file() {
|
|
|
|
test_query_file("/hidden/hi.txt", "static/hidden/hi.txt", Status::Ok).await;
|
|
|
|
test_query_file("/hidden/hi.txt?v=1", "static/hidden/hi.txt", Status::Ok).await;
|
|
|
|
test_query_file("/hidden/hi.txt?v=1&a=b", "static/hidden/hi.txt", Status::Ok).await;
|
2017-02-18 07:01:12 +00:00
|
|
|
}
|
|
|
|
|
2019-08-24 17:27:10 +00:00
|
|
|
#[rocket::async_test]
|
|
|
|
async fn test_icon_file() {
|
|
|
|
test_query_file("/rocket-icon.jpg", "static/rocket-icon.jpg", Status::Ok).await;
|
|
|
|
test_query_file("/rocket-icon.jpg", "static/rocket-icon.jpg", Status::Ok).await;
|
2017-02-18 07:01:12 +00:00
|
|
|
}
|
|
|
|
|
2019-08-24 17:27:10 +00:00
|
|
|
#[rocket::async_test]
|
|
|
|
async fn test_invalid_path() {
|
|
|
|
test_query_file("/thou_shalt_not_exist", None, Status::NotFound).await;
|
|
|
|
test_query_file("/thou/shalt/not/exist", None, Status::NotFound).await;
|
|
|
|
test_query_file("/thou/shalt/not/exist?a=b&c=d", None, Status::NotFound).await;
|
2017-02-18 07:01:12 +00:00
|
|
|
}
|