When should you check the number of items in a response? When shouldn't you?

  • 17 March 2021
  • 0 replies
  • 3 views

Userlevel 1
Badge +2

I’m curious in what situation you test the size of responses coming back from APIs, specifically in terms of collections.

 

It’s pretty easy to assert length in API Fortress, but I’m going back and forth on whether it’s always worthwhile.  I’ve come up with a couple of scenarios

 

I would always check length if:

An API lets you specify the number of responses

For instance, if I can get the first {n} invoices I might make sure exactly {n} are returned.  Or, if I can limit the responses to no more then {x} I might check the length is equal to or less than {x}.

 

An API has multiple, anonymous objects that must be returned

If I’m working with an API which only identifies what data is what by position, I might want to count the number of entries.

For instance, say I have a beehive app, and posting to addHive adds a number of hives to my collection.  As a response, it returns the new hive’s ID, the number of existing hives, and an estimate of honey production in grams.  If this API returns these as key:value pairs, great!  But there are plenty of not-great API implementations that might return them as arrays, instead:
 

// Great
{hiveID: '1234sigma', existingHives: '46', honeyEstimate: 458245}

//Bad
{[ '1234sigma', '46', '458245' ]}

I consider that kind of response an API smell, but if I couldn’t change it, I’d assert the response had a size of 3.

 

I’m unlikely to check length if:

There can be multiple objects but it’s optional

If I’m checking (say) a search result that can have multiple responses, I’m probably going to check the first object only.  If a second object exists, I might check that it’s well formed, but after that, I’m happy to assume the rest of the response is fine.

 

There’s only one result expected; It’s unlikely two will ever be returned, and an absent result is an error

In this case, I’m probably just going to operate on the first result in my response.  Attempting to get that result will fail if it’s missing, erroring my test without me specifically having to do so.

 

 

What about the rest of y’all?  Do you always check result length in some form, or do you follow some rules to decide if it’s required?


0 replies

Be the first to reply!

Reply