pub struct MockService<Request, Response, Assertion, Error = BoxError> { /* private fields */ }Expand description
A service implementation that allows intercepting requests for checking them.
The type is generic over the request and response types, and also has an extra generic type
parameter that’s used as a tag to determine if the internal assertions should panic or return
errors for proptest minimization. See [AssertionType] for more information.
The mock service can be cloned, and provides methods for checking the received requests as well as responding to them individually.
Internally, the instance that’s operating as the service will forward requests to a
broadcast channel that the other instances listen to.
See the module-level documentation for an example.
Implementations§
Source§impl MockService<(), (), ()>
An entry point for starting the MockServiceBuilder.
impl MockService<(), (), ()>
An entry point for starting the MockServiceBuilder.
This impl block exists for ergonomic reasons. The generic type parameters don’t matter,
because they are actually set by MockServiceBuilder::finish.
Sourcepub fn build() -> MockServiceBuilder
pub fn build() -> MockServiceBuilder
Create a MockServiceBuilder to help with the creation of a MockService.
Source§impl<Request, Response, Error> MockService<Request, Response, PanicAssertion, Error>
Implementation of MockService methods that use standard Rust panicking assertions.
impl<Request, Response, Error> MockService<Request, Response, PanicAssertion, Error>
Implementation of MockService methods that use standard Rust panicking assertions.
Sourcepub async fn expect_request(
&mut self,
expected: Request,
) -> ResponseSender<Request, Response, Error>
pub async fn expect_request( &mut self, expected: Request, ) -> ResponseSender<Request, Response, Error>
Expect a specific request to be received.
The expected request should be the next one in the internal queue, or if the queue is
empty, it should be received in at most the max delay time configured by
MockServiceBuilder::with_max_request_delay.
If the received request matches the expected request, a ResponseSender is returned
which can be used to inspect the request and respond to it. If no response is sent, the
sender of the requests receives an error.
§Panics
If no request is received or if a request is received that’s not equal to the expected request, this method panics.
§Example
let call = tokio::spawn(mock_service.clone().oneshot("request"));
mock_service.expect_request("request").await.respond("response");
assert!(matches!(call.await, Ok(Ok("response"))));Sourcepub async fn expect_request_that(
&mut self,
condition: impl FnOnce(&Request) -> bool,
) -> ResponseSender<Request, Response, Error>where
Request: Debug,
pub async fn expect_request_that(
&mut self,
condition: impl FnOnce(&Request) -> bool,
) -> ResponseSender<Request, Response, Error>where
Request: Debug,
Expect a request to be received that matches a specified condition.
There should be a request already in the internal queue, or a request should be received in
at most the max delay time configured by MockServiceBuilder::with_max_request_delay.
The received request is passed to the condition function, which should return true if
it matches the expected condition or false otherwise. If true is returned, a
ResponseSender is returned which can be used to inspect the request again and respond
to it. If no response is sent, the sender of the requests receives an error.
§Panics
If the condition function returns false, this method panics.
§Example
let call = tokio::spawn(mock_service.clone().oneshot(1));
mock_service.expect_request_that(|request| *request > 0).await.respond("response");
assert!(matches!(call.await, Ok(Ok("response"))));Sourcepub async fn expect_no_requests(&mut self)where
Request: Debug,
pub async fn expect_no_requests(&mut self)where
Request: Debug,
Expect no requests to be received.
The internal queue of received requests should be empty, and no new requests should arrive
for the max delay time configured by MockServiceBuilder::with_max_request_delay.
§Panics
If the queue is not empty or if a request is received before the max request delay timeout expires.
§Example
mock_service.expect_no_requests().await;Sourcepub fn poll_count(&self) -> usize
pub fn poll_count(&self) -> usize
Returns a count of the number of times this service has been polled.
Note: The poll count wraps around on overflow.
Source§impl<Request, Response, Error> MockService<Request, Response, PropTestAssertion, Error>
Implementation of MockService methods that use proptest assertions.
impl<Request, Response, Error> MockService<Request, Response, PropTestAssertion, Error>
Implementation of MockService methods that use proptest assertions.
Sourcepub async fn expect_request(
&mut self,
expected: Request,
) -> Result<ResponseSender<Request, Response, Error>, TestCaseError>
pub async fn expect_request( &mut self, expected: Request, ) -> Result<ResponseSender<Request, Response, Error>, TestCaseError>
Expect a specific request to be received.
The expected request should be the next one in the internal queue, or if the queue is
empty, it should be received in at most the max delay time configured by
MockServiceBuilder::with_max_request_delay.
If the received request matches the expected request, a ResponseSender is returned
which can be used to inspect the request and respond to it. If no response is sent, the
sender of the requests receives an error.
If no request is received or if a request is received that’s not equal to the expected
request, this method returns an error generated by a proptest assertion.
§Example
let call = tokio::spawn(mock_service.clone().oneshot("request"));
// NOTE: The try operator `?` is required for errors to be handled by proptest.
mock_service
.expect_request("request").await?
.respond("response");
prop_assert!(matches!(call.await, Ok(Ok("response"))));Sourcepub async fn expect_request_that(
&mut self,
condition: impl FnOnce(&Request) -> bool,
) -> Result<ResponseSender<Request, Response, Error>, TestCaseError>where
Request: Debug,
pub async fn expect_request_that(
&mut self,
condition: impl FnOnce(&Request) -> bool,
) -> Result<ResponseSender<Request, Response, Error>, TestCaseError>where
Request: Debug,
Expect a request to be received that matches a specified condition.
There should be a request already in the internal queue, or a request should be received in
at most the max delay time configured by MockServiceBuilder::with_max_request_delay.
The received request is passed to the condition function, which should return true if
it matches the expected condition or false otherwise. If true is returned, a
ResponseSender is returned which can be used to inspect the request again and respond
to it. If no response is sent, the sender of the requests receives an error.
If the condition function returns false, this method returns an error generated by a
proptest assertion.
§Example
let call = tokio::spawn(mock_service.clone().oneshot(1));
// NOTE: The try operator `?` is required for errors to be handled by proptest.
mock_service
.expect_request_that(|request| *request > 0).await?
.respond("OK");
prop_assert!(matches!(call.await, Ok(Ok("OK"))));Sourcepub async fn expect_no_requests(&mut self) -> Result<(), TestCaseError>where
Request: Debug,
pub async fn expect_no_requests(&mut self) -> Result<(), TestCaseError>where
Request: Debug,
Expect no requests to be received.
The internal queue of received requests should be empty, and no new requests should arrive
for the max delay time configured by MockServiceBuilder::with_max_request_delay.
If the queue is not empty or if a request is received before the max request delay timeout
expires, an error generated by a proptest assertion is returned.
§Example
// NOTE: The try operator `?` is required for errors to be handled by proptest.
mock_service.expect_no_requests().await?;Sourcepub fn poll_count(&self) -> usize
pub fn poll_count(&self) -> usize
Returns a count of the number of times this service has been polled.
Note: The poll count wraps around on overflow.
Source§impl<Request, Response, Assertion, Error> MockService<Request, Response, Assertion, Error>
Code that is independent of the assertions used in MockService.
impl<Request, Response, Assertion, Error> MockService<Request, Response, Assertion, Error>
Code that is independent of the assertions used in MockService.
Sourcepub async fn try_next_request(
&mut self,
) -> Option<ResponseSender<Request, Response, Error>>
pub async fn try_next_request( &mut self, ) -> Option<ResponseSender<Request, Response, Error>>
Try to get the next request received.
Returns the next element in the queue. If the queue is empty, waits at most the max request
delay configured by MockServiceBuilder::with_max_request_delay for a request, and
returns it.
If no request is received, returns None.
If too many requests are received and the queue fills up, the oldest requests are dropped
and ignored. This means that calling this may not receive the next request if the queue is
not dimensioned properly with the MockServiceBuilder::with_proxy_channel_size method.
Trait Implementations§
Source§impl<Request, Response, Assertion, Error> Clone for MockService<Request, Response, Assertion, Error>
impl<Request, Response, Assertion, Error> Clone for MockService<Request, Response, Assertion, Error>
Source§fn clone(&self) -> Self
fn clone(&self) -> Self
Clones the MockService.
This is a cheap operation, because it simply clones the broadcast channel endpoints.
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<Request, Response, Assertion, Error> Service<Request> for MockService<Request, Response, Assertion, Error>
The tower::Service implementation of the MockService.
impl<Request, Response, Assertion, Error> Service<Request> for MockService<Request, Response, Assertion, Error>
The tower::Service implementation of the MockService.
The MockService is always ready, and it intercepts the requests wrapping them in a
ResponseSender which can be used to send a response.
Source§type Future = Pin<Box<dyn Future<Output = Result<<MockService<Request, Response, Assertion, Error> as Service<Request>>::Response, <MockService<Request, Response, Assertion, Error> as Service<Request>>::Error>> + Send>>
type Future = Pin<Box<dyn Future<Output = Result<<MockService<Request, Response, Assertion, Error> as Service<Request>>::Response, <MockService<Request, Response, Assertion, Error> as Service<Request>>::Error>> + Send>>
Auto Trait Implementations§
impl<Request, Response, Assertion, Error> Freeze for MockService<Request, Response, Assertion, Error>
impl<Request, Response, Assertion, Error = Box<dyn Error + Send + Sync>> !RefUnwindSafe for MockService<Request, Response, Assertion, Error>
impl<Request, Response, Assertion, Error> Send for MockService<Request, Response, Assertion, Error>
impl<Request, Response, Assertion, Error> Sync for MockService<Request, Response, Assertion, Error>
impl<Request, Response, Assertion, Error> Unpin for MockService<Request, Response, Assertion, Error>where
Assertion: Unpin,
impl<Request, Response, Assertion, Error = Box<dyn Error + Send + Sync>> !UnwindSafe for MockService<Request, Response, Assertion, Error>
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> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
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> 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 moreSource§impl<S, Request> IsReady<Request> for S
impl<S, Request> IsReady<Request> for S
Source§impl<D> OwoColorize for D
impl<D> OwoColorize for D
Source§fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>where
C: Color,
fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>where
C: Color,
Source§fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>where
C: Color,
fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>where
C: Color,
Source§fn black(&self) -> FgColorDisplay<'_, Black, Self>
fn black(&self) -> FgColorDisplay<'_, Black, Self>
Source§fn on_black(&self) -> BgColorDisplay<'_, Black, Self>
fn on_black(&self) -> BgColorDisplay<'_, Black, Self>
Source§fn red(&self) -> FgColorDisplay<'_, Red, Self>
fn red(&self) -> FgColorDisplay<'_, Red, Self>
Source§fn on_red(&self) -> BgColorDisplay<'_, Red, Self>
fn on_red(&self) -> BgColorDisplay<'_, Red, Self>
Source§fn green(&self) -> FgColorDisplay<'_, Green, Self>
fn green(&self) -> FgColorDisplay<'_, Green, Self>
Source§fn on_green(&self) -> BgColorDisplay<'_, Green, Self>
fn on_green(&self) -> BgColorDisplay<'_, Green, Self>
Source§fn yellow(&self) -> FgColorDisplay<'_, Yellow, Self>
fn yellow(&self) -> FgColorDisplay<'_, Yellow, Self>
Source§fn on_yellow(&self) -> BgColorDisplay<'_, Yellow, Self>
fn on_yellow(&self) -> BgColorDisplay<'_, Yellow, Self>
Source§fn blue(&self) -> FgColorDisplay<'_, Blue, Self>
fn blue(&self) -> FgColorDisplay<'_, Blue, Self>
Source§fn on_blue(&self) -> BgColorDisplay<'_, Blue, Self>
fn on_blue(&self) -> BgColorDisplay<'_, Blue, Self>
Source§fn magenta(&self) -> FgColorDisplay<'_, Magenta, Self>
fn magenta(&self) -> FgColorDisplay<'_, Magenta, Self>
Source§fn on_magenta(&self) -> BgColorDisplay<'_, Magenta, Self>
fn on_magenta(&self) -> BgColorDisplay<'_, Magenta, Self>
Source§fn purple(&self) -> FgColorDisplay<'_, Magenta, Self>
fn purple(&self) -> FgColorDisplay<'_, Magenta, Self>
Source§fn on_purple(&self) -> BgColorDisplay<'_, Magenta, Self>
fn on_purple(&self) -> BgColorDisplay<'_, Magenta, Self>
Source§fn cyan(&self) -> FgColorDisplay<'_, Cyan, Self>
fn cyan(&self) -> FgColorDisplay<'_, Cyan, Self>
Source§fn on_cyan(&self) -> BgColorDisplay<'_, Cyan, Self>
fn on_cyan(&self) -> BgColorDisplay<'_, Cyan, Self>
Source§fn white(&self) -> FgColorDisplay<'_, White, Self>
fn white(&self) -> FgColorDisplay<'_, White, Self>
Source§fn on_white(&self) -> BgColorDisplay<'_, White, Self>
fn on_white(&self) -> BgColorDisplay<'_, White, Self>
Source§fn default_color(&self) -> FgColorDisplay<'_, Default, Self>
fn default_color(&self) -> FgColorDisplay<'_, Default, Self>
Source§fn on_default_color(&self) -> BgColorDisplay<'_, Default, Self>
fn on_default_color(&self) -> BgColorDisplay<'_, Default, Self>
Source§fn bright_black(&self) -> FgColorDisplay<'_, BrightBlack, Self>
fn bright_black(&self) -> FgColorDisplay<'_, BrightBlack, Self>
Source§fn on_bright_black(&self) -> BgColorDisplay<'_, BrightBlack, Self>
fn on_bright_black(&self) -> BgColorDisplay<'_, BrightBlack, Self>
Source§fn bright_red(&self) -> FgColorDisplay<'_, BrightRed, Self>
fn bright_red(&self) -> FgColorDisplay<'_, BrightRed, Self>
Source§fn on_bright_red(&self) -> BgColorDisplay<'_, BrightRed, Self>
fn on_bright_red(&self) -> BgColorDisplay<'_, BrightRed, Self>
Source§fn bright_green(&self) -> FgColorDisplay<'_, BrightGreen, Self>
fn bright_green(&self) -> FgColorDisplay<'_, BrightGreen, Self>
Source§fn on_bright_green(&self) -> BgColorDisplay<'_, BrightGreen, Self>
fn on_bright_green(&self) -> BgColorDisplay<'_, BrightGreen, Self>
Source§fn bright_yellow(&self) -> FgColorDisplay<'_, BrightYellow, Self>
fn bright_yellow(&self) -> FgColorDisplay<'_, BrightYellow, Self>
Source§fn on_bright_yellow(&self) -> BgColorDisplay<'_, BrightYellow, Self>
fn on_bright_yellow(&self) -> BgColorDisplay<'_, BrightYellow, Self>
Source§fn bright_blue(&self) -> FgColorDisplay<'_, BrightBlue, Self>
fn bright_blue(&self) -> FgColorDisplay<'_, BrightBlue, Self>
Source§fn on_bright_blue(&self) -> BgColorDisplay<'_, BrightBlue, Self>
fn on_bright_blue(&self) -> BgColorDisplay<'_, BrightBlue, Self>
Source§fn bright_magenta(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
fn bright_magenta(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
Source§fn on_bright_magenta(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
fn on_bright_magenta(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
Source§fn bright_purple(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
fn bright_purple(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
Source§fn on_bright_purple(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
fn on_bright_purple(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
Source§fn bright_cyan(&self) -> FgColorDisplay<'_, BrightCyan, Self>
fn bright_cyan(&self) -> FgColorDisplay<'_, BrightCyan, Self>
Source§fn on_bright_cyan(&self) -> BgColorDisplay<'_, BrightCyan, Self>
fn on_bright_cyan(&self) -> BgColorDisplay<'_, BrightCyan, Self>
Source§fn bright_white(&self) -> FgColorDisplay<'_, BrightWhite, Self>
fn bright_white(&self) -> FgColorDisplay<'_, BrightWhite, Self>
Source§fn on_bright_white(&self) -> BgColorDisplay<'_, BrightWhite, Self>
fn on_bright_white(&self) -> BgColorDisplay<'_, BrightWhite, Self>
Source§fn bold(&self) -> BoldDisplay<'_, Self>
fn bold(&self) -> BoldDisplay<'_, Self>
Source§fn dimmed(&self) -> DimDisplay<'_, Self>
fn dimmed(&self) -> DimDisplay<'_, Self>
Source§fn italic(&self) -> ItalicDisplay<'_, Self>
fn italic(&self) -> ItalicDisplay<'_, Self>
Source§fn underline(&self) -> UnderlineDisplay<'_, Self>
fn underline(&self) -> UnderlineDisplay<'_, Self>
Source§fn blink(&self) -> BlinkDisplay<'_, Self>
fn blink(&self) -> BlinkDisplay<'_, Self>
Source§fn blink_fast(&self) -> BlinkFastDisplay<'_, Self>
fn blink_fast(&self) -> BlinkFastDisplay<'_, Self>
Source§fn reversed(&self) -> ReversedDisplay<'_, Self>
fn reversed(&self) -> ReversedDisplay<'_, Self>
Source§fn strikethrough(&self) -> StrikeThroughDisplay<'_, Self>
fn strikethrough(&self) -> StrikeThroughDisplay<'_, Self>
Source§fn color<Color>(&self, color: Color) -> FgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
fn color<Color>(&self, color: Color) -> FgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
OwoColorize::fg or
a color-specific method, such as OwoColorize::green, Read moreSource§fn on_color<Color>(&self, color: Color) -> BgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
fn on_color<Color>(&self, color: Color) -> BgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
OwoColorize::bg or
a color-specific method, such as OwoColorize::on_yellow, Read moreSource§fn fg_rgb<const R: u8, const G: u8, const B: u8>(
&self,
) -> FgColorDisplay<'_, CustomColor<R, G, B>, Self>
fn fg_rgb<const R: u8, const G: u8, const B: u8>( &self, ) -> FgColorDisplay<'_, CustomColor<R, G, B>, Self>
Source§fn bg_rgb<const R: u8, const G: u8, const B: u8>(
&self,
) -> BgColorDisplay<'_, CustomColor<R, G, B>, Self>
fn bg_rgb<const R: u8, const G: u8, const B: u8>( &self, ) -> BgColorDisplay<'_, CustomColor<R, G, B>, Self>
Source§fn truecolor(&self, r: u8, g: u8, b: u8) -> FgDynColorDisplay<'_, Rgb, Self>
fn truecolor(&self, r: u8, g: u8, b: u8) -> FgDynColorDisplay<'_, Rgb, Self>
Source§fn on_truecolor(&self, r: u8, g: u8, b: u8) -> BgDynColorDisplay<'_, Rgb, Self>
fn on_truecolor(&self, r: u8, g: u8, b: u8) -> BgDynColorDisplay<'_, Rgb, Self>
Source§impl<Response, Error> ResponseResult<Response, Error> for Response
impl<Response, Error> ResponseResult<Response, Error> for Response
Source§fn into_result(self) -> Result<Response, Error>
fn into_result(self) -> Result<Response, Error>
Result that can be sent as a response.Source§impl<T, Request> ServiceExt<Request> for T
impl<T, Request> ServiceExt<Request> for T
Source§fn ready(&mut self) -> Ready<'_, Self, Request>where
Self: Sized,
fn ready(&mut self) -> Ready<'_, Self, Request>where
Self: Sized,
Source§fn ready_and(&mut self) -> Ready<'_, Self, Request>where
Self: Sized,
fn ready_and(&mut self) -> Ready<'_, Self, Request>where
Self: Sized,
ServiceExt::ready method insteadSource§fn ready_oneshot(self) -> ReadyOneshot<Self, Request>where
Self: Sized,
fn ready_oneshot(self) -> ReadyOneshot<Self, Request>where
Self: Sized,
Source§fn oneshot(self, req: Request) -> Oneshot<Self, Request>where
Self: Sized,
fn oneshot(self, req: Request) -> Oneshot<Self, Request>where
Self: Sized,
Service, calling with the providing request once it is ready.Source§fn and_then<F>(self, f: F) -> AndThen<Self, F>
fn and_then<F>(self, f: F) -> AndThen<Self, F>
poll_ready method. Read moreSource§fn map_response<F, Response>(self, f: F) -> MapResponse<Self, F>
fn map_response<F, Response>(self, f: F) -> MapResponse<Self, F>
poll_ready method. Read moreSource§fn map_err<F, Error>(self, f: F) -> MapErr<Self, F>
fn map_err<F, Error>(self, f: F) -> MapErr<Self, F>
poll_ready method. Read moreSource§fn map_result<F, Response, Error>(self, f: F) -> MapResult<Self, F>
fn map_result<F, Response, Error>(self, f: F) -> MapResult<Self, F>
Result<Self::Response, Self::Error>)
to a different value, regardless of whether the future succeeds or
fails. Read more