Skip to content

Commit

Permalink
Add asserts in JSON output
Browse files Browse the repository at this point in the history
  • Loading branch information
fabricereix committed Oct 16, 2021
1 parent 85bb81b commit 8be45d5
Show file tree
Hide file tree
Showing 9 changed files with 295 additions and 18 deletions.
71 changes: 71 additions & 0 deletions integration/tests/assert_header.output.json
Original file line number Diff line number Diff line change
@@ -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
}
]
}
]
}
]
32 changes: 32 additions & 0 deletions integration/tests/error_assert_match_utf8.output.json
Original file line number Diff line number Diff line change
@@ -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
}
]
}
]
}
]
28 changes: 28 additions & 0 deletions integration/tests/error_assert_status.output.json
Original file line number Diff line number Diff line change
@@ -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
}
]
}
]
}
]
Original file line number Diff line number Diff line change
@@ -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
}
]
}
]
}
]
67 changes: 67 additions & 0 deletions integration/tests/error_assert_value_error.output.json
Original file line number Diff line number Diff line change
@@ -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 <text/html; charset=utf-8>\n | expected: string <XXX>\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 <text/html; charset=utf-8>\n | expected: string <text/html; charset=utf-8>\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 <Hello>\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
}
]
}
]
}
]
32 changes: 32 additions & 0 deletions integration/tests/error_query_header_not_found.output.json
Original file line number Diff line number Diff line change
@@ -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
}
]
}
]
}
]
14 changes: 10 additions & 4 deletions integration/tests/hello.output.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,17 @@
},
"asserts": [
{
"actual": "1.0",
"expected": "1.0"
"success": true,
"line": 2
},
{},
{}
{
"success": true,
"line": 2
},
{
"success": true,
"line": 3
}
]
},
{
Expand Down
28 changes: 14 additions & 14 deletions packages/hurl/src/json/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
9 changes: 9 additions & 0 deletions packages/hurl/src/runner/assert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 8be45d5

Please sign in to comment.