Skip to content

Commit

Permalink
[minor] Fix nits
Browse files Browse the repository at this point in the history
  • Loading branch information
lpinca committed Apr 15, 2024
1 parent 53a8888 commit 8f8b66c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 35 deletions.
4 changes: 0 additions & 4 deletions doc/ws.md
Original file line number Diff line number Diff line change
Expand Up @@ -304,10 +304,6 @@ This class represents a WebSocket. It extends the `EventEmitter`.
`'ping'`, and `'pong'` events can be emitted multiple times in the same
tick. To improve compatibility with the WHATWG standard, the default value
is `false`. Setting it to `true` improves performance slightly.
- `createConnection` {Function} An alternative function to use in place of
`tls.createConnection` or `net.createConnection`. This can be used to
manually control exactly how the connection to the server is made, or to
make a connection over an existing Duplex stream obtained elsewhere.
- `finishRequest` {Function} A function which can be used to customize the
headers of each HTTP request before it is sent. See description below.
- `followRedirects` {Boolean} Whether or not to follow redirects. Defaults to
Expand Down
3 changes: 0 additions & 3 deletions lib/websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -628,8 +628,6 @@ module.exports = WebSocket;
* times in the same tick
* @param {Boolean} [options.autoPong=true] Specifies whether or not to
* automatically send a pong in response to a ping
* @param {Function} [options.createConnection] An alternative function to use
* in place of `tls.createConnection` or `net.createConnection`.
* @param {Function} [options.finishRequest] A function which can be used to
* customize the headers of each http request before it is sent
* @param {Boolean} [options.followRedirects=false] Whether or not to follow
Expand Down Expand Up @@ -662,7 +660,6 @@ function initAsClient(websocket, address, protocols, options) {
perMessageDeflate: true,
followRedirects: false,
maxRedirects: 10,
createConnection: undefined,
...options,
socketPath: undefined,
hostname: undefined,
Expand Down
57 changes: 29 additions & 28 deletions test/websocket.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1158,6 +1158,35 @@ describe('WebSocket', () => {
});
});

it('honors the `createConnection` option', (done) => {
const wss = new WebSocket.Server({ noServer: true, path: '/foo' });

server.once('upgrade', (req, socket, head) => {
assert.strictEqual(req.headers.host, 'google.com:22');
wss.handleUpgrade(req, socket, head, NOOP);
});

const ws = new WebSocket('ws://google.com:22/foo', {
createConnection: (options) => {
assert.strictEqual(options.host, 'google.com');
assert.strictEqual(options.port, '22');

// Ignore the `options` argument, and use the correct hostname and
// port to connect to the server manually.
return net.createConnection({
host: 'localhost',
port: server.address().port
});
}
});

ws.on('open', () => {
assert.strictEqual(ws.url, 'ws://google.com:22/foo');
ws.on('close', () => done());
ws.close();
});
});

it('does not follow redirects by default', (done) => {
server.once('upgrade', (req, socket) => {
socket.end(
Expand Down Expand Up @@ -1224,34 +1253,6 @@ describe('WebSocket', () => {
});
});

it('honors the `createConnection` option', (done) => {
const wss = new WebSocket.Server({ noServer: true, path: '/foo' });

server.once('upgrade', (req, socket, head) => {
assert.strictEqual(req.headers.host, 'google.com:22');
wss.handleUpgrade(req, socket, head, NOOP);
});

const ws = new WebSocket('ws://google.com:22/foo', {
createConnection: (options) => {
assert.strictEqual(options.host, 'google.com');
assert.strictEqual(options.port, '22');

// Ignore the invalid host address, and connect to the server manually:
return net.createConnection({
host: 'localhost',
port: server.address().port
});
}
});

ws.on('open', () => {
assert.strictEqual(ws.url, 'ws://google.com:22/foo');
ws.on('close', () => done());
ws.close();
});
});

it('emits an error if the redirect URL is invalid (1/2)', (done) => {
server.once('upgrade', (req, socket) => {
socket.end('HTTP/1.1 302 Found\r\nLocation: ws://\r\n\r\n');
Expand Down

0 comments on commit 8f8b66c

Please sign in to comment.