/
createSocketUrl.test.js
152 lines (139 loc) · 4.03 KB
/
createSocketUrl.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
'use strict';
describe('createSocketUrl', () => {
const samples = [
'?test',
'https://example.com',
'https://example.com/path',
'https://example.com/path/foo.js',
'http://user:password@localhost/',
'http://0.0.0.0',
'https://localhost:123',
'http://user:pass@[::]:8080',
'http://127.0.0.1',
// TODO: comment out after the major release
// https://github.com/webpack/webpack-dev-server/pull/1954#issuecomment-498043376
// 'file://filename',
// eslint-disable-next-line no-undefined
undefined,
];
samples.forEach((url) => {
jest.doMock(
'../../../client-src/default/utils/getCurrentScriptSource.js',
() => () => url
);
const createSocketUrl = require('../../../client-src/default/utils/createSocketUrl');
test(`should return the url when __resourceQuery is ${url}`, () => {
const query = url ? url.querystring : url;
expect(createSocketUrl(query)).toMatchSnapshot();
});
test(`should return the url when the current script source is ${url}`, () => {
expect(createSocketUrl()).toMatchSnapshot();
});
// put here because resetModules mustn't be reset when L20 is finished
jest.resetModules();
});
const samples2 = [
// script source, location, output socket URL
[
'http://example.com',
'https://something.com',
'https://example.com/sockjs-node',
],
[
'http://127.0.0.1',
'https://something.com',
'http://127.0.0.1/sockjs-node',
],
[
'http://0.0.0.0',
'https://something.com',
'https://something.com/sockjs-node',
],
[
'http://0.0.0.0',
'http://something.com',
'http://something.com/sockjs-node',
],
[
'http://example.com',
'http://something.com',
'http://example.com/sockjs-node',
],
[
'https://example.com',
'http://something.com',
'https://example.com/sockjs-node',
],
];
samples2.forEach(([scriptSrc, loc, expected]) => {
jest.doMock(
'../../../client-src/default/utils/getCurrentScriptSource.js',
() => () => scriptSrc
);
const createSocketUrl = require('../../../client-src/default/utils/createSocketUrl');
test(`should return socket ${expected} for script source ${scriptSrc} and location ${loc}`, () => {
// eslint-disable-next-line no-undefined
expect(createSocketUrl(undefined, loc).toString()).toEqual(expected);
});
jest.resetModules();
});
const samples3 = [
// script source, location, output socket URL
[
'?http://example.com',
'https://something.com',
'https://example.com/sockjs-node',
],
[
'?http://127.0.0.1',
'https://something.com',
'http://127.0.0.1/sockjs-node',
],
[
'?http://0.0.0.0',
'https://something.com',
'https://something.com/sockjs-node',
],
[
'?http://0.0.0.0',
'http://something.com',
'http://something.com/sockjs-node',
],
[
'?http://example.com',
'http://something.com',
'http://example.com/sockjs-node',
],
[
'?https://example.com',
'http://something.com',
'https://example.com/sockjs-node',
],
[
'?https://example.com?sockHost=asdf',
'http://something.com',
'https://asdf/sockjs-node',
],
[
'?https://example.com?sockPort=34',
'http://something.com',
'https://example.com:34/sockjs-node',
],
[
'?https://example.com?sockPath=xxx',
'http://something.com',
'https://example.com/xxx',
],
[
'?http://0.0.0.0:8096&sockPort=8097',
'http://localhost',
'http://localhost:8097/sockjs-node',
],
];
samples3.forEach(([scriptSrc, loc, expected]) => {
test(`should return socket ${expected} for query ${scriptSrc} and location ${loc}`, () => {
const createSocketUrl = require('../../../client-src/default/utils/createSocketUrl');
expect(createSocketUrl(scriptSrc, loc).toString()).toEqual(expected);
});
});
});