Skip to content

Commit 4744ee5

Browse files
authored
fix(integ-tests): http flattenResponse (#30361)
### Issue # (if applicable) Closes #30327 ### Reason for this change There was a difference in the behavior of SDK and HTTP integration attribute extraction with the `getAtt` and `getAttString` methods. `awsApiCall` properly implemented and returned JSONPath-ish values by using a `flattenResponse` property. This PR adds the same functionality to `httpApiCall` ### Description of changes Added an implemented `flattenResponse` in the `HttpHandler` custom resource ### Description of how you validated changes Updated integ and unit tests ### Checklist - [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md) ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 7067c89 commit 4744ee5

File tree

11 files changed

+891
-23
lines changed

11 files changed

+891
-23
lines changed

packages/@aws-cdk/integ-tests-alpha/lib/assertions/http-call.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export class HttpApiCall extends ApiCallBase {
3232
parameters: props,
3333
expected: Lazy.any({ produce: () => this.expectedResult }),
3434
stateMachineArn: Lazy.string({ produce: () => this.stateMachineArn }),
35+
flattenResponse: Lazy.string({ produce: () => this.flattenResponse }),
3536
salt: Date.now().toString(),
3637
},
3738
resourceType: `${HTTP_RESOURCE_TYPE}${name}`.substring(0, 60),

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

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
// eslint-disable-next-line import/no-extraneous-dependencies
33
import fetch, { Response } from 'node-fetch';
44
// TODO: can use builtin fetch on node18
5+
import { flatten } from '@aws-cdk/aws-custom-resource-sdk-adapter';
56
import { CustomResourceHandler } from './base';
67
import { HttpRequest, HttpResponseWrapper } from './types';
8+
import { deepParseJson } from './utils';
79

8-
export class HttpHandler extends CustomResourceHandler<HttpRequest, HttpResponseWrapper> {
9-
protected async processEvent(request: HttpRequest): Promise<HttpResponseWrapper> {
10+
export class HttpHandler extends CustomResourceHandler<HttpRequest, HttpResponseWrapper | { [key: string]: unknown }> {
11+
protected async processEvent(request: HttpRequest): Promise<HttpResponseWrapper | { [key: string]: unknown }> {
1012
console.log('request', request);
1113
const response: Response = await fetch(request.parameters.url, request.parameters.fetchOptions);
1214
const result: any = {
@@ -22,8 +24,17 @@ export class HttpHandler extends CustomResourceHandler<HttpRequest, HttpResponse
2224
} catch (e) {
2325
// Okay
2426
}
25-
return {
26-
apiCallResponse: result,
27-
};
27+
28+
let resp: HttpResponseWrapper | { [key: string]: unknown };
29+
if (request.flattenResponse === 'true') {
30+
// Flatten and explode JSON fields
31+
resp = flatten(deepParseJson({ apiCallResponse: result }));
32+
} else {
33+
// Otherwise just return the response as-is, without exploding JSON fields
34+
resp = { apiCallResponse: result };
35+
}
36+
console.log(`Returning result ${JSON.stringify(resp)}`);
37+
38+
return resp;
2839
}
2940
}

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,22 @@ export interface HttpRequest {
2525
* Parameters from the custom resource
2626
*/
2727
readonly parameters: HttpRequestParameters;
28+
29+
/**
30+
* Whether or not to flatten the response from the HTTP request
31+
*
32+
* Valid values are 'true' or 'false' as strings
33+
*
34+
* Typically when using an HttpRequest you will be passing it as the
35+
* `actual` value to an assertion provider so this would be set
36+
* to 'false' (you want the actual response).
37+
*
38+
* If you are using the HttpRequest to perform more of a query to return
39+
* a single value to use, then this should be set to 'true'.
40+
*
41+
* @default 'false'
42+
*/
43+
readonly flattenResponse?: string;
2844
}
2945

3046
/**

packages/@aws-cdk/integ-tests-alpha/test/assertions/providers/integ.http-api-call-assertions.js.snapshot/AssertionsTestDefaultTestDeployAssertDC0672BB.assets.json

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk/integ-tests-alpha/test/assertions/providers/integ.http-api-call-assertions.js.snapshot/AssertionsTestDefaultTestDeployAssertDC0672BB.template.json

Lines changed: 188 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk/integ-tests-alpha/test/assertions/providers/integ.http-api-call-assertions.js.snapshot/InvokeFunctionAssertions.assets.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)