Struct warp::test::RequestBuilder
source · pub struct RequestBuilder { /* private fields */ }Expand description
A request builder for testing filters.
See module documentation for an overview.
Implementations§
source§impl RequestBuilder
impl RequestBuilder
sourcepub fn header<K, V>(self, key: K, value: V) -> Selfwhere
HeaderName: TryFrom<K>,
HeaderValue: TryFrom<V>,
pub fn header<K, V>(self, key: K, value: V) -> Selfwhere
HeaderName: TryFrom<K>,
HeaderValue: TryFrom<V>,
sourcepub fn remote_addr(self, addr: SocketAddr) -> Self
pub fn remote_addr(self, addr: SocketAddr) -> Self
Set the remote address of this request
Default is no remote address.
Example
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
let req = warp::test::request()
.remote_addr(SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080));sourcepub fn extension<T>(self, ext: T) -> Selfwhere
T: Send + Sync + 'static,
pub fn extension<T>(self, ext: T) -> Selfwhere
T: Send + Sync + 'static,
Add a type to the request’s http::Extensions.
sourcepub fn body(self, body: impl AsRef<[u8]>) -> Self
pub fn body(self, body: impl AsRef<[u8]>) -> Self
Set the bytes of this request body.
Default is an empty body.
Example
let req = warp::test::request()
.body("foo=bar&baz=quux");sourcepub fn json(self, val: &impl Serialize) -> Self
pub fn json(self, val: &impl Serialize) -> Self
Set the bytes of this request body by serializing a value into JSON.
Example
let req = warp::test::request()
.json(&true);sourcepub async fn filter<F>(
self,
f: &F
) -> Result<<F::Extract as OneOrTuple>::Output, F::Error>where
F: Filter,
F::Future: Send + 'static,
F::Extract: OneOrTuple + Send + 'static,
F::Error: Send + 'static,
pub async fn filter<F>(
self,
f: &F
) -> Result<<F::Extract as OneOrTuple>::Output, F::Error>where
F: Filter,
F::Future: Send + 'static,
F::Extract: OneOrTuple + Send + 'static,
F::Error: Send + 'static,
Tries to apply the Filter on this request.
Example
async {
let param = warp::path::param::<u32>();
let ex = warp::test::request()
.path("/41")
.filter(¶m)
.await
.unwrap();
assert_eq!(ex, 41);
assert!(
warp::test::request()
.path("/foo")
.filter(¶m)
.await
.is_err()
);
};sourcepub async fn matches<F>(self, f: &F) -> boolwhere
F: Filter,
F::Future: Send + 'static,
F::Extract: Send + 'static,
F::Error: Send + 'static,
pub async fn matches<F>(self, f: &F) -> boolwhere
F: Filter,
F::Future: Send + 'static,
F::Extract: Send + 'static,
F::Error: Send + 'static,
Returns whether the Filter matches this request, or rejects it.
Example
async {
let get = warp::get();
let post = warp::post();
assert!(
warp::test::request()
.method("GET")
.matches(&get)
.await
);
assert!(
!warp::test::request()
.method("GET")
.matches(&post)
.await
);
};