Skip to content

Commit

Permalink
fix: collator headers
Browse files Browse the repository at this point in the history
  • Loading branch information
drodil committed Jan 16, 2024
1 parent 4dd5747 commit d9f1b21
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ describe('DefaultQetaCollatorFactory', () => {
getExternalBaseUrl: jest.fn(),
};
const mockTokenManager: jest.Mocked<TokenManager> = {
getToken: jest.fn().mockResolvedValue({ token: '' }),
getToken: jest.fn().mockResolvedValue({ token: 'test_token' }),
authenticate: jest.fn(),
};
const options = {
Expand All @@ -72,19 +72,23 @@ describe('DefaultQetaCollatorFactory', () => {
describe('getCollator', () => {
let factory: DefaultQetaCollatorFactory;
let collator: Readable;
let lastRequest: any = null;

const worker = setupServer();
setupRequestMockHandlers(worker);

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));
},
),
);
});
Expand All @@ -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 () => {
Expand All @@ -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);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit d9f1b21

Please sign in to comment.