Skip to content

Commit

Permalink
fix: add accept and content-type headers when executing http prefligh…
Browse files Browse the repository at this point in the history
…t checks
  • Loading branch information
pavel-snyk committed Jul 12, 2023
1 parent 3d2b68d commit 5daa65b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
2 changes: 2 additions & 0 deletions lib/client/checks/http/http-executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ export async function executeHttpRequest(
method: httpOptions.method,
url: httpOptions.url,
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
'User-Agent': `broker client/${version} (http check service)`,
},
timeout: httpOptions.timeoutMs,
Expand Down
24 changes: 23 additions & 1 deletion test/functional/client/checks/http/http-executor.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { axiosInstance } from '../../../../../lib/axios';
import { MockServer } from 'jest-mock-server';
import { aHttpCheck } from '../../../../helpers/test-factories';
import { executeHttpRequest } from '../../../../../lib/client/checks/http/http-executor';
Expand Down Expand Up @@ -90,7 +91,28 @@ describe('client/checks/http/http-executor.ts', () => {
});
});

it.only('should throw an error after 3 retries', async () => {
it('should add common http headers to request', async () => {
const spyOnRequestFn = jest.spyOn(axiosInstance, 'request');
server.get(`/broker-server/healthcheck`).mockImplementationOnce((ctx) => {
ctx.status = 200;
ctx.body = { status: 'ok' };
});
const check = aHttpCheck({
url: `${mockServerBaseUrl}/broker-server/healthcheck`,
});

await executeHttpRequest(
{ id: check.id, name: check.name },
{ url: check.url, method: check.method, timeoutMs: check.timeoutMs },
);

expect(spyOnRequestFn.mock.lastCall[0].headers).toMatchObject({
Accept: expect.any(String),
'Content-Type': expect.any(String),
});
});

it.skip('should throw an error after 3 retries', async () => {
const responseWithTimeout = async (ctx) => {
await setTimeout(100, 'waiting 100ms');
ctx.response = 200;
Expand Down

0 comments on commit 5daa65b

Please sign in to comment.