【问题标题】:Mocha web socket testing for https server用于 https 服务器的 Mocha Web 套接字测试
【发布时间】:2016-04-14 12:38:18
【问题描述】:

我一直在尝试测试网络套接字连接。那里有一些很好的教程来描述它,这导致我使用这种虚拟代码只是为了看看我是否可以测试通过 websockets 发送的数据:

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();
        });

    });
});

我已经像这样设置了服务器,其中 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);
    })
})

问题是 client.on('connect') 从未被调用,我可以通过没有打印到控制台来看到这一点。所发生的只是一个超时错误:“超过 2000 毫秒的超时。确保在此测试中调用了 done() 回调。” 我已经移动了 done() 并没有什么区别。我认为问题在于我遵循的教程使用的是 http 服务器,而我使用的是 https 服务器,我认为这可能需要其他方法来连接,但是我对 mocha 很陌生,所以我不知道它们可能是什么。 任何帮助将不胜感激

【问题讨论】:

    标签: javascript node.js sockets https mocha.js


    【解决方案1】:

    您是否使用自签名证书?如果是这样,请像这样运行您的代码:

     env DEBUG=* mocha test/example.js
    

    给出以下错误:

    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"}}}
    

    注意UNABLE_TO_VERIFY_LEAF_SIGNATURE

    因此,将证书颁发机构添加到客户端选项中,例如

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

    修复

        data.data.should.equal(10);
    

    你的测试通过了:

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

    【讨论】:

      【解决方案2】:

      如果你在 mocha 测试用例中通过 done,默认超时时间为 2000 毫秒(2 秒)。连接到您的 Web 套接字连接需要更多时间。

      给定的错误超时错误:“超过2000毫秒的超时。确保在此测试中调用了done()回调。

      应该使用以下代码解决问题:

      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) 告诉 mocha 等待 60 秒以完成测试用例。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-03-06
        • 2013-11-08
        • 2022-06-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-12-10
        相关资源
        最近更新 更多