Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrites the ca tests to use the dispatcher and custom fetch #585

Merged
merged 2 commits into from
Jul 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,

Check failure

Code scanning / CodeQL

Disabling certificate validation

Disabling certificate validation is strongly discouraged.
},
(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