如何测试node.js websocket服务器?

kas*_*lan 6 unit-testing mocha.js websocket node.js sockjs

我正在使用标准配置的sockjs.

   var ws = sockjs.createServer();
    ws.on('connection', function(conn) {
        conn.on('data', function(message) {
            wsParser.parse(conn, message)
        });
        conn.on('close', function() {

        });
    });

    var server = http.createServer(app);
    ws.installHandlers(server, {prefix:'/ws'});
    server.listen(config.server.port, config.server.host);
Run Code Online (Sandbox Code Playgroud)

wsParser.parse 功能是这样的:

function(conn, message) {

(...)

switch(message.action) {
    case "titleAutocomplete":
        titleAutocomplete(conn, message.data);
        break;
    (...) // a lot more of these
 }
Run Code Online (Sandbox Code Playgroud)

在switch中调用的每个方法都会向客户端发送一条消息.

var titleAutocomplete = function(conn, data) {

    redis.hgetall("titles:"+data.query, function(err, titles){
        if(err) ERR(err);

        if(titles) {
            var response = JSON.stringify({"action": "titleAutocomplete", "data": {"titles": titles}});
            conn.write(response);
        } 
    })
};
Run Code Online (Sandbox Code Playgroud)

现在我的问题是我想为我的代码进行测试(比我猜的更好),我不知道该怎么做.我开始用mocha + supertest编写正常的http测试,但我只是不知道如何处理websockets.

我希望只有一个websocket连接可以通过所有测试重用,我在第一个消息之后将websocket连接与用户会话绑定,我也想测试该持久性.

如何利用ws客户端的onmessage事件并在我的测试中使用它?测试如何分辨收到的消息并知道他们应该等待哪一个?

kas*_*lan 2

工作中的同事问是否真的需要客户端连接,还是可以只是模拟一下。事实证明这是正确的选择。我写了一个小助手类 wsMockjs

var wsParser = require("../wsParser.js");

exports.createConnectionMock = function(id) {
    return {
        id: id,
        cb: null,
        write: function(message) {
            this.cb(message);
        },
        send: function(action, data, cb) {
            this.cb = cb;
            var obj = {
                action: action,
                data: data
            }
            var message = JSON.stringify(obj);
            wsParser.parse(this, message);
        },
        sendRaw: function(message, cb) {
            this.cb = cb;
            wsParser.parse(this, message);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

现在在我的摩卡测试中我只是这样做

var wsMock = require("./wsMock.js");
ws = wsMock.createConnectionMock("12345-67890-abcde-fghi-jklmn-opqrs-tuvwxyz");
(...)
describe('Websocket server', function () {

    it('should set sessionId variable after handshake', function (done) {
        ws.send('handshake', {token: data.token}, function (res) {
            var msg = JSON.parse(res);
            msg.action.should.equal('handshake');
            msg.data.should.be.empty;
            ws.should.have.property('sessionId');
            ws.should.not.have.property('session');
            done();
        })
    })

    it('should not return error when making request after handshake', function (done) {
        ws.send('titleAutocomplete', {query: "ter"}, function (res) {
            var msg = JSON.parse(res);
            msg.action.should.equal('titleAutocomplete');
            msg.data.should.be.an.Object;
            ws.should.have.property('session');
            done();
        })
    })
})
Run Code Online (Sandbox Code Playgroud)

它的工作原理就像一个魅力,可以在请求之间保留连接状态和变量。