mirror of
https://github.com/sigoden/dufs.git
synced 2026-04-09 00:59:02 +03:00
test: add integration tests (#36)
This commit is contained in:
80
tests/bind.rs
Normal file
80
tests/bind.rs
Normal file
@@ -0,0 +1,80 @@
|
||||
mod fixtures;
|
||||
|
||||
use fixtures::{port, server, tmpdir, Error, TestServer};
|
||||
|
||||
use assert_cmd::prelude::*;
|
||||
use assert_fs::fixture::TempDir;
|
||||
use regex::Regex;
|
||||
use rstest::rstest;
|
||||
use std::io::{BufRead, BufReader};
|
||||
use std::process::{Command, Stdio};
|
||||
|
||||
#[rstest]
|
||||
#[case(&["-b", "20.205.243.166"])]
|
||||
fn bind_fails(tmpdir: TempDir, port: u16, #[case] args: &[&str]) -> Result<(), Error> {
|
||||
Command::cargo_bin("duf")?
|
||||
.env("RUST_LOG", "false")
|
||||
.arg(tmpdir.path())
|
||||
.arg("-p")
|
||||
.arg(port.to_string())
|
||||
.args(args)
|
||||
.assert()
|
||||
.stderr(predicates::str::contains("creating server listener"))
|
||||
.failure();
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[rstest]
|
||||
fn bind_ipv4(server: TestServer) -> Result<(), Error> {
|
||||
assert!(reqwest::blocking::get(format!("http://127.0.0.1:{}", server.port()).as_str()).is_ok());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[rstest]
|
||||
fn bind_ipv6(#[with(&["-b", "::"])] server: TestServer) -> Result<(), Error> {
|
||||
assert_eq!(
|
||||
reqwest::blocking::get(format!("http://127.0.0.1:{}", server.port()).as_str()).is_ok(),
|
||||
!cfg!(windows)
|
||||
);
|
||||
assert!(reqwest::blocking::get(format!("http://[::1]:{}", server.port()).as_str()).is_ok());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[rstest]
|
||||
#[case(&[] as &[&str])]
|
||||
#[case(&["--path-prefix", "/prefix"])]
|
||||
fn validate_printed_urls(tmpdir: TempDir, port: u16, #[case] args: &[&str]) -> Result<(), Error> {
|
||||
let mut child = Command::cargo_bin("duf")?
|
||||
.env("RUST_LOG", "false")
|
||||
.arg(tmpdir.path())
|
||||
.arg("-p")
|
||||
.arg(port.to_string())
|
||||
.args(args)
|
||||
.stdout(Stdio::piped())
|
||||
.spawn()?;
|
||||
|
||||
// WARN assumes urls list is terminated by an empty line
|
||||
let url_lines = BufReader::new(child.stdout.take().unwrap())
|
||||
.lines()
|
||||
.map(|line| line.expect("Error reading stdout"))
|
||||
.take_while(|line| !line.is_empty()) /* non-empty lines */
|
||||
.collect::<Vec<_>>();
|
||||
let url_lines = url_lines.join("\n");
|
||||
|
||||
let urls = Regex::new(r"http://[a-zA-Z0-9\.\[\]:/]+")
|
||||
.unwrap()
|
||||
.captures_iter(url_lines.as_str())
|
||||
.map(|caps| caps.get(0).unwrap().as_str())
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
assert!(!urls.is_empty());
|
||||
|
||||
for url in urls {
|
||||
reqwest::blocking::get(url)?.error_for_status()?;
|
||||
}
|
||||
|
||||
child.kill()?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user