#[macro_use] extern crate json;
#[macro_use] extern crate noir;
#[macro_use]
mod base_test;
test!();
// Dump request bodies --------------------------------------------------------
#[test]
fn test_provided_response_dump_text() {
let actual = {
API::post("/response/forward")
.with_header(ContentType(Mime(TopLevel::Text, SubLevel::Plain, vec![])))
.provide(responses![
EXAMPLE.post("/forward").dump()
])
.with_body("Response Body")
.collect()
};
assert_fail!(r#"
Response Failure: POST request to \"http://localhost:4000/response/forward\" returned
1 error(s)
1)
Request Failure: POST response provided for \"https://example.com/forward\" returned
1 error(s)
1.1) Request headers dump:
Content-Length: 13
Content-Type: text/plain
Host: example.com
Request body dump:
\"Response Body\"
"#, actual);
}
#[test]
fn test_provided_response_dump_invalid_utf8() {
let actual = {
API::post("/response/forward")
.with_header(ContentType(Mime(TopLevel::Text, SubLevel::Plain, vec![])))
.provide(responses![
EXAMPLE.post("/forward").dump()
])
.with_body([0xf8, 0xa1, 0xa1, 0xa1, 0xa1].to_vec())
.collect()
};
assert_fail!(r#"
Response Failure: POST request to \"http://localhost:4000/response/forward\" returned
1 error(s)
1)
Request Failure: POST response provided for \"https://example.com/forward\" returned
1 error(s)
1.1) Request headers dump:
Content-Length: 5
Content-Type: text/plain
Host: example.com
Request text body contains invalid UTF-8:
Utf8Error { valid_up_to: 0 }
"#, actual);
}
#[test]
fn test_provided_response_dump_raw() {
let actual = {
API::post("/response/forward")
.provide(responses![
EXAMPLE.post("/forward").dump()
])
.with_body(vec![
0x00, 0xa0, 0xff, 0x80, 0x45, 0x13, 0x21, 0x78,
0x67, 0x08, 0x90, 0xca, 0xd4, 0xe5, 0xf4, 0x89
])
.collect()
};
assert_fail!(r#"
Response Failure: POST request to \"http://localhost:4000/response/forward\" returned
1 error(s)
1)
Request Failure: POST response provided for \"https://example.com/forward\" returned
1 error(s)
1.1) Request headers dump:
Content-Length: 16
Content-Type: application/octet-stream
Host: example.com
Request raw body dump of 16 bytes:
[0x00, 0xA0, 0xFF, 0x80, 0x45, 0x13, 0x21, 0x78, 0x67, 0x08, 0x90, 0xCA, 0xD4, 0xE5, 0xF4, 0x89]
"#, actual);
}
#[test]
fn test_provided_response_dump_json() {
let actual = {
API::post("/response/forward")
.with_header(ContentType(Mime(TopLevel::Application, SubLevel::Json, vec![])))
.provide(responses![
EXAMPLE.post("/forward").dump()
])
.with_body(object! {
"key" => "different value",
"list" => vec![2, 3],
"some" => object! {
"very" => object! {
"deeply" => object! {
"nested" => object! {
"array" => array![false, true, false]
}
}
}
},
"additional" => 32
})
.collect()
};
assert_fail!(r#"
Response Failure: POST request to \"http://localhost:4000/response/forward\" returned
1 error(s)
1)
Request Failure: POST response provided for \"https://example.com/forward\" returned
1 error(s)
1.1) Request headers dump:
Content-Length: 121
Content-Type: application/json
Host: example.com
Request body dump:
{
\"additional\": 32,
\"key\": \"different value\",
\"list\": [
2,
3
],
\"some\": {
\"very\": {
\"deeply\": {
\"nested\": {
\"array\": [
false,
true,
false
]
}
}
}
}
}
"#, actual);
}
#[test]
fn test_provided_response_dump_json_invalid() {
let actual = {
API::post("/response/forward")
.with_header(ContentType(Mime(TopLevel::Application, SubLevel::Json, vec![])))
.provide(responses![
EXAMPLE.post("/forward").dump()
])
.with_body("{\"foo\": }")
.collect()
};
assert_fail!(r#"
Response Failure: POST request to \"http://localhost:4000/response/forward\" returned
1 error(s)
1)
Request Failure: POST response provided for \"https://example.com/forward\" returned
1 error(s)
1.1) Request headers dump:
Content-Length: 9
Content-Type: application/json
Host: example.com
Request body json is invalid:
UnexpectedCharacter { ch: \'}\', line: 1, column: 9 }
"#, actual);
}
#[test]
fn test_provided_response_dump_json_invalid_utf8() {
let actual = {
API::post("/response/forward")
.with_header(ContentType(Mime(TopLevel::Application, SubLevel::Json, vec![])))
.provide(responses![
EXAMPLE.post("/forward").dump()
])
.with_body([0xf8, 0xa1, 0xa1, 0xa1, 0xa1].to_vec())
.collect()
};
assert_fail!(r#"
Response Failure: POST request to \"http://localhost:4000/response/forward\" returned
1 error(s)
1)
Request Failure: POST response provided for \"https://example.com/forward\" returned
1 error(s)
1.1) Request headers dump:
Content-Length: 5
Content-Type: application/json
Host: example.com
Request body json contains invalid UTF-8:
Utf8Error { valid_up_to: 0 }
"#, actual);
}
#[test]
fn test_provided_response_dump_form() {
use std::fs::File;
let actual = {
API::post("/response/forward")
.provide(responses![
EXAMPLE.post("/forward").dump()
])
.with_body(form! {
"field" => "someValue\n",
"array[]" => vec!["1", "2", "3", "4", "5\n"],
"vec_file" => (
"file.bin",
Mime(TopLevel::Application, SubLevel::OctetStream, vec![]),
vec![1, 2, 3, 4, 5, 6, 7, 8]
),
"str_file" => (
"readme.md",
Mime(TopLevel::Text, SubLevel::Plain, vec![]),
"Hello World"
),
"fs_file" => (
"form_test.md",
Mime(TopLevel::Text, SubLevel::Plain, vec![]),
File::open("./tests/form_test.md").unwrap()
)
})
.collect()
};
assert_fail!(r#"
Response Failure: POST request to \"http://localhost:4000/response/forward\" returned
1 error(s)
1)
Request Failure: POST response provided for \"https://example.com/forward\" returned
1 error(s)
1.1) Request headers dump:
Content-Length: 857
Content-Type: application/form-data; boundary=boundary12345
Host: example.com
Request form dump with 5 fields:
1) Field \"field\" dump:
\"someValue\\n\"
2) Array \"array[]\" (5 items) dump:
\"1\", \"2\", \"3\", \"4\", \"5\\n\"
3) File \"vec_file\" (\"file.bin\", application/octet-stream) raw body dump of 8 bytes:
[0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08]
4) File \"str_file\" (\"readme.md\", text/plain) body dump:
\"Hello World\"
5) File \"fs_file\" (\"form_test.md\", text/plain) body dump:
\"Form Test Data File\\n\"
"#, actual);
}