aws / aws-cdk

The AWS Cloud Development Kit is a framework for defining cloud infrastructure in code
https://aws.amazon.com/cdk
Apache License 2.0
11.39k stars 3.79k forks source link

(integ-test): HttpApiCall seems to be broken #30327

Open kornicameister opened 1 month ago

kornicameister commented 1 month ago

Describe the bug

I am trying to build a test that codes roughly does:

const getUploadUrl = integ.assertions
  .httpApiCall(`${testStack.apiUrl}outbox/${id}`, {
    method: 'PUT',
  })
  .expect(
    test.ExpectedResult.objectLike({
      status: 201,
      headers: {
        'content-type': ['application/json'],
      },
      body: {
        id: emailId,
      },
    }),
  )
  .waitForAssertions();
const uploadUrl = getUploadUrl.getAttString('body.uploadUrl');

as you can see I am trying to get my hands on uploadUrl from response body. the assertion expressed in expect works fine (although there's no checkup for uploadUrl but just because I am not sure if I can nest expectation). Still, I am confident that uploadUrl is present in response body based on logs.

Is httpApiCall broken? I have located a lot of examples where getAtString is used in cdk own test suites. Difference is that it is rather used with awsSdkCall or lambdaInvokeFunction rather then httpApiCall.

Use case

Use case is to be able to integ test parts of bigger component. Without a need to deploy the whole component.

Expected Behavior

I can obtain part of response via HttApiCall

Current Behavior

Test fails with Vendor response doesn't contain apiCallResponse.body.uploadUrl

Reproduction Steps

import 'source-map-support/register';

import * as test from '@aws-cdk/integ-tests-alpha';
import * as cdk from 'aws-cdk-lib';

const app = new cdk.App({
  analyticsReporting: false,
  stackTraces: true, 
});

const testStack = new cdk.Stack(app, 'repro-api-stack');
const integ = new test.IntegTest(app, 'repro-api-test', {
  testCases: [testStack],
});
const apiCall = integ.assertions.httpApiCall(
  'https://cat-fact.herokuapp.com/facts',
);
const value = apiCall.getAttString('body.0.user');

integ.assertions
  .httpApiCall(`https://cat-fact.herokuapp.com/users/${value}`)
  .expect(
    test.ExpectedResult.objectLike({
      status: 404,
    }),
  );

Possible Solution

N/A

Additional Information/Context

N/A

CDK CLI Version

2.142.1

Framework Version

No response

Node.js Version

20.8

OS

MacOS sierra

Language

TypeScript

Language Version

5.4

Other information

No response

ashishdhingra commented 1 month ago

@kornicameister Good morning. Somehow, the below code works with CDK version 2.143.0:

import 'source-map-support/register';

import * as test from '@aws-cdk/integ-tests-alpha';
import * as cdk from 'aws-cdk-lib';

const app = new cdk.App({
  analyticsReporting: false,
  stackTraces: true, 
});

const testStack = new cdk.Stack(app, 'repro-api-stack');
const integ = new test.IntegTest(app, 'repro-api-test', {
  testCases: [testStack],
});
const apiCall = integ.assertions.httpApiCall(
  'https://cat-fact.herokuapp.com/facts',
);
const value = apiCall.getAttString('body.0.user');

integ.assertions
  .httpApiCall(`https://cat-fact.herokuapp.com/users/${value}`)
  .expect(
    test.ExpectedResult.objectLike({
      status: 404,
    }),
  );

Is this failing at your end? Please confirm along with the error.

In your previous code snippet, did you intend to use body.0.uploadUrl?

Thanks, Ashish

kornicameister commented 1 month ago

I bumped the version, took code from your snippet above and it failed:

Failed resources:
reproapitestDefaultTestDeployAssertA03A3219 | 9:46:24 PM | CREATE_FAILED        | Custom::DeployAssert@HttpCall                          | repro-api-test/DefaultTest/DeployAssert/HttpApiCall420eb7584d44d218b546a254d24187f3/Default/Default (HttpApiCall420eb7584d44d218b546a254d24187f3) CustomResource attribute error: Vendor response doesn't contain apiCallResponse.body.0.user attribute in object arn:aws:cloudformation:eu-central-1:00000000000:stack/reproapitestDefaultTestDeployAssertA03A3219/610896c0-1acf-11ef-bba5-06dfe13ba9f1|HttpApiCallcatfactherokuappcomfacts77c13b1c90b3620a2c6e32ccb72dd4b9|98bf9533-07c3-4b8e-b76d-2825b0abf30c

So.. it is a puzzle.

I don't know if that's relevant but to further describe my environment;

kornicameister commented 1 month ago

In your previous code snippet, did you intend to use body.0.uploadUrl?

No, in the very first snippet (from opening comment in issue) I am in fact expecting uploadUrl to be returned in top root object. Response's schema is loosely:

{
   id: string;
   uploadUrl: string;
}
kornicameister commented 1 month ago

@ashishdhingra any ideas?

kornicameister commented 1 month ago

I had created https://github.com/kornicameister/repro-aws-cdk-30327 that reproduces bug for me. Created today via cdk init

nmussy commented 1 month ago

This might be related to my PR #29705, since it was the last change made to http-call.ts. I'll have a look later today 👍

nmussy commented 1 month ago

This ended up being unrelated to my change, the HTTP handler just did not implement the flattenResponse feature that the SDK handler did:

https://github.com/aws/aws-cdk/blob/a2e8dc5e3cff91e64e5062a8fed630fb46982dbf/packages/%40aws-cdk/integ-tests-alpha/lib/assertions/providers/lambda-handler/http.ts#L25-L27

https://github.com/aws/aws-cdk/blob/a2e8dc5e3cff91e64e5062a8fed630fb46982dbf/packages/%40aws-cdk/integ-tests-alpha/lib/assertions/providers/lambda-handler/sdk.ts#L18-L22

I'm going to open a PR to solve this issue

kornicameister commented 1 month ago

@nmussy appreciate it.

I was blocked by the lack of it and had to skip an important part of the integration tests because of it. Having it is something I look forward to.