use crate::fixture::{Fixture, HttpExpectedResponse, HttpRequest, ValidationErrorExpectation};
use std::collections::HashMap;
pub mod http_call;
pub mod ws_script;
#[derive(Debug)]
pub struct CallCtx<'a> {
pub method: &'a str,
pub path: &'a str,
pub headers: &'a HashMap<String, String>,
pub query_params: &'a HashMap<String, serde_json::Value>,
pub cookies: &'a HashMap<String, String>,
pub body: Option<&'a serde_json::Value>,
pub content_type: Option<&'a str>,
pub response_var: &'a str,
}
impl<'a> CallCtx<'a> {
pub fn from_request(req: &'a HttpRequest, response_var: &'a str) -> Self {
Self {
method: req.method.as_str(),
path: req.path.as_str(),
headers: &req.headers,
query_params: &req.query_params,
cookies: &req.cookies,
body: req.body.as_ref(),
content_type: req.content_type.as_deref(),
response_var,
}
}
}
pub trait TestClientRenderer {
fn language_name(&self) -> &'static str;
fn sanitize_test_name(&self, id: &str) -> String {
crate::escape::sanitize_ident(id)
}
fn render_test_open(&self, out: &mut String, fn_name: &str, description: &str, skip_reason: Option<&str>);
fn render_test_close(&self, out: &mut String);
fn render_call(&self, out: &mut String, ctx: &CallCtx<'_>);
fn render_assert_status(&self, out: &mut String, response_var: &str, status: u16);
fn render_assert_header(&self, out: &mut String, response_var: &str, name: &str, expected: &str);
fn render_assert_json_body(&self, out: &mut String, response_var: &str, expected: &serde_json::Value);
fn render_assert_partial_body(&self, out: &mut String, response_var: &str, expected: &serde_json::Value);
fn render_assert_validation_errors(
&self,
out: &mut String,
response_var: &str,
errors: &[ValidationErrorExpectation],
);
}
pub fn is_skipped(fixture: &Fixture, language: &str) -> bool {
fixture
.skip
.as_ref()
.map(|s| s.languages.iter().any(|l| l == language))
.unwrap_or(false)
}
pub fn has_meaningful_headers(expected: &HttpExpectedResponse) -> bool {
expected
.headers
.iter()
.any(|(k, _)| !k.eq_ignore_ascii_case("content-encoding"))
}
pub fn has_meaningful_body(expected: &HttpExpectedResponse) -> bool {
match &expected.body {
Some(v) if v.is_null() => false,
Some(v) if v.as_str() == Some("") => false,
Some(_) => true,
None => false,
}
}