pub trait AsserhttpHeader<T> {
    const APPLICATION_JSON: &'static str = "application/json";
    const TEXT_PLAIN: &'static str = "text/plain";

    fn expect_header(
        &mut self,
        key: impl Into<HeaderKey>,
        value: impl Into<HeaderValueAsserter>
    ) -> &mut T; fn expect_headers(
        &mut self,
        key: impl Into<HeaderKey>,
        values: impl Into<HeaderValuesAsserter>
    ) -> &mut T; fn expect_header_present(&mut self, key: impl Into<HeaderKey>) -> &mut T; fn expect_header_absent(&mut self, key: impl Into<HeaderKey>) -> &mut T; fn expect_content_type_json(&mut self) -> &mut T { ... } fn expect_content_type_text(&mut self) -> &mut T { ... } }
Expand description

For assertions on http response headers

Provided Associated Constants

Required Methods

Expects response header to be equal

  • key - expected header key
  • value - expected header value or closure
Example
use asserhttp::*;

#[tokio::main]
async fn main() {
    reqwest::blocking::get("http://localhost").expect_header("content-type", "application/json");
    reqwest::blocking::get("http://localhost").expect_header(headers::CONTENT_TYPE, "application/json");
    reqwest::blocking::get("http://localhost").expect_header("content-type", |h: &str| assert!(h.contains("application/")));
    reqwest::blocking::get("http://localhost").expect_header(headers::CONTENT_TYPE, |h: &str| assert!(h.contains("application/")));
    reqwest::get("http://localhost").await.expect_header("content-type", "application/json");
    reqwest::get("http://localhost").await.expect_header(headers::CONTENT_TYPE, "application/json");
    reqwest::get("http://localhost").await.expect_header("content-type", |h: &str| assert!(h.contains("application/")));
    reqwest::get("http://localhost").await.expect_header(headers::CONTENT_TYPE, |h: &str| assert!(h.contains("application/")));

    isahc::get("http://localhost").expect_header("content-type", "application/json");
    isahc::get("http://localhost").expect_header(headers::CONTENT_TYPE, "application/json");
    isahc::get("http://localhost").expect_header("content-type", |h: &str| assert!(h.contains("application/")));
    isahc::get("http://localhost").expect_header(headers::CONTENT_TYPE, |h: &str| assert!(h.contains("application/")));
    isahc::get_async("http://localhost").await.expect_header("content-type", "application/json");
    isahc::get_async("http://localhost").await.expect_header(headers::CONTENT_TYPE, "application/json");
    isahc::get_async("http://localhost").await.expect_header("content-type", |h: &str| assert!(h.contains("application/")));
    isahc::get_async("http://localhost").await.expect_header(headers::CONTENT_TYPE, |h: &str| assert!(h.contains("application/")));

    surf::get("http://localhost").await.expect_header("content-type", "application/json");
    surf::get("http://localhost").await.expect_header(headers::CONTENT_TYPE, "application/json");
    surf::get("http://localhost").await.expect_header("content-type", |h: &str| assert!(h.contains("application/")));
    surf::get("http://localhost").await.expect_header(headers::CONTENT_TYPE, |h: &str| assert!(h.contains("application/")));

    ureq::get("http://localhost").call().or_any_status().expect_header("content-type", "application/json");
    ureq::get("http://localhost").call().or_any_status().expect_header(headers::CONTENT_TYPE, "application/json");
    ureq::get("http://localhost").call().or_any_status().expect_header("content-type", |h: &str| assert!(h.contains("application/")));
    ureq::get("http://localhost").call().or_any_status().expect_header(headers::CONTENT_TYPE, |h: &str| assert!(h.contains("application/")));

    hyper::Client::new().get("http://localhost".parse().unwrap()).await.expect_header("content-type", "application/json");
    hyper::Client::new().get("http://localhost".parse().unwrap()).await.expect_header(headers::CONTENT_TYPE, "application/json");
    hyper::Client::new().get("http://localhost".parse().unwrap()).await.expect_header("content-type", |h: &str| assert!(h.contains("application/")));
    hyper::Client::new().get("http://localhost".parse().unwrap()).await.expect_header(headers::CONTENT_TYPE, |h: &str| assert!(h.contains("application/")));

    awc::Client::default().get("http://localhost").send().await.expect_header("content-type", "application/json");
    awc::Client::default().get("http://localhost").send().await.expect_header(headers::CONTENT_TYPE, "application/json");
    awc::Client::default().get("http://localhost").send().await.expect_header("content-type", |h: &str| assert!(h.contains("application/")));
    awc::Client::default().get("http://localhost").send().await.expect_header(headers::CONTENT_TYPE, |h: &str| assert!(h.contains("application/")));
}

Expects response multi valued headers to be equal

  • key - expected header key
  • value - expected header values or closure
Example
use asserhttp::*;

