Trait asserhttp::AsserhttpBody

source ·
pub trait AsserhttpBody<T> {
    // Required methods
    fn expect_body_json<B, F>(&mut self, asserter: F) -> &mut T
       where B: DeserializeOwned + Serialize + PartialEq + Debug + Unpin,
             F: FnOnce(B);
    fn expect_body_json_eq<B>(&mut self, body: B) -> &mut T
       where B: DeserializeOwned + Serialize + PartialEq + Debug + Unpin + Sized;
    fn expect_body_text<F>(&mut self, asserter: F) -> &mut T
       where F: FnOnce(String);
    fn expect_body_text_eq<B>(&mut self, body: B) -> &mut T
       where B: Into<String>;
    fn expect_body_text_matches<R>(&mut self, regex: R) -> &mut T
       where R: TryInto<Regex, Error = Error>;
    fn expect_body_bytes<F>(&mut self, asserter: F) -> &mut T
       where F: FnOnce(&[u8]);
    fn expect_body_bytes_eq(&mut self, body: &[u8]) -> &mut T;
    fn expect_body_present(&mut self) -> &mut T;
    fn expect_body_absent(&mut self) -> &mut T;
}
Expand description

For assertions on http response body

Required Methods§

source

fn expect_body_json<B, F>(&mut self, asserter: F) -> &mut T

Allows verifying json body in a closure

  • asserter - closure to verify json body
§Example
use asserhttp::*;
use serde_json::{json, Value};

#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
struct MyStruct { a: String }

#[tokio::main]
async fn main() {
    reqwest::blocking::get("http://localhost").expect_body_json(|b: Value| assert_eq!(b, json!({"a": "b"})));
    reqwest::get("http://localhost").await.expect_body_json(|b: Value| assert_eq!(b, json!({"a": "b"})));

    isahc::get("http://localhost").expect_body_json(|b: Value| assert_eq!(b, json!({"a": "b"})));
    isahc::get_async("http://localhost").await.expect_body_json(|b: Value| assert_eq!(b, json!({"a": "b"})));

    surf::get("http://localhost").await.expect_body_json(|b: Value| assert_eq!(b, json!({"a": "b"})));

    ureq::get("http://localhost").call().or_any_status().expect_body_json(|b: Value| assert_eq!(b, json!({"a": "b"})));

    hyper::Client::new().get("http://localhost".parse().unwrap()).await.expect_body_json(|b: Value| assert_eq!(b, json!({"a": "b"})));

    awc::Client::default().get("http://localhost").send().await.expect_body_json(|b: Value| assert_eq!(b, json!({"a": "b"})));

    // or use your structs
    isahc::get("http://localhost").expect_body_json(|b: MyStruct| assert_eq!(b, MyStruct { a: String::from("b") }));
}
source

fn expect_body_json_eq<B>(&mut self, body: B) -> &mut T

Expects response body to be json and equal

  • body - expected json body
§Example
use asserhttp::*;
use serde_json::json;

#[tokio::main]
async fn main() {
    reqwest::blocking::get("http://localhost").expect_body_json_eq(json!({"a": "b"}));
    reqwest::get("http://localhost").await.expect_body_json_eq(json!({"a": "b"}));

    isahc::get("http://localhost").expect_body_json_eq(json!({"a": "b"}));
    isahc::get_async("http://localhost").await.expect_body_json_eq(json!({"a": "b"}));

    surf::get("http://localhost").await.expect_body_json_eq(json!({"a": "b"}));

    ureq::get("http://localhost").call().or_any_status().expect_body_json_eq(json!({"a": "b"}));

    hyper::Client::new().get("http://localhost".parse().unwrap()).await.expect_body_json_eq(json!({"a": "b"}));

    awc::Client::default().get("http://localhost").send().await.expect_body_json_eq(json!({"a": "b"}));
}
source

fn expect_body_text<F>(&mut self, asserter: F) -> &mut T
where F: FnOnce(String),

Allows verifying text body in a closure

  • asserter - closure to verify text body
§Example
use asserhttp::*;

#[tokio::main]
async fn main() {
    reqwest::blocking::get("http://localhost").expect_body_text(|b| assert_eq!(b, "abcd"));
    reqwest::get("http://localhost").await.expect_body_text(|b| assert_eq!(b, "abcd"));

    isahc::get("http://localhost").expect_body_text(|b| assert_eq!(b, "abcd"));
    isahc::get_async("http://localhost").await.expect_body_text(|b| assert_eq!(b, "abcd"));

    surf::get("http://localhost").await.expect_body_text(|b| assert_eq!(b, "abcd"));

    ureq::get("http://localhost").call().or_any_status().expect_body_text(|b| assert_eq!(b, "abcd"));

    hyper::Client::new().get("http://localhost".parse().unwrap()).await.expect_body_text(|b| assert_eq!(b, "abcd"));

    awc::Client::default().get("http://localhost").send().await.expect_body_text(|b| assert_eq!(b, "abcd"));
}
source

