我正在尝试为此websocket编写测试,但总是会收到此错误:
#clients › it should create a room of clients on connection
Unhandled error. (Error: Unexpected server response: 400
at ClientRequest.<anonymous> (node_modules/ws/lib/websocket.js:580:7)
at TCP.onStreamRead (internal/stream_base_commons.js:182:23))Error [ERR_UNHANDLED_ERROR]: Unhandled error. (Error: Unexpected server response: 400
at ClientRequest.<anonymous> (node_modules/ws/lib/websocket.js:580:7)
at TCP.onStreamRead (internal/stream_base_commons.js:182:23))
at abortHandshake (node_modules/ws/lib/websocket.js:698:15)
at ClientRequest.<anonymous> (node_modules/ws/lib/websocket.js:580:7)
这是我的代码,我是测试的新手,有人可以向我解释如何模拟此调用吗?在连接上,我希望能够检索套接字的路径以创建ID
it('it should create a room of clients on connection', (done) => {
let rooms = new Map();
const wss = new WebSocket.Server({ port: 0, path: '/12345?isDoctor=true' }, () => {
const ws = new WebSocket(`ws://localhost:${wss.address().port}`);
expect(rooms.size).toEqual(0)
});
wss.on('connection', (ws, req) => {
console.log(req.url, 'url'); // I never get this log
const path = req.url.slice(1);
const indexOfParams = path.indexOf('?');
const id = path.slice(0, indexOfParams);
if ( rooms.has(id) === false) {
rooms.set(id, [ws]);
}
expect(rooms.size).toEqual(1);
wss.close(done);
});
});