Skip to content

Commit

Permalink
[fix] Add default value for servername option (#1347)
Browse files Browse the repository at this point in the history
Fixes #1346
  • Loading branch information
lpinca authored Apr 2, 2018
1 parent 2d7bf88 commit f335d79
Showing 2 changed files with 7 additions and 3 deletions.
5 changes: 4 additions & 1 deletion lib/websocket.js
Original file line number Diff line number Diff line change
@@ -465,7 +465,9 @@ function initAsClient (address, protocols, options) {

options.createConnection = isSecure ? tlsConnect : netConnect;
options.port = parsedUrl.port || (isSecure ? 443 : 80);
options.host = parsedUrl.hostname;
options.host = parsedUrl.hostname.startsWith('[')
? parsedUrl.hostname.slice(1, -1)
: parsedUrl.hostname;
options.headers = Object.assign({
'Sec-WebSocket-Version': options.protocolVersion,
'Sec-WebSocket-Key': key,
@@ -622,6 +624,7 @@ function netConnect (options) {
*/
function tlsConnect (options) {
options.path = options.socketPath || options._socketPath || undefined;
options.servername = options.servername || options.host;
return tls.connect(options);
}

5 changes: 3 additions & 2 deletions test/websocket.test.js
Original file line number Diff line number Diff line change
@@ -41,12 +41,13 @@ describe('WebSocket', function () {

const agent = new CustomAgent();

agent.addRequest = (req) => {
agent.addRequest = (req, opts) => {
assert.strictEqual(opts.host, '::1');
assert.strictEqual(req.path, '/');
done();
};

const ws = new WebSocket(new url.URL('ws://localhost'), { agent });
const ws = new WebSocket(new url.URL('ws://[::1]'), { agent });
});

describe('options', function () {

0 comments on commit f335d79

Please sign in to comment.