Avicky
Avicky

Reputation: 101

Edit(update, delete) one of headers in karate

I have this header which is in a .js file and I am reading this header using

* configure headers = read('classpath:services/Headers/distheader.js')

Question #1

Question #2

Upvotes: 4

Views: 3736

Answers (1)

Peter Thomas
Peter Thomas

Reputation: 58058

So 90% of your scenarios are "happy path", and you have a headers JS configured.

Now you need some scenarios to have more (or less headers). One option is to hard-code the headers for those scenarios, note that you can do * configure headers = null to disable the "auto" headers. Then use the header (or headers) keyword and build headers manually.

Another option is that you can invoke the headers function and get a JSON - and then mutate (add / remove keys) before setting headers manually. For example:

# you can disable headers if needed
* configure headers = null

# headers.feature is: function(){ return { a: 1, b: 2 } }
* def fun = read('headers.feature')
* def temp = fun()
* remove temp.a
* set temp.c = 3

Given url 'https://httpbin.org'
And path 'anything'
And headers temp
When method get
Then status 200

So the advantage above is in case your headers routine is complex, you can re-use it - but still have fine-grained control.

For more ideas, see: https://stackoverflow.com/a/76672675/143475

Upvotes: 3

Related Questions