#![allow(dead_code)]
use std::collections::{HashMap, VecDeque};
use std::sync::Mutex;
use std::time::Duration;
use serde_json::Value;
use wiremock::matchers::any;
use wiremock::{Mock, MockServer, Request, Respond, ResponseTemplate};
use crate::sdk;
pub const KEY: &str = "sk_test_123";
#[derive(Clone, Debug)]
pub struct Reply {
status: u16,
body: Option<Value>,
text: Option<String>,
raw: Option<(Vec<u8>, String)>,
headers: Vec<(String, String)>,
delay: Option<Duration>,
}
impl Reply {
pub fn status(status: u16) -> Self {
Self {
status,
body: None,
text: None,
raw: None,
headers: Vec::new(),
delay: None,
}
}
pub fn json(status: u16, body: Value) -> Self {
Self {
body: Some(body),
..Self::status(status)
}
}
pub fn text(status: u16, text: &str) -> Self {
Self {
text: Some(text.to_owned()),
..Self::status(status)
}
}
pub fn raw(status: u16, body: impl Into<Vec<u8>>, content_type: &str) -> Self {
Self {
raw: Some((body.into(), content_type.to_owned())),
..Self::status(status)
}
}
pub fn header(mut self, name: &str, value: &str) -> Self {
self.headers.push((name.to_owned(), value.to_owned()));
self
}
pub fn delay(mut self, delay: Duration) -> Self {
self.delay = Some(delay);
self
}
fn template(&self) -> ResponseTemplate {
let mut template = ResponseTemplate::new(self.status);
if let Some(body) = &self.body {
template = template.set_body_json(body);
}
if let Some(text) = &self.text {
template = template.set_body_string(text.clone());
}
if let Some((bytes, content_type)) = &self.raw {
template = template.set_body_raw(bytes.clone(), content_type);
}
for (name, value) in &self.headers {
template = template.insert_header(name.as_str(), value.as_str());
}
if let Some(delay) = self.delay {
template = template.set_delay(delay);
}
template
}
}
struct Queue(Mutex<VecDeque<Reply>>);
impl Respond for Queue {
fn respond(&self, _request: &Request) -> ResponseTemplate {
let next = self.0.lock().unwrap().pop_front();
match next {
Some(reply) => reply.template(),
None => ResponseTemplate::new(599).set_body_string("no reply queued"),
}
}
}
#[derive(Clone, Debug)]
pub struct Call {
pub method: String,
pub path: String,
pub query: Vec<(String, String)>,
pub headers: HashMap<String, String>,
pub body: Option<Value>,
}
impl Call {
pub fn header(&self, name: &str) -> Option<&str> {
self.headers
.get(&name.to_ascii_lowercase())
.map(String::as_str)
}
}
pub struct MockApi {
server: MockServer,
}
impl MockApi {
pub async fn start(replies: Vec<Reply>) -> Self {
let server = MockServer::start().await;
Mock::given(any())
.respond_with(Queue(Mutex::new(replies.into())))
.mount(&server)
.await;
Self { server }
}
pub fn url(&self) -> String {
self.server.uri()
}
pub fn builder(&self) -> sdk::ClientBuilder {
sdk::ClientBuilder::new()
.api_key(KEY)
.base_url(self.url())
.timeout(Duration::from_secs(5))
.retry(sdk::RetryPolicy {
max_retries: 2,
initial_backoff: Duration::from_millis(2),
max_backoff: Duration::from_millis(4),
max_retry_after: Duration::from_secs(1),
})
}
pub fn client(&self) -> sdk::Client {
self.builder().build().unwrap()
}
pub async fn calls(&self) -> Vec<Call> {
let requests = self.server.received_requests().await.unwrap();
requests
.iter()
.map(|request| Call {
method: request.method.to_string(),
path: request.url.path().to_owned(),
query: request
.url
.query_pairs()
.map(|(name, value)| (name.into_owned(), value.into_owned()))
.collect(),
headers: request
.headers
.iter()
.map(|(name, value)| {
(
name.as_str().to_ascii_lowercase(),
value.to_str().unwrap_or_default().to_owned(),
)
})
.collect(),
body: if request.body.is_empty() {
None
} else {
Some(serde_json::from_slice(&request.body).unwrap())
},
})
.collect()
}
}
pub fn pairs(query: &[(&str, &str)]) -> Vec<(String, String)> {
query
.iter()
.map(|(name, value)| ((*name).to_owned(), (*value).to_owned()))
.collect()
}
fn same(a: &Value, b: &Value) -> bool {
match (a, b) {
(Value::Number(a), Value::Number(b)) => a.as_f64() == b.as_f64(),
(Value::Array(a), Value::Array(b)) => {
a.len() == b.len() && a.iter().zip(b).all(|(a, b)| same(a, b))
}
(Value::Object(a), Value::Object(b)) => {
a.len() == b.len()
&& a.iter()
.all(|(key, a)| b.get(key).is_some_and(|b| same(a, b)))
}
_ => a == b,
}
}
#[track_caller]
pub fn assert_json<T: serde::Serialize>(actual: &T, expected: &Value) {
let actual = serde_json::to_value(actual).unwrap();
assert!(
same(&actual, expected),
"JSON differs\n actual: {actual}\n expected: {expected}"
);
}
#[track_caller]
pub fn json(text: &str) -> Value {
serde_json::from_str(text).unwrap()
}
#[track_caller]
pub fn from_json<T: serde::de::DeserializeOwned>(text: &str) -> T {
serde_json::from_str(text).unwrap()
}