Skip to content

Commit

Permalink
fix(integ-tests): httpApiCall.expect with resolved URL (#29705)
Browse files Browse the repository at this point in the history
### Issue # (if applicable)

Closes #29700

I've also opened #29701 to catch similar issues at synth time in the future

### Reason for this change

When running `httpApiCall('url').expect`, the `AssertionResults` output logical ID would be overridden with an invalid name, containing the URL slashes.

This issue was not noticed earlier because, as far as I can tell, assertions were only made with `Token`/`Ref` URLs, e.g. `apigw.DomainName`

### Description of changes

* Remove non-alphanumeric characters from the overridden `AssertionResults` output logical ID
* I've also added a bit of documentation to `ExpectedResult`, I noticed it was slightly lacking while creating the integration test

### Description of how you validated changes

I've added unit and integration tests. The integration tests include both tests with API Gateway, to cover unresolved URLs, and to https://httpbin.org/ to test this fix

### 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 committed Apr 8, 2024
1 parent 4eb02a4 commit 49b4aa1
Show file tree
Hide file tree
Showing 12 changed files with 2,483 additions and 1 deletion.
10 changes: 10 additions & 0 deletions packages/@aws-cdk/integ-tests-alpha/lib/assertions/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ export abstract class ExpectedResult {
* ExpectedResult.exact({
* stringParam: 'hello',
* });
*
* @see https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.assertions.Match.html#static-exactpattern
*/
public static exact(expected: any): ExpectedResult {
return {
Expand All @@ -74,11 +76,15 @@ export abstract class ExpectedResult {
* stringParam: 'hello',
* numberParam: 3,
* booleanParam: true,
* objectParam: { prop1: 'value', prop2: 'value' },
* };
* // pass
* ExpectedResult.objectLike({
* stringParam: 'hello',
* objectParam: { prop1: 'value' },
* });
*
* @see https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.assertions.Match.html#static-objectwbrlikepattern
*/
public static objectLike(expected: { [key: string]: any }): ExpectedResult {
return {
Expand Down Expand Up @@ -108,6 +114,8 @@ export abstract class ExpectedResult {
* stringParam: 'hello',
* },
* ]);
*
* @see https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.assertions.Match.html#static-arraywbrwithpattern
*/
public static arrayWith(expected: any[]): ExpectedResult {
return {
Expand All @@ -126,6 +134,8 @@ export abstract class ExpectedResult {
*
* // pass
* ExpectedResult.stringLikeRegexp('value');
*
* @see https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.assertions.Match.html#static-stringwbrlikewbrregexppattern
*/
public static stringLikeRegexp(expected: string): ExpectedResult {
return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export class HttpApiCall extends ApiCallBase {

new CfnOutput(node, 'AssertionResults', {
value: result,
}).overrideLogicalId(`AssertionResults${id}`);
}).overrideLogicalId(`AssertionResults${id.replace(/[\W_]+/g, '')}`);
}
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,27 @@ describe('DeployAssert', () => {
}));
});

test('expect creates a valid CfnOutput', () => {
// GIVEN
const app = new App();
const deplossert = new DeployAssert(app);

// WHEN
const query = deplossert.httpApiCall('https://example.com/test/123?param=value&param2#hash');
query.expect(ExpectedResult.objectLike({ status: 200 }));

// THEN
Template.fromStack(deplossert.scope).hasOutput(
// Output name should only contain alphanumeric characters
'AssertionResultsHttpApiCallexamplecomtest1237c0018be9f253e38cad30092c2fa2a91',
{
Value: {
'Fn::GetAtt': ['HttpApiCallexamplecomtest1237c0018be9f253e38cad30092c2fa2a91', 'assertion'],
},
},
);
});

test('multiple calls can be configured', () => {
// GIVEN
const app = new App();
Expand Down

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

Loading

0 comments on commit 49b4aa1

Please sign in to comment.