Skip to content

Commit f4aeac3

Browse files
author
Jonathan Ginsburg
authoredJun 1, 2021
fix(server): clean up vestigial code from proxy (#3640)
When using the proxy feature of Karma, the target value can include the [scheme](https://tools.ietf.org/html/std66#section-3.1). It was used to determine the `https` variable to be sent to the [`http-proxy`](https://www.npmjs.com/package/http-proxy) `.createProxyServer` method. However, it is now disregarded by that package. This change cleans it up.
1 parent 94cf15e commit f4aeac3

File tree

2 files changed

+41
-37
lines changed

2 files changed

+41
-37
lines changed
 

‎lib/middleware/proxy.js

+7-11
Original file line numberDiff line numberDiff line change
@@ -34,20 +34,16 @@ function parseProxyConfig (proxies, config) {
3434

3535
const hostname = proxyDetails.hostname || config.hostname
3636
const protocol = proxyDetails.protocol || config.protocol
37-
const https = proxyDetails.protocol === 'https:'
38-
let port
39-
if (proxyDetails.port) {
40-
port = proxyDetails.port
41-
} else if (proxyDetails.protocol) {
42-
port = https ? '443' : '80'
43-
} else {
44-
port = config.port
37+
const defaultPorts = {
38+
'http:': '80',
39+
'https:': '443'
4540
}
41+
const port = proxyDetails.port || defaultPorts[proxyDetails.protocol] || config.port
4642
const changeOrigin = proxyConfiguration.changeOrigin || false
47-
const Agent = https ? httpsAgent : httpAgent
43+
const Agent = protocol === 'https:' ? httpsAgent : httpAgent
4844
const agent = new Agent({ keepAlive: true })
4945
const proxy = httpProxy.createProxyServer({
50-
target: { host: hostname, port, https, protocol },
46+
target: { host: hostname, port, protocol },
5147
xfwd: true,
5248
changeOrigin: changeOrigin,
5349
secure: config.proxyValidateSSL,
@@ -71,7 +67,7 @@ function parseProxyConfig (proxies, config) {
7167
res.destroy()
7268
})
7369

74-
return { path: proxyPath, baseUrl: pathname, host: hostname, port, https, proxy, agent }
70+
return { path: proxyPath, baseUrl: pathname, host: hostname, port, proxy, agent }
7571
}), 'path').reverse()
7672
}
7773

‎test/unit/middleware/proxy.spec.js

+34-26
Original file line numberDiff line numberDiff line change
@@ -146,10 +146,16 @@ describe('middleware.proxy', () => {
146146
host: 'localhost',
147147
port: '8000',
148148
baseUrl: '/',
149-
path: '/base/',
150-
https: false
149+
path: '/base/'
151150
})
152151
expect(parsedProxyConfig[0].proxy).to.exist
152+
expect(parsedProxyConfig[0].proxy).to.containSubset({
153+
options: {
154+
target: {
155+
protocol: 'http:'
156+
}
157+
}
158+
})
153159
})
154160

155161
it('should set default http port', () => {
@@ -160,10 +166,16 @@ describe('middleware.proxy', () => {
160166
host: 'localhost',
161167
port: '80',
162168
baseUrl: '/',
163-
path: '/base/',
164-
https: false
169+
path: '/base/'
165170
})
166171
expect(parsedProxyConfig[0].proxy).to.exist
172+
expect(parsedProxyConfig[0].proxy).to.containSubset({
173+
options: {
174+
target: {
175+
protocol: 'http:'
176+
}
177+
}
178+
})
167179
})
168180

169181
it('should set default https port', () => {
@@ -174,8 +186,7 @@ describe('middleware.proxy', () => {
174186
host: 'localhost',
175187
port: '443',
176188
baseUrl: '/',
177-
path: '/base/',
178-
https: true
189+
path: '/base/'
179190
})
180191
expect(parsedProxyConfig[0].proxy).to.exist
181192
expect(parsedProxyConfig[0].proxy).to.containSubset({
@@ -195,10 +206,16 @@ describe('middleware.proxy', () => {
195206
host: 'localhost',
196207
port: '8000',
197208
baseUrl: '/proxy',
198-
path: '/base',
199-
https: false
209+
path: '/base'
200210
})
201211
expect(parsedProxyConfig[0].proxy).to.exist
212+
expect(parsedProxyConfig[0].proxy).to.containSubset({
213+
options: {
214+
target: {
215+
protocol: 'http:'
216+
}
217+
}
218+
})
202219
})
203220

204221
it('should determine protocol', () => {
@@ -209,8 +226,7 @@ describe('middleware.proxy', () => {
209226
host: 'localhost',
210227
port: '8000',
211228
baseUrl: '',
212-
path: '/base',
213-
https: true
229+
path: '/base'
214230
})
215231
expect(parsedProxyConfig[0].proxy).to.exist
216232
expect(parsedProxyConfig[0].proxy).to.containSubset({
@@ -231,8 +247,7 @@ describe('middleware.proxy', () => {
231247
host: 'localhost',
232248
port: 9877,
233249
baseUrl: '/proxy/test',
234-
path: '/base',
235-
https: false
250+
path: '/base'
236251
})
237252
expect(parsedProxyConfig[0].proxy).to.exist
238253
})
@@ -246,8 +261,7 @@ describe('middleware.proxy', () => {
246261
host: 'localhost',
247262
port: 9877,
248263
baseUrl: '/proxy/test/',
249-
path: '/base/',
250-
https: false
264+
path: '/base/'
251265
})
252266
expect(parsedProxyConfig[0].proxy).to.exist
253267
})
@@ -261,8 +275,7 @@ describe('middleware.proxy', () => {
261275
host: 'krinkle.dev',
262276
port: '80',
263277
baseUrl: '/w',
264-
path: '/w',
265-
https: false
278+
path: '/w'
266279
})
267280
expect(parsedProxyConfig[0].proxy).to.exist
268281
})
@@ -276,8 +289,7 @@ describe('middleware.proxy', () => {
276289
host: 'krinkle.dev',
277290
port: '443',
278291
baseUrl: '/w',
279-
path: '/w',
280-
https: true
292+
path: '/w'
281293
})
282294
expect(parsedProxyConfig[0].proxy).to.exist
283295
})
@@ -290,8 +302,7 @@ describe('middleware.proxy', () => {
290302
host: 'localhost',
291303
port: '8000',
292304
baseUrl: '/proxy/test/',
293-
path: '/base/',
294-
https: false
305+
path: '/base/'
295306
})
296307
expect(parsedProxyConfig[0].proxy).to.exist
297308
})
@@ -307,16 +318,14 @@ describe('middleware.proxy', () => {
307318
host: 'gstatic.com',
308319
port: '80',
309320
baseUrl: '/something',
310-
path: '/sub/some',
311-
https: false
321+
path: '/sub/some'
312322
})
313323
expect(parsedProxyConfig[0].proxy).to.exist
314324
expect(parsedProxyConfig[1]).to.containSubset({
315325
host: 'localhost',
316326
port: '9000',
317327
baseUrl: '',
318-
path: '/sub',
319-
https: false
328+
path: '/sub'
320329
})
321330
expect(parsedProxyConfig[1].proxy).to.exist
322331
})
@@ -331,8 +340,7 @@ describe('middleware.proxy', () => {
331340
host: 'localhost',
332341
port: '8000',
333342
baseUrl: '/',
334-
path: '/base/',
335-
https: false
343+
path: '/base/'
336344
})
337345
expect(parsedProxyConfig[0].proxy).to.exist
338346
})

0 commit comments

Comments
 (0)
Please sign in to comment.