空手道websocket发送请求正文替换令牌失败



我正在用空手道测试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)')

但这总是错误的,似乎消息从未发出。有人能告诉我如何解决这个问题吗?

感谢

实际上,我认为您需要进行以下更改:

* socket.send(body)

把圆括号想象成JS,而不是标准的空手道表达:https://github.com/intuit/karate#enclosed-javascript

还要注意,对于字符串(而不是JSON(,可以使用replace:https://github.com/intuit/karate#replace

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

最新更新