Crate asserhttp[][src]

Expand description

Allows fluent assertions for various http client responses. Supports reqwest, hyper, awc (Actix Web Client), surf and isahc.

It works for blocking or async client methods and for responses wrapped in Result.

API

Here’s the list of all the provided asserters.

use asserhttp::*;

#[test]
fn my_test() {
    // status
    .expect_status_eq(200)
    .expect_status_in_range(200, 400)
    .expect_status_success()
    .expect_status_redirection()
    .expect_status_client_error()
    .expect_status_server_error()
    .expect_status_ok()
    .expect_status_created()
    .expect_status_accepted()
    .expect_status_no_content()
    .expect_status_partial_content()
    .expect_status_bad_request()
    .expect_status_unauthorized()
    .expect_status_forbidden()
    .expect_status_not_found()
    .expect_status_conflict()
    .expect_status_gone()
    .expect_status_internal_server_error()
    // header
    .expect_header("content-type", "application/pdf")
    .expect_headers("cache-control", ["no-cache", "no-store"])
    .expect_header_present("x-my-header")
    .expect_header_absent("x-my-header")
    .expect_content_type_json()
    .expect_content_type_text()
    // body
    .expect_body_json(|b: Value| assert_eq!(b, json!({"a": "b"})))
    .expect_body_json_eq(json!({"name": "jdoe"}))
    .expect_body_text(|b| assert_eq!(b, "abcd"))
    .expect_body_text_eq("abcd")
    .expect_body_text_matches("[a-z]+")
    .expect_body_bytes(|b| assert_eq!(b, b"abcd"))
    .expect_body_bytes_eq(b"abcd")
    .expect_body_present()
    .expect_body_absent();
}

Example

reqwest

use reqwest;
use asserhttp::*;

#[tokio::test]
async fn sample_test() {
    reqwest::get("http://localhost/api/any").await.unwrap().expect_status_ok();
    // no need to call `.unwrap()` directly
    reqwest::get("http://localhost/api/any").await.expect_status_eq(200);
    reqwest::get("http://localhost/api/any").await.expect_status_ok();
    reqwest::get("http://localhost/api/any").await.expect_status_bad_request();
    reqwest::get("http://localhost/api/any").await.expect_status_internal_server_error();
    // chain expectations
    reqwest::get("http://localhost/api/any").await
        .expect_status_ok()
        .expect_content_type_json()
        .expect_body_json_eq(json!({"name": "jdoe"}));
    // and many more !
}

hyper

use hyper::Client;
use asserhttp::*;

#[tokio::test]
async fn sample_test() {
    Client::new().get("http://localhost/api/any").await.unwrap().expect_status_ok();
    // no need to call `.unwrap()` directly
    Client::new().get("http://localhost/api/any").await.expect_status_eq(200);
    Client::new().get("http://localhost/api/any").await.expect_status_ok();
    Client::new().get("http://localhost/api/any").await.expect_status_bad_request();
    Client::new().get("http://localhost/api/any").await.expect_status_internal_server_error();
    // chain expectations
    Client::new().get("http://localhost/api/any").await
        .expect_status_ok()
        .expect_content_type_json()
        .expect_body_json_eq(json!({"name": "jdoe"}));
    // and many more !
}

awc (Actix Web Client)

use actix_rt::System;
use awc::Client;
use asserhttp::*;

#[test]
fn sample_test() {
    System::new("test").block_on(async move {
        Client::default().get("http://localhost/api/any").await.unwrap().expect_status_ok();
        // no need to call `.unwrap()` directly
        Client::default().get("http://localhost/api/any").await.expect_status_eq(200);
        Client::default().get("http://localhost/api/any").await.expect_status_ok();
        Client::default().get("http://localhost/api/any").await.expect_status_bad_request();
        Client::default().get("http://localhost/api/any").await.expect_status_internal_server_error();
        // chain expectations
        Client::default().get("http://localhost/api/any").await
            .expect_status_ok()
            .expect_content_type_json()
            .expect_body_json_eq(json!({"name": "jdoe"}));
        // and many more !
    });
}

surf

use surf;
use asserhttp::*;

#[async_std::test]
async fn sample_test() {
    surf::get("http://localhost/api/any").await.unwrap().expect_status_ok();
    // no need to call `.unwrap()` directly
    surf::get("http://localhost/api/any").await.expect_status_eq(200);
    surf::get("http://localhost/api/any").await.expect_status_ok();
    surf::get("http://localhost/api/any").await.expect_status_bad_request();
    surf::get("http://localhost/api/any").await.expect_status_internal_server_error();
    // chain expectations
    surf::get("http://localhost/api/any").await
        .expect_status_ok()
        .expect_content_type_json()
        .expect_body_json_eq(json!({"name": "jdoe"}));
    // and many more !
}

isahc

use isahc;
use asserhttp::*;

#[async_std::test]
async fn sample_test() {
    isahc::get_async("http://localhost/api/any").await.unwrap().expect_status_ok();
    // no need to call `.unwrap()` directly
    isahc::get_async("http://localhost/api/any").await.expect_status_eq(200);
    isahc::get_async("http://localhost/api/any").await.expect_status_ok();
    isahc::get_async("http://localhost/api/any").await.expect_status_bad_request();
    isahc::get_async("http://localhost/api/any").await.expect_status_internal_server_error();
    // chain expectations
    isahc::get_async("http://localhost/api/any").await
        .expect_status_ok()
        .expect_content_type_json()
        .expect_body_json_eq(json!({"name": "jdoe"}));
    // and many more !
}

Traits

For assertions on http response

For assertions on http response body

For assertions on http response headers

For assertions on http response status