#[tokio::main]
async fn main() {
    reqwest::blocking::get("http://localhost").expect_headers("cache-control", ["no-cache", "no-store"]);
    reqwest::blocking::get("http://localhost").expect_headers(headers::CACHE_CONTROL, ["no-cache", "no-store"]);
    reqwest::blocking::get("http://localhost").expect_headers("cache-control", |h: Vec<&str>| assert!(h.contains(&"no-cache") && h.contains(&"no-store")));
    reqwest::blocking::get("http://localhost").expect_headers(headers::CACHE_CONTROL, |h: Vec<&str>| assert!(h.contains(&"no-cache") && h.contains(&"no-store")));
    reqwest::get("http://localhost").await.expect_headers("cache-control", ["no-cache", "no-store"]);
    reqwest::get("http://localhost").await.expect_headers(headers::CACHE_CONTROL, ["no-cache", "no-store"]);
    reqwest::get("http://localhost").await.expect_headers("cache-control", |h: Vec<&str>| assert!(h.contains(&"no-cache") && h.contains(&"no-store")));
    reqwest::get("http://localhost").await.expect_headers(headers::CACHE_CONTROL, |h: Vec<&str>| assert!(h.contains(&"no-cache") && h.contains(&"no-store")));

    isahc::get("http://localhost").expect_headers("cache-control", ["no-cache", "no-store"]);
    isahc::get("http://localhost").expect_headers(headers::CACHE_CONTROL, ["no-cache", "no-store"]);
    isahc::get("http://localhost").expect_headers("cache-control", |h: Vec<&str>| assert!(h.contains(&"no-cache") && h.contains(&"no-store")));
    isahc::get("http://localhost").expect_headers(headers::CACHE_CONTROL, |h: Vec<&str>| assert!(h.contains(&"no-cache") && h.contains(&"no-store")));
    isahc::get_async("http://localhost").await.expect_headers("cache-control", ["no-cache", "no-store"]);
    isahc::get_async("http://localhost").await.expect_headers(headers::CACHE_CONTROL, ["no-cache", "no-store"]);
    isahc::get_async("http://localhost").await.expect_headers("cache-control", |h: Vec<&str>| assert!(h.contains(&"no-cache") && h.contains(&"no-store")));
    isahc::get_async("http://localhost").await.expect_headers(headers::CACHE_CONTROL, |h: Vec<&str>| assert!(h.contains(&"no-cache") && h.contains(&"no-store")));

    surf::get("http://localhost").await.expect_headers("cache-control", ["no-cache", "no-store"]);
    surf::get("http://localhost").await.expect_headers(headers::CACHE_CONTROL, ["no-cache", "no-store"]);
    surf::get("http://localhost").await.expect_headers("cache-control", |h: Vec<&str>| assert!(h.contains(&"no-cache") && h.contains(&"no-store")));
    surf::get("http://localhost").await.expect_headers(headers::CACHE_CONTROL, |h: Vec<&str>| assert!(h.contains(&"no-cache") && h.contains(&"no-store")));

    ureq::get("http://localhost").call().or_any_status().expect_headers("cache-control", ["no-cache", "no-store"]);
    ureq::get("http://localhost").call().or_any_status().expect_headers(headers::CACHE_CONTROL, ["no-cache", "no-store"]);
    ureq::get("http://localhost").call().or_any_status().expect_headers("cache-control", |h: Vec<&str>| assert!(h.contains(&"no-cache") && h.contains(&"no-store")));
    ureq::get("http://localhost").call().or_any_status().expect_headers(headers::CACHE_CONTROL, |h: Vec<&str>| assert!(h.contains(&"no-cache") && h.contains(&"no-store")));

    hyper::Client::new().get("http://localhost".parse().unwrap()).await.expect_headers("cache-control", ["no-cache", "no-store"]);
    hyper::Client::new().get("http://localhost".parse().unwrap()).await.expect_headers(headers::CACHE_CONTROL, ["no-cache", "no-store"]);
    hyper::Client::new().get("http://localhost".parse().unwrap()).await.expect_headers("cache-control", |h: Vec<&str>| assert!(h.contains(&"no-cache") && h.contains(&"no-store")));
    hyper::Client::new().get("http://localhost".parse().unwrap()).await.expect_headers(headers::CACHE_CONTROL, |h: Vec<&str>| assert!(h.contains(&"no-cache") && h.contains(&"no-store")));

    awc::Client::default().get("http://localhost").send().await.expect_headers("cache-control", ["no-cache", "no-store"]);
    awc::Client::default().get("http://localhost").send().await.expect_headers(headers::CACHE_CONTROL, ["no-cache", "no-store"]);
    awc::Client::default().get("http://localhost").send().await.expect_headers("cache-control", |h: Vec<&str>| assert!(h.contains(&"no-cache") && h.contains(&"no-store")));
    awc::Client::default().get("http://localhost").send().await.expect_headers(headers::CACHE_CONTROL, |h: Vec<&str>| assert!(h.contains(&"no-cache") && h.contains(&"no-store")));
}

