mirror of
https://github.com/github/codeql-action.git
synced 2026-10-03 17:41:28 +00:00
Base custom request options on defaults, and add basic tests for makeProxyRequestOptions
This commit is contained in:
@@ -2,6 +2,7 @@ import * as github from "@actions/github";
|
||||
import * as githubUtils from "@actions/github/lib/utils";
|
||||
import test from "ava";
|
||||
import * as sinon from "sinon";
|
||||
import { ProxyAgent } from "undici";
|
||||
|
||||
import * as actionsUtil from "./actions-util";
|
||||
import * as api from "./api-client";
|
||||
@@ -251,3 +252,23 @@ test("getRegistryProxyConfig - gets the configuration from the env vars", async
|
||||
)
|
||||
.passes(t.like, { host, port, ca });
|
||||
});
|
||||
|
||||
test("makeProxyRequestOptions - returns defaults without custom proxy", async (t) => {
|
||||
t.deepEqual(
|
||||
api.makeProxyRequestOptions(undefined),
|
||||
githubUtils.defaults.request,
|
||||
);
|
||||
});
|
||||
|
||||
test("makeProxyRequestOptions - returns fetch with custom proxy", async (t) => {
|
||||
const opts = api.makeProxyRequestOptions(
|
||||
new ProxyAgent("http://localhost:1080"),
|
||||
);
|
||||
// Fetch should be different from the defaults.
|
||||
t.notDeepEqual(opts?.fetch, githubUtils.defaults.request?.fetch);
|
||||
// The options should be the same aside from that.
|
||||
t.deepEqual(
|
||||
{ ...opts, fetch: githubUtils.defaults.request?.fetch },
|
||||
githubUtils.defaults.request,
|
||||
);
|
||||
});
|
||||
|
||||
@@ -108,12 +108,19 @@ export function getRegistryProxy(
|
||||
* Constructs a `RequestRequestOptions` with a custom `fetch` implementation
|
||||
* that uses `dispatcher` as a proxy for requests.
|
||||
*
|
||||
* @param dispatcher The proxy to use.
|
||||
* @param dispatcher The proxy to use, if any.
|
||||
*/
|
||||
export function makeProxyRequestOptions(
|
||||
dispatcher: ProxyAgent,
|
||||
): RequestRequestOptions {
|
||||
dispatcher: ProxyAgent | undefined,
|
||||
): RequestRequestOptions | undefined {
|
||||
// If we don't have a custom `ProxyAgent`, return the defaults.
|
||||
if (dispatcher === undefined) {
|
||||
return githubUtils.defaults.request;
|
||||
}
|
||||
|
||||
// Otherwise, construct the custom `fetch` and add it onto the defaults.
|
||||
return {
|
||||
...githubUtils.defaults.request,
|
||||
fetch: (req: RequestInfo, init?: RequestInit) => {
|
||||
return undiciFetch(req, { ...init, dispatcher });
|
||||
},
|
||||
@@ -136,10 +143,7 @@ function createApiClientWithDetails(
|
||||
const auth =
|
||||
(allowExternal && apiDetails.externalRepoAuth) || apiDetails.auth;
|
||||
const retryingOctokit = githubUtils.GitHub.plugin(retry.retry);
|
||||
const requestOptions =
|
||||
proxy === undefined
|
||||
? githubUtils.defaults.request
|
||||
: makeProxyRequestOptions(proxy);
|
||||
const requestOptions = makeProxyRequestOptions(proxy);
|
||||
return new retryingOctokit(
|
||||
githubUtils.getOctokitOptions(auth, {
|
||||
baseUrl: apiDetails.apiURL,
|
||||
|
||||
Reference in New Issue
Block a user