From efcab31d382deff0c414bd974084bd94c1d1ee82 Mon Sep 17 00:00:00 2001 From: Bethany Date: Mon, 7 Aug 2023 08:43:39 -0700 Subject: [PATCH] pass in http client to constructor --- .../__tests__/artifact-http-client.test.ts | 7 ++++--- .../internal/shared/artifact-twirp-client.ts | 18 +++++++++++------- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/packages/artifact/__tests__/artifact-http-client.test.ts b/packages/artifact/__tests__/artifact-http-client.test.ts index 3ab79116..14c0a9cf 100644 --- a/packages/artifact/__tests__/artifact-http-client.test.ts +++ b/packages/artifact/__tests__/artifact-http-client.test.ts @@ -11,11 +11,11 @@ describe("artifact-http-client", () => { jest.spyOn(core, "debug").mockImplementation(() => {}) jest.spyOn(core, "info").mockImplementation(() => {}) jest.spyOn(core, "warning").mockImplementation(() => {}) + jest.spyOn(config, "getResultsServiceUrl").mockReturnValue("http://localhost:8080") + jest.spyOn(config, "getRuntimeToken").mockReturnValue("token") }) it("should successfully create a client", () => { - jest.spyOn(config, "getResultsServiceUrl").mockReturnValue("http://localhost:8080") - jest.spyOn(config, "getRuntimeToken").mockReturnValue("token") const client = createArtifactTwirpClient("upload") expect(client).toBeDefined() }) @@ -27,7 +27,8 @@ describe("artifact-http-client", () => { workflowRunBackendId: "1234", workflowJobRunBackendId: "5678", name: "artifact", - version: 4} + version: 4 + } ) expect(artifact).toBeDefined() }) diff --git a/packages/artifact/src/internal/shared/artifact-twirp-client.ts b/packages/artifact/src/internal/shared/artifact-twirp-client.ts index 6e9345ea..7241b0c5 100644 --- a/packages/artifact/src/internal/shared/artifact-twirp-client.ts +++ b/packages/artifact/src/internal/shared/artifact-twirp-client.ts @@ -21,13 +21,17 @@ class ArtifactHttpClient implements Rpc { private baseRetryIntervalMilliseconds: number = 3000 private retryMultiplier: number = 1.5 - constructor(userAgent: string) { + constructor(userAgent: string, httpClient?: HttpClient) { const token = getRuntimeToken() - this.httpClient = new HttpClient( - userAgent, - [new BearerCredentialHandler(token)], - ) this.baseUrl = getResultsServiceUrl() + if (httpClient) { + this.httpClient = httpClient + } else { + this.httpClient = new HttpClient( + userAgent, + [new BearerCredentialHandler(token)], + ) + } } // This function satisfies the Rpc interface. It is compatible with the JSON @@ -127,7 +131,7 @@ class ArtifactHttpClient implements Rpc { } } -export function createArtifactTwirpClient(type: "upload" | "download"): ArtifactServiceClientJSON { - const client = new ArtifactHttpClient(`@actions/artifact-${type}`) +export function createArtifactTwirpClient(type: "upload" | "download", httpClient?: HttpClient): ArtifactServiceClientJSON { + const client = new ArtifactHttpClient(`@actions/artifact-${type}`, httpClient) return new ArtifactServiceClientJSON(client) }