Expects response header to be present

  • key - expected present header key
Example
use asserhttp::*;

#[tokio::main]
async fn main() {
    reqwest::blocking::get("http://localhost").expect_header_present("content-type");
    reqwest::blocking::get("http://localhost").expect_header_present(headers::CONTENT_TYPE);
    reqwest::get("http://localhost").await.expect_header_present("content-type");
    reqwest::get("http://localhost").await.expect_header_present(headers::CONTENT_TYPE);

    isahc::get("http://localhost").expect_header_present("content-type");
    isahc::get("http://localhost").expect_header_present(headers::CONTENT_TYPE);
    isahc::get_async("http://localhost").await.expect_header_present("content-type");
    isahc::get_async("http://localhost").await.expect_header_present(headers::CONTENT_TYPE);

    surf::get("http://localhost").await.expect_header_present("content-type");
    surf::get("http://localhost").await.expect_header_present(headers::CONTENT_TYPE);

    ureq::get("http://localhost").call().or_any_status().expect_header_present("content-type");
    ureq::get("http://localhost").call().or_any_status().expect_header_present(headers::CONTENT_TYPE);

    hyper::Client::new().get("http://localhost".parse().unwrap()).await.expect_header_present("content-type");
    hyper::Client::new().get("http://localhost".parse().unwrap()).await.expect_header_present(headers::CONTENT_TYPE);

    awc::Client::default().get("http://localhost").send().await.expect_header_present("content-type");
    awc::Client::default().get("http://localhost").send().await.expect_header_present(headers::CONTENT_TYPE);
}

Expects response header to be absent

  • key - expected absent header key
Example
use asserhttp::*;

#[tokio::main]
async fn main() {
    reqwest::blocking::get("http://localhost").expect_header_absent("content-type");
    reqwest::blocking::get("http://localhost").expect_header_absent(headers::CONTENT_TYPE);
    reqwest::get("http://localhost").await.expect_header_absent("content-type");
    reqwest::get("http://localhost").await.expect_header_absent(headers::CONTENT_TYPE);

    isahc::get("http://localhost").expect_header_absent("content-type");
    isahc::get("http://localhost").expect_header_absent(headers::CONTENT_TYPE);
    isahc::get_async("http://localhost").await.expect_header_absent("content-type");
    isahc::get_async("http://localhost").await.expect_header_absent(headers::CONTENT_TYPE);

    surf::get("http://localhost").await.expect_header_absent("content-type");
    surf::get("http://localhost").await.expect_header_absent(headers::CONTENT_TYPE);

    ureq::get("http://localhost").call().or_any_status().expect_header_absent("content-type");
    ureq::get("http://localhost").call().or_any_status().expect_header_absent(headers::CONTENT_TYPE);

    hyper::Client::new().get("http://localhost".parse().unwrap()).await.expect_header_absent("content-type");
    hyper::Client::new().get("http://localhost".parse().unwrap()).await.expect_header_absent(headers::CONTENT_TYPE);

    awc::Client::default().get("http://localhost").send().await.expect_header_absent("content-type");
    awc::Client::default().get("http://localhost").send().await.expect_header_absent(headers::CONTENT_TYPE);
}

Provided Methods

Expects response header Content-Type: application/json

Example
use asserhttp::*;

#[tokio::main]
async fn main() {
    reqwest::blocking::get("http://localhost").expect_content_type_json();
    reqwest::get("http://localhost").await.expect_content_type_json();

    isahc::get("http://localhost").expect_content_type_json();
    isahc::get_async("http://localhost").await.expect_content_type_json();

    surf::get("http://localhost").await.expect_content_type_json();

    ureq::get("http://localhost").call().or_any_status().expect_content_type_json();

    hyper::Client::new().get("http://localhost".parse().unwrap()).await.expect_content_type_json();

    awc::Client::default().get("http://localhost").send().await.expect_content_type_json();
}

Expects response header Content-Type: text/plain

Example
use asserhttp::*;

#[tokio::main]
async fn main() {
    reqwest::blocking::get("http://localhost").expect_content_type_text();
    reqwest::get("http://localhost").await.expect_content_type_text();

    isahc::get("http://localhost").expect_content_type_text();
    isahc::get_async("http://localhost").await.expect_content_type_text();

    surf::get("http://localhost").await.expect_content_type_text();

    ureq::get("http://localhost").call().or_any_status().expect_content_type_text();

    hyper::Client::new().get("http://localhost".parse().unwrap()).await.expect_content_type_text();

    awc::Client::default().get("http://localhost").send().await.expect_content_type_text();
}

Implementations on Foreign Types

Implementors