Struct poem::test::TestRequestBuilder
source · [−]pub struct TestRequestBuilder<'a, E> { /* private fields */ }
Available on crate feature
test
only.Expand description
A request builder for testing.
Implementations
sourceimpl<'a, E> TestRequestBuilder<'a, E>
impl<'a, E> TestRequestBuilder<'a, E>
sourcepub fn query(self, name: impl Into<String>, value: &impl Serialize) -> Self
pub fn query(self, name: impl Into<String>, value: &impl Serialize) -> Self
Sets the query string for this request.
Example
use poem::{handler, test::TestClient, web::Query, Route};
use serde::Deserialize;
#[derive(Deserialize)]
struct Params {
key: String,
value: i32,
}
#[handler]
fn index(Query(params): Query<Params>) -> String {
format!("{}={}", params.key, params.value)
}
let app = Route::new().at("/", index);
let cli = TestClient::new(app);
let resp = cli
.get("/")
.query("key", &"a")
.query("value", &10)
.send()
.await;
resp.assert_status_is_ok();
resp.assert_text("a=10").await;
sourcepub fn header<K, V>(self, key: K, value: V) -> Selfwhere
K: TryInto<HeaderName>,
V: TryInto<HeaderValue>,
pub fn header<K, V>(self, key: K, value: V) -> Selfwhere
K: TryInto<HeaderName>,
V: TryInto<HeaderValue>,
Sets the header value for this request.
sourcepub fn typed_header<T: Header>(self, header: T) -> Self
pub fn typed_header<T: Header>(self, header: T) -> Self
Inserts a typed header to this request.
sourcepub fn content_type(self, content_type: impl AsRef<str>) -> Self
pub fn content_type(self, content_type: impl AsRef<str>) -> Self
Sets the content type for this request.
sourcepub fn body_json(self, body: &impl Serialize) -> Self
pub fn body_json(self, body: &impl Serialize) -> Self
Sets the JSON body for this request with application/json
content
type.
sourcepub fn body_xml(self, body: &impl Serialize) -> Self
pub fn body_xml(self, body: &impl Serialize) -> Self
Sets the XML body for this request with application/xml
content
type.
sourcepub fn form(self, form: &impl Serialize) -> Self
pub fn form(self, form: &impl Serialize) -> Self
Sets the form data for this request with
application/x-www-form-urlencoded
content type.
sourcepub fn multipart(self, form: TestForm) -> Self
pub fn multipart(self, form: TestForm) -> Self
Sets the multipart body for this request with multipart/form-data
content type.
sourcepub fn data<T>(self, data: T) -> Selfwhere
T: Send + Sync + 'static,
pub fn data<T>(self, data: T) -> Selfwhere
T: Send + Sync + 'static,
Sets the extension data for this request.
Example
use poem::{handler, test::TestClient, web::Data, Route};
#[handler]
fn index(Data(value): Data<&i32>) -> String {
value.to_string()
}
let app = Route::new().at("/", index);
let cli = TestClient::new(app);
let resp = cli.get("/").data(100i32).send().await;
resp.assert_status_is_ok();
resp.assert_text("100").await;
sourcepub async fn send(self) -> TestResponsewhere
E: Endpoint,
pub async fn send(self) -> TestResponsewhere
E: Endpoint,
Send this request to endpoint to get the response.
Auto Trait Implementations
impl<'a, E> !RefUnwindSafe for TestRequestBuilder<'a, E>
impl<'a, E> Send for TestRequestBuilder<'a, E>where
E: Sync,
impl<'a, E> Sync for TestRequestBuilder<'a, E>where
E: Sync,
impl<'a, E> Unpin for TestRequestBuilder<'a, E>
impl<'a, E> !UnwindSafe for TestRequestBuilder<'a, E>
Blanket Implementations
sourceimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more