use crate::{
CacheOptions, CachePolicy,
validation::{AfterResponse, BeforeRequest},
};
use std::time::{Duration, SystemTime};
use trillium_client::{Client, Conn, ConnExt, KnownHeaderName, Method, Status};
use trillium_testing::ServerConnector;
pub fn exchange(
method: Method,
request_headers: &[(KnownHeaderName, &str)],
status: Status,
response_headers: &[(KnownHeaderName, &str)],
) -> Conn {
let client = Client::new(ServerConnector::new(Status::InternalServerError));
let mut conn = match method {
Method::Get => client.get("http://example.com/"),
Method::Post => client.post("http://example.com/"),
Method::Put => client.put("http://example.com/"),
Method::Head => client.build_conn(Method::Head, "http://example.com/"),
other => client.build_conn(other, "http://example.com/"),
};
for (n, v) in request_headers {
conn.request_headers_mut().insert(*n, v.to_string());
}
conn.set_status(status);
for (n, v) in response_headers {
conn.response_headers_mut().insert(*n, v.to_string());
}
conn
}
pub fn request(method: Method, headers: &[(KnownHeaderName, &str)]) -> Conn {
let client = Client::new(ServerConnector::new(Status::InternalServerError));
let mut conn = match method {
Method::Get => client.get("http://example.com/"),
Method::Head => client.build_conn(Method::Head, "http://example.com/"),
m => client.build_conn(m, "http://example.com/"),
};
for (n, v) in headers {
conn.request_headers_mut().insert(*n, v.to_string());
}
conn
}
pub fn private_cache() -> CacheOptions {
CacheOptions::default()
}
pub fn shared_cache() -> CacheOptions {
CacheOptions {
shared: true,
..CacheOptions::default()
}
}
pub fn t0() -> SystemTime {
SystemTime::UNIX_EPOCH + Duration::from_secs(1_700_000_000)
}
pub fn at(t: SystemTime, secs: u64) -> SystemTime {
t + Duration::from_secs(secs)
}
pub fn policy_from(conn: &Conn, response_time: SystemTime, options: CacheOptions) -> CachePolicy {
CachePolicy::new(
conn.method(),
conn.request_headers(),
conn.status().expect("response not yet received"),
conn.response_headers().clone(),
response_time,
options,
)
}
pub fn is_storable(conn: &Conn, options: &CacheOptions) -> bool {
CachePolicy::is_storable(
conn.method(),
conn.request_headers(),
conn.status().expect("response not yet received"),
conn.response_headers(),
options,
)
}
pub fn before_request(policy: &CachePolicy, conn: &Conn, now: SystemTime) -> BeforeRequest {
policy.before_request(conn.request_headers(), now)
}
pub fn after_response(
policy: &CachePolicy,
conn: &Conn,
response_time: SystemTime,
) -> AfterResponse {
policy.after_response(
conn.request_headers(),
conn.status().expect("response not yet received"),
conn.response_headers(),
response_time,
)
}