-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6b23cb5
commit ff0eb6a
Showing
1 changed file
with
83 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import { ProxyHttpAgent, ProxyHttpsAgent, createPacAgent, getProxy, proxyAgentFor } from '../../src/proxy.js'; | ||
import { PacProxyAgent } from 'pac-proxy-agent'; | ||
|
||
describe('proxy', () => { | ||
describe('getProxy', () => { | ||
beforeEach(async () => { | ||
process.env.http_proxy = 'http://proxy.com:8080'; | ||
}); | ||
|
||
it('should return proxy object if proxy is set', () => { | ||
const options = { protocol: 'http:', hostname: 'example.com' }; | ||
const proxy = getProxy(options); | ||
expect(proxy).toBeInstanceOf(Object); | ||
}); | ||
|
||
it('should return undefined if no proxy is set', () => { | ||
delete process.env.http_proxy; | ||
const options = { protocol: 'http:', hostname: 'example.com' }; | ||
expect(getProxy(options)).toBeUndefined(); | ||
}); | ||
}); | ||
|
||
describe('createPacAgent', () => { | ||
it('should create a PAC proxy agent successfully', () => { | ||
const pacUrl = 'http://example.com/proxy.pac'; | ||
const options = { keepAlive: true }; | ||
const agent = createPacAgent(pacUrl, options); | ||
expect(agent).toBeInstanceOf(PacProxyAgent); | ||
}); | ||
|
||
it('should throw an error if PAC proxy agent creation fails', () => { | ||
const pacUrl = 'http://invalid-url/proxy.pac'; | ||
const options = { keepAlive: true }; | ||
createPacAgent(pacUrl, options); | ||
expect(createPacAgent).toThrow(); | ||
}); | ||
}); | ||
|
||
describe('proxyAgentFor', () => { | ||
beforeEach(async () => { | ||
proxyAgentFor.cache?.clear(); | ||
process.env.PERCY_PAC_FILE_URL = 'http://example.com/proxy.pac'; | ||
}); | ||
|
||
afterEach(async () => { | ||
delete process.env.PERCY_PAC_FILE_URL; | ||
}); | ||
|
||
it('should return cached agent if available', () => { | ||
delete process.env.PERCY_PAC_FILE_URL; | ||
const url = 'http://example.com'; | ||
const options = {}; | ||
const agent = new ProxyHttpAgent(options); | ||
proxyAgentFor.cache.set('http://example.com', agent); | ||
expect(proxyAgentFor(url, options)).toBe(agent); | ||
}); | ||
|
||
it('should create and cache new HTTP agent if not available', () => { | ||
delete process.env.PERCY_PAC_FILE_URL; | ||
const url = 'http://example.com'; | ||
const options = {}; | ||
const agent = proxyAgentFor(url, options); | ||
expect(agent).toBeInstanceOf(ProxyHttpAgent); | ||
expect(proxyAgentFor.cache.get('http://example.com')).toBe(agent); | ||
}); | ||
|
||
it('should create and cache new HTTPS agent if not available', () => { | ||
delete process.env.PERCY_PAC_FILE_URL; | ||
const url = 'https://example.com'; | ||
const options = {}; | ||
const agent = proxyAgentFor(url, options); | ||
expect(agent).toBeInstanceOf(ProxyHttpsAgent); | ||
expect(proxyAgentFor.cache.get('https://example.com')).toBe(agent); | ||
}); | ||
|
||
it('should create PAC proxy agent if PAC URL is provided', () => { | ||
const url = 'http://example.com'; | ||
const options = {}; | ||
const agent = proxyAgentFor(url, options); | ||
expect(agent).toBeInstanceOf(PacProxyAgent); | ||
}); | ||
}); | ||
}); |