Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1354,6 +1354,48 @@ describe('Cases webhook service', () => {
'[Action][Webhook - Case Management]: Unable to update case with id 1. Error: Unsupported content type: text/html in GET https://example.com. Supported content types: application/json.'
);
});

it('it should throw if the request status is a 204 and has data', async () => {
requestMock.mockImplementation(() =>
createAxiosResponse({
data: 'some data',
headers: { ['content-type']: 'text/html' },
status: 204,
})
);

await expect(service.updateIncident(incident)).rejects.toThrow(
'[Action][Webhook - Case Management]: Unable to update case with id 1. Error: Unsupported content type: text/html in GET https://example.com. Supported content types: application/json.'
);
});

it('it should NOT throw if the request status is a 204 and is empty', async () => {
// Initial mock for the update call
requestMock.mockImplementationOnce(() =>
createAxiosResponse({
data: undefined,
headers: { ['content-type']: 'text/html' },
status: 204,
})
);

// Second mock for the getIncident call inside updateIncident
requestMock.mockImplementationOnce(() =>
createAxiosResponse({
data: {
id: '1',
key: 'CK-1',
},
})
);

await expect(service.updateIncident(incident)).resolves.toEqual({
id: '1',
title: 'CK-1',
pushedDate: mockTime.toISOString(),
url: 'https://coolsite.net/browse/CK-1',
});
});
});

describe('createComment', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,32 @@ describe('cases_webhook/utils', () => {
headers: new AxiosHeaders({}),
},
};

it('Does not throw when the response status is 204 and there is no data', () => {
expect(() =>
throwDescriptiveErrorIfResponseIsNotValid({
res: {
...res,
status: 204,
data: undefined,
},
})
).not.toThrow();
});

it('Throws when the response status is 204 and requiredAttributesToBeInTheResponse is set', () => {
expect(() =>
throwDescriptiveErrorIfResponseIsNotValid({
res: {
...res,
status: 204,
data: undefined,
},
requiredAttributesToBeInTheResponse: ['field.simple'],
})
).toThrow();
});

it('Throws error when missing content-type', () => {
expect(() =>
throwDescriptiveErrorIfResponseIsNotValid({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ export const throwDescriptiveErrorIfResponseIsNotValid = ({
const contentType = res.headers['content-type'];
const data = res.data;

// If status is 204 and there is no data, we just return
if (res.status === 204 && isEmpty(data) && requiredAttributesToBeInTheResponse.length === 0) {
return;
}

/**
* Check that the content-type of the response is application/json.
* Then includes is added because the header can be application/json;charset=UTF-8.
Expand Down