Struct Builder

Source
pub struct Builder<R, T, RequestState> { /* private fields */ }
Expand description

Builder for creating crate::mock::Mock services and testing them with a tower_async_layer::Layer.

This generic builder is designed to make it easy to test your own tower_async_layer::Layer in a type-safe and guided manner. It achieves this by using the “Builder” pattern and a secure state flow guided by the Rust type system.

§Flow

                              define another cycle
                              ┌───────┐
                              │       │
                              │       │
┌───────────────┐     ┌───────▼───────┴───────┐              ┌─────────────────────────┐
│Define         │     │Define <Response>      │              │Execute and await the    │
│<Input> Request├─────►or <Error> to send from├──────────────►test using the previously│
└───────────────┘     │within the Mock Service│              │defined flow and using   │
                      │to your Layer.         │   ┌──────────►the passed in Layer to   │
                      └──────▲─────┬──────────┘   │          │generate the service used│
                             │     │              │          │for testing.             │
                   define    │     │              │          └─────────────┬───────────┘
                   another   │     │              │                        │
                   cycle     │     │              │                        │
                       ┌─────┴─────▼──────────────┴─┐        ┌─────────────▼─────────────┐
                       │Define the expected         │        │Optionally expect the final│
                       │<Request> received by       │        │Output or Error (Result).  │
                       │the Mock Service (Optional).│        │                           │
                       └────────────────────────────┘        └───────────────────────────┘

§Examples

use tower_async_test::Builder;
use tower_async_layer::Identity;

#[tokio::main]
async fn main() {
    Builder::new("ping")
        .send_response("pong")
        .expect_request("ping")
        .test(Identity::new())
        .await
        .expect_response("pong");
}

Implementations§

Source§

impl<R> Builder<R, None, None>

Virgin Builder

Source

pub fn new(request: R) -> Self

Creates a new builder with the given request.

Source

pub fn send_response<Response>( self, response: Response, ) -> Builder<R, Vec<Test<R, Ok<Response>>>, None>

Register the sending of a (successful) response.

Source

pub fn send_error<Error>( self, error: Error, ) -> Builder<R, Vec<Test<R, Err<Error>>>, None>

Register the sending of an error.

Source§

impl<R, Response, RequestState> Builder<R, Vec<Test<R, Ok<Response>>>, RequestState>

Ok-only test builder

Source

pub fn send_response( self, response: Response, ) -> Builder<R, Vec<Test<R, Ok<Response>>>, None>

Register the sending of an additional (successful) response.

Source

pub fn send_error<Error>( self, error: Error, ) -> Builder<R, Vec<Test<R, Result<Response, Error>>>, None>

Register the sending of an additional error.

Source§

impl<R, Response, RequestState> Builder<R, Vec<Test<R, Ok<Response>>>, RequestState>
where R: Send + Sync + Debug + PartialEq, Response: Send + Sync,

Source

pub async fn test<L>( self, layer: L, ) -> ResponseTester<<<L as Layer<Mock<R, Response, Infallible>>>::Service as Service<R>>::Response, <<L as Layer<Mock<R, Response, Infallible>>>::Service as Service<R>>::Error>
where L: Layer<Mock<R, Response, Infallible>>, L::Service: Service<R>,

Test the given tower_async_layer::Layer with the previously registered tests.

§Panics

Panics if there are less requests returned then there are responses+errors registered.

Source§

impl<R, Response> Builder<R, Vec<Test<R, Ok<Response>>>, None>

Source

pub fn expect_request( self, request: R, ) -> Builder<R, Vec<Test<R, Ok<Response>>>, Defined>

Register the expectation of a request, for the same cycle as the previously added successful response.

Source§

impl<R, Error, RequestState> Builder<R, Vec<Test<R, Err<Error>>>, RequestState>

Error-only test builder

Source

pub fn send_response<Response>( self, response: Response, ) -> Builder<R, Vec<Test<R, Result<Response, Error>>>, None>

Register the sending of an additional (successful) response.

Source

pub fn send_error( self, error: Error, ) -> Builder<R, Vec<Test<R, Err<Error>>>, None>

Register the sending of an additional error.

Source§

impl<R, Error, RequestState> Builder<R, Vec<Test<R, Err<Error>>>, RequestState>
where R: Send + Sync + Debug + PartialEq, Error: Send + Sync,

Source

pub async fn test<L>( self, layer: L, ) -> ResponseTester<<<L as Layer<Mock<R, (), Error>>>::Service as Service<R>>::Response, <<L as Layer<Mock<R, (), Error>>>::Service as Service<R>>::Error>
where L: Layer<Mock<R, (), Error>>, L::Service: Service<R>,

Test the given layer with the previously registered tests.

§Panics

Panics if there are less requests returned then there are responses+errors registered.

Source§

impl<R, Error> Builder<R, Vec<Test<R, Err<Error>>>, None>

Source

pub fn expect_request( self, request: R, ) -> Builder<R, Vec<Test<R, Err<Error>>>, Defined>

Register the expectation of a request, for the same cycle as the previously added error.

Source§

impl<R, Response, Error, RequestState> Builder<R, Vec<Test<R, Result<Response, Error>>>, RequestState>

Full Result (Ok+Err mix) test builder

Source

pub fn send_response( self, response: Response, ) -> Builder<R, Vec<Test<R, Result<Response, Error>>>, None>

Register the sending of an additional (successful) response.

Source

pub fn send_error( self, error: Error, ) -> Builder<R, Vec<Test<R, Result<Response, Error>>>, None>

Register the sending of an additional error.

Source§

impl<R, Response, Error, RequestState> Builder<R, Vec<Test<R, Result<Response, Error>>>, RequestState>
where R: Send + Sync + Debug + PartialEq, Response: Send + Sync, Error: Send + Sync,

Source

pub async fn test<L>( self, layer: L, ) -> ResponseTester<<<L as Layer<Mock<R, Response, Error>>>::Service as Service<R>>::Response, <<L as Layer<Mock<R, Response, Error>>>::Service as Service<R>>::Error>
where L: Layer<Mock<R, Response, Error>>, L::Service: Service<R>,

Test the given layer with the previously registered tests.

§Panics

Panics if there are less requests returned then there are responses+errors registered.

Source§

impl<R, Response, Error> Builder<R, Vec<Test<R, Result<Response, Error>>>, None>

Source

pub fn expect_request( self, request: R, ) -> Builder<R, Vec<Test<R, Result<Response, Error>>>, Defined>

Register the expectation of a request, for the same cycle as the previously added result.

Trait Implementations§

Source§

impl<R: Debug, T: Debug, RequestState: Debug> Debug for Builder<R, T, RequestState>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<R, T, RequestState> Freeze for Builder<R, T, RequestState>
where R: Freeze, T: Freeze, RequestState: Freeze,

§

impl<R, T, RequestState> RefUnwindSafe for Builder<R, T, RequestState>
where R: RefUnwindSafe, T: RefUnwindSafe, RequestState: RefUnwindSafe,

§

impl<R, T, RequestState> Send for Builder<R, T, RequestState>
where R: Send, T: Send, RequestState: Send,

§

impl<R, T, RequestState> Sync for Builder<R, T, RequestState>
where R: Sync, T: Sync, RequestState: Sync,

§

impl<R, T, RequestState> Unpin for Builder<R, T, RequestState>
where R: Unpin, T: Unpin, RequestState: Unpin,

§

impl<R, T, RequestState> UnwindSafe for Builder<R, T, RequestState>
where R: UnwindSafe, T: UnwindSafe, RequestState: UnwindSafe,

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, 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, 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.