检查空手道中是否存在请求标头

发布于 2025-01-29 00:22:07 字数 598 浏览 1 评论 0原文

我正在尝试创建一个模拟服务器,以处理空手道的功能测试。为此,我需要根据某些元素(例如在传入请求中的方法,路径和“授权”标头的存在)匹配某些传入请求。

我拥有的条件类似:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

二智少女 2025-02-05 00:22:07

我们计划在以后的发行版中改善这一点: https://githbithub.com/karatelabs/karatelabs/karate/karate/karate/karate/karate/karate/karate/karate/karate/karate- /essugy/1962

同时,我本来想到一个简单的javaScript条件,例如requestheaders.uthorization将起作用。例如:

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

不幸的是,这是对病例敏感的,这是我们计划在以后的版本中修复的方法。您可以暂时使用或条件。

有关更多说明,请参阅: 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:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文