【问题标题】:Karate DSL - How to read token string from __arg and send this string in json request?空手道 DSL - 如何从 __arg 读取令牌字符串并在 json 请求中发送此字符串?
【发布时间】:2019-12-26 13:58:17
【问题描述】:

如何从 __arg 读取令牌字符串并使用 Karate DSL 在 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”,

【问题讨论】:

标签: javascript java json dsl karate


【解决方案1】:

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

当将未知(不是json、yaml、csv aso.)读取资源嵌入到json之类的纯js表达式中时,结果如下:

{
  "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)'} 

【讨论】:

  • 非常有趣。谢谢!我会尝试这种解决方法并将其写回到这里。
猜你喜欢
  • 2020-12-02
  • 2017-01-07
  • 2023-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-27
  • 2016-03-28
  • 1970-01-01
相关资源
最近更新 更多