Struct tower_async_test::builder::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>
impl<R> Builder<R, None, None>
Virgin Builder
source§impl<R, Response, RequestState> Builder<R, Vec<Test<R, Ok<Response>>>, RequestState>
impl<R, Response, RequestState> Builder<R, Vec<Test<R, Ok<Response>>>, RequestState>
Ok-only test builder
source§impl<R, Response, RequestState> Builder<R, Vec<Test<R, Ok<Response>>>, RequestState>where
R: Send + Sync + Debug + PartialEq,
Response: Send + Sync,
impl<R, Response, RequestState> Builder<R, Vec<Test<R, Ok<Response>>>, RequestState>where R: Send + Sync + Debug + PartialEq, Response: Send + Sync,
sourcepub 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>,
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, Error, RequestState> Builder<R, Vec<Test<R, Err<Error>>>, RequestState>
impl<R, Error, RequestState> Builder<R, Vec<Test<R, Err<Error>>>, RequestState>
Error-only test builder
source§impl<R, Error, RequestState> Builder<R, Vec<Test<R, Err<Error>>>, RequestState>where
R: Send + Sync + Debug + PartialEq,
Error: Send + Sync,
impl<R, Error, RequestState> Builder<R, Vec<Test<R, Err<Error>>>, RequestState>where R: Send + Sync + Debug + PartialEq, Error: Send + Sync,
sourcepub 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>,
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, Response, Error, RequestState> Builder<R, Vec<Test<R, Result<Response, Error>>>, RequestState>
impl<R, Response, Error, RequestState> Builder<R, Vec<Test<R, Result<Response, Error>>>, RequestState>
Full Result (Ok+Err mix) test builder
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,
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,
sourcepub 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>,
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.