Skip to content

Commit 6d31984

Browse files
authoredMay 7, 2019
fix(client-src): don't use self.location.port (#1838)
ISSUE: #1777 REF: #1792 REF: #1664
1 parent 34058a6 commit 6d31984

File tree

2 files changed

+29
-22
lines changed

2 files changed

+29
-22
lines changed
 

‎client-src/default/index.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,6 @@ const onSocketMsg = {
198198

199199
let hostname = urlParts.hostname;
200200
let protocol = urlParts.protocol;
201-
let port = urlParts.port;
202201

203202
// check ipv4 and ipv6 `all hostname`
204203
if (hostname === '0.0.0.0' || hostname === '::') {
@@ -208,7 +207,6 @@ if (hostname === '0.0.0.0' || hostname === '::') {
208207
// eslint-disable-next-line no-bitwise
209208
if (self.location.hostname && !!~self.location.protocol.indexOf('http')) {
210209
hostname = self.location.hostname;
211-
port = self.location.port;
212210
}
213211
}
214212

@@ -228,8 +226,8 @@ const socketUrl = url.format({
228226
hostname,
229227
port:
230228
urlParts.path == null || urlParts.path === '/'
231-
? port
232-
: querystring.parse(urlParts.path).sockPort || port,
229+
? urlParts.port
230+
: querystring.parse(urlParts.path).sockPort || urlParts.port,
233231
// If sockPath is provided it'll be passed in via the __resourceQuery as a
234232
// query param so it has to be parsed out of the querystring in order for the
235233
// client to open the socket to the correct location.

‎test/Client.test.js

+27-18
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,20 @@ const helper = require('./helper');
77
const config = require('./fixtures/client-config/webpack.config');
88
const runBrowser = require('./helpers/run-browser');
99

10-
function startProxy(port) {
11-
const proxy = express();
12-
proxy.use(
13-
'/',
14-
httpProxy({
15-
target: 'http://localhost:9001',
16-
ws: true,
17-
changeOrigin: true,
18-
})
19-
);
20-
return proxy.listen(port);
21-
}
22-
2310
describe('Client code', () => {
11+
function startProxy(port) {
12+
const proxy = express();
13+
proxy.use(
14+
'/',
15+
httpProxy({
16+
target: 'http://localhost:9001',
17+
ws: true,
18+
changeOrigin: true,
19+
})
20+
);
21+
return proxy.listen(port);
22+
}
23+
2424
beforeAll((done) => {
2525
const options = {
2626
compress: true,
@@ -38,6 +38,7 @@ describe('Client code', () => {
3838

3939
afterAll(helper.close);
4040

41+
// [HPM] Proxy created: / -> http://localhost:9001
4142
describe('behind a proxy', () => {
4243
let proxy;
4344

@@ -47,13 +48,21 @@ describe('Client code', () => {
4748
proxy = startProxy(9000);
4849
});
4950

50-
afterAll(() => {
51-
proxy.close();
51+
afterAll((done) => {
52+
proxy.close(() => {
53+
done();
54+
});
5255
});
5356

5457
it('responds with a 200', (done) => {
55-
const req = request('http://localhost:9000');
56-
req.get('/sockjs-node').expect(200, 'Welcome to SockJS!\n', done);
58+
{
59+
const req = request('http://localhost:9000');
60+
req.get('/sockjs-node').expect(200, 'Welcome to SockJS!\n', done);
61+
}
62+
{
63+
const req = request('http://localhost:9001');
64+
req.get('/sockjs-node').expect(200, 'Welcome to SockJS!\n', done);
65+
}
5766
});
5867

5968
it('requests websocket through the proxy with proper port number', (done) => {
@@ -62,7 +71,7 @@ describe('Client code', () => {
6271
.waitForRequest((requestObj) => requestObj.url().match(/sockjs-node/))
6372
.then((requestObj) => {
6473
expect(requestObj.url()).toMatch(
65-
/^http:\/\/localhost:9000\/sockjs-node/
74+
/^http:\/\/localhost:9001\/sockjs-node/
6675
);
6776
browser.close().then(done);
6877
});

0 commit comments

Comments
 (0)
Please sign in to comment.