#[macro_use] extern crate json;
#[macro_use] extern crate noir;
#[macro_use]
mod base_test;
test!();
// Dump Responses -------------------------------------------------------------
#[test]
fn test_dump_response_with_raw_body() {
let actual = {
API::post("/echo")
.with_header(Accept(vec![
qitem(Mime(TopLevel::Application, SubLevel::Json, vec![]))
]))
.with_body([
0x00, 0xa0, 0xff, 0x80, 0x45, 0x13, 0x21, 0x78,
0x67, 0x08, 0x90, 0xca, 0xd4, 0xe5, 0xf4, 0x89,
0x00, 0xa0, 0xff, 0x80, 0x45, 0x13, 0x21, 0x78,
0x67, 0x08, 0x90, 0xca, 0xd4, 0xe5, 0xf4, 0x89,
0x00, 0xa0, 0xff, 0x80, 0x45, 0x13, 0x21, 0x78,
0x67, 0x08, 0x90, 0xca, 0xd4, 0xe5, 0xf4, 0x89,
0x00, 0xa0, 0xff, 0x80, 0x45, 0x13, 0x21, 0x78,
0x67, 0x08, 0x90, 0xca, 0xd4, 0xe5, 0xf4, 0x89
].to_vec())
.dump()
.collect()
};
assert_fail!(r#"
Response Failure: POST request to \"http://localhost:4000/echo\" returned
1 error(s)
1) Response headers dump:
Accept: application/json
Content-Length: 64
Content-Type: application/octet-stream
Date:
Host: localhost:4000
Response raw body dump of 64 bytes:
[0x00, 0xA0, 0xFF, 0x80, 0x45, 0x13, 0x21, 0x78, 0x67, 0x08, 0x90, 0xCA, 0xD4, 0xE5, 0xF4, 0x89,
0x00, 0xA0, 0xFF, 0x80, 0x45, 0x13, 0x21, 0x78, 0x67, 0x08, 0x90, 0xCA, 0xD4, 0xE5, 0xF4, 0x89,
0x00, 0xA0, 0xFF, 0x80, 0x45, 0x13, 0x21, 0x78, 0x67, 0x08, 0x90, 0xCA, 0xD4, 0xE5, 0xF4, 0x89,
0x00, 0xA0, 0xFF, 0x80, 0x45, 0x13, 0x21, 0x78, 0x67, 0x08, 0x90, 0xCA, 0xD4, 0xE5, 0xF4, 0x89]
"#, actual);
}
#[test]
fn test_dump_response_with_text_body() {
let actual = {
API::post("/echo")
.with_header(ContentType(Mime(TopLevel::Text, SubLevel::Plain, vec![])))
.with_header(Accept(vec![
qitem(Mime(TopLevel::Application, SubLevel::Json, vec![]))
]))
.with_body("Hello World Body Text JSON \nA new line.")
.dump()
.collect()
};
assert_fail!(r#"
Response Failure: POST request to \"http://localhost:4000/echo\" returned
1 error(s)
1) Response headers dump:
Accept: application/json
Content-Length: 39
Content-Type: text/plain
Date:
Host: localhost:4000
Response body dump:
\"Hello World Body Text JSON \\nA new line.\"
"#, actual);
}
#[test]
fn test_dump_response_with_text_body_invalid_utf8() {
let actual = {
API::post("/echo")
.with_header(ContentType(Mime(TopLevel::Text, SubLevel::Plain, vec![])))
.with_header(Accept(vec![
qitem(Mime(TopLevel::Application, SubLevel::Json, vec![]))
]))
.with_body([0xf8, 0xa1, 0xa1, 0xa1, 0xa1].to_vec())
.dump()
.collect()
};
assert_fail!(r#"
Response Failure: POST request to \"http://localhost:4000/echo\" returned
1 error(s)
1) Response headers dump:
Accept: application/json
Content-Length: 5
Content-Type: text/plain
Date:
Host: localhost:4000
Response text body contains invalid UTF-8:
Utf8Error { valid_up_to: 0 }
"#, actual);
}
#[test]
fn test_dump_response_with_json_body() {
let actual = {
API::post("/echo")
.with_header(ContentType(Mime(TopLevel::Application, SubLevel::Json, vec![])))
.with_header(Accept(vec![
qitem(Mime(TopLevel::Application, SubLevel::Json, vec![]))
]))
.with_body(object!{
"key" => "Value",
"number" => 123,
"array" => vec![0, 1, 2, 3],
"null" => json::Null
})
.dump()
.collect()
};
assert_fail!(r#"
Response Failure: POST request to \"http://localhost:4000/echo\" returned
1 error(s)
1) Response headers dump:
Accept: application/json
Content-Length: 58
Content-Type: application/json
Date:
Host: localhost:4000
Response body dump:
{
\"array\": [
0,
1,
2,
3
],
\"key\": \"Value\",
\"null\": null,
\"number\": 123
}
"#, actual);
}
#[test]
fn test_dump_response_with_json_body_invalid() {
let actual = {
API::post("/echo")
.with_header(ContentType(Mime(TopLevel::Application, SubLevel::Json, vec![])))
.with_body("{\"key\": }")
.dump()
.collect()
};
assert_fail!(r#"
Response Failure: POST request to \"http://localhost:4000/echo\" returned
1 error(s)
1) Response headers dump:
Content-Length: 9
Content-Type: application/json
Date:
Host: localhost:4000
Response body json is invalid:
UnexpectedCharacter { ch: \'}\', line: 1, column: 9 }
"#, actual);
}