pub struct HttpFake { /* private fields */ }Expand description
An active HTTP fake, with its patterns and the requests it answered.
The handle can’t be cloned, because its lifetime decides when the fake is active.
Implementations§
Source§impl HttpFake
impl HttpFake
Sourcepub fn with(self, pattern: &str, fake: impl Into<FakeHandler>) -> HttpFake
pub fn with(self, pattern: &str, fake: impl Into<FakeHandler>) -> HttpFake
Answer requests whose URL matches pattern with this fake.
The fake may be a status number, a string body, a JSON value, a response, a
sequence, a failed_connection, or a closure that receives the Request and
returns any of these. When several patterns match a URL, the first one answers.
use laravel::http::client::Request;
use serde_json::json;
let http = client::fake()
.with("github.com/*", json!({ "login": "taylor" }))
.with("stripe.com/*", client::sequence().push(500).push(200))
.with("example.com/*", client::failed_connection())
.with("api.example.com/*", |request: &Request| {
if request.has_header("authorization", "Bearer secret") { 200 } else { 401 }
})
.with("*", "Hello, world!");Sourcepub fn prevent_stray_requests(self) -> HttpFake
pub fn prevent_stray_requests(self) -> HttpFake
Fail requests that no pattern matches with Error::Invalid, instead of giving them an
empty 200.
Sourcepub fn allow_stray_requests<S>(
self,
patterns: impl IntoIterator<Item = S>,
) -> HttpFake
pub fn allow_stray_requests<S>( self, patterns: impl IntoIterator<Item = S>, ) -> HttpFake
Let requests that no pattern matches reach the network when their URL matches one of these patterns, such as a local service’s URL.
Sourcepub fn sent(&self) -> Vec<Request>
pub fn sent(&self) -> Vec<Request>
The requests sent while this fake was active, in order, including requests that got no response.
Sourcepub fn recorded(&self) -> Vec<(Request, Response)>
pub fn recorded(&self) -> Vec<(Request, Response)>
The requests that got a response while this fake was active, with their responses.
Sourcepub fn assert_sent(&self, predicate: impl Fn(&Request) -> bool) -> &HttpFake
pub fn assert_sent(&self, predicate: impl Fn(&Request) -> bool) -> &HttpFake
Assert that at least one request matching the predicate was sent.
§Panics
Panics when no matching request was sent. The message lists the requests that were sent.
Sourcepub fn assert_not_sent(&self, predicate: impl Fn(&Request) -> bool) -> &HttpFake
pub fn assert_not_sent(&self, predicate: impl Fn(&Request) -> bool) -> &HttpFake
Assert that no request matching the predicate was sent.
§Panics
Panics when a matching request was sent. The message lists the requests that were sent.
Sourcepub fn assert_sent_count(&self, expected: usize) -> &HttpFake
pub fn assert_sent_count(&self, expected: usize) -> &HttpFake
Assert how many requests were sent, counting each retried attempt.
§Panics
Panics when the number of requests sent differs from expected.
Sourcepub fn assert_nothing_sent(&self) -> &HttpFake
pub fn assert_nothing_sent(&self) -> &HttpFake
Assert that no request was sent.
§Panics
Panics when any request was sent. The message lists the requests.
Trait Implementations§
Auto Trait Implementations§
impl Forget for HttpFake
impl Freeze for HttpFake
impl Move for HttpFake
impl RefUnwindSafe for HttpFakewhere
Atomic<usize>: RefUnwindSafe,
Mutex<Vec<Arc<Recording>>>: RefUnwindSafe,
Mutex<State>: RefUnwindSafe,
impl Send for HttpFake
impl Sync for HttpFake
impl Unpin for HttpFake
impl UnsafeUnpin for HttpFakewhere
*const ArcInner<Mutex<Vec<Arc<Recording>>>> is !null: UnsafeUnpin,
PhantomData<ArcInner<Mutex<Vec<Arc<Recording>>>>>: UnsafeUnpin,
*const ArcInner<Recording> is !null: UnsafeUnpin,
PhantomData<ArcInner<Recording>>: UnsafeUnpin,
impl UnwindSafe for HttpFake
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
impl<T> ErasedDestructor for Twhere
T: 'static,
Source§impl<K> ForeignKey<K> for K
impl<K> ForeignKey<K> for K
Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
Source§fn with_current_context(self) -> WithContext<Self> ⓘ
fn with_current_context(self) -> WithContext<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 more