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_text<F>(&mut self, asserter: F) -> &mut T
where F: FnOnce(String);
fn expect_body_bytes<F>(&mut self, asserter: F) -> &mut T
where F: FnOnce(&[u8]);
fn expect_body_present(&mut self) -> &mut T;
fn expect_body_absent(&mut self) -> &mut T;
// Provided methods
fn expect_body_json_eq<B>(&mut self, body: B) -> &mut T
where B: DeserializeOwned + Serialize + PartialEq + Debug + Unpin { ... }
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: Into<String> { ... }
fn expect_body_bytes_eq(&mut self, body: &[u8]) -> &mut T { ... }
}
Expand description
For assertions on http response body
Required Methods§
sourcefn expect_body_json<B, F>(&mut self, asserter: F) -> &mut Twhere
B: DeserializeOwned + Serialize + PartialEq + Debug + Unpin,
F: FnOnce(B),
fn expect_body_json<B, F>(&mut self, asserter: F) -> &mut Twhere B: DeserializeOwned + Serialize + PartialEq + Debug + Unpin, F: FnOnce(B),
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") }));
}
sourcefn expect_body_text<F>(&mut self, asserter: F) -> &mut Twhere
F: FnOnce(String),
fn expect_body_text<F>(&mut self, asserter: F) -> &mut Twhere 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"));
}
sourcefn expect_body_bytes<F>(&mut self, asserter: F) -> &mut Twhere
F: FnOnce(&[u8]),
fn expect_body_bytes<F>(&mut self, asserter: F) -> &mut Twhere 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"));
}
sourcefn expect_body_present(&mut self) -> &mut T
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();
}
sourcefn expect_body_absent(&mut self) -> &mut T
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();
}
Provided Methods§
sourcefn expect_body_json_eq<B>(&mut self, body: B) -> &mut Twhere
B: DeserializeOwned + Serialize + PartialEq + Debug + Unpin,
fn expect_body_json_eq<B>(&mut self, body: B) -> &mut Twhere B: DeserializeOwned + Serialize + PartialEq + Debug + Unpin,
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"}));
}
sourcefn expect_body_text_eq<B>(&mut self, body: B) -> &mut Twhere
B: Into<String>,
fn expect_body_text_eq<B>(&mut self, body: B) -> &mut Twhere 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");
}
sourcefn expect_body_text_matches<R>(&mut self, regex: R) -> &mut Twhere
R: Into<String>,
fn expect_body_text_matches<R>(&mut self, regex: R) -> &mut Twhere R: Into<String>,
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]+");
}
sourcefn expect_body_bytes_eq(&mut self, body: &[u8]) -> &mut T
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");
}