pub struct CaseBuilder<'c, W = DefaultWith> { /* private fields */ }
Expand description
Builder for specific mock cases
§Example
let mut builder = Connector::builder();
let mut case_builder = builder.expect();
case_builder
.with_uri("https://test.example/some/path")
.times(3)
.returning("Some response")?;
Implementations§
Source§impl<'c> CaseBuilder<'c>
impl<'c> CaseBuilder<'c>
Sourcepub fn with<W, E, R>(self, with: W) -> CaseBuilder<'c, W>
pub fn with<W, E, R>(self, with: W) -> CaseBuilder<'c, W>
Pass a function or closure to check if the incoming payload matches this mock case
If you only need to validate the Uri
, Method
, headers, or incoming payload, you
should use one of the other with_*
methods. You also cannot combine this validator with
the other with
methods.
§Example
let mut builder = Connector::builder();
builder
.expect()
.with(|req: &Request<String>| Ok::<_, Infallible>(req.body().contains("hello")))
.returning("OK")?;
Sourcepub fn with_uri<U>(self, uri: U) -> CaseBuilder<'c, WithHandler>
pub fn with_uri<U>(self, uri: U) -> CaseBuilder<'c, WithHandler>
Sourcepub fn with_method<M>(self, method: M) -> CaseBuilder<'c, WithHandler>
pub fn with_method<M>(self, method: M) -> CaseBuilder<'c, WithHandler>
Sourcepub fn with_header<K, V>(self, key: K, value: V) -> CaseBuilder<'c, WithHandler>where
K: TryInto<HeaderName>,
K::Error: Into<Error>,
V: TryInto<HeaderValue>,
V::Error: Into<Error>,
pub fn with_header<K, V>(self, key: K, value: V) -> CaseBuilder<'c, WithHandler>where
K: TryInto<HeaderName>,
K::Error: Into<Error>,
V: TryInto<HeaderValue>,
V::Error: Into<Error>,
Match requests that contains the specific header
An HTTP request can contain multiple headers with the same key, but different values. This
checks that there is at least one value matching. If you want to ensure that there is only
one entry for this key, consider using with_header_once
.
§Example
let mut builder = Connector::builder();
builder
.expect()
.with_header("content-type", "application/json")
.returning("OK")?;
§Remark
You can combine this with other validators, such as with_uri
, but not with with
.
Sourcepub fn with_header_once<K, V>(
self,
key: K,
value: V,
) -> CaseBuilder<'c, WithHandler>where
K: TryInto<HeaderName>,
K::Error: Into<Error>,
V: TryInto<HeaderValue>,
V::Error: Into<Error>,
pub fn with_header_once<K, V>(
self,
key: K,
value: V,
) -> CaseBuilder<'c, WithHandler>where
K: TryInto<HeaderName>,
K::Error: Into<Error>,
V: TryInto<HeaderValue>,
V::Error: Into<Error>,
Match requests that contains the specific header
An HTTP request can contain multiple headers with the same key, but different values. This checks that there is only one value for the given header.
§Example
let mut builder = Connector::builder();
builder
.expect()
.with_header_once("content-type", "application/json")
.returning("OK")?;
§Remark
You can combine this with other validators, such as with_uri
, but not with with
.
Sourcepub fn with_header_all<K, IV, V>(
self,
key: K,
values: IV,
) -> CaseBuilder<'c, WithHandler>where
K: TryInto<HeaderName>,
K::Error: Into<Error>,
IV: IntoIterator<Item = V>,
V: TryInto<HeaderValue>,
V::Error: Into<Error>,
pub fn with_header_all<K, IV, V>(
self,
key: K,
values: IV,
) -> CaseBuilder<'c, WithHandler>where
K: TryInto<HeaderName>,
K::Error: Into<Error>,
IV: IntoIterator<Item = V>,
V: TryInto<HeaderValue>,
V::Error: Into<Error>,
Match requests that contains the specific header
An HTTP request can contain multiple headers with the same key, but different values. This checks that all entries correspond to the given set of values.
If you want to check that a header name has multiple values, but do not mind if there are
additional values, you can use with_header
multiple times instead.
If you want to ensure that a header name only has one value, you can use with_header_once
instead.
§Example
let mut builder = Connector::builder();
builder
.expect()
.with_header_all("content-type", ["application/json", "text/html"])
.returning("OK")?;
§Remark
You can combine this with other validators, such as with_uri
, but not with with
.
Sourcepub fn with_body<B>(self, body: B) -> CaseBuilder<'c, WithHandler>where
B: ToString,
pub fn with_body<B>(self, body: B) -> CaseBuilder<'c, WithHandler>where
B: ToString,
Match requests that contains the provided payload
§Example
let mut builder = Connector::builder();
builder
.expect()
.with_body("some body")
.returning("OK")?;
§Remark
You can combine this with other validators, such as with_uri
, but not with with
.
A mock case only supports with_body
, with_json
, or with_json_value
, but not multiple
ones at the same time.
Sourcepub fn with_json<V>(self, value: V) -> CaseBuilder<'c, WithHandler>where
V: Serialize,
pub fn with_json<V>(self, value: V) -> CaseBuilder<'c, WithHandler>where
V: Serialize,
Match requests with a body that exactly matches the provided JSON payload
§Example
let mut builder = Connector::builder();
builder
.expect()
.with_json(serde_json::json!({"status": "OK"}))
.returning("OK")?;
§Remark
You can combine this with other validators, such as with_uri
, but not with with
.
A mock case only supports with_body
, with_json
, or with_json_value
, but not multiple
ones at the same time.
Sourcepub fn with_json_partial<V>(self, value: V) -> CaseBuilder<'c, WithHandler>where
V: Serialize,
pub fn with_json_partial<V>(self, value: V) -> CaseBuilder<'c, WithHandler>where
V: Serialize,
Match requests that contains the provided JSON payload, but may contain other properties
You can combine this with other validators, such as with_uri
, but not with with
.
Source§impl<W> CaseBuilder<'_, W>
impl<W> CaseBuilder<'_, W>
Source§impl<W> CaseBuilder<'_, W>where
W: With + 'static,
impl<W> CaseBuilder<'_, W>where
W: With + 'static,
Sourcepub fn returning<R>(self, returning: R) -> Result<(), Error>where
R: Returning + 'static,
pub fn returning<R>(self, returning: R) -> Result<(), Error>where
R: Returning + 'static,
Mark what will generate the response for a given mock case
You can either pass a static value, or a function or closure that takes a Request<String>
as an input.
See the documentation for Returning
to see the full list of what is accepted by this
method.
§Errors
This will fail if any of the previous steps in CaseBuilder
failed, or if it fails to
store the case into the connector.
Auto Trait Implementations§
impl<'c, W = DefaultWith> !Freeze for CaseBuilder<'c, W>
impl<'c, W = DefaultWith> !RefUnwindSafe for CaseBuilder<'c, W>
impl<'c, W> Send for CaseBuilder<'c, W>where
W: Send,
impl<'c, W> Sync for CaseBuilder<'c, W>where
W: Sync,
impl<'c, W> Unpin for CaseBuilder<'c, W>where
W: Unpin,
impl<'c, W = DefaultWith> !UnwindSafe for CaseBuilder<'c, W>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more