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
impl StubClient
Sourcepub fn new(stub_settings: StubSettings) -> Self
pub fn new(stub_settings: StubSettings) -> Self
Create a new instance of StubClient
.
Please consult StubSettings for more information about the possible settings.
Sourcepub fn stub<'cl>(&'cl mut self, url: Url) -> RequestStubber<'cl>
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
impl Client for StubClient
Source§fn execute(
&self,
config: Option<&ClientConfig>,
request: Request,
) -> Result<Response, Error>
fn execute( &self, config: Option<&ClientConfig>, request: Request, ) -> Result<Response, Error>
Execute a request. Read more
Source§fn config(&self) -> &ClientConfig
fn config(&self) -> &ClientConfig
Returns a immutable reference to the internal config.
Source§fn config_mut(&mut self) -> &mut ClientConfig
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>
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>
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>
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>
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>
fn delete<'cl, U: IntoUrl>(&'cl self, url: U) -> RequestBuilder<'cl, Self>
Convenience method to make a
DELETE
request to a URL.Source§impl From<StubClient> for GenericClient
impl From<StubClient> for GenericClient
Source§fn from(c: StubClient) -> Self
fn from(c: StubClient) -> Self
Converts to this type from the input type.
Auto Trait Implementations§
impl Freeze for StubClient
impl RefUnwindSafe for StubClient
impl Send for StubClient
impl Sync for StubClient
impl Unpin for StubClient
impl UnwindSafe for StubClient
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
Mutably borrows from an owned value. Read more