I am using sockjs with a standard configuration.
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);
Function
wsParser.parse works as follows:
function(conn, message) { (...) switch(message.action) { case "titleAutocomplete": titleAutocomplete(conn, message.data); break; (...)
Each method called on the switch sends a message back to the client.
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); } }) };
Now my problem is that I would like to do tests for my code (better late than I guess), and I have no idea how to do this. I started writing regular http tests using mocha + supertest, but I just don't know how to handle websockets.
I would like to have only one network connection to reuse all tests, I bind the websocket connection to the user session after the first message, and I want to check this persistence as well.
How can I use the ws client onmessage event and use it in my tests? How can tests tell you about all messages received and find out which one they should wait for?