extern crate httpmock;
use isahc::{prelude::*, Request};
use serde_json::{json, Value};
use httpmock::Method::POST;
use httpmock::MockServer;
#[test]
fn json_value_body_test() {
let server = MockServer::start();
let m = server.mock(|when, then| {
when.method(POST)
.path("/users")
.header("Content-Type", "application/json")
.json_body(json!({ "name": "Fred" }));
then.status(201)
.header("Content-Type", "application/json")
.json_body(json!({ "name": "Hans" }));
});
let mut response = Request::post(&format!("http://{}/users", server.address()))
.header("Content-Type", "application/json")
.body(json!({ "name": "Fred" }).to_string())
.unwrap()
.send()
.unwrap();
let user: Value =
serde_json::from_str(&response.text().unwrap()).expect("cannot deserialize JSON");
m.assert();
assert_eq!(response.status(), 201);
assert_eq!(user.as_object().unwrap().get("name").unwrap(), "Hans");
}
#[test]
fn json_body_object_serde_test() {
#[derive(serde::Serialize, serde::Deserialize)]
struct TestUser {
name: String,
}
let server = MockServer::start();
let m = server.mock(|when, then| {
when.method(POST)
.path("/users")
.header("Content-Type", "application/json")
.json_body_obj(&TestUser {
name: String::from("Fred"),
});
then.status(201)
.header("Content-Type", "application/json")
.json_body_obj(&TestUser {
name: String::from("Hans"),
});
});
let mut response = Request::post(&format!("http://{}/users", server.address()))
.header("Content-Type", "application/json")
.body(
json!(&TestUser {
name: "Fred".to_string()
})
.to_string(),
)
.unwrap()
.send()
.unwrap();
let user: TestUser =
serde_json::from_str(&response.text().unwrap()).expect("cannot deserialize JSON");
m.assert();
assert_eq!(response.status(), 201);
assert_eq!(user.name, "Hans");
}
#[test]
fn partial_json_body_test() {
let server = MockServer::start();
#[derive(serde::Serialize, serde::Deserialize)]
struct ChildStructure {
some_attribute: String,
}
#[derive(serde::Serialize, serde::Deserialize)]
struct ParentStructure {
some_other_value: String,
child: ChildStructure,
}
let m = server.mock(|when, then| {
when.method(POST).path("/users").json_body_partial(
r#"
{
"child" : {
"some_attribute" : "Fred"
}
}
"#,
);
then.status(201).body(r#"{"result":"success"}"#);
});
let uri = format!("http://{}/users", m.server_address());
let response = Request::post(&uri)
.header("Content-Type", "application/json")
.header("User-Agent", "rust-test")
.body(
serde_json::to_string(&ParentStructure {
child: ChildStructure {
some_attribute: "Fred".to_string(),
},
some_other_value: "Flintstone".to_string(),
})
.unwrap(),
)
.unwrap()
.send()
.unwrap();
m.assert();
assert_eq!(response.status(), 201);
}