From 8478cac30ca251951901430e4a695744e449ef48 Mon Sep 17 00:00:00 2001 From: Gregor Martynus <39992+gr2m@users.noreply.github.com> Date: Tue, 3 Sep 2024 13:11:35 -0700 Subject: [PATCH] fix: respect `baseUrl` passed as part of request parameters (#641) --- src/hook.ts | 2 +- test/index.test.ts | 52 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/src/hook.ts b/src/hook.ts index e1ba4262f..46aac02ea 100644 --- a/src/hook.ts +++ b/src/hook.ts @@ -92,7 +92,7 @@ export async function hook( state, // @ts-expect-error TBD {}, - request, + request.defaults({ baseUrl: endpoint.baseUrl }), ); endpoint.headers.authorization = `token ${token}`; diff --git a/test/index.test.ts b/test/index.test.ts index 69192b215..dfe9b7629 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -2416,3 +2416,55 @@ test("auth.hook() uses app auth even for requests with query strings. (#374)", a expect(mock.done()).toBe(true); }); + +test("auth.hook() respects `baseUrl` passed as part of request parameters #640", async () => { + const mock = fetchMock + .sandbox() + .postOnce( + "https://not-api.github.com/app/installations/123/access_tokens", + { + token: "secret123", + expires_at: "1970-01-01T01:00:00.000Z", + permissions: { + metadata: "read", + }, + repository_selection: "all", + }, + ) + .get( + "https://not-api.github.com/repos/octocat/hello-world", + { id: 123 }, + { + headers: { + authorization: "token secret123", + }, + repeat: 4, + }, + ); + + const auth = createAppAuth({ + appId: APP_ID, + privateKey: PRIVATE_KEY, + installationId: 123, + }); + + const requestWithMock = request.defaults({ + headers: { + "user-agent": "test", + }, + request: { + fetch: mock, + }, + }); + const requestWithAuth = requestWithMock.defaults({ + request: { + hook: auth.hook, + }, + }); + + const { data } = await requestWithAuth("GET /repos/octocat/hello-world", { + baseUrl: "https://not-api.github.com", + }); + + expect(data).toEqual({ id: 123 }); +});