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§

Modules§

Structs§

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.