|
|
|
@ -8,15 +8,42 @@ const LARGE: usize =4096*1024;
|
|
|
|
|
const MEDIUM: usize =1024*1024; |
|
|
|
|
const SMALL: usize = 4096; |
|
|
|
|
|
|
|
|
|
struct FetchTest<'a> { |
|
|
|
|
port: u32, |
|
|
|
|
directory: &'a str, |
|
|
|
|
web: bool, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
impl<'a> FetchTest<'a> { |
|
|
|
|
pub fn new(port: u32, directory: &'a str, web: bool) -> Self { |
|
|
|
|
Self { |
|
|
|
|
port, |
|
|
|
|
directory, |
|
|
|
|
web, |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Read file from disk and compare to bytes received from the web server
|
|
|
|
|
pub fn fetch_and_check_file(&self, mut remote: &str) -> Result<()> { |
|
|
|
|
let mut content_file = Vec::new(); |
|
|
|
|
OpenOptions::new().read(true).open(format!("{}/{}", self.directory, remote))?.read_to_end(&mut content_file)?; |
|
|
|
|
if self.web && remote == "index.html" { |
|
|
|
|
remote = "" |
|
|
|
|
} |
|
|
|
|
let content_http = get(format!("http://127.0.0.1:{}/{}", self.port, remote).as_str())?.bytes()?; |
|
|
|
|
(content_http == content_file).then(|| ()).ok_or(anyhow::Error::msg("Contents not equal")) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fn prepare_data() -> Result<()> { |
|
|
|
|
create_dir("tests/testdata")?; |
|
|
|
|
create_file("tests/testdata/index.html", LARGE)?; |
|
|
|
|
create_file("tests/testdata/foo", MEDIUM)?; |
|
|
|
|
create_file("tests/testdata/bar", SMALL)?; |
|
|
|
|
create_random_file("tests/testdata/index.html", LARGE)?; |
|
|
|
|
create_random_file("tests/testdata/foo", MEDIUM)?; |
|
|
|
|
create_random_file("tests/testdata/bar", SMALL)?; |
|
|
|
|
Ok(()) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fn create_file(path: &str, length: usize) -> Result<()> { |
|
|
|
|
fn create_random_file(path: &str, length: usize) -> Result<()> { |
|
|
|
|
let mut file = OpenOptions::new().create(true).write(true).open(path)?; |
|
|
|
|
let mut rng = rand::thread_rng(); |
|
|
|
|
let mut buf = vec![0; length]; |
|
|
|
@ -27,14 +54,6 @@ fn create_file(path: &str, length: usize) -> Result<()> {
|
|
|
|
|
Ok(()) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Read file from disk and compare to bytes received from the web server
|
|
|
|
|
fn fetch_and_check_file(file: &str) -> Result<()> { |
|
|
|
|
let content_http = get(format!("http://localhost:8080/{}", file).as_str())?.bytes()?; |
|
|
|
|
let mut content_file = Vec::new(); |
|
|
|
|
OpenOptions::new().read(true).open(format!("tests/testdata/{}", file))?.read_to_end(&mut content_file)?; |
|
|
|
|
(content_http == content_file).then(|| ()).ok_or(anyhow::Error::msg("Contents not equal")) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#[test] |
|
|
|
|
fn test_file_serve() { |
|
|
|
|
prepare_data(); |
|
|
|
@ -44,14 +63,41 @@ fn test_file_serve() {
|
|
|
|
|
.spawn() |
|
|
|
|
.expect("Could not spawn server!"); |
|
|
|
|
std::thread::sleep(std::time::Duration::from_secs(1)); |
|
|
|
|
let test = FetchTest::new(8080, "tests/testdata", false); |
|
|
|
|
let mut succ = true; |
|
|
|
|
if let Err(_) = test.fetch_and_check_file("index.html") { |
|
|
|
|
succ = false |
|
|
|
|
} |
|
|
|
|
if let Err(_) = test.fetch_and_check_file("foo") { |
|
|
|
|
succ = false |
|
|
|
|
} |
|
|
|
|
if let Err(_) = test.fetch_and_check_file("bar") { |
|
|
|
|
succ = false |
|
|
|
|
} |
|
|
|
|
cmd.kill().expect("Could not kill server."); |
|
|
|
|
assert!(succ) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[test] |
|
|
|
|
fn test_web_serve() { |
|
|
|
|
prepare_data(); |
|
|
|
|
let mut cmd = std::process::Command::new("./target/debug/rusty-conveyor") |
|
|
|
|
.args(&["-p", "8090"]) |
|
|
|
|
.args(&["-d", "tests/testdata"]) |
|
|
|
|
.args(&["--web"]) |
|
|
|
|
.spawn() |
|
|
|
|
.expect("Could not spawn server!"); |
|
|
|
|
std::thread::sleep(std::time::Duration::from_secs(1)); |
|
|
|
|
let test = FetchTest::new(8090, "tests/testdata", true); |
|
|
|
|
let mut succ = true; |
|
|
|
|
if let Err(_) = fetch_and_check_file("index.html") { |
|
|
|
|
if let Err(_) = test.fetch_and_check_file("index.html") { |
|
|
|
|
succ = false |
|
|
|
|
} |
|
|
|
|
if let Err(_) = fetch_and_check_file("foo") { |
|
|
|
|
if let Err(_) = test.fetch_and_check_file("foo") { |
|
|
|
|
succ = false |
|
|
|
|
} |
|
|
|
|
if let Err(_) = fetch_and_check_file("bar") { |
|
|
|
|
if let Err(_) = test.fetch_and_check_file("bar") { |
|
|
|
|
succ = false |
|
|
|
|
} |
|
|
|
|
cmd.kill().expect("Could not kill server."); |
|
|
|
|