Checking for the presence of a Request header in Karate

Viewed 169

I am trying to create a mock server for handling functional tests in Karate. For that purpose I need to match certain incoming requests based on certain elements like method, path and presence of "Authorization" header in the incoming requests.

The condition I have is something like:

methodIs('get') && pathMatches('/mypath')

I need to write a condition for the presence of an "Authorization" header in the request.

As per documentation, we can use:

karate.get('requestHeaders.Authorization[0]') == 'foo'

However, when I am trying to use the above, it isn't working. I checked for the presence of requestHeaders.Authorization[0] but that is being returned as Null. My idea was to modify the above to something like karate.get('requestHeaders.Authorization[0]') == '#notnull'.

I ended up trying headerContains('Authorization',''), which seems to be working - however I am not sure if that is the right way to check for the presence of that specific header. Is there any other (better) way to do this?

1 Answers

We are planning to improve this in a future release: https://github.com/karatelabs/karate/issues/1962

Meanwhile I would have thought that a simple JavaScript condition like requestHeaders.Authorization would work. For e.g:

Scenario: requestHeaders.Authorization && methodIs('get')

Unfortunately it is case-sensitive, which is what we plan to fix in a future release. You could use an OR condition for the time being.

For more explanation, refer: https://stackoverflow.com/a/55823180/143475

Related