Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(assertions): cannot use HTTP apis that do not return JSON #27463

Merged
merged 2 commits into from
Oct 11, 2023
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 @@ -15,11 +15,12 @@ export class HttpHandler extends CustomResourceHandler<HttpRequest, HttpResponse
statusText: response.statusText,
headers: response.headers.raw(),
};

result.body = await response.text();
try {
const jsonResponse = await response.json();
result.body = jsonResponse;
result.body = JSON.parse(result.body);
} catch (e) {
result.body = {};
// Okay
}
return {
apiCallResponse: result,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ export interface HttpResponse {
readonly statusText?: string;

/**
* The response as JSON
* The response, either as parsed JSON or a string literal.
*/
readonly responseJson?: { [key: string]: any };
readonly body?: any;

/**
* Headers associated with the response
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { HttpHandler } from '../../../../lib/assertions/providers/lambda-handler/http';
import * as fetch from 'node-fetch';
import { HttpRequest, HttpResponseWrapper } from '../../../../lib';
import { HttpRequest } from '../../../../lib';

let fetchMock = jest.fn();
jest.mock('node-fetch');
Expand Down Expand Up @@ -34,15 +34,14 @@ beforeEach(() => {
describe('HttpHandler', () => {
test('default', async () => {
// GIVEN
const handler = httpHandler() as any;
const request: HttpRequest = {
parameters: {
url: 'url',
},
};

// WHEN
const response: HttpResponseWrapper = await handler.processEvent(request);
const response = await processEvent(request);

// THEN
expect(response.apiCallResponse).toEqual({
Expand All @@ -63,7 +62,6 @@ describe('HttpHandler', () => {

test('with fetch options', async () => {
// GIVEN
const handler = httpHandler() as any;
const request: HttpRequest = {
parameters: {
url: 'url',
Expand All @@ -79,7 +77,7 @@ describe('HttpHandler', () => {
};

// WHEN
const response: HttpResponseWrapper = await handler.processEvent(request);
const response = await processEvent(request);

// THEN
expect(response.apiCallResponse).toEqual({
Expand All @@ -104,4 +102,34 @@ describe('HttpHandler', () => {
},
});
});

test('JSON is parsed', async () => {
// GIVEN
fetchMock.mockResolvedValue(
new Response(JSON.stringify({ key: 'value' }), { status: 200, statusText: 'OK', ok: true }),
);

// WHEN
const response = await processEvent({ parameters: { url: 'x' } });

// THEN
expect(response.apiCallResponse.body).toEqual({ key: 'value' });
});

test('Non-JSON is not parsed', async () => {
// GIVEN
fetchMock.mockResolvedValue(
new Response('this is a string', { status: 200, statusText: 'OK', ok: true }),
);

// WHEN
const response = await processEvent({ parameters: { url: 'x' } });

// THEN
expect(response.apiCallResponse.body).toEqual('this is a string');
});
});

function processEvent(request: HttpRequest): ReturnType<HttpHandler['processEvent']> {
return (httpHandler() as any).processEvent(request);
}