fn expect_body_text_eq<B>(&mut self, body: B) -> &mut T
where B: Into<String>,

Expects response body to be text and equal

  • body - expected text body
§Example
use asserhttp::*;

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

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

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

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

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

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

fn expect_body_text_matches<R>(&mut self, regex: R) -> &mut T
where R: TryInto<Regex, Error = Error>,

Expects response body to be text and to match provided regex

  • regex - must match text response body
§Example
use asserhttp::*;

#[tokio::main]
async fn main() {
    reqwest::blocking::get("http://localhost").expect_body_text_matches("[a-z]+");
    reqwest::get("http://localhost").await.expect_body_text_matches("[a-z]+");

    isahc::get("http://localhost").expect_body_text_matches("[a-z]+");
    isahc::get_async("http://localhost").await.expect_body_text_matches("[a-z]+");

    surf::get("http://localhost").await.expect_body_text_matches("[a-z]+");

    ureq::get("http://localhost").call().or_any_status().expect_body_text_matches("[a-z]+");

    hyper::Client::new().get("http://localhost".parse().unwrap()).await.expect_body_text_matches("[a-z]+");

    awc::Client::default().get("http://localhost").send().await.expect_body_text_matches("[a-z]+");
}
source

fn expect_body_bytes<F>(&mut self, asserter: F) -> &mut T
where F: FnOnce(&[u8]),

Allows verifying response body bytes in a closure

  • asserter - closure to verify response body as bytes
§Example
use asserhttp::*;

#[tokio::main]
async fn main() {
    reqwest::blocking::get("http://localhost").expect_body_bytes(|b| assert_eq!(b, b"abcd"));
    reqwest::get("http://localhost").await.expect_body_bytes(|b| assert_eq!(b, b"abcd"));

    isahc::get("http://localhost").expect_body_bytes(|b| assert_eq!(b, b"abcd"));
    isahc::get("http://localhost").expect_body_bytes(|b| assert_eq!(b, &[97, 98, 99, 100]));
    isahc::get("http://localhost").expect_body_bytes(|b| assert_eq!(b, &vec![97, 98, 99, 100]));

    // on any client
    isahc::get("http://localhost").expect_body_bytes(|b| assert_eq!(b, b"abcd"));
    isahc::get_async("http://localhost").await.expect_body_bytes(|b| assert_eq!(b, b"abcd"));

    surf::get("http://localhost").await.expect_body_bytes(|b| assert_eq!(b, b"abcd"));

    ureq::get("http://localhost").call().or_any_status().expect_body_bytes(|b| assert_eq!(b, b"abcd"));

    hyper::Client::new().get("http://localhost".parse().unwrap()).await.expect_body_bytes(|b| assert_eq!(b, b"abcd"));

    awc::Client::default().get("http://localhost").send().await.expect_body_bytes(|b| assert_eq!(b, b"abcd"));
}
source

fn expect_body_bytes_eq(&mut self, body: &[u8]) -> &mut T

Expects response body to be equal by comparing bytes

  • body - expected bytes response body
§Example
use asserhttp::*;

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

    isahc::get("http://localhost").expect_body_bytes_eq(b"abcd");
    isahc::get("http://localhost").expect_body_bytes_eq(&[97, 98, 99, 100]);
    isahc::get("http://localhost").expect_body_bytes_eq(&vec![97, 98, 99, 100]);

    // on any client
    isahc::get("http://localhost").expect_body_bytes_eq(b"abcd");
    isahc::get_async("http://localhost").await.expect_body_bytes_eq(b"abcd");

    surf::get("http://localhost").await.expect_body_bytes_eq(b"abcd");

    ureq::get("http://localhost").call().or_any_status().expect_body_bytes_eq(b"abcd");

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

    awc::Client::default().get("http://localhost").send().await.expect_body_bytes_eq(b"abcd");
}
source

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

Expects a response body to be present and not empty

§Example
use asserhttp::*;

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

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

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

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

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

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

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

Expects a response body to be absent or empty

§Example
use asserhttp::*;

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

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

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

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

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

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

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

source§

impl<T, E> AsserhttpBody<T> for Result<T, E>
where T: BodyAccessor, E: Debug,

source§

fn expect_body_json<B, F>(&mut self, asserter: F) -> &mut T

source§

fn expect_body_json_eq<B>(&mut self, body: B) -> &mut T

source§

fn expect_body_text<F>(&mut self, asserter: F) -> &mut T
where F: FnOnce(String),

source§

fn expect_body_text_eq<B>(&mut self, body: B) -> &mut T
where B: Into<String>,

source§

fn expect_body_text_matches<R>(&mut self, regex: R) -> &mut T
where R: TryInto<Regex, Error = Error>,

source§

fn expect_body_bytes<F>(&mut self, asserter: F) -> &mut T
where F: FnOnce(&[u8]),

source§

fn expect_body_bytes_eq(&mut self, body: &[u8]) -> &mut T

source§

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

source§

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

Implementors§

source§

impl<T> AsserhttpBody<T> for T
where T: BodyAccessor,