Skip to content

Commit

Permalink
[minor] Use consistent error messages
Browse files Browse the repository at this point in the history
Make some server error messages consistent with the respective client
error messages.
  • Loading branch information
lpinca committed May 21, 2022
1 parent 0fdcc0a commit fb658bd
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 16 deletions.
12 changes: 6 additions & 6 deletions lib/websocket-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,23 +234,23 @@ class WebSocketServer extends EventEmitter {
const version = +req.headers['sec-websocket-version'];

if (req.method !== 'GET') {
abortHandshake(socket, 405, 'The HTTP method is invalid');
abortHandshake(socket, 405, 'Invalid HTTP method');
return;
}

if (req.headers.upgrade.toLowerCase() !== 'websocket') {
abortHandshake(socket, 400, 'The Upgrade header is invalid');
abortHandshake(socket, 400, 'Invalid Upgrade header');
return;
}

if (!key || !keyRegex.test(key)) {
const message = 'The Sec-WebSocket-Key header is missing or invalid';
const message = 'Missing or invalid Sec-WebSocket-Key header';
abortHandshake(socket, 400, message);
return;
}

if (version !== 8 && version !== 13) {
const message = 'The Sec-WebSocket-Version header is missing or invalid';
const message = 'Missing or invalid Sec-WebSocket-Version header';
abortHandshake(socket, 400, message);
return;
}
Expand All @@ -267,7 +267,7 @@ class WebSocketServer extends EventEmitter {
try {
protocols = subprotocol.parse(secWebSocketProtocol);
} catch (err) {
const message = 'The Sec-WebSocket-Protocol header is invalid';
const message = 'Invalid Sec-WebSocket-Protocol header';
abortHandshake(socket, 400, message);
return;
}
Expand Down Expand Up @@ -295,7 +295,7 @@ class WebSocketServer extends EventEmitter {
}
} catch (err) {
const message =
'The Sec-WebSocket-Extensions header is invalid or not acceptable';
'Invalid or unacceptable Sec-WebSocket-Extensions header';
abortHandshake(socket, 400, message);
return;
}
Expand Down
19 changes: 9 additions & 10 deletions test/websocket-server.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ describe('WebSocketServer', () => {
res.on('end', () => {
assert.strictEqual(
Buffer.concat(chunks).toString(),
'The Sec-WebSocket-Key header is missing or invalid'
'Missing or invalid Sec-WebSocket-Key header'
);
wss.close(done);
});
Expand Down Expand Up @@ -577,7 +577,7 @@ describe('WebSocketServer', () => {
res.on('end', () => {
assert.strictEqual(
Buffer.concat(chunks).toString(),
'The HTTP method is invalid'
'Invalid HTTP method'
);
wss.close(done);
});
Expand Down Expand Up @@ -613,7 +613,7 @@ describe('WebSocketServer', () => {
res.on('end', () => {
assert.strictEqual(
Buffer.concat(chunks).toString(),
'The Upgrade header is invalid'
'Invalid Upgrade header'
);
wss.close(done);
});
Expand Down Expand Up @@ -647,7 +647,7 @@ describe('WebSocketServer', () => {
res.on('end', () => {
assert.strictEqual(
Buffer.concat(chunks).toString(),
'The Sec-WebSocket-Key header is missing or invalid'
'Missing or invalid Sec-WebSocket-Key header'
);
wss.close(done);
});
Expand Down Expand Up @@ -682,7 +682,7 @@ describe('WebSocketServer', () => {
res.on('end', () => {
assert.strictEqual(
Buffer.concat(chunks).toString(),
'The Sec-WebSocket-Key header is missing or invalid'
'Missing or invalid Sec-WebSocket-Key header'
);
wss.close(done);
});
Expand Down Expand Up @@ -717,7 +717,7 @@ describe('WebSocketServer', () => {
res.on('end', () => {
assert.strictEqual(
Buffer.concat(chunks).toString(),
'The Sec-WebSocket-Version header is missing or invalid'
'Missing or invalid Sec-WebSocket-Version header'
);
wss.close(done);
});
Expand Down Expand Up @@ -753,7 +753,7 @@ describe('WebSocketServer', () => {
res.on('end', () => {
assert.strictEqual(
Buffer.concat(chunks).toString(),
'The Sec-WebSocket-Version header is missing or invalid'
'Missing or invalid Sec-WebSocket-Version header'
);
wss.close(done);
});
Expand Down Expand Up @@ -790,7 +790,7 @@ describe('WebSocketServer', () => {
res.on('end', () => {
assert.strictEqual(
Buffer.concat(chunks).toString(),
'The Sec-WebSocket-Protocol header is invalid'
'Invalid Sec-WebSocket-Protocol header'
);
wss.close(done);
});
Expand Down Expand Up @@ -833,8 +833,7 @@ describe('WebSocketServer', () => {
res.on('end', () => {
assert.strictEqual(
Buffer.concat(chunks).toString(),
'The Sec-WebSocket-Extensions header is invalid or not ' +
'acceptable'
'Invalid or unacceptable Sec-WebSocket-Extensions header'
);
wss.close(done);
});
Expand Down

0 comments on commit fb658bd

Please sign in to comment.