diff --git a/integration/tests/assert_header.output.json b/integration/tests/assert_header.output.json new file mode 100644 index 00000000000..1b67e7354e0 --- /dev/null +++ b/integration/tests/assert_header.output.json @@ -0,0 +1,71 @@ +[ + { + "filename": "tests/assert_header.hurl", + "entries": [ + { + "request": { + "method": "GET", + "url": "http://localhost:8000/assert-header" + }, + "response": { + "httpVersion": "HTTP/1.0", + "status": 200 + }, + "asserts": [ + { + "line": 2, + "success": true + }, + { + "line": 2, + "success": true + }, + { + "line": 3, + "success": true + }, + { + "line": 4, + "success": true + }, + { + "line": 5, + "success": true + }, + { + "line": 7, + "success": true + }, + { + "line": 8, + "success": true + }, + { + "line": 9, + "success": true + }, + { + "line": 10, + "success": true + }, + { + "line": 11, + "success": true + }, + { + "line": 12, + "success": true + }, + { + "line": 13, + "success": true + }, + { + "line": 14, + "success": true + } + ] + } + ] + } +] \ No newline at end of file diff --git a/integration/tests/error_assert_match_utf8.output.json b/integration/tests/error_assert_match_utf8.output.json new file mode 100644 index 00000000000..97e8f039fc9 --- /dev/null +++ b/integration/tests/error_assert_match_utf8.output.json @@ -0,0 +1,32 @@ +[ + { + "filename": "tests/error_assert_match_utf8.hurl", + "entries": [ + { + "request": { + "method": "GET", + "url": "http://localhost:8000/error-assert/match-utf8" + }, + "response": { + "httpVersion": "HTTP/1.0", + "status": 200 + }, + "asserts": [ + { + "success": true, + "line": 2 + }, + { + "success": true, + "line": 2 + }, + { + "success": false, + "message": "Invalid Decoding\n --> tests/error_assert_match_utf8.hurl:4:1\n |\n 4 | body matches \".*\"\n | ^^^^ The body can not be decoded with charset 'utf-8'\n |", + "line": 4 + } + ] + } + ] + } +] diff --git a/integration/tests/error_assert_status.output.json b/integration/tests/error_assert_status.output.json new file mode 100644 index 00000000000..1a817de882b --- /dev/null +++ b/integration/tests/error_assert_status.output.json @@ -0,0 +1,28 @@ +[ + { + "filename": "tests/error_assert_status.hurl", + "entries": [ + { + "request": { + "method": "GET", + "url": "http://localhost:8000/not_found" + }, + "response": { + "httpVersion": "HTTP/1.0", + "status": 404 + }, + "asserts": [ + { + "success": true, + "line": 2 + }, + { + "success": false, + "message": "Assert Status\n --> tests/error_assert_status.hurl:2:10\n |\n 2 | HTTP/1.0 200\n | ^^^ actual value is <404>\n |", + "line": 2 + } + ] + } + ] + } +] diff --git a/integration/tests/error_assert_template_variable_not_found.output.json b/integration/tests/error_assert_template_variable_not_found.output.json new file mode 100644 index 00000000000..8cc145baeb3 --- /dev/null +++ b/integration/tests/error_assert_template_variable_not_found.output.json @@ -0,0 +1,32 @@ +[ + { + "filename": "tests/error_assert_template_variable_not_found.hurl", + "entries": [ + { + "request": { + "method": "GET", + "url": "http://localhost:8000/error-assert-template-variable-not-found" + }, + "response": { + "httpVersion": "HTTP/1.0", + "status": 200 + }, + "asserts": [ + { + "success": true, + "line": 2 + }, + { + "success": true, + "line": 2 + }, + { + "success": false, + "message": "Undefined Variable\n --> tests/error_assert_template_variable_not_found.hurl:4:33\n |\n 4 | header \"content-type\" equals \"{{content_type}}\"\n | ^^^^^^^^^^^^ You must set the variable content_type\n |", + "line": 4 + } + ] + } + ] + } +] diff --git a/integration/tests/error_assert_value_error.output.json b/integration/tests/error_assert_value_error.output.json new file mode 100644 index 00000000000..94d5f01c289 --- /dev/null +++ b/integration/tests/error_assert_value_error.output.json @@ -0,0 +1,67 @@ +[ + { + "filename": "tests/error_assert_value_error.hurl", + "entries": [ + { + "request": { + "method": "GET", + "url": "http://localhost:8000/error-assert-value" + }, + "response": { + "httpVersion": "HTTP/1.0", + "status": 200 + }, + "asserts": [ + { + "success": true, + "line": 2 + }, + { + "success": true, + "line": 2 + }, + { + "success": false, + "message": "Assert Failure\n --> tests/error_assert_value_error.hurl:4:0\n |\n 4 | header \"content-type\" equals \"XXX\"\n | actual: string \n | expected: string \n |", + "line": 4 + }, + { + "success": false, + "message": "Assert Failure\n --> tests/error_assert_value_error.hurl:5:0\n |\n 5 | header \"content-type\" notEquals \"text/html; charset=utf-8\"\n | actual: string \n | expected: string \n |", + "line": 5 + }, + { + "success": false, + "message": "Assert Failure\n --> tests/error_assert_value_error.hurl:6:0\n |\n 6 | jsonpath \"$.id\" equals \"000001\"\n | actual: none\n | expected: string <000001>\n |", + "line": 6 + }, + { + "success": false, + "message": "Assert Failure\n --> tests/error_assert_value_error.hurl:7:0\n |\n 7 | jsonpath \"$.values\" includes 100\n | actual: [int <1>, int <2>, int <3>]\n | expected: includes int <100>\n |", + "line": 7 + }, + { + "success": false, + "message": "Assert Failure\n --> tests/error_assert_value_error.hurl:8:0\n |\n 8 | jsonpath \"$.values\" not contains \"Hello\"\n | actual: [int <1>, int <2>, int <3>]\n | expected: not contains string \n | >>> types between actual and expected are not consistent\n |", + "line": 8 + }, + { + "success": false, + "message": "Assert Failure\n --> tests/error_assert_value_error.hurl:9:0\n |\n 9 | jsonpath \"$.count\" greaterThan 5\n | actual: int <2>\n | expected: greater than int <5>\n |", + "line": 9 + }, + { + "success": false, + "message": "Assert Failure\n --> tests/error_assert_value_error.hurl:10:0\n |\n10 | jsonpath \"$.count\" isFloat\n | actual: int <2>\n | expected: float\n |", + "line": 10 + }, + { + "success": false, + "message": "Assert Failure\n --> tests/error_assert_value_error.hurl:11:0\n |\n11 | bytes contains hex,00;\n | actual: byte array <7b202276616c756573223a205b312c322c335d2c2022636f756e74223a20327d>\n | expected: contains byte array <00>\n |", + "line": 11 + } + ] + } + ] + } +] diff --git a/integration/tests/error_query_header_not_found.output.json b/integration/tests/error_query_header_not_found.output.json new file mode 100644 index 00000000000..ad0ba9ee604 --- /dev/null +++ b/integration/tests/error_query_header_not_found.output.json @@ -0,0 +1,32 @@ +[ + { + "filename": "tests/error_query_header_not_found.hurl", + "entries": [ + { + "request": { + "method": "GET", + "url": "http://localhost:8000/error-query-header-not-found" + }, + "response": { + "httpVersion": "HTTP/1.0", + "status": 200 + }, + "asserts": [ + { + "success": true, + "line": 2 + }, + { + "success": true, + "line": 2 + }, + { + "success": false, + "message": "Header not Found\n --> tests/error_query_header_not_found.hurl:3:1\n |\n 3 | Custom: XXX\n | ^^^^^^ This header has not been found in the response\n |", + "line": 3 + } + ] + } + ] + } +] diff --git a/integration/tests/hello.output.json b/integration/tests/hello.output.json index fc2d532376a..dbee9543d69 100644 --- a/integration/tests/hello.output.json +++ b/integration/tests/hello.output.json @@ -13,11 +13,17 @@ }, "asserts": [ { - "actual": "1.0", - "expected": "1.0" + "success": true, + "line": 2 }, - {}, - {} + { + "success": true, + "line": 2 + }, + { + "success": true, + "line": 3 + } ] }, { diff --git a/packages/hurl/src/json/result.rs b/packages/hurl/src/json/result.rs index 95da7f5b3ca..3160d3a68fc 100644 --- a/packages/hurl/src/json/result.rs +++ b/packages/hurl/src/json/result.rs @@ -247,21 +247,21 @@ impl CaptureResult { } impl AssertResult { - fn to_json(&self, _lines: &[String], _filename: String) -> serde_json::Value { + fn to_json(&self, lines: &[String], filename: String) -> serde_json::Value { let mut map = serde_json::Map::new(); - if let AssertResult::Version { - actual, expected, .. - } = self - { - map.insert( - "actual".to_string(), - serde_json::Value::String(actual.clone()), - ); - map.insert( - "expected".to_string(), - serde_json::Value::String(expected.clone()), - ); - }; + + let success = self.clone().error().is_none(); + map.insert("success".to_string(), serde_json::Value::Bool(success)); + + if let Some(err) = self.clone().error() { + let message = crate::cli::error_string(lines, filename, &err); + map.insert("message".to_string(), serde_json::Value::String(message)); + } + map.insert( + "line".to_string(), + serde_json::Value::Number(serde_json::Number::from(self.line())), + ); + serde_json::Value::Object(map) } } diff --git a/packages/hurl/src/runner/assert.rs b/packages/hurl/src/runner/assert.rs index fb3c6218fd9..224a6fd6468 100644 --- a/packages/hurl/src/runner/assert.rs +++ b/packages/hurl/src/runner/assert.rs @@ -126,6 +126,15 @@ impl AssertResult { _ => None, } } + pub fn line(&self) -> usize { + match self { + AssertResult::Version { source_info, .. } => source_info.start.line, + AssertResult::Status { source_info, .. } => source_info.start.line, + AssertResult::Header { source_info, .. } => source_info.start.line, + AssertResult::Body { source_info, .. } => source_info.start.line, + AssertResult::Explicit { source_info, .. } => source_info.start.line, + } + } } pub fn eval_assert(