Module warp::test[][src]

Test utilities to test your filters.

Filters can be easily tested without starting up an HTTP server, by making use of the RequestBuilder in this module.

Testing Filters

It’s easy to test filters, especially if smaller filters are used to build up your full set. Consider these example filters:

use warp::Filter;

fn sum() -> impl Filter<Extract = (u32,), Error = warp::Rejection> + Copy {
        .map(|x: u32, y: u32| {
            x + y

fn math() -> impl Filter<Extract = (String,), Error = warp::Rejection> + Copy {
        .map(|z: u32| {
            format!("Sum = {}", z)

We can test some requests against the sum filter like this:

async fn test_sum() {
    let filter = sum();

    // Execute `sum` and get the `Extract` back.
    let value = warp::test::request()
    assert_eq!(value, 3);

    // Or simply test if a request matches (doesn't reject).

If the filter returns something that implements Reply, and thus can be turned into a response sent back to the client, we can test what exact response is returned. The math filter uses the sum filter, but returns a String that can be turned into a response.

fn test_math() {
    let filter = math();

    let res = warp::test::request()
    assert_eq!(res.status(), 405, "GET is not allowed");

    let res = warp::test::request()
    assert_eq!(res.status(), 200);
    assert_eq!(res.body(), "Sum is 3");



A request builder for testing filters.


A Websocket builder for testing filters.


A test client for Websocket filters.


An error from Websocket filter tests.



Starts a new test RequestBuilder.


Starts a new test WsBuilder.