A webservice returns this json
{
"title": "Test how to filter the response",
"features": [
{ "feature": "AB1", "price": "1.00"},
{ "feature": "AB1", "price": "1.23"},
{ "feature": "CD2", "price": "2.00"},
{ "feature": "EF3", "price": "3.00"},
{ "feature": "GH4", "price": "4.00"}
]
}
With intellij-http-client or in my case the jetbrains-rider-implementation of http-client the following code should
AB1
was only 1 time present (expected = 1, actual = 2)AB1
is 0.00
(expected) and not something else (actual = 1.00
and 1.23
)> {%
// this should fail since 2 AB1-features were sent
client.test("feature AB1 was sent 1 time", function() {
var features = response.body.features;
const result = features.filter(obj => obj.feature === "AB1");
result.forEach(r => client.log(JSON.stringify(r)));
client.assert(result.length, "1");
});
// should fail since the price is not 0
client.test("feature AB1 price is 0.00", function() {
var features = response.body.features;
const result = features.filter(obj => obj.feature === "AB1");
client.assert(result[0].price, "0.00");
client.log("price of " + result[0].feature + " = " + result[0].price);
});
%}
But the above code used in rider has not the result i expected
The cause was a wrong use of the http-client API: client.assert(assertion, [message])
where the assertion must be a boolean expression. The message is optional.
// must contain an expression that returns a boolean
client.assert(result.length == "1")