Struct StubClient

Source
pub struct StubClient { /* private fields */ }
Expand description

A client which allows you to stub out the response to a request explicitly.

§Examples

use reqwest_mock::{Client, Method, StubClient, StubDefault, StubSettings, StubStrictness, Url};

let mut client = StubClient::new(StubSettings {
    // If a request without a corresponding stub is made we want an error
    // to be returned when our code executes the request.
    default: StubDefault::Error,

    // We want the `StubClient` to compare actual requests and provided
    // mocks by their method and their url.
    strictness: StubStrictness::MethodUrl,
});

// Mock a request.
client
    .stub(Url::parse("http://example.com/mocking").unwrap())
        .method(Method::GET)
    .response()
        .body("Mocking is fun!")
        .mock();

let response = client.get("http://example.com/mocking").send().unwrap();
assert_eq!(response.body_to_utf8().unwrap(), "Mocking is fun!".to_string());

Implementations§

Source§

impl StubClient

Source

pub fn new(stub_settings: StubSettings) -> Self

Create a new instance of StubClient.

Please consult StubSettings for more information about the possible settings.

Source

pub fn stub<'cl>(&'cl mut self, url: Url) -> RequestStubber<'cl>

Provide a stub for a request to the provided url.

This will return a RequestStubber, which in a first step will allow you to specify the full details of the request. Make sure that they match the StubStrictness provided in the settings.

After you are finished specifying the details of the matching request, call response() to return a ResponseStubber instance and start specifying the response. Finally use ResponseStubber::mock() to register the mock into the client.

Trait Implementations§

Source§

impl Client for StubClient

Source§

fn execute( &self, config: Option<&ClientConfig>, request: Request, ) -> Result<Response, Error>

Execute a request. Read more
Source§

fn config(&self) -> &ClientConfig

Returns a immutable reference to the internal config.
Source§

fn config_mut(&mut self) -> &mut ClientConfig

Returns a mutable reference to the internal config.
Source§

fn get<'cl, U: IntoUrl>(&'cl self, url: U) -> RequestBuilder<'cl, Self>

Convenience method to make a GET request to a URL.
Source§

fn post<'cl, U: IntoUrl>(&'cl self, url: U) -> RequestBuilder<'cl, Self>

Convenience method to make a POST request to a URL.
Source§

fn put<'cl, U: IntoUrl>(&'cl self, url: U) -> RequestBuilder<'cl, Self>

Convenience method to make a PUT request to a URL.
Source§

fn patch<'cl, U: IntoUrl>(&'cl self, url: U) -> RequestBuilder<'cl, Self>

Convenience method to make a PATCH request to a URL.
Source§

fn delete<'cl, U: IntoUrl>(&'cl self, url: U) -> RequestBuilder<'cl, Self>

Convenience method to make a DELETE request to a URL.
Source§

fn head<'cl, U: IntoUrl>(&'cl self, url: U) -> RequestBuilder<'cl, Self>

Convenience method to make a HEAD request to a URL.
Source§

fn request<'cl, U: IntoUrl>( &'cl self, method: Method, url: U, ) -> RequestBuilder<'cl, Self>

Returns a RequestBuilder for the given method and URL, which allows for further configuration of the request, like including additional headers, and sending it.
Source§

impl From<StubClient> for GenericClient

Source§

fn from(c: StubClient) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

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> 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, 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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

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

impl<T> ErasedDestructor for T
where T: 'static,