/
ClientOptions.test.js
293 lines (269 loc) · 7.31 KB
/
ClientOptions.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
'use strict';
const express = require('express');
const httpProxy = require('http-proxy-middleware');
const request = require('supertest');
const testServer = require('../helpers/test-server');
const config = require('../fixtures/client-config/webpack.config');
const runBrowser = require('../helpers/run-browser');
describe('Client code', () => {
function startProxy(port) {
const proxy = express();
proxy.use(
'/',
httpProxy({
target: 'http://localhost:9001',
ws: true,
changeOrigin: true,
})
);
return proxy.listen(port);
}
beforeAll((done) => {
const options = {
compress: true,
port: 9001,
host: '0.0.0.0',
disableHostCheck: true,
inline: true,
hot: true,
watchOptions: {
poll: true,
},
};
testServer.startAwaitingCompilation(config, options, done);
});
afterAll(testServer.close);
// [HPM] Proxy created: / -> http://localhost:9001
describe('behind a proxy', () => {
let proxy;
beforeAll(() => {
proxy = startProxy(9000);
});
afterAll((done) => {
proxy.close(() => {
done();
});
});
it('responds with a 200', (done) => {
{
const req = request('http://localhost:9000');
req.get('/sockjs-node').expect(200, 'Welcome to SockJS!\n', done);
}
{
const req = request('http://localhost:9001');
req.get('/sockjs-node').expect(200, 'Welcome to SockJS!\n', done);
}
});
it('requests websocket through the proxy with proper port number', (done) => {
runBrowser().then(({ page, browser }) => {
page
.waitForRequest((requestObj) => requestObj.url().match(/sockjs-node/))
.then((requestObj) => {
expect(requestObj.url()).toMatch(
/^http:\/\/localhost:9001\/sockjs-node/
);
browser.close().then(done);
});
page.goto('http://localhost:9000/main');
});
});
});
});
describe('Client complex inline script path', () => {
beforeAll((done) => {
const options = {
port: 9000,
host: '0.0.0.0',
inline: true,
watchOptions: {
poll: true,
},
public: 'myhost.test',
sockPath: '/foo/test/bar/',
};
testServer.startAwaitingCompilation(config, options, done);
});
afterAll(testServer.close);
describe('browser client', () => {
it('uses the correct public hostname and sockPath', (done) => {
runBrowser().then(({ page, browser }) => {
page
.waitForRequest((requestObj) =>
requestObj.url().match(/foo\/test\/bar/)
)
.then((requestObj) => {
expect(requestObj.url()).toMatch(
/^http:\/\/myhost\.test:9000\/foo\/test\/bar/
);
browser.close().then(done);
});
page.goto('http://localhost:9000/main');
});
});
});
});
describe('Client complex inline script path with sockPort', () => {
beforeAll((done) => {
const options = {
port: 9000,
host: '0.0.0.0',
inline: true,
watchOptions: {
poll: true,
},
sockPath: '/foo/test/bar/',
sockPort: 8080,
};
testServer.startAwaitingCompilation(config, options, done);
});
afterAll(testServer.close);
describe('browser client', () => {
it('uses the correct sockPort', (done) => {
runBrowser().then(({ page, browser }) => {
page
.waitForRequest((requestObj) =>
requestObj.url().match(/foo\/test\/bar/)
)
.then((requestObj) => {
expect(requestObj.url()).toMatch(
/^http:\/\/localhost:8080\/foo\/test\/bar/
);
browser.close().then(done);
});
page.goto('http://localhost:9000/main');
});
});
});
});
// previously, using sockPort without sockPath had the ability
// to alter the sockPath (based on a bug in client-src/index.js)
// so we need to make sure sockPath is not altered in this case
describe('Client complex inline script path with sockPort, no sockPath', () => {
beforeAll((done) => {
const options = {
port: 9000,
host: '0.0.0.0',
inline: true,
watchOptions: {
poll: true,
},
sockPort: 8080,
};
testServer.startAwaitingCompilation(config, options, done);
});
afterAll(testServer.close);
describe('browser client', () => {
it('uses the correct sockPort and sockPath', (done) => {
runBrowser().then(({ page, browser }) => {
page
.waitForRequest((requestObj) => requestObj.url().match(/sockjs-node/))
.then((requestObj) => {
expect(requestObj.url()).toMatch(
/^http:\/\/localhost:8080\/sockjs-node/
);
browser.close().then(done);
});
page.goto('http://localhost:9000/main');
});
});
});
});
describe('Client complex inline script path with sockHost', () => {
beforeAll((done) => {
const options = {
port: 9000,
host: '0.0.0.0',
inline: true,
watchOptions: {
poll: true,
},
sockHost: 'myhost.test',
};
testServer.startAwaitingCompilation(config, options, done);
});
afterAll(testServer.close);
describe('browser client', () => {
it('uses the correct sockHost', (done) => {
runBrowser().then(({ page, browser }) => {
page
.waitForRequest((requestObj) => requestObj.url().match(/sockjs-node/))
.then((requestObj) => {
expect(requestObj.url()).toMatch(
/^http:\/\/myhost\.test:9000\/sockjs-node/
);
browser.close().then(done);
});
page.goto('http://localhost:9000/main');
});
});
});
});
describe('Client console.log', () => {
const baseOptions = {
port: 9000,
host: '0.0.0.0',
};
const cases = [
{
title: 'hot disabled',
options: {
hot: false,
},
},
{
title: 'hot enabled',
options: {
hot: true,
},
},
{
title: 'liveReload disabled',
options: {
liveReload: false,
},
},
{
title: 'liveReload enabled',
options: {
liveReload: true,
},
},
{
title: 'clientLogLevel is silent',
options: {
clientLogLevel: 'silent',
},
},
];
for (const { title, options } of cases) {
it(title, () => {
const res = [];
const testOptions = Object.assign({}, baseOptions, options);
// TODO: use async/await when Node.js v6 support is dropped
return Promise.resolve()
.then(() => {
return new Promise((resolve) => {
testServer.startAwaitingCompilation(config, testOptions, resolve);
});
})
.then(runBrowser)
.then(({ page, browser }) => {
return new Promise((resolve) => {
page.goto('http://localhost:9000/main');
page.on('console', ({ _text }) => {
res.push(_text);
});
setTimeout(() => {
expect(res).toMatchSnapshot();
browser.close().then(resolve);
}, 3000);
});
})
.then(() => {
return new Promise((resolve) => {
testServer.close(resolve);
});
});
});
}
});