#![allow(dead_code)]
pub struct IgnitionMock {
pub server: wiremock::MockServer,
}
impl IgnitionMock {
pub async fn start() -> Self {
Self {
server: wiremock::MockServer::start().await,
}
}
pub fn uri(&self) -> String {
self.server.uri()
}
pub async fn list_json(&self, method: &str, path: &str, body: serde_json::Value) {
wiremock::Mock::given(wiremock::matchers::method(method))
.and(wiremock::matchers::path(path))
.respond_with(wiremock::ResponseTemplate::new(200).set_body_json(body))
.expect(1)
.mount(&self.server)
.await
}
pub async fn html_error(&self, method: &str, path: &str, status: u16) {
wiremock::Mock::given(wiremock::matchers::method(method))
.and(wiremock::matchers::path(path))
.respond_with(wiremock::ResponseTemplate::new(status).set_body_raw(
jetty_error_html(status, path),
"text/html;charset=iso-8859-1",
))
.expect(1)
.mount(&self.server)
.await
}
pub async fn redirect(&self, path: &str, location: &str) {
wiremock::Mock::given(wiremock::matchers::method("GET"))
.and(wiremock::matchers::path(path))
.respond_with(wiremock::ResponseTemplate::new(302).insert_header("Location", location))
.expect(1)
.mount(&self.server)
.await
}
pub async fn status_json(
&self,
method: &str,
path: &str,
status: u16,
body: serde_json::Value,
) {
wiremock::Mock::given(wiremock::matchers::method(method))
.and(wiremock::matchers::path(path))
.respond_with(wiremock::ResponseTemplate::new(status).set_body_json(body))
.expect(1)
.mount(&self.server)
.await
}
pub async fn literal_true(&self, method: &str, path: &str) {
wiremock::Mock::given(wiremock::matchers::method(method))
.and(wiremock::matchers::path(path))
.respond_with(wiremock::ResponseTemplate::new(200).set_body_string("true"))
.expect(1)
.mount(&self.server)
.await
}
}
pub fn jetty_error_html(status: u16, uri: &str) -> String {
let message = match status {
401 => "Unauthorized",
403 => "Forbidden",
404 => "Not Found",
500 => "Server Error",
503 => "Service Unavailable",
_ => "Error",
};
format!(
concat!(
r#"<html><head><meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1"/>"#,
r#"<title>Error {status}</title></head><body><h2>HTTP ERROR {status} {message}</h2><table>"#,
r#"<tr><th>URI:</th><td>{uri}</td></tr><tr><th>STATUS:</th><td>{status}</td></tr>"#,
r#"<tr><th>MESSAGE:</th><td>{message}</td></tr></table></body></html>"#
),
status = status,
message = message,
uri = uri,
)
}