From d9f1b21947b731c3b85b9da44e0f536770fa6e49 Mon Sep 17 00:00:00 2001 From: Heikki Hellgren Date: Tue, 16 Jan 2024 16:00:35 +0200 Subject: [PATCH] fix: collator headers --- .../collators/DefaultQetaCollatorFactory.test.ts | 14 +++++++++++--- .../src/collators/DefaultQetaCollatorFactory.ts | 5 ++--- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/plugins/search-backend-module-qeta/src/collators/DefaultQetaCollatorFactory.test.ts b/plugins/search-backend-module-qeta/src/collators/DefaultQetaCollatorFactory.test.ts index 1a7866e6..691a0d55 100644 --- a/plugins/search-backend-module-qeta/src/collators/DefaultQetaCollatorFactory.test.ts +++ b/plugins/search-backend-module-qeta/src/collators/DefaultQetaCollatorFactory.test.ts @@ -55,7 +55,7 @@ describe('DefaultQetaCollatorFactory', () => { getExternalBaseUrl: jest.fn(), }; const mockTokenManager: jest.Mocked = { - getToken: jest.fn().mockResolvedValue({ token: '' }), + getToken: jest.fn().mockResolvedValue({ token: 'test_token' }), authenticate: jest.fn(), }; const options = { @@ -72,6 +72,7 @@ describe('DefaultQetaCollatorFactory', () => { describe('getCollator', () => { let factory: DefaultQetaCollatorFactory; let collator: Readable; + let lastRequest: any = null; const worker = setupServer(); setupRequestMockHandlers(worker); @@ -79,12 +80,15 @@ describe('DefaultQetaCollatorFactory', () => { beforeEach(async () => { factory = DefaultQetaCollatorFactory.fromConfig(config, options); collator = await factory.getCollator(); + lastRequest = null; worker.use( rest.get( 'http://test-backend/api/qeta/questions', - (_: any, res: any, ctx: any) => - res(ctx.status(200), ctx.json(mockQuestions)), + (req: any, res: any, ctx: any) => { + lastRequest = req; + return res(ctx.status(200), ctx.json(mockQuestions)); + }, ), ); }); @@ -103,6 +107,9 @@ describe('DefaultQetaCollatorFactory', () => { mockQuestions.questions.length * mockComments.length + mockAnswers.length * mockComments.length; expect(documents).toHaveLength(totalDocuments); + expect(lastRequest.headers.get('authorization')).toEqual( + 'Bearer test_token', + ); }); it('non-authenticated backend', async () => { @@ -122,6 +129,7 @@ describe('DefaultQetaCollatorFactory', () => { mockQuestions.questions.length * mockComments.length + mockAnswers.length * mockComments.length; expect(documents).toHaveLength(totalDocuments); + expect(lastRequest.headers.get('authorization')).toEqual(null); }); }); }); diff --git a/plugins/search-backend-module-qeta/src/collators/DefaultQetaCollatorFactory.ts b/plugins/search-backend-module-qeta/src/collators/DefaultQetaCollatorFactory.ts index 2b9d091b..004fb020 100644 --- a/plugins/search-backend-module-qeta/src/collators/DefaultQetaCollatorFactory.ts +++ b/plugins/search-backend-module-qeta/src/collators/DefaultQetaCollatorFactory.ts @@ -52,10 +52,9 @@ export class DefaultQetaCollatorFactory implements DocumentCollatorFactory { const params = new URLSearchParams(); params.append('includeAnswers', 'true'); params.append('includeComments', 'true'); - const response = await fetch( - `${baseUrl}/questions?${params.toString()}`, + const response = await fetch(`${baseUrl}/questions?${params.toString()}`, { headers, - ); + }); const data = (await response.json()) as QuestionsResponseBody; if ('errors' in data) {