1

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?

Aditya K
  • 487
  • 3
  • 11

1 Answers1

1

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

Peter Thomas
  • 54,465
  • 21
  • 84
  • 248
  • 1
    Thanks @Peter for your reply. Unfortunately the `requestHeaders.authorization` isn't working. In my headers, I notice the lower case header though I have tried both. However I did a pretty print of the `requestHeaders` and I do see the `authorization` header there, which is an array. However, I tried `requestHeaders.Authorization != undefined` and that seems to be working ok. – Aditya K May 16 '22 at 01:53
  • 1
    (I meant `requestHeaders.authorization != undefined`) – Aditya K May 16 '22 at 02:02
  • @AdityaK okay, thanks for the details. I'll try to improve this for the future. p.s: how are things otherwise Mr. Songwala ;) – Peter Thomas May 16 '22 at 02:30
  • 1
    Oh my God. It took me a while to get it but I get it now. I will message you separately, if that's ok! – Aditya K May 16 '22 at 04:05
  • @AdityaK sure, that's ok :) – Peter Thomas May 16 '22 at 04:30