简体   繁体   English

适用于https服务器的Mocha Web套接字测试

[英]Mocha web socket testing for https server

I've been trying to test a web socket connection. 我一直在尝试测试Web套接字连接。 There a few good tutorials out there that describe it which led me to this sort of dummy code just to see if I can test data being sent over websockets: 那里有一些很好的教程来描述它,这使我找到了这种伪代码,只是看我是否可以测试通过websocket发送的数据:

var socketURL= 'https://localhost:9002';
var io = require('socket.io-client');
options={
transports: ['websocket'],
'force new connection': true
};
describe("Connection test", function(){
it('to check if data is received',function(done){
    var client = io.connect(socketURL,options);

    client.on('connect',function(){
        console.log("in connect");
        client.emit('data',{data:10});

        client.on('result',function(data){
            console.log(data);
            data.should.equal(10);
            done();
        });

    });
});

I've set up the server like so, where the OPTIONS determine the key and cert to read 我已经这样设置服务器,其中OPTIONS确定要读取的密钥和证书

var app = express();
var httpsServer = https.createServer(OPTIONS, app);

httpsServer.listen(9002);
var io = require('socket.io').listen(httpsServer);
io.on('connection', function(socket){
    socket.on('data', function(data){
        socket.emit('result',data);
    })
})

The problem is that the client.on('connect') is never called, I can see this by nothing printing out to the console. 问题是从来没有调用过client.on('connect'),我什么也没看到打印到控制台。 All that happens is a time out error: "timeout of 2000ms exceeded. Ensure the done() callback is being called in this test." 发生的所有事情都是一个超时错误:“超过了2000ms的超时。请确保在此测试中调用了done()回调。” I've moved the done() and it doesn't make a diffence. 我已经移动了done()并没有什么区别。 I think the problem is that the tutorials I've follow use a http server whereas I'm using a https server, and I figure that this may require addition methods to connect, however I'm quite new to mocha so I don't know what they may be. 我认为问题在于我遵循的教程使用的是http服务器,而我使用的是https服务器,我认为这可能需要使用其他方法进行连接,但是我对Mocha相当陌生,所以我不需要知道他们可能是什么。 Any help would be appreciated 任何帮助,将不胜感激

Are you using self-signed certificates? 您是否正在使用自签名证书? If so, running your code like this: 如果是这样,请像这样运行代码:

 env DEBUG=* mocha test/example.js

gives the following error: 给出以下错误:

Mon, 04 Jul 2016 11:22:20 GMT engine.io-client:socket socket error {"type":"TransportError","description":{"code":"UNABLE_TO_VERIFY_LEAF_SIGNATURE","type":"error","target":{"domain":null,"_events":{},"_eventsCount":4,"_socket":null,"_ultron":null,"_closeReceived":false,"bytesReceived":0,"readyState":0,"supports":{"binary":true},"extensions":{},"_isServer":false,"url":"wss://localhost:9002/socket.io/?EIO=3&transport=websocket","protocolVersion":13,"binaryType":"buffer"}}}

Note the UNABLE_TO_VERIFY_LEAF_SIGNATURE 注意UNABLE_TO_VERIFY_LEAF_SIGNATURE

So, add the certificate authority to the client options, eg 因此,将证书颁发机构添加到客户端选项,例如

options={
ca: fs.readFileSync(path.join(__dirname, '..', 'certs', 'client', 'my-root-ca.crt.pem')),
transports: ['websocket'],
'force new connection': true
};

fix 固定

    data.data.should.equal(10);

and your test passes: 并且您的测试通过:

  Connection test
in connect
{ data: 10 }
    √ to check if data is received (68ms)


  1 passing (75ms)

If you pass done in mocha test case, by default timeout period is 2000ms (2 seconds). 如果您在mocha测试用例中通过完,则默认情况下,超时时间为2000ms(2秒)。 It needs more time to connect to your web socket connection. 连接到Web套接字连接需要更多时间。

The given error time out error: "timeout of 2000ms exceeded. Ensure the done() callback is being called in this test. 给定的错误超时错误:“超时超过2000毫秒。请确保在此测试中调用了done()回调。

Problem should be fixed with the following code : 问题应使用以下代码解决:

it('to check if data is received',function(done){

    this.timeout(60000);  // 60 seconds
    var client = io.connect(socketURL,options);

    client.on('connect',function(){
        console.log("in connect");
        client.emit('data',{data:10});

        client.on('result',function(data){
            console.log(data);
            data.should.equal(10);
            done();
        });

    });
});

this.timeout(60000) tells mocha to wait 60 seconds to complete the test case. this.timeout(60000)告诉摩卡咖啡等待60秒以完成测试用例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM