AsserhttpHeader

Trait AsserhttpHeader 

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

    // Required methods
    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;

    // Provided methods
    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§

Source

const APPLICATION_JSON: &'static str = "application/json"

Source

const TEXT_PLAIN: &'static str = "text/plain"

Required Methods§

Source

fn expect_header( &mut self, key: impl Into<HeaderKey>, value: impl Into<HeaderValueAsserter>, ) -> &mut T

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/")));
}
Source

fn expect_headers( &mut self, key: impl Into<HeaderKey>, values: impl Into<HeaderValuesAsserter>, ) -> &mut T

Expects response multivalued 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")));
}
Source

fn expect_header_present(&mut self, key: impl Into<HeaderKey>) -> &mut T

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);
}
Source

fn expect_header_absent(&mut self, key: impl Into<HeaderKey>) -> &mut T

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§

Source

fn expect_content_type_json(&mut self) -> &mut T

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();
}
Source

fn expect_content_type_text(&mut self) -> &mut T

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();
}

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl<T, E> AsserhttpHeader<T> for Result<T, E>
where T: HeaderAccessor, E: Debug,

Source§

fn expect_header( &mut self, key: impl Into<HeaderKey>, value: impl Into<HeaderValueAsserter>, ) -> &mut T

Source§

fn expect_headers( &mut self, key: impl Into<HeaderKey>, values: impl Into<HeaderValuesAsserter>, ) -> &mut T

Source§

fn expect_header_present(&mut self, key: impl Into<HeaderKey>) -> &mut T

Source§

fn expect_header_absent(&mut self, key: impl Into<HeaderKey>) -> &mut T

Implementors§

Source§

impl<T> AsserhttpHeader<T> for T
where T: HeaderAccessor,