Skip to content

Commit cae9db6

Browse files
authored
fix(assertions): cannot use HTTP apis that do not return JSON (#27463)
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*
1 parent ea06f7d commit cae9db6

File tree

3 files changed

+39
-10
lines changed
  • packages/@aws-cdk/integ-tests-alpha

3 files changed

+39
-10
lines changed

packages/@aws-cdk/integ-tests-alpha/lib/assertions/providers/lambda-handler/http.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@ export class HttpHandler extends CustomResourceHandler<HttpRequest, HttpResponse
1515
statusText: response.statusText,
1616
headers: response.headers.raw(),
1717
};
18+
19+
result.body = await response.text();
1820
try {
19-
const jsonResponse = await response.json();
20-
result.body = jsonResponse;
21+
result.body = JSON.parse(result.body);
2122
} catch (e) {
22-
result.body = {};
23+
// Okay
2324
}
2425
return {
2526
apiCallResponse: result,

packages/@aws-cdk/integ-tests-alpha/lib/assertions/providers/lambda-handler/types.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,9 @@ export interface HttpResponse {
9292
readonly statusText?: string;
9393

9494
/**
95-
* The response as JSON
95+
* The response, either as parsed JSON or a string literal.
9696
*/
97-
readonly responseJson?: { [key: string]: any };
97+
readonly body?: any;
9898

9999
/**
100100
* Headers associated with the response

packages/@aws-cdk/integ-tests-alpha/test/assertions/providers/lambda-handler/http.test.ts

+33-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { HttpHandler } from '../../../../lib/assertions/providers/lambda-handler/http';
22
import * as fetch from 'node-fetch';
3-
import { HttpRequest, HttpResponseWrapper } from '../../../../lib';
3+
import { HttpRequest } from '../../../../lib';
44

55
let fetchMock = jest.fn();
66
jest.mock('node-fetch');
@@ -34,15 +34,14 @@ beforeEach(() => {
3434
describe('HttpHandler', () => {
3535
test('default', async () => {
3636
// GIVEN
37-
const handler = httpHandler() as any;
3837
const request: HttpRequest = {
3938
parameters: {
4039
url: 'url',
4140
},
4241
};
4342

4443
// WHEN
45-
const response: HttpResponseWrapper = await handler.processEvent(request);
44+
const response = await processEvent(request);
4645

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

6463
test('with fetch options', async () => {
6564
// GIVEN
66-
const handler = httpHandler() as any;
6765
const request: HttpRequest = {
6866
parameters: {
6967
url: 'url',
@@ -79,7 +77,7 @@ describe('HttpHandler', () => {
7977
};
8078

8179
// WHEN
82-
const response: HttpResponseWrapper = await handler.processEvent(request);
80+
const response = await processEvent(request);
8381

8482
// THEN
8583
expect(response.apiCallResponse).toEqual({
@@ -104,4 +102,34 @@ describe('HttpHandler', () => {
104102
},
105103
});
106104
});
105+
106+
test('JSON is parsed', async () => {
107+
// GIVEN
108+
fetchMock.mockResolvedValue(
109+
new Response(JSON.stringify({ key: 'value' }), { status: 200, statusText: 'OK', ok: true }),
110+
);
111+
112+
// WHEN
113+
const response = await processEvent({ parameters: { url: 'x' } });
114+
115+
// THEN
116+
expect(response.apiCallResponse.body).toEqual({ key: 'value' });
117+
});
118+
119+
test('Non-JSON is not parsed', async () => {
120+
// GIVEN
121+
fetchMock.mockResolvedValue(
122+
new Response('this is a string', { status: 200, statusText: 'OK', ok: true }),
123+
);
124+
125+
// WHEN
126+
const response = await processEvent({ parameters: { url: 'x' } });
127+
128+
// THEN
129+
expect(response.apiCallResponse.body).toEqual('this is a string');
130+
});
107131
});
132+
133+
function processEvent(request: HttpRequest): ReturnType<HttpHandler['processEvent']> {
134+
return (httpHandler() as any).processEvent(request);
135+
}

0 commit comments

Comments
 (0)