use body::Body;
use client::stub::error::RegisterStubError;
use client::stub::{StubClient, StubRequest, StubResponse};
use reqwest::header::{HeaderMap, HeaderValue, IntoHeaderName};
use reqwest::{Method, StatusCode, Url};
#[must_use]
pub struct RequestStubber<'cl> {
client: &'cl mut StubClient,
url: Url,
_method: Option<Method>,
_body: Option<Body>,
_headers: Option<HeaderMap>,
}
impl<'cl> RequestStubber<'cl> {
pub(super) fn new(client: &'cl mut StubClient, url: Url) -> RequestStubber<'cl> {
RequestStubber {
client: client,
url: url,
_method: None,
_body: None,
_headers: None,
}
}
pub fn method(mut self, method: Method) -> Self {
self._method = Some(method);
self
}
pub fn body<B: Into<Body>>(mut self, body: B) -> Self {
self._body = Some(body.into());
self
}
pub fn header<HN: IntoHeaderName>(mut self, name: HN, value: HeaderValue) -> Self {
self._headers = Some(self._headers.map_or_else(HeaderMap::new, |mut hs| {
hs.insert(name, value);
hs
}));
self
}
pub fn headers(mut self, headers: HeaderMap) -> Self {
self._headers = Some(self._headers.map_or_else(HeaderMap::new, |mut hs| {
hs.extend(headers);
hs
}));
self
}
pub fn response(self) -> ResponseStubber<'cl> {
ResponseStubber {
client: self.client,
req: StubRequest {
url: self.url,
method: self._method,
body: self._body,
headers: self._headers.map(|hs| ::helper::serialize_headers(&hs)),
},
_status_code: StatusCode::OK,
_body: None,
_headers: HeaderMap::new(),
}
}
}
#[must_use]
pub struct ResponseStubber<'cl> {
client: &'cl mut StubClient,
req: StubRequest,
_status_code: StatusCode,
_body: Option<Body>,
_headers: HeaderMap,
}
impl<'cl> ResponseStubber<'cl> {
pub fn status_code(mut self, status: StatusCode) -> Self {
self._status_code = status;
self
}
pub fn body<B: Into<Body>>(mut self, body: B) -> Self {
self._body = Some(body.into());
self
}
pub fn header<HN: IntoHeaderName>(mut self, name: HN, value: HeaderValue) -> Self {
self._headers.insert(name, value);
self
}
pub fn headers(mut self, headers: HeaderMap) -> Self {
self._headers.extend(headers);
self
}
pub fn mock(self) -> Result<(), RegisterStubError> {
let resp = StubResponse {
status_code: self._status_code,
body: self._body,
headers: self._headers,
};
self.client.register_stub(
self.req
.try_to_key()
.map_err(|e| RegisterStubError::ReadFile(e))?,
resp,
)
}
}