-
Notifications
You must be signed in to change notification settings - Fork 5
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
b8b6bdb
commit 2cfe141
Showing
1 changed file
with
123 additions
and
80 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 |
---|---|---|
@@ -1,91 +1,134 @@ | ||
import { getAgent } from './agent' | ||
import { getAgent } from "./agent"; | ||
|
||
jest.mock('agentkeepalive', () => { | ||
const MockHttp = mockHttpAgent('http') | ||
MockHttp['HttpsAgent'] = mockHttpAgent('https') | ||
return MockHttp | ||
}) | ||
jest.mock('https-proxy-agent', () => mockHttpAgent('https-proxy')) | ||
jest.mock("agentkeepalive", () => { | ||
const MockHttp = mockHttpAgent("http"); | ||
MockHttp["HttpsAgent"] = mockHttpAgent("https"); | ||
return MockHttp; | ||
}); | ||
jest.mock("https-proxy-agent", () => mockHttpAgent("https-proxy")); | ||
|
||
function mockHttpAgent (type: string) { | ||
return function Agent (opts: any) { // eslint-disable-line @typescript-eslint/no-explicit-any | ||
return { | ||
...opts, | ||
__type: type, | ||
} | ||
} | ||
function mockHttpAgent(type: string) { | ||
return function Agent(opts: any) { | ||
// eslint-disable-line @typescript-eslint/no-explicit-any | ||
return { | ||
...opts, | ||
__type: type, | ||
}; | ||
}; | ||
} | ||
|
||
const OPTS = { | ||
agent: null, | ||
ca: 'ca', | ||
cert: 'cert', | ||
key: 'key', | ||
localAddress: 'localAddress', | ||
maxSockets: 5, | ||
strictSsl: true, | ||
timeout: 5, | ||
} | ||
agent: null, | ||
ca: "ca", | ||
cert: "cert", | ||
key: "key", | ||
localAddress: "localAddress", | ||
maxSockets: 5, | ||
strictSsl: true, | ||
timeout: 5, | ||
}; | ||
|
||
test('all expected options passed down to HttpAgent', () => { | ||
expect(getAgent('http://foo.com/bar', OPTS)).toEqual({ | ||
__type: 'http', | ||
localAddress: 'localAddress', | ||
maxSockets: 5, | ||
timeout: 6, | ||
}) | ||
}) | ||
test("all expected options passed down to HttpAgent", () => { | ||
expect(getAgent("http://foo.com/bar", OPTS)).toEqual({ | ||
__type: "http", | ||
localAddress: "localAddress", | ||
maxSockets: 5, | ||
timeout: 6, | ||
}); | ||
}); | ||
|
||
test('all expected options passed down to HttpsAgent', () => { | ||
expect(getAgent('https://foo.com/bar', OPTS)).toEqual({ | ||
__type: 'https', | ||
ca: 'ca', | ||
cert: 'cert', | ||
key: 'key', | ||
localAddress: 'localAddress', | ||
maxSockets: 5, | ||
rejectUnauthorized: true, | ||
timeout: 6, | ||
}) | ||
}) | ||
test("all expected options passed down to HttpsAgent", () => { | ||
expect(getAgent("https://foo.com/bar", OPTS)).toEqual({ | ||
__type: "https", | ||
ca: "ca", | ||
cert: "cert", | ||
key: "key", | ||
localAddress: "localAddress", | ||
maxSockets: 5, | ||
rejectUnauthorized: true, | ||
timeout: 6, | ||
}); | ||
}); | ||
|
||
test('all expected options passed down to proxy agent', () => { | ||
const opts = { | ||
httpsProxy: 'https://user:[email protected]:1234/foo', | ||
noProxy: 'qar.com, bar.com', | ||
...OPTS, | ||
} | ||
expect((getAgent('https://foo.com/bar', opts) as any).proxy).toEqual({ | ||
ALPNProtocols: ['http 1.1'], | ||
auth: 'user:pass', | ||
ca: 'ca', | ||
cert: 'cert', | ||
host: 'my.proxy', | ||
key: 'key', | ||
localAddress: 'localAddress', | ||
maxSockets: 5, | ||
port: 1234, | ||
protocol: 'https:', | ||
rejectUnauthorized: true, | ||
timeout: 6, | ||
}) | ||
}) | ||
test("all expected options passed down to proxy agent", () => { | ||
const opts = { | ||
httpsProxy: "https://user:[email protected]:1234/foo", | ||
noProxy: "qar.com, bar.com", | ||
...OPTS, | ||
}; | ||
expect((getAgent("https://foo.com/bar", opts) as any).proxy).toEqual({ | ||
ALPNProtocols: ["http 1.1"], | ||
auth: "user:pass", | ||
ca: "ca", | ||
cert: "cert", | ||
host: "my.proxy", | ||
key: "key", | ||
localAddress: "localAddress", | ||
maxSockets: 5, | ||
port: 1234, | ||
protocol: "https:", | ||
rejectUnauthorized: true, | ||
timeout: 6, | ||
}); | ||
}); | ||
|
||
test("don't use a proxy when the URL is in noProxy", () => { | ||
const opts = { | ||
httpsProxy: 'https://user:[email protected]:1234/foo', | ||
noProxy: 'foo.com, bar.com', | ||
...OPTS, | ||
} | ||
expect(getAgent('https://foo.com/bar', opts)).toEqual({ | ||
__type: 'https', | ||
ca: 'ca', | ||
cert: 'cert', | ||
key: 'key', | ||
localAddress: 'localAddress', | ||
maxSockets: 5, | ||
rejectUnauthorized: true, | ||
timeout: 6, | ||
}) | ||
}) | ||
const opts = { | ||
httpsProxy: "https://user:[email protected]:1234/foo", | ||
noProxy: "foo.com, bar.com", | ||
...OPTS, | ||
}; | ||
expect(getAgent("https://foo.com/bar", opts)).toEqual({ | ||
__type: "https", | ||
ca: "ca", | ||
cert: "cert", | ||
key: "key", | ||
localAddress: "localAddress", | ||
maxSockets: 5, | ||
rejectUnauthorized: true, | ||
timeout: 6, | ||
}); | ||
}); | ||
|
||
test("should return the correct client certificates", () => { | ||
const agent = getAgent("https://foo.com/bar", { | ||
clientCertificates: { | ||
"//foo.com/": { | ||
ca: "ca", | ||
cert: "cert", | ||
key: "key", | ||
}, | ||
}, | ||
}); | ||
|
||
expect(agent).toEqual({ | ||
ca: "ca", | ||
cert: "cert", | ||
key: "key", | ||
localAddress: undefined, | ||
maxSockets: 50, | ||
rejectUnauthorized: undefined, | ||
timeout: 0, | ||
__type: "https", | ||
}); | ||
}); | ||
|
||
test("should not return client certificates for a different host", () => { | ||
const agent = getAgent("https://foo.com/bar", { | ||
clientCertificates: { | ||
"//bar.com/": { | ||
ca: "ca", | ||
cert: "cert", | ||
key: "key", | ||
}, | ||
}, | ||
}); | ||
|
||
expect(agent).toEqual({ | ||
localAddress: undefined, | ||
maxSockets: 50, | ||
rejectUnauthorized: undefined, | ||
timeout: 0, | ||
__type: "https", | ||
}); | ||
}); |