Skip to content

Commit

Permalink
fix(assertions): cannot use HTTP apis that do not return JSON (#27463)
Browse files Browse the repository at this point in the history
The `HttpApiCall` handler only supports endpoints that return JSON. Also let it handle regular strings, if the endpoint returns something that is not parseable as JSON.

Fix a type issue in the declared types as well, they never returned a `responseJson` field, just a `body` field.

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
rix0rrr authored Oct 10, 2023
1 parent ea06f7d commit cae9db6
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 10 deletions.
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);
}

0 comments on commit cae9db6

Please sign in to comment.