WebSocketClient should allow for listening multiple times
See original GitHub issueCurrently, once the WebSocketClient produced by karate.webSocket(url, handlerDelegate);
has responded to it’s listen
method, subsequent calls to listen
will always return the first message returned. This is not helpful, particularly when you may change the handler via setTextHandler
after the first listen.
Here’s the rough scenario I’m running into.
Our WebSocket API requires authentication sent thru the first frame before we will receive messages. That authentication may take a few seconds. Currently, as soon as I send the auth frame in Karate, the rest of the suite executes. Since the authentication did not complete, we don’t receive any of the expected messages on from the socket connection. Our objective would be to send the auth frame, then wait for a response. Once we have the expected auth response, continue with the rest of our test and then listen for the next expected web socket.
Our code roughly follows this pattern:
var msgHandlerDelegate = function(msg) {
var jsonMsg = JSON.parse(msg);
if (jsonMessage.eventType == 'ExpectedEvent') {
return true;
}
return false;
};
var authHandlerDelegate = function(msg) {
var jsonMsg = JSON.parse(msg);
if (jsonMsg.statusCode == 200 && jsonMsg.body == "OK") {
return true;
}
return false;
};
var webSocketClient = karate.webSocket(websocketUrl, authHandlerDelegate);
var authPayload = { "message": "auth","data": { "auth_token": auth_token } };
webSocketClient.send(JSON.stringify(authPayload));
var response = webSocketClient.listen(20000);
expect response != null;
webSocketClient.setTextHandler(karate.toJava(msgHandlerDelegate));
// Perform remainder of test
var webSocketMessage = webSocketClient.listen(20000);
expect webSocketMessage == { eventType: 'ExpectedEvent' }
Issue Analytics
- State:
- Created a year ago
- Comments:23 (11 by maintainers)
Top GitHub Comments
@ptrthomas , Apologies for not getting back sooner, No worries about a breaking change on the implementation. I’ll try to test it out in the next few days.
@ptrthomas , thank you for your responsiveness getting these changes incorporated. much appreciated.