Struct axum_test::TestResponse
source · pub struct TestResponse { /* private fields */ }
Expand description
The TestResponse
is the result of a request created using a TestServer
.
The TestServer
builds a TestRequest
, which when awaited,
will produce the response.
use ::axum::Json;
use ::axum::routing::Router;
use ::axum::routing::get;
use ::serde::Deserialize;
use ::serde::Serialize;
use ::axum_test::TestServer;
let app = Router::new()
.route(&"/test", get(|| async { "hello!" }));
let server = TestServer::new(app)?;
// This builds a `TestResponse`
let response = server.get(&"/todo").await;
§Extracting Response
The functions TestResponse::json()
, TestResponse::text()
,
and TestResponse::form()
,
allow you to extract the underlying response content in different formats.
let todo_response = server.get(&"/todo")
.await
.json::<Todo>();
let response_as_raw_text = server.get(&"/todo")
.await
.text();
TestResponse::as_bytes()
and TestResponse::into_bytes()
,
offer the underlying raw bytes to allow custom decoding.
Full code examples can be found within their documentation.
§Assertions
The result of a response can also be asserted using the many assertion functions.
use ::axum::Json;
use ::axum::routing::Router;
use ::axum::routing::get;
use ::serde::Deserialize;
use ::serde::Serialize;
use ::axum_test::TestServer;
let app = Router::new()
.route(&"/test", get(|| async { "hello!" }));
let server = TestServer::new(app)?;
let response = server.get(&"/todo").await;
// These assertions will panic if they are not fulfilled by the response.
response.assert_status_ok();
response.assert_text("hello!");
Implementations§
source§impl TestResponse
impl TestResponse
sourcepub fn text(&self) -> String
pub fn text(&self) -> String
Returns the underlying response, extracted as a UTF-8 string.
§Example
use ::axum::Json;
use ::axum::routing::Router;
use ::axum::routing::get;
use ::serde_json::json;
use ::serde_json::Value;
use ::axum_test::TestServer;
async fn route_get_todo() -> Json<Value> {
Json(json!({
"description": "buy milk",
}))
}
let app = Router::new()
.route(&"/todo", get(route_get_todo));
let server = TestServer::new(app)?;
let response = server.get(&"/todo").await;
// Extract the response as a string on it's own.
let raw_text = response.text();
sourcepub fn json<T>(&self) -> Twhere
T: DeserializeOwned,
pub fn json<T>(&self) -> Twhere
T: DeserializeOwned,
Deserializes the response, as Json, into the type given.
If deserialization fails then this will panic.
§Example
use ::axum::Json;
use ::axum::routing::Router;
use ::axum::routing::get;
use ::serde::Deserialize;
use ::serde::Serialize;
use ::axum_test::TestServer;
#[derive(Serialize, Deserialize, Debug)]
struct Todo {
description: String,
}
async fn route_get_todo() -> Json<Todo> {
Json(Todo {
description: "buy milk".to_string(),
})
}
let app = Router::new()
.route(&"/todo", get(route_get_todo));
let server = TestServer::new(app)?;
let response = server.get(&"/todo").await;
// Extract the response as a `Todo` item.
let todo = response.json::<Todo>();
sourcepub fn yaml<T>(&self) -> Twhere
T: DeserializeOwned,
Available on crate feature yaml
only.
pub fn yaml<T>(&self) -> Twhere
T: DeserializeOwned,
yaml
only.Deserializes the response, as Yaml, into the type given.
If deserialization fails then this will panic.
§Example
use ::axum::routing::Router;
use ::axum::routing::get;
use ::axum_yaml::Yaml;
use ::serde::Deserialize;
use ::serde::Serialize;
use ::axum_test::TestServer;
#[derive(Serialize, Deserialize, Debug)]
struct Todo {
description: String,
}
async fn route_get_todo() -> Yaml<Todo> {
Yaml(Todo {
description: "buy milk".to_string(),
})
}
let app = Router::new()
.route(&"/todo", get(route_get_todo));
let server = TestServer::new(app)?;
let response = server.get(&"/todo").await;
// Extract the response as a `Todo` item.
let todo = response.yaml::<Todo>();
sourcepub fn msgpack<T>(&self) -> Twhere
T: DeserializeOwned,
Available on crate feature msgpack
only.
pub fn msgpack<T>(&self) -> Twhere
T: DeserializeOwned,
msgpack
only.Deserializes the response, as MsgPack, into the type given.
If deserialization fails then this will panic.
§Example
use ::axum::routing::Router;
use ::axum::routing::get;
use ::axum_msgpack::MsgPack;
use ::serde::Deserialize;
use ::serde::Serialize;
use ::axum_test::TestServer;
#[derive(Serialize, Deserialize, Debug)]
struct Todo {
description: String,
}
async fn route_get_todo() -> MsgPack<Todo> {
MsgPack(Todo {
description: "buy milk".to_string(),
})
}
let app = Router::new()
.route(&"/todo", get(route_get_todo));
let server = TestServer::new(app)?;
let response = server.get(&"/todo").await;
// Extract the response as a `Todo` item.
let todo = response.msgpack::<Todo>();
sourcepub fn form<T>(&self) -> Twhere
T: DeserializeOwned,
pub fn form<T>(&self) -> Twhere
T: DeserializeOwned,
Deserializes the response, as an urlencoded Form, into the type given.
If deserialization fails then this will panic.
§Example
use ::axum::Form;
use ::axum::routing::Router;
use ::axum::routing::get;
use ::serde::Deserialize;
use ::serde::Serialize;
use ::axum_test::TestServer;
#[derive(Serialize, Deserialize, Debug)]
struct Todo {
description: String,
}
async fn route_get_todo() -> Form<Todo> {
Form(Todo {
description: "buy milk".to_string(),
})
}
let app = Router::new()
.route(&"/todo", get(route_get_todo));
let server = TestServer::new(app)?;
let response = server.get(&"/todo").await;
// Extract the response as a `Todo` item.
let todo = response.form::<Todo>();
sourcepub fn into_bytes<'a>(self) -> Bytes
pub fn into_bytes<'a>(self) -> Bytes
Consumes this returning the underlying Bytes
in the response.
sourcepub fn status_code(&self) -> StatusCode
pub fn status_code(&self) -> StatusCode
The status_code of the response.
sourcepub fn request_url(&self) -> Url
pub fn request_url(&self) -> Url
The full URL that was used to produce this response.
sourcepub fn maybe_header<N>(&self, header_name: N) -> Option<HeaderValue>where
N: AsHeaderName,
pub fn maybe_header<N>(&self, header_name: N) -> Option<HeaderValue>where
N: AsHeaderName,
Finds a header with the given name.
If there are multiple headers with the same name,
then only the first HeaderValue
will be returned.
None
is returned when no header was found.
sourcepub fn headers<'a>(&'a self) -> &'a HeaderMap<HeaderValue>
pub fn headers<'a>(&'a self) -> &'a HeaderMap<HeaderValue>
Returns the headers returned from the response.
sourcepub fn header<N>(&self, header_name: N) -> HeaderValue
pub fn header<N>(&self, header_name: N) -> HeaderValue
Finds a header with the given name. If there are multiple headers with the same name, then only the first will be returned.
If no header is found, then this will panic.
sourcepub fn iter_headers<'a>(
&'a self
) -> impl Iterator<Item = (&'a HeaderName, &'a HeaderValue)>
pub fn iter_headers<'a>( &'a self ) -> impl Iterator<Item = (&'a HeaderName, &'a HeaderValue)>
Iterates over all of the headers contained in the response.
sourcepub fn iter_headers_by_name<'a, N>(
&'a self,
header_name: N
) -> impl Iterator<Item = &'a HeaderValue>where
N: AsHeaderName,
pub fn iter_headers_by_name<'a, N>(
&'a self,
header_name: N
) -> impl Iterator<Item = &'a HeaderValue>where
N: AsHeaderName,
Iterates over all of the headers for a specific name, contained in the response.
Finds a Cookie
with the given name.
If there are multiple matching cookies,
then only the first will be returned.
None
is returned if no Cookie is found.
Finds a Cookie
with the given name.
If there are multiple matching cookies,
then only the first will be returned.
If no Cookie
is found, then this will panic.
Returns all of the cookies contained in the response,
within a CookieJar
object.
See the cookie
crate for details.
Iterate over all of the cookies in the response.
sourcepub fn assert_text<C>(&self, other: C)
pub fn assert_text<C>(&self, other: C)
This performs an assertion comparing the whole body of the response, against the text provided.
sourcepub fn assert_json<T>(&self, other: &T)
pub fn assert_json<T>(&self, other: &T)
Deserializes the contents of the request as Json, and asserts it matches the value given.
If other
does not match, or the response is not Json,
then this will panic.
sourcepub fn assert_yaml<T>(&self, other: &T)
Available on crate feature yaml
only.
pub fn assert_yaml<T>(&self, other: &T)
yaml
only.Deserializes the contents of the request as Yaml, and asserts it matches the value given.
If other
does not match, or the response is not Yaml,
then this will panic.
sourcepub fn assert_msgpack<T>(&self, other: &T)
Available on crate feature msgpack
only.
pub fn assert_msgpack<T>(&self, other: &T)
msgpack
only.Deserializes the contents of the request as MsgPack, and asserts it matches the value given.
If other
does not match, or the response is not MsgPack,
then this will panic.
sourcepub fn assert_form<T>(&self, other: &T)
pub fn assert_form<T>(&self, other: &T)
Deserializes the contents of the request as an url encoded form, and asserts it matches the value given.
If other
does not match, or the response cannot be deserialized,
then this will panic.
sourcepub fn assert_status_success(&self)
pub fn assert_status_success(&self)
Assert that the status code is within the 2xx range. i.e. The range from 200-299.
sourcepub fn assert_status_failure(&self)
pub fn assert_status_failure(&self)
Assert that the status code is outside the 2xx range. i.e. A status code less than 200, or 300 or more.
sourcepub fn assert_status_bad_request(&self)
pub fn assert_status_bad_request(&self)
Assert the response status code is 400.
sourcepub fn assert_status_not_found(&self)
pub fn assert_status_not_found(&self)
Assert the response status code is 404.
Assert the response status code is 401.
sourcepub fn assert_status_forbidden(&self)
pub fn assert_status_forbidden(&self)
Assert the response status code is 403.
sourcepub fn assert_status_ok(&self)
pub fn assert_status_ok(&self)
Assert the response status code is 200.
sourcepub fn assert_status_not_ok(&self)
pub fn assert_status_not_ok(&self)
Assert the response status code is not 200.
sourcepub fn assert_status(&self, expected_status_code: StatusCode)
pub fn assert_status(&self, expected_status_code: StatusCode)
Assert the response status code matches the one given.
sourcepub fn assert_not_status(&self, expected_status_code: StatusCode)
pub fn assert_not_status(&self, expected_status_code: StatusCode)
Assert the response status code does not match the one given.
Trait Implementations§
source§impl Clone for TestResponse
impl Clone for TestResponse
source§fn clone(&self) -> TestResponse
fn clone(&self) -> TestResponse
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more