【问题标题】:Test NodeJS socket.io with custom headers使用自定义标头测试 NodeJS socket.io
【发布时间】:2015-04-22 16:51:22
【问题描述】:

我正在尝试编写简单的测试来测试我的 NodeJS socket.io 应用程序。问题是在握手阶段我确实需要某些值(两个cookie和标题)。这就是我现在所拥有的:

var options = {
        transports: ['websocket'],
        'force new connection': true,
        headers: {'accept-langauge': 'foo'}
};

it("send data", function(done) {
   var client = io.connect('http://localhost:3000', options);

   client.once("connect", function(s) {
       expect(s.handshake).to.not.be(undefined);
       expect(s.handshake.headers).to.be.an('object');
       expect(s.handshake.headers['accept-language']).to.be('en');

       client.once("send_premise_to_snet", function(id) {
           id.should.equal("123");

           client.disconnect();
           done();
       });

       client.emit("send_data", 123);
   });
});

我希望能够设置接受语言和 cookie,以便它们出现在握手中,从而可以通过握手属性访问。

在正常的浏览器请求中,浏览器会正确填充标题,现在我也想在测试阶段这样做。

【问题讨论】:

  • 你有什么问题?您是否在验证来自服务器的握手标头时遇到问题,或者您是否在询问如何从服务器发送正确的标头?
  • 问题是 - 如何在执行“client = io.connect”时传递标头。这样他们我应该能够从握手数据中读取它们。请注意,io.connect 是在 nodejs unittest case 中完成的。

标签: node.js sockets testing cookies socket.io


【解决方案1】:

在使用正确的 Google 查询词成功命中后,我设法找到了更多信息。似乎目前无法使用当前的 socket.io 和 socket.io-client 实现。

很少有替代方法被采用,但没有一种方法能够成功实现实际的包。

更多信息:

https://github.com/Automattic/socket.io/issues/2036

https://github.com/Automattic/socket.io-client/issues/648

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-10-06
    • 2017-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-04
    • 2013-08-18
    相关资源
    最近更新 更多