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§
- multipart
- This supplies the building blocks for sending multipart forms using
TestRequest::multipart()
. - transport_
layer - util
Structs§
- Test
Request - A
TestRequest
is for building and executing a HTTP request to theTestServer
. - Test
Response - The
TestResponse
is the result of a request created using aTestServer
. TheTestServer
builds aTestRequest
, which when awaited, will produce the response. - Test
Server - The
TestServer
runs your Axum application, allowing you to make HTTP requests against it. - Test
Server Builder - A builder for
crate::TestServer
. Inside is acrate::TestServerConfig
, configured by each method, and then turn into a server bycrate::TestServerBuilder::build
. - Test
Server Config - This is for customising the
TestServer
on construction. It implementsDefault
to ease building. - Test
WebSocket ws