Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow http(s) scheme in the WebSocket constructor #2161

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions lib/websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -667,17 +667,22 @@ function initAsClient(websocket, address, protocols, options) {

if (address instanceof URL) {
parsedUrl = address;
websocket._url = address.href;
} else {
try {
parsedUrl = new URL(address);
} catch (e) {
throw new SyntaxError(`Invalid URL: ${address}`);
}
}

websocket._url = address;
if (parsedUrl.protocol === 'http:') {
parsedUrl.protocol = 'ws:';
} else if (parsedUrl.protocol === 'https:') {
parsedUrl.protocol = 'wss:';
}

websocket._url = parsedUrl.href;

const isSecure = parsedUrl.protocol === 'wss:';
const isIpcUrl = parsedUrl.protocol === 'ws+unix:';
let invalidUrlMessage;
Expand Down
44 changes: 39 additions & 5 deletions test/websocket.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe('WebSocket', () => {
);

assert.throws(
() => new WebSocket('https://websocket-echo.com'),
() => new WebSocket('http+ws://websocket-echo.com'),
/^SyntaxError: The URL's protocol must be one of "ws:", "wss:", or "ws\+unix:"$/
);

Expand Down Expand Up @@ -72,6 +72,30 @@ describe('WebSocket', () => {
const ws = new WebSocket(new URL('ws://[::1]'), { agent });
});

it('allows the http scheme', (done) => {
const agent = new CustomAgent();

agent.addRequest = (req, opts) => {
assert.strictEqual(opts.host, 'localhost');
assert.strictEqual(opts.port, 80);
done();
};

const ws = new WebSocket('http://localhost', { agent });
});

it('allows the https scheme', (done) => {
const agent = new https.Agent();

agent.addRequest = (req, opts) => {
assert.strictEqual(opts.host, 'localhost');
assert.strictEqual(opts.port, 443);
done();
};

const ws = new WebSocket('https://localhost', { agent });
});

describe('options', () => {
it('accepts the `options` object as 3rd argument', () => {
const agent = new http.Agent();
Expand Down Expand Up @@ -539,10 +563,18 @@ describe('WebSocket', () => {
});

it('exposes the server url', () => {
const url = 'ws://localhost';
const ws = new WebSocket(url, { agent: new CustomAgent() });
const schemes = new Map([
['ws', 'ws'],
['wss', 'wss'],
['http', 'ws'],
['https', 'wss']
]);

assert.strictEqual(ws.url, url);
for (const [key, value] of schemes) {
const ws = new WebSocket(`${key}://localhost/`, { lookup() {} });

assert.strictEqual(ws.url, `${value}://localhost/`);
}
});
});
});
Expand Down Expand Up @@ -1174,7 +1206,9 @@ describe('WebSocket', () => {

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

const ws = new WebSocket(`ws://localhost:${server.address().port}`, {
Expand Down