2022-06-12 03:43:50 +03:00
|
|
|
mod fixtures;
|
|
|
|
|
2022-06-19 09:23:10 +03:00
|
|
|
use fixtures::{port, server, tmpdir, wait_for_port, Error, TestServer};
|
2022-06-12 03:43:50 +03:00
|
|
|
|
|
|
|
use assert_cmd::prelude::*;
|
|
|
|
use assert_fs::fixture::TempDir;
|
|
|
|
use regex::Regex;
|
|
|
|
use rstest::rstest;
|
2022-06-21 16:19:51 +03:00
|
|
|
use std::io::Read;
|
2022-06-12 03:43:50 +03:00
|
|
|
use std::process::{Command, Stdio};
|
|
|
|
|
|
|
|
#[rstest]
|
|
|
|
#[case(&["-b", "20.205.243.166"])]
|
|
|
|
fn bind_fails(tmpdir: TempDir, port: u16, #[case] args: &[&str]) -> Result<(), Error> {
|
2022-06-19 17:53:51 +03:00
|
|
|
Command::cargo_bin("dufs")?
|
2022-06-12 03:43:50 +03:00
|
|
|
.arg(tmpdir.path())
|
|
|
|
.arg("-p")
|
|
|
|
.arg(port.to_string())
|
|
|
|
.args(args)
|
|
|
|
.assert()
|
2022-06-15 14:33:51 +03:00
|
|
|
.stderr(predicates::str::contains("Failed to bind"))
|
2022-06-12 03:43:50 +03:00
|
|
|
.failure();
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[rstest]
|
2022-06-15 14:33:51 +03:00
|
|
|
#[case(server(&[] as &[&str]), true, true)]
|
|
|
|
#[case(server(&["-b", "0.0.0.0"]), true, false)]
|
|
|
|
#[case(server(&["-b", "127.0.0.1", "-b", "::1"]), true, true)]
|
|
|
|
fn bind_ipv4_ipv6(
|
|
|
|
#[case] server: TestServer,
|
|
|
|
#[case] bind_ipv4: bool,
|
|
|
|
#[case] bind_ipv6: bool,
|
|
|
|
) -> Result<(), Error> {
|
2022-06-12 03:43:50 +03:00
|
|
|
assert_eq!(
|
|
|
|
reqwest::blocking::get(format!("http://127.0.0.1:{}", server.port()).as_str()).is_ok(),
|
2022-06-15 14:33:51 +03:00
|
|
|
bind_ipv4
|
2022-06-12 03:43:50 +03:00
|
|
|
);
|
2022-06-15 14:33:51 +03:00
|
|
|
assert_eq!(
|
|
|
|
reqwest::blocking::get(format!("http://[::1]:{}", server.port()).as_str()).is_ok(),
|
|
|
|
bind_ipv6
|
|
|
|
);
|
|
|
|
|
2022-06-12 03:43:50 +03:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[rstest]
|
|
|
|
#[case(&[] as &[&str])]
|
|
|
|
#[case(&["--path-prefix", "/prefix"])]
|
|
|
|
fn validate_printed_urls(tmpdir: TempDir, port: u16, #[case] args: &[&str]) -> Result<(), Error> {
|
2022-06-19 17:53:51 +03:00
|
|
|
let mut child = Command::cargo_bin("dufs")?
|
2022-06-12 03:43:50 +03:00
|
|
|
.arg(tmpdir.path())
|
|
|
|
.arg("-p")
|
|
|
|
.arg(port.to_string())
|
|
|
|
.args(args)
|
|
|
|
.stdout(Stdio::piped())
|
|
|
|
.spawn()?;
|
|
|
|
|
2022-06-19 09:23:10 +03:00
|
|
|
wait_for_port(port);
|
|
|
|
|
2022-06-21 16:19:51 +03:00
|
|
|
let stdout = child.stdout.as_mut().expect("Failed to get stdout");
|
|
|
|
let mut buf = [0; 1000];
|
|
|
|
let buf_len = stdout.read(&mut buf)?;
|
|
|
|
let output = std::str::from_utf8(&buf[0..buf_len])?;
|
|
|
|
let url_lines = output
|
2022-06-12 03:43:50 +03:00
|
|
|
.lines()
|
|
|
|
.take_while(|line| !line.is_empty()) /* non-empty lines */
|
2022-06-21 16:19:51 +03:00
|
|
|
.collect::<Vec<_>>()
|
|
|
|
.join("\n");
|
2022-06-12 03:43:50 +03:00
|
|
|
|
|
|
|
let urls = Regex::new(r"http://[a-zA-Z0-9\.\[\]:/]+")
|
|
|
|
.unwrap()
|
|
|
|
.captures_iter(url_lines.as_str())
|
2022-06-21 16:19:51 +03:00
|
|
|
.filter_map(|caps| caps.get(0).map(|v| v.as_str()))
|
2022-06-12 03:43:50 +03:00
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
assert!(!urls.is_empty());
|
2023-11-03 10:25:20 +03:00
|
|
|
reqwest::blocking::get(urls[0])?.error_for_status()?;
|
2022-06-12 03:43:50 +03:00
|
|
|
|
|
|
|
child.kill()?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|