With DHC by Restlet, you can easily import and test your Swagger-defined APIs, define assertions to check the results of your API calls, with advanced assertions including JSON Path or XPath, and more.
What’s interesting, to make your tests more powerful, is to chain requests together, to create full-blown test scenarios outlining the actual behavior of your APIs. For that purpose, DHC has been featuring “expressions”, so that you can say that the input of request B (a query parameter, a header, etc.) is coming from the output of request A (a status code, the content of the body of the payload, etc.). And what’s new in DHC lately? The ability to define those chained calls with a visual expression builder.
Let’s step back a little. I have an API listing cars with the make, model, and inception year. When I call /cars, I’ll get an array of JSON objects representing my list of cars. When I call /cars/234, I get a particular car, with its make, model and year.
Concretely, for my real test scenario, I’m going to call the list of cars:
I can create a couple of assertions to check the status code for that call is 200, and that the make of the first car in the list is Ford:
And then I’ll pick up the first item of that list, the Ford model T, and call this individual resource:
Again, a couple assertions to test the model this time:
But the problem with my second call is that I had to define the whole URL explicitly. What happens if the first call gives me cars in a different order, how can I check that it’s still the first item of the list that is my Ford model T? Wouldn’t it be cool if I could get the id of the first car of the list, and compose the URL of that car resource from the id I’d get from the first item of the list? That’s what expressions are for:
In the screenshot above, notice how I actually use some special notation to say that that this placeholder should be replaced with the value of the id property of the first item of the response body of the previous call!
However, you’ll notice that there’s a bit of typing involved, as you need to type the name of your test scenario, the name of the other call, that you saved, etc. It can even be error-prone if you make a typo along the way. That’s why the latest version of DHC introduced the visual expression builder, to make it trivial and straightforward to create those chained API calls with these expressions.
By clicking the little magic wand icon that appears when the focus is in a text field (pretty much anywhere in the DHC UI where you can input values), you’ll see the visual expression builder popup that will allow you to define your expression visually:
With the visual expression builder, it’s never been easier to build complex scenarios of tests to check the behavior of your APIs!