pub struct TestRequest { /* private fields */ }Expand description
In-memory HTTP request builder.
Build requests through crate::TestApp helpers such as
crate::TestApp::get and crate::TestApp::post. send panics on
request-construction failures; use Self::try_send when testing invalid
headers or URIs.
use nidus_testing::TestApp;
use serde_json::json;
let response = app
.post("/users")
.header("x-request-id", "550e8400-e29b-41d4-a716-446655440000")
.query(&[("dry_run", "true")])
.json(&json!({ "name": "Ada" }))
.send()
.await;Implementations§
Source§impl TestRequest
impl TestRequest
Sourcepub fn header<N, V>(self, name: N, value: V) -> Selfwhere
N: TryInto<HeaderName>,
N::Error: Into<Error>,
V: TryInto<HeaderValue>,
V::Error: Into<Error>,
pub fn header<N, V>(self, name: N, value: V) -> Selfwhere
N: TryInto<HeaderName>,
N::Error: Into<Error>,
V: TryInto<HeaderValue>,
V::Error: Into<Error>,
Sets a request header.
Panics if the name or value cannot be converted into an HTTP header. Use
Self::try_header for fallible construction.
Sourcepub fn try_header<N, V>(self, name: N, value: V) -> Result<Self, Error>where
N: TryInto<HeaderName>,
N::Error: Into<Error>,
V: TryInto<HeaderValue>,
V::Error: Into<Error>,
pub fn try_header<N, V>(self, name: N, value: V) -> Result<Self, Error>where
N: TryInto<HeaderName>,
N::Error: Into<Error>,
V: TryInto<HeaderValue>,
V::Error: Into<Error>,
Tries to set a request header.
Sourcepub fn text(self, body: impl Into<String>) -> Self
pub fn text(self, body: impl Into<String>) -> Self
Sets a UTF-8 text request body.
Also sets content-type: text/plain; charset=utf-8.
Sourcepub fn body(self, body: impl Into<Bytes>) -> Self
pub fn body(self, body: impl Into<Bytes>) -> Self
Sets a raw request body.
This does not set content-type.
Sourcepub fn json<T: Serialize>(self, body: &T) -> Self
pub fn json<T: Serialize>(self, body: &T) -> Self
Sets a JSON request body.
Also sets content-type: application/json.
Sourcepub fn try_json<T: Serialize>(self, body: &T) -> Result<Self, Error>
pub fn try_json<T: Serialize>(self, body: &T) -> Result<Self, Error>
Tries to set a JSON request body.
Sourcepub fn query<T: Serialize>(self, query: &T) -> Self
pub fn query<T: Serialize>(self, query: &T) -> Self
Appends URL-encoded query parameters.
Existing query strings are preserved and new pairs are appended with the
correct ? or & separator.
Sourcepub fn try_query<T: Serialize>(self, query: &T) -> Result<Self, Error>
pub fn try_query<T: Serialize>(self, query: &T) -> Result<Self, Error>
Tries to append URL-encoded query parameters.
Sourcepub async fn send(self) -> TestResponse
pub async fn send(self) -> TestResponse
Sends the request against the in-memory app, panicking if request construction fails.
Sourcepub async fn try_send(self) -> Result<TestResponse, TestRequestError>
pub async fn try_send(self) -> Result<TestResponse, TestRequestError>
Tries to send the request against the in-memory app.