Edit(update, delete) one of headers in karate

别说谁变了你拦得住时间么 提交于 2021-02-05 07:14:25

问题


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

  • How can I edit one of the headers in this file at run-time using my scenario data? I tried setting * header 'xyz' = 'value' but it doesn't edit it.

Question #2

  • If I want to delete one of the headers, how is it possible?

回答1:


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.



来源:https://stackoverflow.com/questions/54302483/editupdate-delete-one-of-headers-in-karate

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!