空手道DSL - 如何从__arg读取令牌字符串并在json请求中发送此字符串?



如何使用空手道DSL从__arg读取令牌字符串并在json请求中发送此字符串?

存储从第一个响应到__arg的字符串令牌值

这效果很好:

Then match response.result.token == '#string'
* def tokenPathInstance =
"""
function() { karate.write(response.result.token, '__arg'); }
"""
* def tokenPath = call tokenPathInstance

但是对于下一个 json 请求,我已经尝试了下一个代码,但这不适合我的需求:

def jsonInstance =
"""
function(token) {
var DTO = Java.type('requestbody.DTO');
var li = new DTO();
li.setToken(token);
return li.toJson();
}
"""
def token = read('file:target/__arg')
def reqBody = call jsonInstance token

我只在请求中看到: "令牌": "java.io.FileInputStream@693dc9dc",

这看起来像一个错误。创建 https://github.com/intuit/karate/issues/1009

当将未知(不是json,yaml,csv aso(读取资源嵌入到纯js表达式(如json(中时,结果如下所示:

{
"token": {
"tokenParam": "java.io.FileInputStream@424bd12a"
}
}

将令牌写入 txt 文件是一种解决方法。MIME 类型检测仅检查后缀。

以下方案应说明这一点:

Scenario: Test to read/write within karate..
* def token = 'xyz12345'
* eval  karate.write(karate.get('token'), 'token.txt'); 
* def tokenToJson = 
"""
function(tokenParam) {
return { token: tokenParam}
}
"""
* string tokenFromFile = read('file:target/token.txt')
* def tokenAsJson = call tokenToJson tokenFromFile
* print tokenAsJson
* match tokenAsJson == {token: '#(token)'} 

最新更新