Expand description
Axum Test is a library for writing tests for web servers written using Axum:
- You create a
TestServer
within a test, - use that to build
TestRequest
against your application, - receive back a
TestResponse
, - then assert the response is how you expect.
It includes built in support for serializing and deserializing request and response bodies using Serde, support for cookies and headers, and other common bits you would expect.
TestServer
will pass http requests directly to the handler,
or can be run on a random IP / Port address.
§Getting Started
Create a TestServer
running your Axum Router
:
use axum::Router;
use axum::extract::Json;
use axum::routing::put;
use axum_test::TestServer;
use serde_json::json;
use serde_json::Value;
async fn route_put_user(Json(user): Json<Value>) -> () {
// todo
}
let my_app = Router::new()
.route("/users", put(route_put_user));
let server = TestServer::new(my_app)?;
Then make requests against it:
let response = server.put("/users")
.json(&json!({
"username": "Terrance Pencilworth",
}))
.await;
Re-exports§
pub use http;
Modules§
- This supplies the building blocks for sending multipart forms using
TestRequest::multipart()
.
Structs§
- A
TestRequest
is for building and executing a HTTP request to theTestServer
. - The
TestResponse
is the result of a request created using aTestServer
. TheTestServer
builds aTestRequest
, which when awaited, will produce the response. - The
TestServer
runs your Axum application, allowing you to make HTTP requests against it. - A builder for
crate::TestServer
. Inside is acrate::TestServerConfig
, configured by each method, and then turn into a server bycrate::TestServerBuilder::build
. - This is for customising the
TestServer
on construction. It implementsDefault
to ease building.
Enums§
- Transport is for setting which transport mode for the
TestServer
to use when making requests. - An enum representing the various forms of a WebSocket message.