use std::time::Duration;
use ::http::header::{CONTENT_TYPE, IntoHeaderName};
use ::http::{HeaderMap, HeaderValue, StatusCode, Uri};
use bytes::Bytes;
#[derive(Debug, Clone)]
pub struct MockPage {
pub(super) status: StatusCode,
pub(super) headers: HeaderMap,
pub(super) body: Bytes,
pub(super) location: Option<Uri>,
pub(super) latency: Duration,
}
impl MockPage {
#[must_use]
pub fn status(status: StatusCode) -> Self {
Self {
status,
headers: HeaderMap::new(),
body: Bytes::new(),
location: None,
latency: Duration::ZERO,
}
}
#[must_use]
pub fn html(body: impl Into<Bytes>) -> Self {
Self::content("text/html; charset=utf-8", body)
}
#[must_use]
pub fn text(body: impl Into<Bytes>) -> Self {
Self::content("text/plain; charset=utf-8", body)
}
#[must_use]
pub fn json(body: impl Into<Bytes>) -> Self {
Self::content("application/json", body)
}
#[must_use]
pub fn xml(body: impl Into<Bytes>) -> Self {
Self::content("application/xml", body)
}
fn content(content_type: &'static str, body: impl Into<Bytes>) -> Self {
Self::status(StatusCode::OK)
.with_header(CONTENT_TYPE, HeaderValue::from_static(content_type))
.with_body(body)
}
#[must_use]
pub fn redirect(location: &str) -> Self {
let mut page = Self::status(StatusCode::FOUND);
page.location = Some(super::site::parse(location));
page
}
#[must_use]
pub const fn with_status(mut self, status: StatusCode) -> Self {
self.status = status;
self
}
#[must_use]
pub fn with_header(mut self, name: impl IntoHeaderName, value: HeaderValue) -> Self {
self.headers.insert(name, value);
self
}
#[must_use]
pub const fn with_latency(mut self, latency: Duration) -> Self {
self.latency = latency;
self
}
#[must_use]
pub fn with_body(mut self, body: impl Into<Bytes>) -> Self {
self.body = body.into();
self
}
}