空手道-发送带有每个请求正文字段格式错误请求的 API 请求的任何非丑陋方式? [英] Karate- Any non-ugly way to send API requests with malformed requests for each request body field?

查看:29
本文介绍了空手道-发送带有每个请求正文字段格式错误请求的 API 请求的任何非丑陋方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个奇怪的问题,我已经有了解决方案,但它非常难看.我想知道是否有更好的方法通过空手道或 JS 来做到这一点.我是这两个方面的新手,所以请多多包涵.

I have a strange problem for which I already have a solution for but it's VERY ugly. I am wondering if there is a better way to do it through Karate or JS. I am new at both so please bear with me.

我正在发送一个带有相当大请求正文(总共 19 个字段)的 POST 调用.我必须有目的地为每个字段发送格式错误的请求,而不是输入一个合法的键值,而是放置垃圾值(或完全删除该值)并确保按预期返回 400.

I am sending a POST call with a rather large request body (total of 19 fields). I have to purposefully send malformed requests for EACH field where instead of inputting a legitimate value for a key, I am putting garbage values (or removing the value altogether) and making sure I get a 400 back as expected.

例如请求正文如下(为了说明目的而缩短):

For example the request body is as follows (shortened for illustration purposes):

{
    "Age": 20
    "School": "UIC"
    "Sex": "Female"
}

我发送的请求如下:

* def payload = {"Age":'#(age)',"School":'#(schoolName)', "Sex":'#(gender)'}
* copy payload1 = payload
* copy payload2 = payload
* copy payload3 = payload
* remove payload1.Age
* remove payload2.School
* remove payload3.Sex
* table callTable
    |payload  |status|
    |payload1 | 400  |
    |payload2 | 400  |
    |payload3 | 400  |
* call read (call to the actual POST API here)

现在从表面上看,上面的内容可能看起来并不难看,但想象一下对 19 个字段执行此操作,而不是删除,每个字段大约有五个不同的请求(而不是上面只有一个删除").

Now on surface the above may not look ugly but imagine doing that for 19 fields and instead of remove, there is about FIVE different requests PER field (instead of just one "remove" above).

我曾考虑使用 JS 和循环函数,但我知道的还不够多,而且我的尝试失败了.

I have contemplated using JS and a loop function but I don't know enough and my attempts failed spectacularly.

请告知在空手道和/或 JS 中是否有更好的方法.

Please advise if there is a way better to do it in Karate and/or JS.

提前感谢您的阅读和时间!

Thank you in advance for reading and for your time!

推荐答案

您可以使用循环来构建 JSON.例如:

You can use a loop to build JSON. For example:

* def keys = ['foo', 'bar', 'baz']
* def body = {}
* keys.forEach(k => body[k] = 'blah')
* match body == { foo: 'blah', bar: 'blah', baz: 'blah' }

另见https://stackoverflow.com/a/69149219/143475

以及 JSON 转换的主要文档:https://github.com/intuit/karate#json-transforms

And the main documentation for JSON transforms: https://github.com/intuit/karate#json-transforms

这篇关于空手道-发送带有每个请求正文字段格式错误请求的 API 请求的任何非丑陋方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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