检查空手道中是否存在请求标头
我正在尝试创建一个模拟服务器,以处理空手道的功能测试。为此,我需要根据某些元素(例如在传入请求中的方法,路径和“授权”标头的存在)匹配某些传入请求。
我拥有的条件类似:
methodIs('get') && pathMatches('/mypath')
我需要为请求中存在“授权”标头编写条件。
根据文档,我们可以使用:
karate.get('requestHeaders.Authorization[0]') == 'foo'
但是,当我尝试使用上述内容时,它不起作用。我检查了requestheaders.authorization [0]
的存在,但这被返回为null
。我的想法是将上述内容修改为karate.get('requestheaders.authorization [0]')=='#notnull'
。
我最终尝试了headerContains('授权','')
,它似乎在起作用 - 但是我不确定这是否是检查该特定标头的存在的正确方法。还有其他(更好的)方法吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我们计划在以后的发行版中改善这一点: https://githbithub.com/karatelabs/karatelabs/karate/karate/karate/karate/karate/karate/karate/karate/karate/karate- /essugy/1962
同时,我本来想到一个简单的javaScript条件,例如
requestheaders.uthorization
将起作用。例如:不幸的是,这是对病例敏感的,这是我们计划在以后的版本中修复的方法。您可以暂时使用或条件。
有关更多说明,请参阅: https://stackoverflow.com/a/555823180/143475
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: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