Skip to content

Commit

Permalink
Fix proxy agent error (#89)
Browse files Browse the repository at this point in the history
  • Loading branch information
Enrique Gonzalez authored Jul 2, 2020
1 parent 2ccf570 commit d677522
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 26 deletions.
28 changes: 16 additions & 12 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,19 +146,23 @@ export function isValidType (option, expectedType) {
export function createProxyAgent (proxy) {
var proxyURL = url.parse(proxy)
if (proxyURL.protocol === 'https:') {
return tunnel.httpsOverHttps({
proxy: {
host: proxyURL.hostname,
port: proxyURL.port
}
})
return {
https: tunnel.httpsOverHttps({
proxy: {
host: proxyURL.hostname,
port: proxyURL.port
}
})
}
} else if (proxyURL.protocol === 'http:') {
return tunnel.httpsOverHttp({
proxy: {
host: proxyURL.hostname,
port: proxyURL.port
}
})
return {
https: tunnel.httpsOverHttp({
proxy: {
host: proxyURL.hostname,
port: proxyURL.port
}
})
}
}

throw new Error('Only http and https protocols are supported for proxying traffic.'
Expand Down
50 changes: 36 additions & 14 deletions tests/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,21 +87,43 @@ test('isValidType', () => {

describe('createProxyAgent', ()=> {
expect(createProxyAgent('http://my.proxy.com:8080'))
.toEqual(expect.objectContaining({
proxyOptions: {
host: 'my.proxy.com',
port: '8080'
},
request: http.request
}))
.toEqual({
https: {
_events: expect.anything(),
_eventsCount: 1,
createSocket: expect.anything(),
defaultPort: 443,
maxSockets: Infinity,
options:
{proxy: {host: 'my.proxy.com', 'port': '8080'}},
requests: [],
sockets: [],
proxyOptions: {
host: 'my.proxy.com',
port: '8080'
},
request: http.request
}
})
expect(createProxyAgent('https://my.proxy.com:443'))
.toEqual(expect.objectContaining({
proxyOptions: {
host: 'my.proxy.com',
port: '443'
},
request: https.request
}))
.toEqual({
https: {
_events: expect.anything(),
_eventsCount: 1,
createSocket: expect.anything(),
defaultPort: 443,
maxSockets: Infinity,
options:
{proxy: {host: 'my.proxy.com', 'port': '443'}},
requests: [],
sockets: [],
proxyOptions: {
host: 'my.proxy.com',
port: '443'
},
request: https.request
}
})
expect(() => {
createProxyAgent('ftp://my.proxy.com:21')
}).toThrowError(/Only http and https protocols are supported for proxying traffic./)
Expand Down

0 comments on commit d677522

Please sign in to comment.