25 releases

0.7.1 Apr 18, 2024
0.6.1 Apr 21, 2023
0.6.0 Feb 17, 2023
0.5.2 Dec 20, 2022
0.2.0 Jul 6, 2021

#75 in HTTP client

Download history 195/week @ 2024-07-20 528/week @ 2024-07-27 966/week @ 2024-08-03 247/week @ 2024-08-10 766/week @ 2024-08-17 1067/week @ 2024-08-24 1063/week @ 2024-08-31 800/week @ 2024-09-07 967/week @ 2024-09-14 847/week @ 2024-09-21 832/week @ 2024-09-28 60/week @ 2024-10-05 961/week @ 2024-10-12 432/week @ 2024-10-19 105/week @ 2024-10-26 317/week @ 2024-11-02

1,841 downloads per month
Used in 3 crates

Apache-2.0

165KB
2K SLoC

asserhttp

Fluent http response assertions


A standard trait for doing fluent assertions over many http client response. Currently, supports actix-web, rocket, reqwest, hyper, axum, awc (Actix Web Client), surf, ureq and isahc.

Getting started

Add it to your Cargo.toml

asserhttp = { version = "0.6.1", features = ["reqwest"] }
#                             or features = ["hyper"]
#                             or features = ["actix"]
#                             or features = ["axum"]
#                             or features = ["actix-web-client"]
#                             or features = ["rocket"]
#                             or features = ["surf"]
#                             or features = ["ureq"]
#                             or features = ["isahc"]

Then use it in your tests, for example on actix-web,

use actix_web::{App, HttpResponse, test::{call_service, init_service, TestRequest}, web};
use asserhttp::*;

#[actix_web::test]
async fn sample_test() {
    let app = App::new().route("/", web::get().to(|| async { HttpResponse::Ok().body(json!({"a": "b"})) }));
    call_service(&mut init_service(app).await, TestRequest::get().to_request()).await
        .expect_status_ok()
        .expect_content_type_json()
        .expect_body_json_eq(json!({"a": "b"}));
}

or on reqwest

use reqwest;
use asserhttp::*;

#[tokio::test]
async fn my_test() {
    reqwest::get("http://localhost").await
        .expect_status_ok()
        .expect_content_type_json()
        .expect_body_json_eq(json!({"name": "jdoe"}));
}

Customize

You don't like the asserhttp methods name ? That's fine, you can define yours. Define you own trait and use asserhttp methods to define your own !

As simple as this:

asserhttp::asserhttp_customize!(MyHttpDsl);

pub trait MyHttpDsl<T>: asserhttp::Asserhttp<T> {
    fn is_status_ok(&mut self) -> &mut T {
        self.expect_status_ok()
    }
    fn is_json(&mut self) -> &mut T {
        self.expect_content_type_json()
    }
    fn has_body(&mut self) -> &mut T { self.expect_body_present() }
}

gRPC

Asserting gRPC is also supported with a tonic client. Simply turn on the tonic feature and use it like this:

use asserhttp::grpc::*;

#[tokio::main]
async fn main() {
    // success
    client.call_svc(tonic::Request::new(Payload)).await.expect_status_ok().expect_body(Payload);
    // error
    client.call_svc(tonic::Request::new(Payload)).await.expect_status_error(Code::NotFound);
}

Dependencies

~9–47MB
~766K SLoC