Skip to content

Commit

Permalink
style: update
Browse files Browse the repository at this point in the history
  • Loading branch information
zkochan committed Jan 18, 2024
1 parent aea8ad2 commit d57f791
Show file tree
Hide file tree
Showing 3 changed files with 239 additions and 234 deletions.
238 changes: 119 additions & 119 deletions network/agent/agent.spec.ts
Original file line number Diff line number Diff line change
@@ -1,134 +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,
};
};
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",
},
},
});
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",
});
});
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",
},
},
});
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",
});
});
expect(agent).toEqual({
localAddress: undefined,
maxSockets: 50,
rejectUnauthorized: undefined,
timeout: 0,
__type: 'https',
})
})
Loading

0 comments on commit d57f791

Please sign in to comment.