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 @@ -49,34 +49,43 @@ describe('sendRequest', () => {
expect(mockedSendRequest).toHaveBeenCalledTimes(1);
});

it('should send multiple requests', async () => {
mockedSendRequest.mockResolvedValue([
{
response: {
statusCode: 200,
describe('with multiple requests', () => {
it('should return results with exceptions', async () => {
mockedSendRequest.mockResolvedValue([
{
response: {
statusCode: 200,
},
},
},
{
response: {
statusCode: 200,
{
response: {
statusCode: 200,
},
},
},
]);

const args = {
http: mockContextValue.services.http,
requests: [
{ method: 'GET', url: 'test-1', data: [] },
{ method: 'GET', url: 'test-2', data: [] },
],
};
const results = await sendRequest(args);
{
response: {
statusCode: 400,
},
},
]);

const [firstRequest, secondRequest] = results;
expect(firstRequest.response.statusCode).toEqual(200);
expect(secondRequest.response.statusCode).toEqual(200);
expect(mockedSendRequest).toHaveBeenCalledWith(args);
expect(mockedSendRequest).toHaveBeenCalledTimes(1);
const args = {
http: mockContextValue.services.http,
requests: [
{ method: 'GET', url: 'success', data: [] },
{ method: 'GET', url: 'success', data: [] },
{ method: 'GET', url: 'fail', data: [] },
],
};
const results = await sendRequest(args);

const [firstCall, secondCall, thirdCall] = results;
expect(firstCall.response.statusCode).toEqual(200);
expect(secondCall.response.statusCode).toEqual(200);
expect(thirdCall.response.statusCode).toEqual(400);
expect(mockedSendRequest).toHaveBeenCalledWith(args);
expect(mockedSendRequest).toHaveBeenCalledTimes(1);
});
});

it('should handle errors', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ export interface RequestResult<V = unknown> {
response: ResponseObject<V>;
}

const getContentType = (response: Response | undefined) =>
(response?.headers.get('Content-Type') as BaseResponseType) ?? '';

let CURRENT_REQ_ID = 0;
export function sendRequest(args: RequestArgs): Promise<RequestResult[]> {
const requests = args.requests.slice();
Expand Down Expand Up @@ -111,7 +114,7 @@ export function sendRequest(args: RequestArgs): Promise<RequestResult[]> {
timeMs: Date.now() - startTime,
statusCode: response.status,
statusText: response.statusText,
contentType: response.headers.get('Content-Type') as BaseResponseType,
contentType: getContentType(response),
value,
},
request: {
Expand All @@ -128,9 +131,8 @@ export function sendRequest(args: RequestArgs): Promise<RequestResult[]> {
} catch (error) {
let value;
const { response, body } = error as IHttpFetchError;
const contentType = response?.headers.get('Content-Type') ?? '';
const statusCode = response?.status ?? 500;
const statusText = error?.response?.statusText ?? 'error';
const statusText = response?.statusText ?? 'error';

if (body) {
value = JSON.stringify(body, null, 2);
Expand All @@ -142,10 +144,10 @@ export function sendRequest(args: RequestArgs): Promise<RequestResult[]> {
value = '# ' + req.method + ' ' + req.url + '\n' + value;
}

reject({
const result = {
response: {
value,
contentType,
contentType: getContentType(response),
timeMs: Date.now() - startTime,
statusCode,
statusText,
Expand All @@ -155,7 +157,16 @@ export function sendRequest(args: RequestArgs): Promise<RequestResult[]> {
method,
path,
},
});
};

// Reject on unknown errors
if (!response) {
reject(result);
}

// Add error to the list of results
results.push(result);
await sendNextRequest();
}
};

Expand Down