Struct CaseBuilder

Source
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>

Source

pub fn with<W, E, R>(self, with: W) -> CaseBuilder<'c, W>
where for<'r> W: Fn(&'r Request<String>) -> Result<R, E>, R: Into<Report>, E: StdError + Send + Sync + 'static,

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")?;
Source

pub fn with_uri<U>(self, uri: U) -> CaseBuilder<'c, WithHandler>
where U: TryInto<Uri>, U::Error: Into<Error>,

Match requests with the specified Uri

§Example
let mut builder = Connector::builder();
builder
    .expect()
    .with_uri("https://example.test/hello")
    .returning("OK")?;
§Remark

You can combine this with other validators, such as with_header, but not with with.

Source

pub fn with_method<M>(self, method: M) -> CaseBuilder<'c, WithHandler>
where M: TryInto<Method>, M::Error: Into<Error>,

Match requests with the specified Method

§Example
let mut builder = Connector::builder();
builder
    .expect()
    .with_method("GET")
    .returning("OK")?;
§Remark

You can combine this with other validators, such as with_uri, but not with with.

Source

pub fn with_header<K, V>(self, key: K, value: V) -> CaseBuilder<'c, WithHandler>

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.

Source

pub fn with_header_once<K, V>( self, key: K, value: V, ) -> CaseBuilder<'c, WithHandler>

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.

Source

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.

Source

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.

Source

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.

Source

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>

Source

pub fn times(self, count: usize) -> Self

Mark how many times this mock case can be called

Nothing enforces how many times a mock case is called, but you can use the checkpoint method on the Connector to ensure all methods were called the right amount of times.

Source§

impl<W> CaseBuilder<'_, W>
where W: With + 'static,

Source

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more