Crate actix_test

Source
Expand description

Integration testing tools for Actix Web applications.

The main integration testing tool is TestServer. It spawns a real HTTP server on an unused port and provides methods that use a real HTTP client. Therefore, it is much closer to real-world cases than using init_service, which skips HTTP encoding and decoding.

§Examples

use actix_web::{get, web, test, App, HttpResponse, Error, Responder};

#[get("/")]
async fn my_handler() -> Result<impl Responder, Error> {
    Ok(HttpResponse::Ok())
}

#[actix_rt::test]
async fn test_example() {
    let srv = actix_test::start(||
        App::new().service(my_handler)
    );

    let req = srv.get("/");
    let res = req.send().await.unwrap();

    assert!(res.status().is_success());
}

Structs§

Client
An asynchronous HTTP and WebSocket client.
ClientRequest
An HTTP Client request builder
ClientResponse
Client Response
Connector
Manages HTTP client network connectivity.
TestBuffer
Async I/O test buffer.
TestRequest
Test Request builder.
TestServer
A basic HTTP server controller that simplifies the process of writing integration tests for Actix Web applications.
TestServerConfig

Enums§

PayloadError
A set of errors that can occur during payload parsing.

Functions§

call_and_read_body
Helper function that returns a response body of a TestRequest
call_and_read_body_json
Helper function that returns a deserialized response body of a TestRequest
call_service
Calls service and waits for response future completion.
config
Create default test server config.
init_service
Initialize service from application builder instance.
ok_service
Creates service that always responds with 200 OK and no body.
read_body
Helper function that returns a response body of a ServiceResponse.
read_body_json
Helper function that returns a deserialized response body of a ServiceResponse.
start
Start default TestServer.
start_with
Start test server with custom configuration
status_service
Creates service that always responds with given status code and no body.
to_bytes
Collects all the bytes produced by body.
unused_addr
Get a localhost socket address with random, unused port.