#[cfg(feature = "http-client-cookies")]
use crate::api::http::cookies::CookieRequestContext;
use crate::api::http::http_request_method::HttpRequestMethod;
use crate::api::http::http_response::HttpResponseResult;
use crate::api::http::retry::HttpRetryPolicy;
use crate::api::retry::RetrySetting;
use async_trait::async_trait;
use bytes::Bytes;
use http::HeaderMap;
#[async_trait]
pub trait HttpRequestTrait: Send + 'static {
fn get_path(&self) -> String;
fn get_method(&self) -> HttpRequestMethod;
fn get_req_data(&self) -> String {
String::new()
}
fn get_req_body(&self) -> Bytes {
Bytes::from(self.get_req_data())
}
fn has_req_body(&self) -> bool {
false
}
fn headers(&self) -> HeaderMap {
HeaderMap::new()
}
#[cfg(feature = "http-client-cookies")]
fn cookie_context(&self) -> CookieRequestContext {
CookieRequestContext::default()
}
fn retry_setting(&self) -> RetrySetting<HttpRetryPolicy> {
RetrySetting::Inherit
}
fn on_response_headers(&self, _headers: &HeaderMap) {}
async fn deal_with_response(self: Box<Self>, result: HttpResponseResult);
}
#[async_trait]
impl<T> HttpRequestTrait for Box<T>
where
T: HttpRequestTrait + ?Sized,
{
fn get_path(&self) -> String {
(**self).get_path()
}
fn get_method(&self) -> HttpRequestMethod {
(**self).get_method()
}
fn get_req_data(&self) -> String {
(**self).get_req_data()
}
fn get_req_body(&self) -> Bytes {
(**self).get_req_body()
}
fn has_req_body(&self) -> bool {
(**self).has_req_body()
}
fn headers(&self) -> HeaderMap {
(**self).headers()
}
#[cfg(feature = "http-client-cookies")]
fn cookie_context(&self) -> CookieRequestContext {
(**self).cookie_context()
}
fn retry_setting(&self) -> RetrySetting<HttpRetryPolicy> {
(**self).retry_setting()
}
fn on_response_headers(&self, headers: &HeaderMap) {
(**self).on_response_headers(headers);
}
async fn deal_with_response(self: Box<Self>, result: HttpResponseResult) {
(*self).deal_with_response(result).await;
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::api::http::http_response::HttpResponse;
use http::StatusCode;
type TestResult = Result<(), Box<dyn std::error::Error + Send + Sync>>;
fn check(condition: bool, message: &str) -> TestResult {
if condition {
Ok(())
} else {
Err(std::io::Error::other(message).into())
}
}
struct EmptyRequest;
#[async_trait]
impl HttpRequestTrait for EmptyRequest {
fn get_path(&self) -> String {
"/health".to_owned()
}
fn get_method(&self) -> HttpRequestMethod {
HttpRequestMethod::GET
}
async fn deal_with_response(self: Box<Self>, result: HttpResponseResult) {
let _ = result;
}
}
#[test]
fn defaults_keep_request_contract_replay_safe_and_header_free() -> TestResult {
let request = EmptyRequest;
check(request.get_path() == "/health", "path contract changed")?;
check(
request.get_method() == HttpRequestMethod::GET,
"method contract changed",
)?;
check(
request.get_req_data().is_empty(),
"default body must be empty",
)?;
check(
request.headers().is_empty(),
"default headers must be empty",
)?;
check(
matches!(request.retry_setting(), RetrySetting::Inherit),
"default requests must not retry",
)
}
#[tokio::test]
async fn callback_receives_owned_result_contract() -> TestResult {
let response = HttpResponse::new(
StatusCode::NO_CONTENT,
HeaderMap::new(),
bytes::Bytes::new(),
1,
crate::api::http::http_response::HttpRequestId(1),
);
Box::new(EmptyRequest)
.deal_with_response(Ok(response))
.await;
Ok(())
}
}