asserhttp 0.4.1

Fluent http response assertions
Documentation

Allows fluent assertions for various http client responses. Supports actix-web, rocket, 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 isahc;
# use serde::Deserialize;
# use serde_json::{json, Value};
use asserhttp::*;

#[test]
fn my_test() {
#    isahc::get("http://localhost/api/any")
// 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

actix-web

Use actix feature.

For unit testing

use actix_web::{HttpRequest, HttpResponse, test::TestRequest};
use serde_json::json;
use asserhttp::*;

#[actix_web::test]
async fn sample_test() {
async fn handler(_: HttpRequest) -> HttpResponse { HttpResponse::Ok().body(json!({"a": "b"})) }
handler(TestRequest::get().to_http_request()).await
.expect_status_ok()
.expect_content_type_json()
.expect_body_json_eq(json!({"a": "b"}));
// and many more !
}

For integration tests

use actix_web::{App, HttpResponse, test::{call_service, init_service, TestRequest}, web};
use serde_json::json;
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"}));
// and many more !
}

rocket

Use rocket feature.

use rocket::{get, http::Status, local::asynchronous::Client, routes};
use serde_json::{json, Value};
use asserhttp::*;

#[rocket::async_test]
async fn sample_test() {
#[get("/")]
fn endpoint() -> Value { json!({"a": "b"}) }
let client = Client::tracked(rocket::build().mount("/", routes![endpoint])).await.unwrap();
client.get("/").dispatch().await
.expect_status_ok()
.expect_content_type_json()
.expect_body_json_eq(json!({"a": "b"}));
}

reqwest

Use reqwest feature.

use reqwest;
use asserhttp::*;
use serde_json::json;

#[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 feature.

use hyper::Client;
use asserhttp::*;
use serde_json::json;

#[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-web-client feature.

use awc::Client;
use asserhttp::*;
use serde_json::json;

#[actix_web::test]
async fn sample_test() {
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 feature.

use surf;
use asserhttp::*;
use serde_json::json;

#[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 feature.

use isahc;
use asserhttp::*;
use serde_json::json;

#[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 !
}