1

我正在尝试为多个套接字连接编写测试,我需要(同时)有多个会话,因为它们是由服务器端的会话 ID 标识的......

在下面的示例中,client1将连接并且握手将通过带有会话 id 的 cookie,因此我可以使用express-socket.io-session获取该用户的会话数据....

我遇到的问题是当我想连接第二个套接字时,传递的握手数据显然与第一个套接字相同。

我不能只发送注销请求并更改用户,因为我正在尝试测试多人游戏功能。

var agent = chai.request.agent(app)

describe("authentication", function () {
    it('login as user', function (done) {
        agent
            .post('/login')
            .send({
                username: username,
                password: password
            })
            .then(function (res) {
                expect(res).to.have.status(200);
                expect(res).to.have.cookie('connect.sid');
                done()
            })
            .catch(function (err) {
                done(err)
            })
    });

})


describe("socket tests", function () {
    var client1, client2;

    it('can connect to socket', function (done) {
        client1 = io.connect("http://localhost:3000/", options);
        client1.on('connect', () => {
            done();
        })
    });

    it('can connect second user to socket', function (done) {
        client2 = io.connect("http://localhost:3000/", options);
        client2.on('connect', () => {
            done();
        })
    });
})
4

0 回答 0