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

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

问题描述

使用空手道,我正在尝试找出如何根据请求正文的内容返回不同的模拟响应.

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

我在空手道日志中看到,前两个场景已被跳过,比赛进行中(空).但是,日志还会打印出看起来正确的请求正文,因此令我惊讶的是第二种情况与我发送的请求不匹配.

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?

推荐答案

是的,==符号不适用于复杂对象(任何语言),这就是

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天全站免登陆