Crate axum_test

Source
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§

TestRequest
A TestRequest is for building and executing a HTTP request to the TestServer.
TestResponse
The TestResponse is the result of a request created using a TestServer. The TestServer builds a TestRequest, which when awaited, will produce the response.
TestServer
The TestServer runs your Axum application, allowing you to make HTTP requests against it.
TestServerBuilder
A builder for crate::TestServer. Inside is a crate::TestServerConfig, configured by each method, and then turn into a server by crate::TestServerBuilder::build.
TestServerConfig
This is for customising the TestServer on construction. It implements Default to ease building.
TestWebSocketws

Enums§

Transport
Transport is for setting which transport mode for the TestServer to use when making requests.
WsMessagews
An enum representing the various forms of a WebSocket message.