空手道模拟 - 如何匹配请求正文内容 [英] Karate mock - how to match against request body content

查看:40
本文介绍了空手道模拟 - 如何匹配请求正文内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于空手道,我正在尝试研究如何根据请求正文的内容返回不同的模拟响应.

With Karate, I'm trying to work out how to return different mock responses based on the content of the request body.

我有

Feature: ...
Scenario: pathMatches('/users/login') && methodIs('post') && request == {"username": "gooduser", "password": "goodpassword"}
  * def responseStatus = 200
  * def response = {"status: login ok"}
Scenario: pathMatches('/users/login') && methodIs('post') && request == {"username": "baduser", "password": "badpassword"}
  * def responseStatus = 401
  * def response = {"status: login not ok"}
Scenario:
  * print request
  * print requestHeaders

当我发送带有gooduser"或baduser"详细信息的请求时,它们会陷入默认情况.这会打印请求,看起来就像我所期望的那样.

When I send a request with either the "gooduser" or "baduser" details, they're falling through to the default scenario. This prints the request, which looks like I'd expect.

例如,如果我运行

curl -X POST -d '{"username":"baduser","password":"badpassword"}' http://localhost:8999/users/login

我可以在空手道日志中看到前 2 个场景被跳过并且比赛开始(空).但是,日志也会打印出看起来正确的请求正文,所以我很惊讶第二种情况与我发送的请求不匹配.

I can see in the Karate logs that the first 2 scenarios are being skipped and the match is on (empty). However, the logs are also printing out the request body which looks correct, so I'm surprised the 2nd scenario isn't matching the request I'm sending.

另外,如果我删除 '&&场景中的 request = {...}' 子句,匹配工作正常.

Also, if I remove the '&& request = {...}' clause from the scenario, the match works fine.

感觉我遗漏了一些明显的东西 - 谁能指出我正确的方向?

Feels like I'm missing something obvious - can anyone please point me in the right direction?

推荐答案

是的,== 符号不适用于复杂对象(任何语言),这就是为什么 match 空手道的语法非常重要.请记住,Scenario 表达式被评估为纯 JavaScript.

Yes, the == sign won't work for complex objects (in any language), which is why the match syntax of Karate is such a big deal. Keep in mind that the Scenario expression is evaluated as pure JavaScript.

对你来说最简单的选择是直接使用 request 对象.由于在您的情况下它是 JSON,因此像 request.username == 'gooduser' 这样的简单 JS 表达式将起作用!

for you the simplest option, is to just use the request object directly. Since it is JSON in your case, a simple JS expression like request.username == 'gooduser' will work !

对于那些使用 XML 的人,有一些空手道功能可以满足您的需要.bodyPath() 是什么也将为您工作.最好根据有效负载中的一两个值做出决定,而不是全部,这就是 real 服务器无论如何都会做的事情:

For those who use XML, there are some Karate functions that will do what you need. bodyPath() is what will also work for you. And it is better to make the decision based on one or two values in the payload, not the whole thing, and this is what real servers do anyways:

Scenario: pathMatches('/users/login') && methodIs('post') && bodyPath('$.username') == 'gooduser'

你可以使用 karate.match(request, json) 也是,但不会那么优雅.

You can use karate.match(request, json) also, but it won't be as elegant.

正如我最初在评论中提到的,您可以将决定响应什么的逻辑移到 Scenario 的正文中,如下所示:

And as I initially mentioned in the comment, you could move the logic of deciding what to respond with into the body of the Scenario, something like this:

* def response = request.username == 'gooduser' ? read('good.json') : read('bad.json')

这篇关于空手道模拟 - 如何匹配请求正文内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