use std::{
fs,
future::Future,
io::{Cursor, Error as IoError, Read, Write},
process::Command,
str,
time::{Duration, SystemTime},
};
use http::{header, Request, StatusCode};
use http_body_util::BodyExt;
use httpdate::fmt_http_date;
use hyper::body::Buf;
use hyper_staticfile::{
vfs::{FileAccess, MemoryFs},
AcceptEncoding, Body, Encoding, Static,
};
use tempfile::TempDir;
type Response = hyper::Response
;
type ResponseResult = Result;
struct Harness {
dir: TempDir,
static_: Static,
}
impl Harness {
fn new(files: Vec<(&str, &str)>) -> Harness {
let dir = Self::create_temp_dir(files);
let mut static_ = Static::new(dir.path());
static_
.cache_headers(Some(3600))
.allowed_encodings(AcceptEncoding::all());
Harness { dir, static_ }
}
fn create_temp_dir(files: Vec<(&str, &str)>) -> TempDir {
let dir = TempDir::new().unwrap();
for (subpath, contents) in files {
let fullpath = dir.path().join(subpath);
fs::create_dir_all(fullpath.parent().unwrap())
.and_then(|_| fs::File::create(fullpath))
.and_then(|mut file| file.write_all(contents.as_bytes()))
.expect("failed to write fixtures");
}
dir
}
fn append(&self, subpath: &str, content: &str) {
let path = self.dir.path().join(subpath);
let mut f = fs::File::options()
.append(true)
.open(path)
.expect("failed to append to fixture");
f.write_all(content.as_bytes())
.expect("failed to append to fixture");
}
fn request(&self, req: Request) -> impl Future