// global
var ws = null;
var msgCount = 0;
var onMessageHandler = null;
// test #1 - this test passes successfully
describe('connect to wsserver test', function() {
it('should successfully connect to wsserver', function(done) {
this.timeout(0);
ws = new FayeWebSocket.Client('wss://server.com', null, {
headers: {
"authToken": "someToken"
}
});
ws.on('open', function() {
done();
});
ws.on('message', function(msg) {
if (onMessageHandler && typeof onMessageHandler === 'function') {
onMessageHandler(msg);
}
});
ws.on('close', function(event) {
console.log('closing websocket!');
});
ws.on('error', function(err) {
console.log("error!: " + err.message);
});
});
});
// test #2 - this test blocks indefinitely
describe('send request and get back 3 response messages', function() {
it('should get back 3 response messages from the wsserver', function(done) {
this.timeout(0);
// this function is called ONLY once, although the server is sending 3 messages
onMessageHandler = function(msg) {
msgCount++;
console.log(msg);
if (msgCount >= 3) {
done();
}
}
var sendThisRequest = {
'some': 'json',
'with': 'some key/value pairs'
}
// this line sends a request to the wsserver
ws.send(JSON.stringify(sendMsg));
});
});
我正在尝试编写一些基本的单元测试来测试我的websocket apis。这些mocha测试模拟客户端,而不是websocket服务器。
如果有人能指出我正确的方向来解决这个问题,那就太棒了。感谢
答案 0 :(得分:0)
我认为你无法使用当前的mocha单元测试来进行数据流传输。
ws.send(JSON.stringify(sendMsg));
您只调用此语句一次,这意味着只传输一次数据。您需要多次调用它来检查n time streaming
。
答案 1 :(得分:0)
此代码正常运行。我传递给服务器的输入存在一些问题。遗憾!