#[macro_use] extern crate json; #[macro_use] extern crate noir; #[macro_use] mod base_test; test!(); use json::JsonValue; // Expected Body -------------------------------------------------------------- #[test] fn test_body_expected_text() { let actual = { API::get("/get/hello") .expected_body("Hello World") .collect() }; assert_pass!(actual); } #[test] fn test_body_with_expected() { let actual = { API::post("/body/echo") .with_body("Form Data") .expected_body("Form Data") .collect() }; assert_pass!(actual); } #[test] fn test_body_text_mismatch_diff_added() { let actual = { API::get("/get/hello") .expected_body("Hello \n") .collect() }; assert_fail!(r#"
Response Failure: GET request to \"http://localhost:4000/get/hello\" returned
1 error(s)
1) Response does not match, expected: \"Hello \\n\" but got: \"
Hello World\" difference: \"Hello World \\n\" "#, actual); } #[test] fn test_body_text_mismatch_diff_removed() { let actual = { API::get("/get/hello") .expected_body("Hello World Message\n") .collect() }; assert_fail!(r#"
Response Failure: GET request to \"http://localhost:4000/get/hello\" returned
1 error(s)
1) Response does not match, expected: \"Hello World Message\\n\" but got: \"
Hello World\" difference: \"Hello World Message\\n\" "#, actual); } #[test] fn test_body_with_expected_text_invalid_utf8() { let actual = { API::post("/echo") .with_header(ContentType( Mime(TopLevel::Text, SubLevel::Plain, vec![]) )) .with_body([0xf8, 0xa1, 0xa1, 0xa1, 0xa1].to_vec()) .expected_body("") .collect() }; assert_fail!(r#"
Response Failure: POST request to \"http://localhost:4000/echo\" returned
1 error(s)
1) Response text body contains invalid UTF-8:
Utf8Error { valid_up_to: 0 } "#, actual); } #[test] fn test_body_with_expected_text_expected_invalid_utf8() { let actual = { API::post("/echo") .with_header(ContentType( Mime(TopLevel::Text, SubLevel::Plain, vec![]) )) .with_body("") .expected_body([0xf8, 0xa1, 0xa1, 0xa1, 0xa1].to_vec()) .collect() }; assert_fail!(r#"
Response Failure: POST request to \"http://localhost:4000/echo\" returned
1 error(s)
1) Response body, expected text provided by test contains invalid UTF-8:
Utf8Error { valid_up_to: 0 } "#, actual); } #[test] fn test_body_expected_raw() { let actual = { API::post("/echo") .with_body([ 0x00, 0xa0, 0xff, 0x80, 0x45, 0x13, 0x21, 0x78, 0x67, 0x08, 0x90, 0xca, 0xd4, 0xe5, 0xf4, 0x89 ].to_vec()) .expected_body([ 0x00, 0xa0, 0xff, 0x80, 0x45, 0x13, 0x21, 0x78, 0x67, 0x08, 0x90, 0xca, 0xd4, 0xe5, 0xf4, 0x89 ].to_vec()) .collect() }; assert_pass!(actual); } #[test] fn test_body_expected_raw_mismatch() { let actual = { API::post("/echo") .with_body([ 0x00, 0xa0, 0xff, 0x80, 0x45, 0x13, 0x21, 0x78, 0x67, 0x08, 0x90, 0xca, 0xd4, 0xe5, 0xf4, 0x89 ].to_vec()) .expected_body([ 0x67, 0x08, 0x90, 0xca, 0xd4, 0xe5, 0xf4, 0x89, 0x00, 0xa0, 0xff, 0x80, 0x45, 0x13, 0x21, 0x78 ].to_vec()) .collect() }; assert_fail!(r#"
Response Failure: POST request to \"http://localhost:4000/echo\" returned
1 error(s)
1) Response raw body data does not match, expected the following 16 bytes: [0x67, 0x08, 0x90, 0xCA, 0xD4, 0xE5, 0xF4, 0x89, 0x00, 0xA0, 0xFF, 0x80, 0x45, 0x13, 0x21, 0x78] but got the following
16 bytes instead: [
0x00,
0xA0,
0xFF,
0x80,
0x45,
0x13,
0x21,
0x78,
0x67,
0x08,
0x90,
0xCA,
0xD4,
0xE5,
0xF4,
0x89] "#, actual); } #[test] fn test_body_with_expected_json() { let actual = { API::post("/echo") .with_body(object! { "key" => "value", "list" => vec![2, 3, 4], "some" => object! { "very" => object! { "deeply" => object! { "nested" => object! { "array" => array![true, false] } } } }, "missing" => JsonValue::Null }) .expected_body(object! { "key" => "value", "list" => vec![2, 3, 4], "some" => object! { "very" => object! { "deeply" => object! { "nested" => object! { "array" => array![true, false] } } } }, "missing" => JsonValue::Null }) .collect() }; assert_pass!(actual); } #[test] fn test_body_with_expected_json_mismatch() { let actual = { API::post("/echo") .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 }) .expected_body(object! { "key" => "value", "list" => vec![2, 3, 4], "some" => object! { "very" => object! { "deeply" => object! { "nested" => object! { "array" => array![true, false] } } } }, "missing" => JsonValue::Null }) .collect() }; assert_fail!(r#"
Response Failure: POST request to \"http://localhost:4000/echo\" returned
1 error(s)
1) Response body json does not match, expected: - json.key: String does not match, expected: \"different value\" but got: \"
value\" difference: \"different value\" - json.list: Array with
2 item(s) does not match expected length of 3 - json.some.very.deeply.nested.array: Array with
3 item(s) does not match expected length of 2 - json.some.very.deeply.nested.array[0]: Boolean (
false) does not match expected value (true) - json.some.very.deeply.nested.array[1]: Boolean (
true) does not match expected value (false) - json: Object is missing
1 key(s) (
missing) "#, actual); } #[test] fn test_body_with_expected_json_mismatch_exact() { let actual = { API::post("/echo") .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 }) .expected_exact_body(object! { "key" => "value", "list" => vec![2, 3, 4], "some" => object! { "very" => object! { "deeply" => object! { "nested" => object! { "array" => array![true, false] } } } }, "missing" => JsonValue::Null }) .collect() }; assert_fail!(r#"
Response Failure: POST request to \"http://localhost:4000/echo\" returned
1 error(s)
1) Response body json does not match, expected: - json.key: String does not match, expected: \"different value\" but got: \"
value\" difference: \"different value\" - json.list: Array with
2 item(s) does not match expected length of 3 - json.some.very.deeply.nested.array: Array with
3 item(s) does not match expected length of 2 - json.some.very.deeply.nested.array[0]: Boolean (
false) does not match expected value (true) - json.some.very.deeply.nested.array[1]: Boolean (
true) does not match expected value (false) - json: Object is missing
1 key(s) (
missing) - json: Object has
1 additional unexpected key(s) (
additional) "#, actual); } #[test] fn test_body_with_expected_json_invalid() { let actual = { API::post("/echo") .with_header(ContentType( Mime(TopLevel::Application, SubLevel::Json, vec![]) )) .with_body("{\"foo\": }") .expected_body(object! { "key" => "value" }) .collect() }; assert_fail!(r#"
Response Failure: POST request to \"http://localhost:4000/echo\" returned
1 error(s)
1) Response body json is invalid:
UnexpectedCharacter { ch: \'}\', line: 1, column: 9 } "#, actual); } #[test] fn test_body_with_expected_json_invalid_utf8() { let actual = { API::post("/echo") .with_header(ContentType( Mime(TopLevel::Application, SubLevel::Json, vec![]) )) .with_body([0xf8, 0xa1, 0xa1, 0xa1, 0xa1].to_vec()) .expected_body(object! { "key" => "value" }) .collect() }; assert_fail!(r#"
Response Failure: POST request to \"http://localhost:4000/echo\" returned
1 error(s)
1) Response body json contains invalid UTF-8:
Utf8Error { valid_up_to: 0 } "#, actual); } #[test] fn test_body_with_expected_json_expected_invalid() { let actual = { API::post("/echo") .with_header(ContentType( Mime(TopLevel::Application, SubLevel::Json, vec![]) )) .with_body(object! { "key" => "value" }) .expected_body("{\"foo\": }") .collect() }; assert_fail!(r#"
Response Failure: POST request to \"http://localhost:4000/echo\" returned
1 error(s)
1) Response body, expected body json provided by test is invalid:
UnexpectedCharacter { ch: \'}\', line: 1, column: 9 } "#, actual); } #[test] fn test_body_with_expected_json_expected_invalid_utf8() { let actual = { API::post("/echo") .with_header(ContentType( Mime(TopLevel::Application, SubLevel::Json, vec![]) )) .with_body(object! { "key" => "value" }) .expected_body([0xf8, 0xa1, 0xa1, 0xa1, 0xa1].to_vec()) .collect() }; assert_fail!(r#"
Response Failure: POST request to \"http://localhost:4000/echo\" returned
1 error(s)
1) Response body, expected body json provided by test contains invalid UTF-8:
Utf8Error { valid_up_to: 0 } "#, actual); } #[test] fn test_body_with_expected_json_compare_depth_one() { use noir::Options; let actual = { API::post("/echo") .with_options(Options { // This will only enter the first level after the top level json_compare_depth: 1, .. Default::default() }) .with_body(object! { "key" => "value", "deep" => object! { "compare" => "foo" } }) .expected_body(object! { "key" => "otherValue", "deep" => object! { "compare" => "bar" } }) .collect() }; assert_fail!(r#"
Response Failure: POST request to \"http://localhost:4000/echo\" returned
1 error(s)
1) Response body json does not match, expected: - json.key: String does not match, expected: \"value\" but got: \"
otherValue\" difference: \"value otherValue\" "#, actual); } #[test] fn test_body_with_expected_json_compare_depth_zero() { use noir::Options; let actual = { API::post("/echo") .with_options(Options { // This will perform no comparisons at all json_compare_depth: 0, .. Default::default() }) .with_body(object! { "key" => "value", "deep" => object! { "compare" => "foo" } }) .expected_body(object! { "key" => "otherValue", "deep" => object! { "compare" => "bar" } }) .collect() }; assert_pass!(actual); } // Set Headers from Body ------------------------------------------------------ #[test] fn test_body_set_header_from_body_raw() { let actual = { API::post("/echo") .with_body(vec![1, 2, 3, 4, 5]) .expected_body(vec![1, 2, 3, 4, 5]) .expected_header(ContentType( Mime(TopLevel::Application, SubLevel::OctetStream, vec![]) )) .collect() }; assert_pass!(actual); } #[test] fn test_body_set_header_from_body_text() { let actual = { API::post("/echo") .with_body("Hello World") .expected_body("Hello World") .expected_header(ContentType( Mime(TopLevel::Text, SubLevel::Plain, vec![]) )) .collect() }; assert_pass!(actual); } #[test] fn test_body_set_header_from_body_json() { let actual = { API::post("/echo") .with_body(object! { "key" => "value" }) .expected_body(object! { "key" => "value" }) .expected_header(ContentType( Mime(TopLevel::Application, SubLevel::Json, vec![]) )) .collect() }; assert_pass!(actual); } #[test] fn test_body_override_header_from_body() { let actual = { API::post("/echo") .with_header(ContentType( Mime(TopLevel::Text, SubLevel::Plain, vec![]) )) .with_body(object! { "key" => "value" }) .expected_body("{\"key\":\"value\"}") .expected_header(ContentType( Mime(TopLevel::Text, SubLevel::Plain, vec![]) )) .collect() }; assert_pass!(actual); }