feat: promisify session proxy methods (#17222)
This commit is contained in:
parent
34fb6c2f35
commit
2769e75b49
7 changed files with 193 additions and 25 deletions
|
@ -633,7 +633,7 @@ describe('session module', () => {
|
|||
})
|
||||
})
|
||||
|
||||
describe('ses.setProxy(options, callback)', () => {
|
||||
describe('ses.setProxy(options)', () => {
|
||||
let server = null
|
||||
let customSession = null
|
||||
|
||||
|
@ -655,30 +655,78 @@ describe('session module', () => {
|
|||
}
|
||||
})
|
||||
|
||||
it('allows configuring proxy settings', (done) => {
|
||||
it('allows configuring proxy settings', async () => {
|
||||
const config = { proxyRules: 'http=myproxy:80' }
|
||||
await customSession.setProxy(config)
|
||||
const proxy = await customSession.resolveProxy('http://example.com/')
|
||||
assert.strictEqual(proxy, 'PROXY myproxy:80')
|
||||
})
|
||||
|
||||
// TODO(codebytere): remove when Promisification is complete
|
||||
it('allows configuring proxy settings (callback)', (done) => {
|
||||
const config = { proxyRules: 'http=myproxy:80' }
|
||||
customSession.setProxy(config, () => {
|
||||
customSession.resolveProxy('http://example.com/', (proxy) => {
|
||||
customSession.resolveProxy('http://example.com/', proxy => {
|
||||
assert.strictEqual(proxy, 'PROXY myproxy:80')
|
||||
done()
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
it('allows removing the implicit bypass rules for localhost', (done) => {
|
||||
it('allows removing the implicit bypass rules for localhost', async () => {
|
||||
const config = {
|
||||
proxyRules: 'http=myproxy:80',
|
||||
proxyBypassRules: '<-loopback>'
|
||||
}
|
||||
customSession.setProxy(config, () => {
|
||||
customSession.resolveProxy('http://localhost', (proxy) => {
|
||||
|
||||
await customSession.setProxy(config)
|
||||
const proxy = await customSession.resolveProxy('http://localhost')
|
||||
assert.strictEqual(proxy, 'PROXY myproxy:80')
|
||||
})
|
||||
|
||||
// TODO(codebytere): remove when Promisification is complete
|
||||
it('allows removing the implicit bypass rules for localhost (callback)', (done) => {
|
||||
const config = {
|
||||
proxyRules: 'http=myproxy:80',
|
||||
proxyBypassRules: '<-loopback>'
|
||||
}
|
||||
customSession.setProxy(config).then(() => {
|
||||
customSession.resolveProxy('http://localhost').then(proxy => {
|
||||
assert.strictEqual(proxy, 'PROXY myproxy:80')
|
||||
done()
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
it('allows configuring proxy settings with pacScript', (done) => {
|
||||
it('allows configuring proxy settings with pacScript', async () => {
|
||||
server = http.createServer((req, res) => {
|
||||
const pac = `
|
||||
function FindProxyForURL(url, host) {
|
||||
return "PROXY myproxy:8132";
|
||||
}
|
||||
`
|
||||
res.writeHead(200, {
|
||||
'Content-Type': 'application/x-ns-proxy-autoconfig'
|
||||
})
|
||||
res.end(pac)
|
||||
})
|
||||
return new Promise((resolve, reject) => {
|
||||
server.listen(0, '127.0.0.1', async () => {
|
||||
try {
|
||||
const config = { pacScript: `http://127.0.0.1:${server.address().port}` }
|
||||
await customSession.setProxy(config)
|
||||
const proxy = await customSession.resolveProxy('https://google.com')
|
||||
assert.strictEqual(proxy, 'PROXY myproxy:8132')
|
||||
resolve()
|
||||
} catch (error) {
|
||||
reject(error)
|
||||
}
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
// TODO(codebytere): reconfigure when Promisification is complete
|
||||
it('allows configuring proxy settings with pacScript (callback)', (done) => {
|
||||
server = http.createServer((req, res) => {
|
||||
const pac = `
|
||||
function FindProxyForURL(url, host) {
|
||||
|
@ -693,7 +741,7 @@ describe('session module', () => {
|
|||
server.listen(0, '127.0.0.1', () => {
|
||||
const config = { pacScript: `http://127.0.0.1:${server.address().port}` }
|
||||
customSession.setProxy(config, () => {
|
||||
customSession.resolveProxy('https://google.com', (proxy) => {
|
||||
customSession.resolveProxy('https://google.com', proxy => {
|
||||
assert.strictEqual(proxy, 'PROXY myproxy:8132')
|
||||
done()
|
||||
})
|
||||
|
@ -701,13 +749,24 @@ describe('session module', () => {
|
|||
})
|
||||
})
|
||||
|
||||
it('allows bypassing proxy settings', (done) => {
|
||||
it('allows bypassing proxy settings', async () => {
|
||||
const config = {
|
||||
proxyRules: 'http=myproxy:80',
|
||||
proxyBypassRules: '<local>'
|
||||
}
|
||||
await customSession.setProxy(config)
|
||||
const proxy = await customSession.resolveProxy('http://example/')
|
||||
assert.strictEqual(proxy, 'DIRECT')
|
||||
})
|
||||
|
||||
// TODO(codebytere): remove when Promisification is complete
|
||||
it('allows bypassing proxy settings (callback)', (done) => {
|
||||
const config = {
|
||||
proxyRules: 'http=myproxy:80',
|
||||
proxyBypassRules: '<local>'
|
||||
}
|
||||
customSession.setProxy(config, () => {
|
||||
customSession.resolveProxy('http://example/', (proxy) => {
|
||||
customSession.resolveProxy('http://example/', proxy => {
|
||||
assert.strictEqual(proxy, 'DIRECT')
|
||||
done()
|
||||
})
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue