Exercise - how many ways to count the values in a json array returned from a REST API call?


  • Moderador

    TLDR: When we use multiple tools and existing tool features, we open up new options in how we approach our testing. This can help us identify workarounds when we identify testability feature requests, and might even remove the need for the testability feature.

    I set myself a Practice Test Exercise. You might want to try it yourself before reading the full text of this post.

    <a name=“more”></a>

    Exercise Described

    • How many ways do you have to count the values in a json array returned from a REST API call?
      • find a REST API that returns a JSON array object e.g.
        • {"todos":[{...},{...},{etc.}]}
      • how can you identify how many “todos” are in the list

    To take part you’ll need to:

    I used Thingifier, created a bunch of ‘todos’ and then used GET /todos to return an array of ‘todos’

    I eventually came up with six different ways to count the number in the array before I stopped.

    I learned some new stuff along the way, and remembered some old stuff. I found this a useful exercise.

    If you identify other ways to count the JSON than I did, then I’d be interested in learning so send me a message or leave a comment somewhere.

    Roots of the Exercise

    I’ve been working on Thingifier which at the moment exposes a REST API for a simple Todo Manager

    Elisabeth Hocke and Thomas Rinke bravely picked the app to perform some testing on, and created a useful write up.

    One note in the write up mentioned that it could be useful if the responses described how many items were returned.

    e.g. GET /todos returns a list of todos, but if there are a lot of them then it can be hard to know how many are there.

    Elisabeth and Thomas were using Postman to test the app, and Postman does not show how many items are returned.

    I wrote some comments to Elisabeth and Thomas, and at the time the only ‘workaround’ I could think of was to use Insomnia, because that can show a filter count in the responses.

    I suggested this because:

    • feature requests for testing can be useful
    • they can take time to build
    • we often need workarounds

    But my only option made me realise that I didn’t have enough workarounds available in my mind to handle this situation.

    I used this limitation as a Personal Test Improvement Exercise.

    Here’s the exercise I set myself:

    How many ways to count the values in a json array returned from a REST API call?

    I found six ways before I stopped.

    • Using the Json filter in Insomnia Response view
    • Using the pretty print view in Postman console
    • Script in Tests in a request in Postman
    • Paste the JSON into a browser dev console
    • Paste it into an online JSON viewer
    • Send Requests/Response through a Proxy and use the Proxy viewer

    I hadn’t used the Postman scripting before so I learned a new set of knowledge when I tried that out. I’ll write up a fuller set of learnings on Postman scripting in a later post.

    Using the Json filter in Insomnia Response view todos.length

    We don’t have to stick to one tool. Insomnia is a REST Client that I often use for Exploratory testing.

    It has a handy filter facility in the Response, which I can use to show the number of todos returned.

    Using the Json filter in Insomnia Response view todos.length

    Using the pretty print view in Postman console

    In Postman show the console with “View \ Show Postman Console”

    Then use the pretty print view to see how many items are in the JSON array.

    Starts at 0 so when it shows 201 at the bottom it means there are 202 todos in the returned array.

    Script in Tests in a request in Postman

    I can write any arbitrary code I want in the Postman ‘Tests’ or ‘Pre-request Script’ and I can use that to support my testing, not just assert on values.

    e.g. the code below parses the response and logs to the Postman Dev console the number of items returned in the ‘todos’ array in the response.

    var response = JSON.parse(pm.response.text());console.log("length : " + response.todos.length);
    

    There are many ways to hack this in Postman: in the request, at a folder level, at a collection level, or possibly re-using a script created by a collection. But basically - view the ‘Tests’ functionality as ‘Code to run after request is completed’ functionality.

    Paste the JSON into a browser dev console

    Paste directly and you might see the result output:

    > {todos: Array(202)}

    or paste it into a variable e.g.

    var result = {...pasted JSON here...}> result.todos.length<- 202
    

    Paste it into an online JSON viewer

    e.g. http://jsonviewer.stack.hu/

    Send Requests/Response through a Proxy and use the Proxy viewer

    I configured Insomnia to route through Charles proxy and used the Charles JSON view to show the number of items in the array. Other proxies have similar functionality.

    End Notes

    This was triggered by the very useful work that Elisabeth Hocke and Thomas Rinke describe in their write up.

    I wanted to expand the options available to me when testing.

    If you are interested in API testing then I have a book you might like to look at Automating and Testing a REST API.