use std::sync::{Arc, Mutex};
use std::thread::JoinHandle;
use std::time::{Duration, Instant};
use percent_encoding::{AsciiSet, NON_ALPHANUMERIC, percent_decode_str, utf8_percent_encode};
use reqwest::blocking::RequestBuilder;
use serde::Deserialize;
use serde::de::DeserializeOwned;
use crate::error::{ApiError, Error, Result};
pub type Query = Vec<(String, String)>;
pub const DEFAULT_MAX_RESPONSE_SIZE: u64 = 256 * 1024 * 1024;
const DEFAULT_CONNECT_TIMEOUT: Duration = Duration::from_secs(10);
const MAX_RETRIES: u32 = 3;
const RETRY_STATUSES: [u16; 3] = [502, 503, 504];
const CONNECT_RETRY_WAIT: Duration = Duration::from_secs(1);
const MAX_RETRY_WAIT: Duration = Duration::from_secs(60);
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Method {
Get,
Post,
Put,
Patch,
Delete,
}
impl Method {
pub fn as_str(self) -> &'static str {
match self {
Self::Get => "GET",
Self::Post => "POST",
Self::Put => "PUT",
Self::Patch => "PATCH",
Self::Delete => "DELETE",
}
}
fn is_idempotent(self) -> bool {
!matches!(self, Self::Post | Self::Patch)
}
fn to_reqwest(self) -> reqwest::Method {
match self {
Self::Get => reqwest::Method::GET,
Self::Post => reqwest::Method::POST,
Self::Put => reqwest::Method::PUT,
Self::Patch => reqwest::Method::PATCH,
Self::Delete => reqwest::Method::DELETE,
}
}
}
impl std::fmt::Display for Method {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct InvalidMethod(pub String);
impl std::fmt::Display for InvalidMethod {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "invalid HTTP method: {}", self.0)
}
}
impl std::error::Error for InvalidMethod {}
impl std::str::FromStr for Method {
type Err = InvalidMethod;
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
match s.to_ascii_uppercase().as_str() {
"GET" => Ok(Self::Get),
"POST" => Ok(Self::Post),
"PUT" => Ok(Self::Put),
"PATCH" => Ok(Self::Patch),
"DELETE" => Ok(Self::Delete),
_ => Err(InvalidMethod(s.to_owned())),
}
}
}
#[derive(Clone)]
pub struct Client {
http: reqwest::blocking::Client,
api_url: String,
token: Option<String>,
timeout: Duration,
connect_timeout: Duration,
max_response_size: u64,
throttle: Option<Arc<Throttle>>,
}
impl std::fmt::Debug for Client {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Client")
.field("api_url", &self.api_url)
.field("token", &self.token.as_ref().map(|_| "<redacted>"))
.field("timeout", &self.timeout)
.field("connect_timeout", &self.connect_timeout)
.field("max_response_size", &self.max_response_size)
.field("throttle", &self.throttle)
.finish_non_exhaustive()
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Upload {
pub filename: String,
pub content: Arc<[u8]>,
pub content_type: String,
}
impl Upload {
pub fn new(filename: impl Into<String>, content: impl Into<Arc<[u8]>>, content_type: impl Into<String>) -> Self {
Self {
filename: filename.into(),
content: content.into(),
content_type: content_type.into(),
}
}
pub fn from_path(path: impl AsRef<std::path::Path>) -> std::io::Result<Self> {
let path = path.as_ref();
let filename = path.file_name().map(|n| n.to_string_lossy().into_owned()).unwrap_or_default();
let content_type = guess_content_type(&filename);
Ok(Self::new(filename, std::fs::read(path)?, content_type))
}
}
fn guess_content_type(filename: &str) -> &'static str {
let ext = filename.rsplit_once('.').map(|(_, e)| e.to_ascii_lowercase()).unwrap_or_default();
match ext.as_str() {
"pdf" => "application/pdf",
"png" => "image/png",
"jpg" | "jpeg" => "image/jpeg",
"gif" => "image/gif",
"webp" => "image/webp",
"svg" => "image/svg+xml",
"txt" => "text/plain",
"csv" => "text/csv",
"xml" => "application/xml",
"json" => "application/json",
_ => "application/octet-stream",
}
}
#[derive(Clone)]
pub struct ClientBuilder {
base_url: String,
token: Option<String>,
timeout: Duration,
connect_timeout: Option<Duration>,
max_response_size: u64,
allow_insecure_http: bool,
}
impl ClientBuilder {
pub fn from_env() -> Result<Self> {
let url = std::env::var("CT_URL").map_err(|_| Error::InvalidBaseUrl("CT_URL is not set".into()))?;
Ok(Client::builder(&url).token(std::env::var("CT_TOKEN").ok()))
}
pub fn token(mut self, token: impl Into<Option<String>>) -> Self {
self.token = token.into();
self
}
pub fn timeout(mut self, timeout: Duration) -> Self {
self.timeout = timeout;
self
}
pub fn connect_timeout(mut self, timeout: Duration) -> Self {
self.connect_timeout = Some(timeout);
self
}
pub fn max_response_size(mut self, bytes: u64) -> Self {
self.max_response_size = bytes;
self
}
pub fn allow_insecure_http(mut self, allow: bool) -> Self {
self.allow_insecure_http = allow;
self
}
pub fn build(self) -> Result<Client> {
let connect_timeout = self.connect_timeout.unwrap_or(DEFAULT_CONNECT_TIMEOUT).min(self.timeout);
let http = http_client()
.timeout(self.timeout)
.connect_timeout(connect_timeout)
.build()
.map_err(Error::transport)?;
Ok(Client {
http,
api_url: normalize_base_url(&self.base_url, self.allow_insecure_http)?,
token: self.token,
timeout: self.timeout,
connect_timeout,
max_response_size: self.max_response_size,
throttle: None,
})
}
}
fn http_client() -> reqwest::blocking::ClientBuilder {
if rustls::crypto::CryptoProvider::get_default().is_none() {
let _ = rustls::crypto::ring::default_provider().install_default();
}
reqwest::blocking::Client::builder()
.user_agent(USER_AGENT)
.redirect(redirect_policy())
}
fn redirect_policy() -> reqwest::redirect::Policy {
reqwest::redirect::Policy::custom(|attempt| {
if attempt.previous().len() >= 10 {
attempt.error("too many redirects")
} else if matches!(attempt.status().as_u16(), 307 | 308) {
attempt.follow()
} else {
attempt.stop()
}
})
}
const USER_AGENT: &str = concat!("churchtools-api/", env!("CARGO_PKG_VERSION"));
#[derive(Debug)]
struct Throttle {
interval: Duration,
next: Mutex<Instant>,
}
impl Throttle {
fn wait(&self) {
let now = Instant::now();
let start = {
let mut next = self.next.lock().unwrap_or_else(|e| e.into_inner());
let start = (*next).max(now);
*next = start + self.interval;
start
};
std::thread::sleep(start - now);
}
}
impl Client {
pub fn new(base_url: &str, token: Option<String>) -> Result<Self> {
Self::builder(base_url).token(token).build()
}
pub fn builder(base_url: &str) -> ClientBuilder {
ClientBuilder {
base_url: base_url.to_owned(),
token: None,
timeout: Duration::from_secs(30),
connect_timeout: None,
max_response_size: DEFAULT_MAX_RESPONSE_SIZE,
allow_insecure_http: false,
}
}
pub fn with_min_interval(mut self, min_interval: Duration) -> Self {
self.throttle = Some(Arc::new(Throttle {
interval: min_interval,
next: Mutex::new(Instant::now()),
}));
self
}
pub fn login_with_password(&self, username: &str, password: &str) -> Result<Self> {
let session = Self {
http: http_client()
.timeout(self.timeout)
.connect_timeout(self.connect_timeout)
.cookie_store(true)
.build()
.map_err(Error::transport)?,
token: None,
..self.clone()
};
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct LoginData {
person_id: i64,
}
let body = serde_json::json!({ "username": username, "password": password, "rememberMe": false });
let login: LoginData = session.request_data(Method::Post, "/login", &[], Some(&body))?;
let token: String = session.request_data(
Method::Get,
&format!("/persons/{}/logintoken", login.person_id),
&[],
None,
)?;
Ok(Self {
token: Some(token),
..self.clone()
})
}
pub fn from_env() -> Result<Self> {
ClientBuilder::from_env()?.build()
}
pub fn api_url(&self) -> &str {
&self.api_url
}
pub fn request(
&self,
method: Method,
path: &str,
query: &[(String, String)],
body: Option<&serde_json::Value>,
) -> Result<Vec<u8>> {
self.send(method, path, query, |builder| match body {
Some(body) => Ok(builder.json(body)),
None => Ok(builder),
})
}
pub fn request_json_bytes(
&self,
method: Method,
path: &str,
query: &[(String, String)],
body: Option<&[u8]>,
) -> Result<Vec<u8>> {
self.send(method, path, query, |builder| match body {
Some(body) => Ok(builder
.header(reqwest::header::CONTENT_TYPE, "application/json")
.body(body.to_vec())),
None => Ok(builder),
})
}
pub fn request_multipart(
&self,
method: Method,
path: &str,
query: &[(String, String)],
form: &[(String, String)],
file_field: &str,
files: &[Upload],
) -> Result<Vec<u8>> {
self.send(method, path, query, |builder| {
let mut multipart = reqwest::blocking::multipart::Form::new();
for (key, value) in form {
multipart = multipart.text(key.clone(), value.clone());
}
for file in files {
let reader = std::io::Cursor::new(file.content.clone());
let part = reqwest::blocking::multipart::Part::reader_with_length(reader, file.content.len() as u64)
.file_name(file.filename.clone())
.mime_str(&file.content_type)
.map_err(Error::transport)?;
multipart = multipart.part(file_field.to_owned(), part);
}
Ok(builder.multipart(multipart))
})
}
fn send(
&self,
method: Method,
path: &str,
query: &[(String, String)],
with_body: impl Fn(RequestBuilder) -> Result<RequestBuilder>,
) -> Result<Vec<u8>> {
check_path(path)?;
let mut attempt = 0;
let mut connect_retried = false;
loop {
if let Some(throttle) = &self.throttle {
throttle.wait();
}
let mut builder = self
.http
.request(method.to_reqwest(), format!("{}{}", self.api_url, path))
.header("Accept", "application/json")
.query(query);
if let Some(token) = &self.token {
let mut value = reqwest::header::HeaderValue::try_from(format!("Login {token}"))
.map_err(|_| Error::InvalidToken)?;
value.set_sensitive(true);
builder = builder.header(reqwest::header::AUTHORIZATION, value);
}
let response = match with_body(builder)?.send() {
Ok(response) => response,
Err(err) if err.is_connect() && !connect_retried => {
connect_retried = true;
std::thread::sleep(CONNECT_RETRY_WAIT);
continue;
}
Err(err) => return Err(Error::transport(err)),
};
let status = response.status().as_u16();
let retryable = status == 429 || (RETRY_STATUSES.contains(&status) && method.is_idempotent());
if retryable && attempt < MAX_RETRIES {
attempt += 1;
std::thread::sleep(retry_after(response.headers(), attempt));
continue;
}
if response.status().is_redirection() {
let location = response.headers().get(reqwest::header::LOCATION);
return Err(Error::Redirect {
status,
location: location.and_then(|v| v.to_str().ok()).map(str::to_owned),
});
}
let bytes = read_body(response, self.max_response_size)?;
if !(200..300).contains(&status) {
return Err(ApiError::from_response(status, bytes).into());
}
return Ok(bytes);
}
}
pub fn request_data<T: DeserializeOwned>(
&self,
method: Method,
path: &str,
query: &[(String, String)],
body: Option<&serde_json::Value>,
) -> Result<T> {
decode_data(&self.request(method, path, query, body)?)
}
pub fn request_json<T: DeserializeOwned>(
&self,
method: Method,
path: &str,
query: &[(String, String)],
body: Option<&serde_json::Value>,
) -> Result<T> {
decode(&self.request(method, path, query, body)?)
}
pub fn pages(&self, path: &str, query: Query) -> Pages {
Pages { fetcher: PageFetcher::new(self, path, query) }
}
pub fn collect_pages<T: DeserializeOwned>(&self, path: &str, query: Query) -> Result<Vec<T>> {
self.items(path, query).collect()
}
pub fn items<T: DeserializeOwned>(&self, path: &str, query: Query) -> Items<T> {
Items { fetcher: PageFetcher::new(self, path, query), buffer: Vec::new().into_iter() }
}
}
struct PageFetcher {
client: Client,
path: String,
query: Query,
pending: Option<Pending>,
handed_out: u64,
prefetch: bool,
}
enum Pending {
Page(u64),
Spawned(u64, JoinHandle<Result<Vec<u8>>>),
}
impl PageFetcher {
fn new(client: &Client, path: &str, query: Query) -> Self {
Self {
client: client.clone(),
path: path.to_owned(),
query: query.into_iter().filter(|(k, _)| k != "page").collect(),
pending: Some(Pending::Page(1)),
handed_out: 0,
prefetch: true,
}
}
fn fetch(&mut self) -> Option<(u64, Result<Vec<u8>>)> {
let (page, result) = match self.pending.take()? {
Pending::Page(page) => (page, request_page(&self.client, &self.path, &self.query, page)),
Pending::Spawned(page, handle) => {
(page, handle.join().unwrap_or_else(|panic| std::panic::resume_unwind(panic)))
}
};
self.handed_out += 1;
Some((page, result))
}
fn advance(&mut self, next: Option<u64>) {
self.pending = next.map(|page| {
if !self.prefetch || self.handed_out < 2 {
return Pending::Page(page);
}
let (client, path, query) = (self.client.clone(), self.path.clone(), self.query.clone());
let thread = std::thread::Builder::new().name("churchtools-api prefetch".into());
match thread.spawn(move || request_page(&client, &path, &query, page)) {
Ok(handle) => Pending::Spawned(page, handle),
Err(_) => Pending::Page(page),
}
});
}
}
fn request_page(client: &Client, path: &str, query: &Query, page: u64) -> Result<Vec<u8>> {
let mut query = query.clone();
query.push(("page".into(), page.to_string()));
client.request(Method::Get, path, &query, None)
}
pub struct Items<T> {
fetcher: PageFetcher,
buffer: std::vec::IntoIter<T>,
}
impl<T> Items<T> {
pub fn prefetch(mut self, enabled: bool) -> Self {
self.fetcher.prefetch = enabled;
self
}
}
impl<T: DeserializeOwned> Iterator for Items<T> {
type Item = Result<T>;
fn next(&mut self) -> Option<Self::Item> {
#[derive(Deserialize)]
struct Page<T> {
data: Vec<T>,
meta: Option<serde_json::Value>,
}
loop {
if let Some(item) = self.buffer.next() {
return Some(Ok(item));
}
let (page, result) = self.fetcher.fetch()?;
match result.and_then(|bytes| decode::<Page<T>>(&bytes)) {
Ok(body) => {
let meta = body.meta.and_then(|m| serde_json::from_value(m).ok());
self.fetcher.advance(next_page(meta, !body.data.is_empty(), page));
self.buffer = body.data.into_iter();
}
Err(e) => return Some(Err(e)),
}
}
}
}
pub struct Pages {
fetcher: PageFetcher,
}
impl Pages {
pub fn prefetch(mut self, enabled: bool) -> Self {
self.fetcher.prefetch = enabled;
self
}
}
impl Iterator for Pages {
type Item = Result<Vec<u8>>;
fn next(&mut self) -> Option<Self::Item> {
let (page, result) = self.fetcher.fetch()?;
if let Ok(bytes) = &result {
self.fetcher.advance(next_page_number(bytes, page));
}
Some(result)
}
}
fn read_body(response: reqwest::blocking::Response, limit: u64) -> Result<Vec<u8>> {
use std::io::Read;
let too_large = || Error::ResponseTooLarge { limit };
if response.content_length().is_some_and(|len| len > limit) {
return Err(too_large());
}
let mut bytes = Vec::with_capacity(response.content_length().unwrap_or(0).min(1 << 20) as usize);
response.take(limit.saturating_add(1)).read_to_end(&mut bytes).map_err(Error::Read)?;
if bytes.len() as u64 > limit {
return Err(too_large());
}
Ok(bytes)
}
#[derive(Deserialize)]
struct Meta {
pagination: Option<Pagination>,
}
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct Pagination {
current: u64,
last_page: u64,
}
fn next_page(meta: Option<Meta>, has_items: bool, requested: u64) -> Option<u64> {
let pagination = meta?.pagination?;
(has_items && pagination.current == requested && pagination.current < pagination.last_page)
.then_some(requested + 1)
}
fn next_page_number(body: &[u8], requested: u64) -> Option<u64> {
#[derive(Deserialize)]
struct Body {
meta: Option<Meta>,
#[serde(default)]
data: HasItems,
}
let body = serde_json::from_slice::<Body>(body).ok()?;
next_page(body.meta, body.data.0, requested)
}
struct HasItems(bool);
impl Default for HasItems {
fn default() -> Self {
Self(true)
}
}
impl<'de> Deserialize<'de> for HasItems {
fn deserialize<D: serde::Deserializer<'de>>(d: D) -> std::result::Result<Self, D::Error> {
use serde::de::{IgnoredAny, SeqAccess, Visitor};
struct V;
impl<'de> Visitor<'de> for V {
type Value = HasItems;
fn expecting(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
f.write_str("any JSON value")
}
fn visit_seq<A: SeqAccess<'de>>(self, mut seq: A) -> std::result::Result<HasItems, A::Error> {
let first = seq.next_element::<IgnoredAny>()?;
while seq.next_element::<IgnoredAny>()?.is_some() {}
Ok(HasItems(first.is_some()))
}
fn visit_map<A: serde::de::MapAccess<'de>>(self, map: A) -> std::result::Result<HasItems, A::Error> {
IgnoredAny.visit_map(map).map(|_| HasItems(true))
}
fn visit_unit<E>(self) -> std::result::Result<HasItems, E> {
Ok(HasItems(true))
}
fn visit_bool<E>(self, _: bool) -> std::result::Result<HasItems, E> {
Ok(HasItems(true))
}
fn visit_i64<E>(self, _: i64) -> std::result::Result<HasItems, E> {
Ok(HasItems(true))
}
fn visit_u64<E>(self, _: u64) -> std::result::Result<HasItems, E> {
Ok(HasItems(true))
}
fn visit_f64<E>(self, _: f64) -> std::result::Result<HasItems, E> {
Ok(HasItems(true))
}
fn visit_str<E>(self, _: &str) -> std::result::Result<HasItems, E> {
Ok(HasItems(true))
}
}
d.deserialize_any(V)
}
}
fn retry_after(headers: &reqwest::header::HeaderMap, attempt: u32) -> Duration {
headers
.get(reqwest::header::RETRY_AFTER)
.and_then(|v| v.to_str().ok())
.and_then(|v| parse_retry_after(v.trim(), chrono::Utc::now()))
.unwrap_or(Duration::from_secs(1 << attempt))
.min(MAX_RETRY_WAIT)
}
fn parse_retry_after(value: &str, now: chrono::DateTime<chrono::Utc>) -> Option<Duration> {
if let Ok(seconds) = value.parse::<u64>() {
return Some(Duration::from_secs(seconds));
}
let at = chrono::DateTime::parse_from_rfc2822(value).ok()?;
Some((at.to_utc() - now).to_std().unwrap_or_default())
}
pub fn decode_data<T: DeserializeOwned>(bytes: &[u8]) -> Result<T> {
if bytes.trim_ascii().is_empty() {
return decode(bytes); }
decode::<Envelope<T>>(bytes).map(|e| e.data).or_else(|err| {
match serde_json::from_slice::<Envelope<serde_json::Value>>(bytes) {
Ok(Envelope { data: serde_json::Value::Array(a) }) if a.is_empty() => {
serde_json::from_value(serde_json::json!({})).map_err(|_| err)
}
_ => Err(err),
}
})
}
#[derive(Deserialize)]
struct Envelope<T> {
data: T,
}
pub fn decode<T: DeserializeOwned>(bytes: &[u8]) -> Result<T> {
if bytes.trim_ascii().is_empty() {
return serde_json::from_value(serde_json::Value::Null).map_err(|source| Error::Decode {
source,
body: String::new(),
});
}
serde_json::from_slice(bytes).map_err(|source| Error::Decode {
source,
body: String::from_utf8_lossy(bytes).into_owned(),
})
}
fn normalize_base_url(base_url: &str, allow_insecure_http: bool) -> Result<String> {
let invalid = |why: &str| Error::InvalidBaseUrl(format!("{base_url}: {why}"));
let mut url = reqwest::Url::parse(base_url.trim()).map_err(|e| invalid(&e.to_string()))?;
match url.scheme() {
"https" => {}
"http" if allow_insecure_http || is_loopback(&url) => {}
"http" => {
return Err(invalid(
"http:// would send the token unencrypted; use https:// \
(or allow_insecure_http for a trusted network)",
));
}
_ => return Err(invalid("expected https://")),
}
if url.host_str().is_none_or(str::is_empty) {
return Err(invalid("no host"));
}
if url.query().is_some() || url.fragment().is_some() {
return Err(invalid("must not contain `?` or `#`"));
}
if !url.username().is_empty() || url.password().is_some() {
return Err(invalid("must not contain user name or password; pass a token instead"));
}
let path = url.path().trim_end_matches('/');
let path = path.strip_suffix("/api").unwrap_or(path).to_owned();
url.set_path(&format!("{path}/api"));
Ok(url.into())
}
fn check_path(path: &str) -> Result<()> {
let valid = path.strip_prefix('/').is_some_and(|rest| {
rest.split('/').all(|segment| {
let decoded = percent_decode_str(segment).decode_utf8_lossy();
!segment.is_empty() && decoded != "." && decoded != ".."
})
});
if valid { Ok(()) } else { Err(Error::InvalidPath(path.to_owned())) }
}
fn is_loopback(url: &reqwest::Url) -> bool {
let host = url.host_str().unwrap_or_default();
host == "localhost"
|| host
.trim_start_matches('[')
.trim_end_matches(']')
.parse::<std::net::IpAddr>()
.is_ok_and(|ip| ip.is_loopback())
}
const PATH_SEGMENT: &AsciiSet = &NON_ALPHANUMERIC.remove(b'-').remove(b'_').remove(b'.').remove(b'~');
pub fn encode_path_segment(value: impl std::fmt::Display) -> String {
utf8_percent_encode(&value.to_string(), PATH_SEGMENT).to_string()
}
#[cfg(test)]
mod tests {
use std::io::{BufRead, BufReader, Read, Write};
use super::*;
fn serve(responses: Vec<String>) -> (String, Arc<Mutex<Vec<String>>>) {
let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
let url = format!("http://{}", listener.local_addr().unwrap());
let seen = Arc::new(Mutex::new(Vec::new()));
let log = seen.clone();
std::thread::spawn(move || {
for (stream, response) in listener.incoming().zip(responses) {
let mut stream = stream.unwrap();
let mut reader = BufReader::new(stream.try_clone().unwrap());
let mut line = String::new();
reader.read_line(&mut line).unwrap();
let request: Vec<&str> = line.split_whitespace().take(2).collect();
log.lock().unwrap().push(request.join(" "));
let mut length = 0;
loop {
let mut header = String::new();
if reader.read_line(&mut header).unwrap() == 0 || header == "\r\n" {
break;
}
if let Some((name, value)) = header.split_once(':')
&& name.eq_ignore_ascii_case("content-length")
{
length = value.trim().parse().unwrap();
}
}
reader.read_exact(&mut vec![0; length]).unwrap();
stream.write_all(response.as_bytes()).unwrap();
}
});
(url, seen)
}
fn reply(status: &str, headers: &str, body: &str) -> String {
format!(
"HTTP/1.1 {status}\r\n{headers}Content-Length: {}\r\nConnection: close\r\n\r\n{body}",
body.len()
)
}
#[test]
fn connect_timeout_defaults_to_10s_but_not_above_timeout() {
let client = Client::new("https://x.church.tools", None).unwrap();
assert_eq!(client.connect_timeout, Duration::from_secs(10));
let short = Client::builder("https://x.church.tools").timeout(Duration::from_secs(3)).build().unwrap();
assert_eq!(short.connect_timeout, Duration::from_secs(3));
let custom = Client::builder("https://x.church.tools")
.timeout(Duration::from_secs(600))
.connect_timeout(Duration::from_millis(200))
.build()
.unwrap();
assert_eq!((custom.timeout, custom.connect_timeout), (Duration::from_secs(600), Duration::from_millis(200)));
let start = Instant::now();
let blackhole = Client::builder("http://10.255.255.1").allow_insecure_http(true);
let blackhole = blackhole.timeout(Duration::from_secs(600)).connect_timeout(Duration::from_millis(200));
assert!(blackhole.build().unwrap().request(Method::Get, "/x", &[], None).is_err());
assert!(start.elapsed() < Duration::from_secs(5));
}
#[test]
fn responses_above_the_size_limit_are_rejected() {
let declared = reply("200 OK", "", r#"{"data":"0123456789"}"#);
let undeclared = "HTTP/1.1 200 OK\r\nConnection: close\r\n\r\n{\"data\":\"0123456789\"}".to_owned();
let small = reply("200 OK", "", r#"{"data":1}"#);
let (url, _) = serve(vec![declared, undeclared, small]);
let client = Client::builder(&url).max_response_size(12).build().unwrap();
for _ in 0..2 {
let err = client.request(Method::Get, "/x", &[], None).unwrap_err();
assert!(matches!(err, Error::ResponseTooLarge { limit: 12 }), "{err}");
}
assert_eq!(client.request(Method::Get, "/x", &[], None).unwrap(), br#"{"data":1}"#);
}
#[test]
fn redirects_that_would_turn_a_write_into_a_get_are_errors() {
let (url, seen) = serve(vec![reply("302 Found", "Location: /api/elsewhere\r\n", "")]);
let client = Client::new(&url, None).unwrap();
let body = serde_json::json!({"name": "x"});
let err = client.request(Method::Post, "/groups", &[], Some(&body)).unwrap_err();
let Error::Redirect { status, location } = err else { panic!("{err}") };
assert_eq!((status, location.as_deref()), (302, Some("/api/elsewhere")));
assert_eq!(*seen.lock().unwrap(), ["POST /api/groups"]); }
#[test]
fn method_preserving_redirects_are_followed() {
let (url, seen) = serve(vec![
reply("307 Temporary Redirect", "Location: /api/moved\r\n", ""),
reply("200 OK", "", r#"{"data":1}"#),
]);
let client = Client::new(&url, None).unwrap();
let body = serde_json::json!({"name": "x"});
let data: i64 = client.request_data(Method::Post, "/groups", &[], Some(&body)).unwrap();
assert_eq!(data, 1);
assert_eq!(*seen.lock().unwrap(), ["POST /api/groups", "POST /api/moved"]);
}
#[test]
fn base_url_gets_api_suffix_once() {
for input in [
"https://x.church.tools",
"https://x.church.tools/",
"https://x.church.tools/api",
"https://x.church.tools/api/",
] {
assert_eq!(normalize_base_url(input, false).unwrap(), "https://x.church.tools/api");
}
assert!(normalize_base_url("x.church.tools", false).is_err());
assert_eq!(normalize_base_url(" https://X.Church.Tools ", false).unwrap(), "https://x.church.tools/api");
assert_eq!(normalize_base_url("https://example.org/ct/", false).unwrap(), "https://example.org/ct/api");
assert_eq!(normalize_base_url("https://example.org:8443/ct/api", false).unwrap(), "https://example.org:8443/ct/api");
for bad in [
"https://x.church.tools?a=b",
"https://x.church.tools/#top",
"https://user:pw@x.church.tools",
"ftp://x.church.tools",
"https://",
] {
assert!(normalize_base_url(bad, false).is_err(), "{bad}");
}
}
#[test]
fn plain_http_only_for_loopback_or_when_allowed() {
for local in ["http://localhost:8080", "http://127.0.0.1:1", "http://[::1]:80", "http://LOCALHOST"] {
assert!(normalize_base_url(local, false).is_ok(), "{local}");
}
let err = normalize_base_url("http://x.church.tools", false).unwrap_err();
assert!(err.to_string().contains("unencrypted"), "{err}");
assert!(normalize_base_url("http://localhost.evil.org", false).is_err());
assert_eq!(
normalize_base_url("http://ct.intern", true).unwrap(),
"http://ct.intern/api"
);
let client = Client::builder("http://ct.intern").allow_insecure_http(true).build().unwrap();
assert_eq!(client.api_url(), "http://ct.intern/api");
}
#[test]
fn pagination_stops_on_last_page_or_missing_meta() {
let page = |c: u64, l: u64| {
format!(r#"{{"data":[{{"id":1}}],"meta":{{"pagination":{{"current":{c},"lastPage":{l}}}}}}}"#)
};
assert_eq!(next_page_number(page(1, 3).as_bytes(), 1), Some(2));
assert_eq!(next_page_number(page(3, 3).as_bytes(), 3), None);
assert_eq!(next_page_number(page(1, 3).as_bytes(), 2), None);
assert_eq!(next_page_number(br#"{"data":[1],"meta":{"count":3}}"#, 1), None);
let empty = br#"{"data":[],"meta":{"pagination":{"current":1,"lastPage":1000000}}}"#;
assert_eq!(next_page_number(empty, 1), None);
let map = br#"{"data":{"a":1},"meta":{"pagination":{"current":1,"lastPage":2}}}"#;
assert_eq!(next_page_number(map, 1), Some(2));
}
#[test]
fn collect_pages_decodes_each_page_once_and_follows_meta() {
let page = |c: u64, items: &str| {
reply("200 OK", "", &format!(r#"{{"data":[{items}],"meta":{{"pagination":{{"current":{c},"lastPage":3}}}}}}"#))
};
let (url, seen) = serve(vec![page(1, "1,2"), page(2, "3"), page(3, "4")]);
let client = Client::new(&url, None).unwrap();
let query = vec![("limit".into(), "2".into()), ("page".into(), "9".into())];
let items: Vec<i64> = client.collect_pages("/x", query).unwrap();
assert_eq!(items, [1, 2, 3, 4]);
assert_eq!(seen.lock().unwrap().len(), 3);
let (url, _) = serve(vec![reply("200 OK", "", r#"{"data":[1],"meta":[]}"#)]);
let items: Vec<i64> = Client::new(&url, None).unwrap().collect_pages("/x", vec![]).unwrap();
assert_eq!(items, [1]);
}
#[test]
fn items_fetch_pages_only_when_needed() {
let page = |c: u64, items: &str| {
reply("200 OK", "", &format!(r#"{{"data":[{items}],"meta":{{"pagination":{{"current":{c},"lastPage":3}}}}}}"#))
};
let (url, seen) = serve(vec![page(1, "1,2"), page(2, "3"), page(3, "4")]);
let client = Client::new(&url, None).unwrap();
let items = client.items("/x", vec![]).prefetch(false);
let first: Vec<i64> = items.take(3).collect::<Result<_>>().unwrap();
assert_eq!(first, [1, 2, 3]);
assert_eq!(*seen.lock().unwrap(), ["GET /api/x?page=1", "GET /api/x?page=2"]);
let (url, _) = serve(vec![reply("404 Not Found", "", "{}")]);
let mut items = Client::new(&url, None).unwrap().items::<i64>("/x", vec![]);
assert!(matches!(items.next(), Some(Err(Error::Api(_)))));
assert!(items.next().is_none());
}
#[test]
fn prefetch_starts_after_the_second_page() {
let page = |c: u64, items: &str| {
reply("200 OK", "", &format!(r#"{{"data":[{items}],"meta":{{"pagination":{{"current":{c},"lastPage":4}}}}}}"#))
};
let (url, seen) = serve(vec![page(1, "1,2"), page(2, "3")]);
let client = Client::new(&url, None).unwrap();
let mut pages = client.pages("/x", vec![]);
assert!(pages.next().unwrap().is_ok());
drop(pages);
std::thread::sleep(Duration::from_millis(50));
assert_eq!(*seen.lock().unwrap(), ["GET /api/x?page=1"]);
let (url, seen) = serve(vec![page(1, "1,2"), page(2, "3"), reply("404 Not Found", "", "{}")]);
let client = Client::new(&url, None).unwrap();
let mut items = client.items::<i64>("/x", vec![]);
assert_eq!(items.by_ref().take(3).collect::<Result<Vec<_>>>().unwrap(), [1, 2, 3]);
let deadline = Instant::now() + Duration::from_secs(5);
while seen.lock().unwrap().len() < 3 && Instant::now() < deadline {
std::thread::sleep(Duration::from_millis(5));
}
assert_eq!(seen.lock().unwrap().len(), 3);
assert!(matches!(items.next(), Some(Err(Error::Api(_)))));
assert!(items.next().is_none());
}
#[test]
fn uploads_are_sent_with_content_and_retried() {
let (url, seen) = serve(vec![
reply("429 Too Many Requests", "Retry-After: 0\r\n", ""),
reply("200 OK", "", r#"{"data":[]}"#),
]);
let client = Client::new(&url, None).unwrap();
let file = Upload::new("brief.txt", b"Hallo Gemeinde".as_slice(), "text/plain");
let copy = file.clone();
assert!(Arc::ptr_eq(&file.content, ©.content)); let form = [("sortKey".to_owned(), "3".to_owned())];
client.request_multipart(Method::Post, "/files/person/7", &[], &form, "files[]", &[file]).unwrap();
assert_eq!(seen.lock().unwrap().len(), 2);
}
#[test]
fn json_bytes_are_sent_unchanged() {
let (url, _) = serve(vec![reply("200 OK", "", r#"{"data":1}"#)]);
let client = Client::new(&url, None).unwrap();
let body = br#"{"firstName": "Ben"}"#;
assert_eq!(client.request_json_bytes(Method::Post, "/x", &[], Some(body)).unwrap(), br#"{"data":1}"#);
}
#[test]
fn empty_body_is_none_for_optional_results() {
assert_eq!(decode::<Option<i64>>(b"").unwrap(), None);
assert_eq!(decode::<Option<i64>>(b"7").unwrap(), Some(7));
assert!(decode::<i64>(b" ").is_err());
}
#[test]
fn retry_after_uses_header_or_backoff() {
let mut headers = reqwest::header::HeaderMap::new();
assert_eq!(retry_after(&headers, 1), Duration::from_secs(2));
assert_eq!(retry_after(&headers, 3), Duration::from_secs(8));
headers.insert(reqwest::header::RETRY_AFTER, "5".parse().unwrap());
assert_eq!(retry_after(&headers, 1), Duration::from_secs(5));
headers.insert(reqwest::header::RETRY_AFTER, "3600".parse().unwrap());
assert_eq!(retry_after(&headers, 1), MAX_RETRY_WAIT);
headers.insert(reqwest::header::RETRY_AFTER, "soon".parse().unwrap());
assert_eq!(retry_after(&headers, 2), Duration::from_secs(4));
let now = "2026-10-21T07:28:00Z".parse().unwrap();
assert_eq!(parse_retry_after("Wed, 21 Oct 2026 07:28:10 GMT", now), Some(Duration::from_secs(10)));
assert_eq!(parse_retry_after("Wed, 21 Oct 2026 07:27:00 GMT", now), Some(Duration::ZERO));
}
#[test]
fn unavailable_server_is_retried_for_idempotent_methods_only() {
let unavailable = || reply("503 Service Unavailable", "Retry-After: 0\r\n", "");
let (url, seen) = serve(vec![unavailable(), reply("200 OK", "", r#"{"data":1}"#), unavailable()]);
let client = Client::new(&url, None).unwrap();
assert_eq!(client.request(Method::Get, "/x", &[], None).unwrap(), br#"{"data":1}"#);
let err = client.request(Method::Post, "/x", &[], None).unwrap_err();
assert!(matches!(&err, Error::Api(api) if api.status == 503), "{err}");
assert_eq!(*seen.lock().unwrap(), ["GET /api/x", "GET /api/x", "POST /api/x"]);
}
#[test]
fn refused_connection_is_retried_once() {
let port = std::net::TcpListener::bind("127.0.0.1:0").unwrap().local_addr().unwrap().port();
let client = Client::new(&format!("http://127.0.0.1:{port}"), None).unwrap();
let start = Instant::now();
let err = client.request(Method::Post, "/x", &[], None).unwrap_err();
assert!(matches!(&err, Error::Transport(e) if e.is_connect()), "{err}");
assert!(start.elapsed() >= CONNECT_RETRY_WAIT);
}
#[test]
fn throttle_spaces_requests() {
let throttle = Throttle {
interval: Duration::from_millis(30),
next: Mutex::new(Instant::now()),
};
let start = Instant::now();
for _ in 0..4 {
throttle.wait();
}
assert!(start.elapsed() >= Duration::from_millis(90));
}
#[test]
fn debug_output_hides_the_token() {
let client = Client::new("https://x.church.tools", Some("geheim123".into())).unwrap();
let debug = format!("{client:?}");
assert!(!debug.contains("geheim123"), "{debug}");
assert!(debug.contains("<redacted>") && debug.contains("https://x.church.tools/api"));
}
#[test]
fn dot_and_empty_segments_are_rejected() {
let client = Client::new("http://127.0.0.1:9", None).unwrap();
for id in ["..", ".", ""] {
let path = format!("/groups/7/members/{}", encode_path_segment(id));
let err = client.request(Method::Delete, &path, &[], None).unwrap_err();
assert!(matches!(err, Error::InvalidPath(_)), "{id:?}: {err}");
}
assert!(check_path("/groups/7/members/%2e%2E").is_err());
assert!(check_path("/groups/7/members/.%2e").is_err());
assert!(check_path("persons").is_err());
assert!(check_path("/persons/").is_err());
assert!(check_path("/persons/a.b/..x").is_ok());
assert_eq!(encode_path_segment("%2e%2e"), "%252e%252e");
assert!(check_path("/files/%252e%252e").is_ok());
}
#[test]
fn path_segments_are_encoded() {
assert_eq!(encode_path_segment("a b/c"), "a%20b%2Fc");
assert_eq!(encode_path_segment(42), "42");
}
}