2017-02-18 07:01:12 +00:00
|
|
|
use std::fs::File;
|
|
|
|
use std::io::Read;
|
|
|
|
|
2020-07-05 18:35:36 +00:00
|
|
|
use rocket::local::blocking::Client;
|
2017-02-18 07:01:12 +00:00
|
|
|
use rocket::http::Status;
|
|
|
|
|
|
|
|
use super::rocket;
|
|
|
|
|
2021-03-05 10:01:24 +00:00
|
|
|
#[track_caller]
|
2020-07-05 18:35:36 +00:00
|
|
|
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-10-15 04:37:16 +00:00
|
|
|
let client = Client::tracked(rocket()).unwrap();
|
2020-07-05 18:35:36 +00:00
|
|
|
let response = client.get(path).dispatch();
|
2017-02-18 07:01:12 +00:00
|
|
|
assert_eq!(response.status(), status);
|
|
|
|
|
2020-07-05 18:35:36 +00:00
|
|
|
let body_data = response.into_bytes();
|
2017-02-18 07:01:12 +00:00
|
|
|
if let Some(filename) = file.into() {
|
2024-03-20 07:00:33 +00:00
|
|
|
let expected_data = read_file_content(filename).expect(filename);
|
2017-02-18 07:01:12 +00:00
|
|
|
assert!(body_data.map_or(false, |s| s == expected_data));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-20 07:00:33 +00:00
|
|
|
fn read_file_content(path: &str) -> std::io::Result<Vec<u8>> {
|
2017-02-18 07:01:12 +00:00
|
|
|
let mut file_content = vec![];
|
2024-03-20 07:00:33 +00:00
|
|
|
let mut fp = File::open(path)?;
|
|
|
|
fp.read_to_end(&mut file_content)?;
|
|
|
|
Ok(file_content)
|
2017-02-18 07:01:12 +00:00
|
|
|
}
|
|
|
|
|
2020-07-05 18:35:36 +00:00
|
|
|
#[test]
|
|
|
|
fn test_index_html() {
|
|
|
|
test_query_file("/", "static/index.html", Status::Ok);
|
|
|
|
test_query_file("/?v=1", "static/index.html", Status::Ok);
|
|
|
|
test_query_file("/?this=should&be=ignored", "static/index.html", Status::Ok);
|
2021-03-05 10:01:24 +00:00
|
|
|
test_query_file("/second/", "static/index.html", Status::Ok);
|
|
|
|
test_query_file("/second/?v=1", "static/index.html", Status::Ok);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_hidden_index_html() {
|
|
|
|
test_query_file("/hidden/", "static/hidden/index.html", Status::Ok);
|
|
|
|
test_query_file("//hidden//", "static/hidden/index.html", Status::Ok);
|
|
|
|
test_query_file("/second/hidden", "static/hidden/index.html", Status::Ok);
|
|
|
|
test_query_file("/second/hidden/", "static/hidden/index.html", Status::Ok);
|
|
|
|
test_query_file("/second/hidden///", "static/hidden/index.html", Status::Ok);
|
2017-02-18 07:01:12 +00:00
|
|
|
}
|
|
|
|
|
2020-07-05 18:35:36 +00:00
|
|
|
#[test]
|
|
|
|
fn test_hidden_file() {
|
|
|
|
test_query_file("/hidden/hi.txt", "static/hidden/hi.txt", Status::Ok);
|
2021-03-05 10:01:24 +00:00
|
|
|
test_query_file("/second/hidden/hi.txt", "static/hidden/hi.txt", Status::Ok);
|
2020-07-05 18:35:36 +00:00
|
|
|
test_query_file("/hidden/hi.txt?v=1", "static/hidden/hi.txt", Status::Ok);
|
|
|
|
test_query_file("/hidden/hi.txt?v=1&a=b", "static/hidden/hi.txt", Status::Ok);
|
2021-03-05 10:01:24 +00:00
|
|
|
test_query_file("/second/hidden/hi.txt?v=1&a=b", "static/hidden/hi.txt", Status::Ok);
|
2017-02-18 07:01:12 +00:00
|
|
|
}
|
|
|
|
|
2020-07-05 18:35:36 +00:00
|
|
|
#[test]
|
|
|
|
fn test_icon_file() {
|
|
|
|
test_query_file("/rocket-icon.jpg", "static/rocket-icon.jpg", Status::Ok);
|
2021-03-05 10:01:24 +00:00
|
|
|
test_query_file("/second/rocket-icon.jpg", "static/rocket-icon.jpg", Status::Ok);
|
2017-02-18 07:01:12 +00:00
|
|
|
}
|
|
|
|
|
2020-07-05 18:35:36 +00:00
|
|
|
#[test]
|
|
|
|
fn test_invalid_path() {
|
2023-04-10 17:48:30 +00:00
|
|
|
test_query_file("/hidden", None, Status::PermanentRedirect);
|
2020-07-05 18:35:36 +00:00
|
|
|
test_query_file("/thou_shalt_not_exist", None, Status::NotFound);
|
|
|
|
test_query_file("/thou/shalt/not/exist", None, Status::NotFound);
|
|
|
|
test_query_file("/thou/shalt/not/exist?a=b&c=d", None, Status::NotFound);
|
2017-02-18 07:01:12 +00:00
|
|
|
}
|