【问题标题】:Karate websocket send request body replace token failed空手道 websocket 发送请求正文替换令牌失败
【发布时间】:2020-10-05 14:38:01
【问题描述】:

我正在使用空手道来测试 websocket,这很有效:

Background:
    * def token = TOKEN
    * def handler = function(msg){ return msg.startsWith('a[')}
    * def socket = karate.webSocket(WS_HOST + 'socket/761/f4t0so3p/websocket', handler)

  Scenario:  Demo Real
  checking dcube-dev
    * socket.send('{"type":"1ffe4b5d___AC_GET_MY_AVAILABLE_TASKS___N","token": "myhardcodedtoken","content":{"msg":null,"counter_api_enabled":false}}')
   

你可以在这里看到,我在请求正文中对令牌进行硬编码,不好,所以我尝试将其移出并使用环境令牌,如下所示:

 Background:
    * def token = TOKEN
    * def handler = function(msg){ return msg.startsWith('a[')}
    * def socket = karate.webSocket(WS_HOST + 'socket/761/f4t0so3p/websocket', handler)

  Scenario:  Demo Real
  checking dcube-dev
    * def body = {"type": "1ffe4b5d___AC_GET_MY_AVAILABLE_TASKS___N", "token": '#(token)', "content":  {"msg":null,"counter_api_enabled":false} }
    * print "Body:", body
    * socket.send( '#(body)')

但这总是错误的,似乎 msg 从未发出过。谁能告诉我如何解决这个问题?

谢谢

【问题讨论】:

    标签: karate


    【解决方案1】:

    实际上我认为您需要进行这一更改:

    * socket.send(body)
    

    将圆括号视为 JS - 不是标准的空手道表达式:https://github.com/intuit/karate#enclosed-javascript

    另请注意,对于字符串(而不是 JSON),您可以使用 replacehttps://github.com/intuit/karate#replace

    * def text = 'hello <foo> world'
    * replace text.foo = 'bar'
    * match text == 'hello bar world'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-23
      • 1970-01-01
      • 2015-08-18
      • 2013-05-02
      • 2013-10-22
      • 1970-01-01
      • 2013-01-24
      相关资源
      最近更新 更多