Skip to content

Commit

Permalink
[maint] Rewrites the ca tests to use the dispatcher and custom fetch (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
nickfloyd authored Jul 10, 2023
1 parent 0b4ad1e commit 94d8c75
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 18 deletions.
36 changes: 35 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@
"semantic-release": "^21.0.0",
"semantic-release-plugin-update-version-in-files": "^1.0.0",
"ts-jest": "^29.0.0",
"typescript": "^5.0.0"
"typescript": "^5.0.0",
"undici": "5.22.1"
},
"jest": {
"transform": {
Expand Down
65 changes: 49 additions & 16 deletions test/agent-ca/agent-ca-test.test.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
import { Agent, createServer } from "https";
import { createServer } from "https";
import { readFileSync } from "fs";
import { resolve } from "path";
import { fetch as undiciFetch, Agent } from "undici";
import { request } from "@octokit/request";

const { Octokit } = require("../../src");
const ca = readFileSync(resolve(__dirname, "./ca.crt"));

// TODO: rewrite tests to use fetch dispatchers
describe.skip("custom client certificate", () => {
describe("custom client certificate", () => {
let server: any;
// let myFetch: any;

beforeAll((done) => {
// Stand up a server that requires a client certificate
// requestCert forces the server to request a certificate
// rejectUnauthorized: false allows us to test with a self-signed certificate
server = createServer(
{
key: readFileSync(resolve(__dirname, "./localhost.key")),
cert: readFileSync(resolve(__dirname, "./localhost.crt")),
requestCert: true,
rejectUnauthorized: false,
},
(request: any, response: any) => {
expect(request.method).toEqual("GET");
Expand All @@ -28,28 +36,53 @@ describe.skip("custom client certificate", () => {
});

it("https.Agent({ca})", () => {
// Setup a dispatcher that uses the undici agent
const agent = new Agent({
ca,
});
const octokit = new Octokit({
baseUrl: "https://localhost:" + server.address().port,
request: { agent },
keepAliveTimeout: 10,
keepAliveMaxTimeout: 10,
connect: { ca: ca },
});

return octokit.request("/");
const myFetch = (url: any, opts: any) => {
return undiciFetch(url, {
...opts,
dispatcher: agent,
});
};

return request("/", {
options: {
baseUrl: "https://localhost:" + server.address().port,
request: {
fetch: myFetch,
},
},
});
});

it("https.Agent({ca, rejectUnauthorized})", () => {
// Setup a dispatcher that uses the undici agent
const agent = new Agent({
ca: "invalid",
rejectUnauthorized: false,
});
const octokit = new Octokit({
baseUrl: "https://localhost:" + server.address().port,
request: { agent },
keepAliveTimeout: 10,
keepAliveMaxTimeout: 10,
connect: { ca: "invalid" },
});

return octokit.request("/");
const myFetch = (url: any, opts: any) => {
return undiciFetch(url, {
...opts,
dispatcher: agent,
});
};

return request("/", {
options: {
baseUrl: "https://localhost:" + server.address().port,
request: {
fetch: myFetch,
},
},
});
});

afterAll((done) => {
Expand Down

0 comments on commit 94d8c75

Please sign in to comment.