2022-07-31 03:27:09 +03:00
|
|
|
mod fixtures;
|
|
|
|
mod utils;
|
|
|
|
|
|
|
|
use diqwest::blocking::WithDigestAuth;
|
|
|
|
use fixtures::{port, tmpdir, wait_for_port, Error};
|
|
|
|
|
|
|
|
use assert_cmd::prelude::*;
|
|
|
|
use assert_fs::fixture::TempDir;
|
|
|
|
use rstest::rstest;
|
|
|
|
use std::io::Read;
|
|
|
|
use std::process::{Command, Stdio};
|
|
|
|
|
|
|
|
#[rstest]
|
2023-06-01 13:52:05 +03:00
|
|
|
#[case(&["-a", "user:pass@/:rw", "--log-format", "$remote_user"], false)]
|
2023-11-03 15:36:23 +03:00
|
|
|
#[case(&["-a", "user:pass@/:rw", "--log-format", "$remote_user"], true)]
|
2022-07-31 03:27:09 +03:00
|
|
|
fn log_remote_user(
|
|
|
|
tmpdir: TempDir,
|
|
|
|
port: u16,
|
|
|
|
#[case] args: &[&str],
|
|
|
|
#[case] is_basic: bool,
|
|
|
|
) -> Result<(), Error> {
|
|
|
|
let mut child = Command::cargo_bin("dufs")?
|
|
|
|
.arg(tmpdir.path())
|
|
|
|
.arg("-p")
|
|
|
|
.arg(port.to_string())
|
|
|
|
.args(args)
|
|
|
|
.stdout(Stdio::piped())
|
|
|
|
.spawn()?;
|
|
|
|
|
|
|
|
wait_for_port(port);
|
|
|
|
|
|
|
|
let stdout = child.stdout.as_mut().expect("Failed to get stdout");
|
|
|
|
|
2023-02-20 06:05:53 +03:00
|
|
|
let req = fetch!(b"GET", &format!("http://localhost:{port}"));
|
2022-07-31 03:27:09 +03:00
|
|
|
|
|
|
|
let resp = if is_basic {
|
|
|
|
req.basic_auth("user", Some("pass")).send()?
|
|
|
|
} else {
|
|
|
|
req.send_with_digest_auth("user", "pass")?
|
|
|
|
};
|
|
|
|
|
|
|
|
assert_eq!(resp.status(), 200);
|
|
|
|
|
2023-11-03 15:36:23 +03:00
|
|
|
let mut buf = [0; 4096];
|
2022-07-31 03:27:09 +03:00
|
|
|
let buf_len = stdout.read(&mut buf)?;
|
|
|
|
let output = std::str::from_utf8(&buf[0..buf_len])?;
|
|
|
|
|
|
|
|
assert!(output.lines().last().unwrap().ends_with("user"));
|
|
|
|
|
|
|
|
child.kill()?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[rstest]
|
|
|
|
#[case(&["--log-format", ""])]
|
|
|
|
fn no_log(tmpdir: TempDir, port: u16, #[case] args: &[&str]) -> Result<(), Error> {
|
|
|
|
let mut child = Command::cargo_bin("dufs")?
|
|
|
|
.arg(tmpdir.path())
|
|
|
|
.arg("-p")
|
|
|
|
.arg(port.to_string())
|
|
|
|
.args(args)
|
|
|
|
.stdout(Stdio::piped())
|
|
|
|
.spawn()?;
|
|
|
|
|
|
|
|
wait_for_port(port);
|
|
|
|
|
|
|
|
let stdout = child.stdout.as_mut().expect("Failed to get stdout");
|
|
|
|
|
2023-02-20 06:05:53 +03:00
|
|
|
let resp = fetch!(b"GET", &format!("http://localhost:{port}")).send()?;
|
2022-07-31 03:27:09 +03:00
|
|
|
assert_eq!(resp.status(), 200);
|
|
|
|
|
2023-11-03 15:36:23 +03:00
|
|
|
let mut buf = [0; 4096];
|
2022-07-31 03:27:09 +03:00
|
|
|
let buf_len = stdout.read(&mut buf)?;
|
|
|
|
let output = std::str::from_utf8(&buf[0..buf_len])?;
|
|
|
|
|
|
|
|
assert_eq!(output.lines().last().unwrap(), "");
|
|
|
|
Ok(())
|
|
|
|
}
|