pub struct StubHttpClient { /* private fields */ }Expand description
An HTTP client that answers from a function rather than a network.
A screen driven from a recorded payload, a robot run that must not depend on a server, a test that wants a specific failure: all of them want one thing, which is to decide what a request answers. Written as a client each time, that is the same dozen lines of body plumbing repeated — and each copy is a chance to answer differently from the real client.
ⓘ
let client: HttpClientRef = Arc::new(StubHttpClient::from_text(move |url| {
Ok(fixture_for(url))
}));Implementations§
Source§impl StubHttpClient
impl StubHttpClient
Sourcepub fn new(
answer: impl Fn(&HttpRequest) -> Result<HttpResponse, HttpError> + Send + Sync + 'static,
) -> Self
pub fn new( answer: impl Fn(&HttpRequest) -> Result<HttpResponse, HttpError> + Send + Sync + 'static, ) -> Self
Answers from a function of the whole request.
Trait Implementations§
Source§impl HttpClient for StubHttpClient
impl HttpClient for StubHttpClient
Source§fn send<'a>(
&'a self,
request: &'a HttpRequest,
_control: HttpControl,
) -> HttpFuture<'a, HttpResponse>
fn send<'a>( &'a self, request: &'a HttpRequest, _control: HttpControl, ) -> HttpFuture<'a, HttpResponse>
Sends
request, resolving when the response’s status line has arrived
and its body is ready to be read.Source§fn get_text<'a>(&'a self, url: &'a str) -> HttpFuture<'a, String>
fn get_text<'a>(&'a self, url: &'a str) -> HttpFuture<'a, String>
The body of a successful
GET, as text.Source§fn get_bytes<'a>(&'a self, url: &'a str) -> HttpFuture<'a, Vec<u8>>
fn get_bytes<'a>(&'a self, url: &'a str) -> HttpFuture<'a, Vec<u8>>
The body of a successful
GET.Source§fn download_to<'a>(
&'a self,
url: &'a str,
target: &'a Path,
control: HttpControl,
) -> HttpFuture<'a, u64>
fn download_to<'a>( &'a self, url: &'a str, target: &'a Path, control: HttpControl, ) -> HttpFuture<'a, u64>
Streams a response into
target, resuming from its current length when
the server supports byte ranges, and returns the file’s whole length. Read moreAuto Trait Implementations§
impl !RefUnwindSafe for StubHttpClient
impl !UnwindSafe for StubHttpClient
impl Freeze for StubHttpClient
impl Send for StubHttpClient
impl Sync for StubHttpClient
impl Unpin for StubHttpClient
impl UnsafeUnpin for StubHttpClient
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