Skip to content

Commit e51d258

Browse files
test: fix
1 parent 222cdfc commit e51d258

File tree

4 files changed

+115
-199
lines changed

4 files changed

+115
-199
lines changed

client-src/utils/createSocketUrl.js

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ function createSocketUrl(parsedURL) {
66
const { auth, query } = parsedURL;
77
let { hostname, protocol, port } = parsedURL;
88

9-
const isInaddrAny = hostname === '0.0.0.0' || hostname === '::';
10-
119
if (!port || port === '0') {
1210
port = self.location.port;
1311
}
1412

13+
const isInaddrAny = hostname === '0.0.0.0' || hostname === '::';
14+
1515
// check ipv4 and ipv6 `all hostname`
1616
// why do we need this check?
1717
// hostname n/a for file protocol (example, when using electron, ionic)
@@ -39,30 +39,22 @@ function createSocketUrl(parsedURL) {
3939
// all of these sock url params are optionally passed in through
4040
// resourceQuery, so we need to fall back to the default if
4141
// they are not provided
42-
let host = query.host || hostname;
43-
const path = query.path || '/ws';
44-
let portOption = query.port || port;
45-
46-
if (portOption === 'location') {
47-
portOption = self.location.port;
48-
}
49-
50-
// In case the host is a raw IPv6 address, it can be enclosed in
51-
// the brackets as the brackets are needed in the final URL string.
52-
// Need to remove those as url.format blindly adds its own set of brackets
53-
// if the host string contains colons. That would lead to non-working
54-
// double brackets (e.g. [[::]]) host
55-
host = typeof host === 'string' ? host.replace(/^\[(.*)\]$/, '$1') : host;
42+
const host = query.host || hostname;
5643

5744
return url.format({
5845
protocol,
5946
auth,
60-
hostname: host,
61-
port: portOption,
47+
// In case the host is a raw IPv6 address, it can be enclosed in
48+
// the brackets as the brackets are needed in the final URL string.
49+
// Need to remove those as url.format blindly adds its own set of brackets
50+
// if the host string contains colons. That would lead to non-working
51+
// double brackets (e.g. [[::]]) host
52+
hostname: host.replace(/^\[(.*)\]$/, '$1'),
53+
port: query.port || port,
6254
// If path is provided it'll be passed in via the resourceQuery as a
6355
// query param so it has to be parsed out of the querystring in order for the
6456
// client to open the socket to the correct location.
65-
pathname: path,
57+
pathname: query.path || '/ws',
6658
});
6759
}
6860

test/client/utils/__snapshots__/createSocketUrl.test.js.snap.webpack4

Lines changed: 0 additions & 45 deletions
This file was deleted.

test/client/utils/__snapshots__/createSocketUrl.test.js.snap.webpack5

Lines changed: 0 additions & 45 deletions
This file was deleted.

test/client/utils/createSocketUrl.test.js

Lines changed: 104 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -2,112 +2,126 @@
22

33
describe('createSocketUrl', () => {
44
const samples = [
5-
'?test',
6-
'https://example.com',
7-
'https://example.com/path',
8-
'https://example.com/path/foo.js',
9-
'http://user:password@localhost/',
10-
'http://0.0.0.0',
11-
'https://localhost:123',
12-
'http://user:pass@[::]:8080',
13-
'http://127.0.0.1',
14-
'file://filename',
15-
// eslint-disable-next-line no-undefined
16-
undefined,
17-
];
18-
19-
samples.forEach((url) => {
20-
jest.doMock('../../../client-src/utils/getCurrentScriptSource', () => () =>
21-
url
22-
);
23-
24-
const createSocketUrl = require('../../../client-src/utils/createSocketUrl');
25-
26-
test(`should return the url when __resourceQuery is ${url}`, () => {
27-
const query = url ? url.querystring : url;
28-
expect(createSocketUrl(query)).toMatchSnapshot();
29-
});
30-
31-
test(`should return the url when the current script source is ${url}`, () => {
32-
expect(createSocketUrl()).toMatchSnapshot();
33-
});
34-
35-
// put here because resetModules mustn't be reset when L20 is finished
36-
jest.resetModules();
37-
});
38-
39-
const samples2 = [
40-
// script source, location, output socket URL
41-
['http://example.com', 'https://something.com', 'https://example.com/ws'],
42-
['http://127.0.0.1', 'https://something.com', 'http://127.0.0.1/ws'],
43-
['http://0.0.0.0', 'https://something.com', 'https://something.com/ws'],
44-
['http://0.0.0.0', 'http://something.com', 'http://something.com/ws'],
45-
['http://[::]', 'https://something.com', 'https://something.com/ws'],
46-
['http://example.com', 'http://something.com', 'http://example.com/ws'],
47-
['https://example.com', 'http://something.com', 'https://example.com/ws'],
48-
];
49-
50-
samples2.forEach(([scriptSrc, loc, expected]) => {
51-
jest.doMock(
52-
'../../../client-src/utils/getCurrentScriptSource.js',
53-
() => () => scriptSrc
54-
);
55-
56-
const createSocketUrl = require('../../../client-src/utils/createSocketUrl');
57-
58-
test(`should return socket ${expected} for script source ${scriptSrc} and location ${loc}`, () => {
59-
// eslint-disable-next-line no-undefined
60-
expect(createSocketUrl(undefined, loc).toString()).toEqual(expected);
61-
});
62-
63-
jest.resetModules();
64-
});
65-
66-
const samples3 = [
67-
// script source, location, output socket URL
68-
['?http://example.com', 'https://something.com', 'https://example.com/ws'],
69-
['?http://127.0.0.1', 'https://something.com', 'http://127.0.0.1/ws'],
70-
['?http://0.0.0.0', 'https://something.com', 'https://something.com/ws'],
71-
['?http://0.0.0.0', 'http://something.com', 'http://something.com/ws'],
72-
['?http://[::]', 'https://something.com', 'https://something.com/ws'],
5+
// __resourceQuery, location and socket URL
6+
['?http://example.com', 'http://example.com', 'http://example.com/ws'],
737
['?http://example.com', 'http://something.com', 'http://example.com/ws'],
74-
['?https://example.com', 'http://something.com', 'https://example.com/ws'],
8+
[null, 'http://example.com', 'http://example.com/ws'],
9+
['?https://example.com', 'https://example.com', 'https://example.com/ws'],
10+
[null, 'https://example.com', 'https://example.com/ws'],
7511
[
76-
'?https://example.com?host=asdf',
77-
'http://something.com',
78-
'https://asdf/ws',
12+
'?http://example.com&port=8080',
13+
'http://example.com:8080',
14+
'http://example.com:8080/ws',
7915
],
8016
[
81-
'?https://example.com?port=34',
82-
'http://something.com',
83-
'https://example.com:34/ws',
17+
'?http://example.com:0',
18+
'http://example.com:8080',
19+
'http://example.com:8080/ws',
8420
],
21+
[null, 'http://example.com:8080', 'http://example.com:8080/ws'],
8522
[
86-
'?https://example.com?path=xxx',
87-
'http://something.com',
88-
'https://example.com/xxx',
23+
'?http://example.com/path/foo.js',
24+
'http://example.com/foo/bar',
25+
'http://example.com/ws',
26+
],
27+
[null, 'http://example.com/foo/bar', 'http://example.com/ws'],
28+
[
29+
'?http://user:password@localhost/',
30+
'http://user:password@localhost/',
31+
'http://user:password@localhost/ws',
8932
],
9033
[
91-
'?http://0.0.0.0:8096&port=8097',
92-
'http://localhost',
93-
'http://localhost:8097/ws',
34+
null,
35+
'http://user:password@localhost/',
36+
'http://user:password@localhost/ws',
9437
],
9538
[
96-
'?http://example.com:8096&port=location',
39+
'?http://user:password@localhost:8080/',
40+
'http://user:password@localhost/',
41+
'http://user:password@localhost:8080/ws',
42+
],
43+
[
44+
null,
45+
'http://user:password@localhost:8080/',
46+
'http://user:password@localhost:8080/ws',
47+
],
48+
['?http://0.0.0.0', 'http://127.0.0.1', 'http://127.0.0.1/ws'],
49+
['?http://0.0.0.0', 'http://192.168.0.1', 'http://192.168.0.1/ws'],
50+
['?http://0.0.0.0', 'https://192.168.0.1', 'https://192.168.0.1/ws'],
51+
['?http://0.0.0.0', 'https://example.com', 'https://example.com/ws'],
52+
[
53+
'?http://0.0.0.0',
54+
'https://example.com:8080',
55+
'https://example.com:8080/ws',
56+
],
57+
[
58+
'?http://0.0.0.0&port=9090',
59+
'https://example.com',
60+
'https://example.com:9090/ws',
61+
],
62+
[
63+
'?http://0.0.0.0&port=9090',
64+
'https://example.com:8080',
65+
'https://example.com:9090/ws',
66+
],
67+
['?http://localhost', 'http://localhost', 'http://localhost/ws'],
68+
[
69+
'?http://localhost:8080',
70+
'http://localhost:8080',
71+
'http://localhost:8080/ws',
72+
],
73+
[
74+
'?https://localhost:8080',
75+
'https://localhost:8080',
76+
'https://localhost:8080/ws',
77+
],
78+
[null, 'https://localhost:8080', 'https://localhost:8080/ws'],
79+
['?http://127.0.0.1', 'http://something.com', 'http://127.0.0.1/ws'],
80+
['?http://127.0.0.1', 'https://something.com', 'http://127.0.0.1/ws'],
81+
['?https://127.0.0.1', 'http://something.com', 'https://127.0.0.1/ws'],
82+
[
83+
'?https://example.com&host=example.com',
9784
'http://something.com',
98-
'http://example.com/ws',
85+
'https://example.com/ws',
9986
],
10087
[
101-
'?http://0.0.0.0:8096&port=location',
102-
'http://localhost:3000',
103-
'http://localhost:3000/ws',
88+
'?https://example.com&path=custom',
89+
'http://something.com',
90+
'https://example.com/custom',
91+
],
92+
[
93+
'?https://example.com&path=/custom',
94+
'http://something.com',
95+
'https://example.com/custom',
10496
],
97+
['?http://[::]', 'http://something.com', 'http://something.com/ws'],
98+
['?http://[::]', 'https://something.com', 'https://something.com/ws'],
99+
['?http://[::1]:8080/', 'http://[::1]:8080/', 'http://[::1]:8080/ws'],
100+
['?https://[::1]:8080/', 'http://[::1]:8080/', 'https://[::1]:8080/ws'],
101+
[null, 'http://[::1]:8080/', 'http://[::1]:8080/ws'],
102+
[null, 'https://[::1]:8080/', 'https://[::1]:8080/ws'],
105103
];
106-
samples3.forEach(([scriptSrc, loc, expected]) => {
107-
test(`should return socket ${expected} for query ${scriptSrc} and location ${loc}`, () => {
108-
const createSocketUrl = require('../../../client-src/utils/createSocketUrl');
109104

110-
expect(createSocketUrl(scriptSrc, loc).toString()).toEqual(expected);
105+
samples.forEach(([__resourceQuery, location, expected]) => {
106+
jest.doMock('../../../client-src/utils/getCurrentScriptSource', () => () =>
107+
location
108+
);
109+
110+
const createSocketUrl = require('../../../client-src/utils/createSocketUrl');
111+
const parseURL = require('../../../client-src/utils/parseURL');
112+
113+
test.only(`should return '${expected}' socket URL when '__resourceQuery' is '${__resourceQuery}' and 'self.location' is '${location}'`, () => {
114+
const selfLocation = new URL(location);
115+
116+
delete window.location;
117+
118+
window.location = selfLocation;
119+
120+
const parsedURL = parseURL(__resourceQuery);
121+
122+
expect(createSocketUrl(parsedURL)).toBe(expected);
111123
});
124+
125+
jest.resetModules();
112126
});
113127
});

0 commit comments

Comments
 (0)