Skip to content

Commit

Permalink
fix(integ-tests): http flattenResponse (#30361)
Browse files Browse the repository at this point in the history
### 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*
  • Loading branch information
nmussy authored Feb 7, 2025
1 parent 7067c89 commit 4744ee5
Show file tree
Hide file tree
Showing 11 changed files with 891 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export class HttpApiCall extends ApiCallBase {
parameters: props,
expected: Lazy.any({ produce: () => this.expectedResult }),
stateMachineArn: Lazy.string({ produce: () => this.stateMachineArn }),
flattenResponse: Lazy.string({ produce: () => this.flattenResponse }),
salt: Date.now().toString(),
},
resourceType: `${HTTP_RESOURCE_TYPE}${name}`.substring(0, 60),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
// eslint-disable-next-line import/no-extraneous-dependencies
import fetch, { Response } from 'node-fetch';
// TODO: can use builtin fetch on node18
import { flatten } from '@aws-cdk/aws-custom-resource-sdk-adapter';
import { CustomResourceHandler } from './base';
import { HttpRequest, HttpResponseWrapper } from './types';
import { deepParseJson } from './utils';

export class HttpHandler extends CustomResourceHandler<HttpRequest, HttpResponseWrapper> {
protected async processEvent(request: HttpRequest): Promise<HttpResponseWrapper> {
export class HttpHandler extends CustomResourceHandler<HttpRequest, HttpResponseWrapper | { [key: string]: unknown }> {
protected async processEvent(request: HttpRequest): Promise<HttpResponseWrapper | { [key: string]: unknown }> {
console.log('request', request);
const response: Response = await fetch(request.parameters.url, request.parameters.fetchOptions);
const result: any = {
Expand All @@ -22,8 +24,17 @@ export class HttpHandler extends CustomResourceHandler<HttpRequest, HttpResponse
} catch (e) {
// Okay
}
return {
apiCallResponse: result,
};

let resp: HttpResponseWrapper | { [key: string]: unknown };
if (request.flattenResponse === 'true') {
// Flatten and explode JSON fields
resp = flatten(deepParseJson({ apiCallResponse: result }));
} else {
// Otherwise just return the response as-is, without exploding JSON fields
resp = { apiCallResponse: result };
}
console.log(`Returning result ${JSON.stringify(resp)}`);

return resp;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,22 @@ export interface HttpRequest {
* Parameters from the custom resource
*/
readonly parameters: HttpRequestParameters;

/**
* Whether or not to flatten the response from the HTTP request
*
* Valid values are 'true' or 'false' as strings
*
* Typically when using an HttpRequest you will be passing it as the
* `actual` value to an assertion provider so this would be set
* to 'false' (you want the actual response).
*
* If you are using the HttpRequest to perform more of a query to return
* a single value to use, then this should be set to 'true'.
*
* @default 'false'
*/
readonly flattenResponse?: string;
}

/**
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 4744ee5

Please sign in to comment.