Skip to content
This repository was archived by the owner on May 3, 2022. It is now read-only.

Commit 3b22cd0

Browse files
Merge pull request #42 from pravipati/fix-missing-proxy-auth
Fix missing proxy auth
2 parents edadda1 + 8815420 commit 3b22cd0

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

__tests__/proxy.test.ts

+21
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as http from 'http'
22
import * as httpm from '../_out'
33
import * as pm from '../_out/proxy'
44
import * as proxy from 'proxy'
5+
import * as tunnelm from 'tunnel'
56

67
let _proxyConnects: string[]
78
let _proxyServer: http.Server
@@ -195,6 +196,26 @@ describe('proxy', () => {
195196
expect(obj.url).toBe('https://httpbin.org/get')
196197
expect(_proxyConnects).toHaveLength(0)
197198
})
199+
200+
it('proxyAuth not set in tunnel agent when authentication is not provided', async () => {
201+
process.env['https_proxy'] = 'http://127.0.0.1:8080'
202+
const httpClient = new httpm.HttpClient()
203+
let agent: tunnelm.TunnelingAgent = httpClient.getAgent('https://some-url')
204+
console.log(agent)
205+
expect(agent.proxyOptions.host).toBe('127.0.0.1')
206+
expect(agent.proxyOptions.port).toBe('8080')
207+
expect(agent.proxyOptions.proxyAuth).toBe(undefined)
208+
})
209+
210+
it('proxyAuth is set in tunnel agent when authentication is provided', async () => {
211+
process.env['https_proxy'] = 'http://user:password@127.0.0.1:8080'
212+
const httpClient = new httpm.HttpClient()
213+
let agent: tunnelm.TunnelingAgent = httpClient.getAgent('https://some-url')
214+
console.log(agent)
215+
expect(agent.proxyOptions.host).toBe('127.0.0.1')
216+
expect(agent.proxyOptions.port).toBe('8080')
217+
expect(agent.proxyOptions.proxyAuth).toBe('user:password')
218+
})
198219
})
199220

200221
function _clearVars() {

index.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -642,7 +642,9 @@ export class HttpClient {
642642
maxSockets: maxSockets,
643643
keepAlive: this._keepAlive,
644644
proxy: {
645-
proxyAuth: `${proxyUrl.username}:${proxyUrl.password}`,
645+
...((proxyUrl.username || proxyUrl.password) && {
646+
proxyAuth: `${proxyUrl.username}:${proxyUrl.password}`
647+
}),
646648
host: proxyUrl.hostname,
647649
port: proxyUrl.port
648650
}

0 commit comments

Comments
 (0)