Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): add cache-control header to cognito identity client #10753

Merged
merged 11 commits into from Dec 12, 2022

Conversation

haverchuck
Copy link
Contributor

@haverchuck haverchuck commented Dec 8, 2022

Description of changes

Adds the cache-control 'no-store' header via middleware to the various instantiations of the Cognito Identity Pool Client.

Please note: the Credentials file instantiates the cognito client multiple times - most likely needlessly. I've left this in place, though, to minimize the scope of the change.

Description of how you validated changes

Manual testing.

Checklist

  • PR description included
  • yarn test passes
  • Tests are changed or added
  • Relevant documentation is changed or added (and PR referenced)

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@@ -296,6 +297,16 @@ export class CredentialsClass {
customUserAgent: getAmplifyUserAgent(),
});

cognitoClient.middlewareStack.add(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there ever a case when we would not want the cache control header to be added? If not, could we just bake this into the CognitoClient constructor?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The answer to your question is 'no'; and while we don't control the client constructor itself (it's an sdk construct) we've moved instantiation up into a separate utility that bakes in this middleware.

@@ -296,6 +297,16 @@ export class CredentialsClass {
customUserAgent: getAmplifyUserAgent(),
});

cognitoClient.middlewareStack.add(
(next, _) => (args: any) => {
args.request.headers['cache-control'] = 'no-store';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Might want to do this immutably to keep the middleware side-effect free.

const updatedArgs = {
  ...args,
  request: {
    ...args.request,
    headers: { 
      ...args.request.headers,
      'cache-control': 'no-store'
    }
  }
};

Also might be worth breaking into a utility function to improve re-usability & test-ability?

Copy link
Contributor

@jimblanc jimblanc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we go back and patch this in v4 as well?

@codecov-commenter
Copy link

codecov-commenter commented Dec 9, 2022

Codecov Report

Merging #10753 (f199384) into main (cde60fc) will decrease coverage by 0.00%.
The diff coverage is 55.00%.

@@            Coverage Diff             @@
##             main   #10753      +/-   ##
==========================================
- Coverage   86.16%   86.16%   -0.01%     
==========================================
  Files         196      197       +1     
  Lines       18354    18368      +14     
  Branches     3906     3906              
==========================================
+ Hits        15815    15826      +11     
- Misses       2464     2468       +4     
+ Partials       75       74       -1     
Impacted Files Coverage Δ
packages/amazon-cognito-identity-js/src/Client.js 50.48% <21.73%> (-1.52%) ⬇️
packages/core/src/Credentials.ts 67.70% <100.00%> (ø)
packages/core/src/Util/CognitoIdentityClient.ts 100.00% <100.00%> (ø)

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

AllanZhengYP
AllanZhengYP previously approved these changes Dec 9, 2022
stocaaro
stocaaro previously approved these changes Dec 9, 2022
cshfang
cshfang previously approved these changes Dec 9, 2022
Copy link
Contributor

@cshfang cshfang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor comments otherwise lgtm

packages/core/src/Util/CognitoIdentityClient.ts Outdated Show resolved Hide resolved
Copy link
Contributor

@elorzafe elorzafe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think of having an E2E for checking the headers instead of doing it on the unit tests? I would try to test the logic on unit tests but test the actual requests headers I would do it on cypress on a real app.

Thanks @haverchuck 🎖️

expect(cognitoClient).toBeTruthy();
});

test('headers should be added by middleware on GetIdCommand', async () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would add expect.assertions and also use done in case the tests ends before doing the assertions

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think the done is necessary if you are using async callback and awaiting, but I added the expect.assertions. Good callout, thanks.

packages/core/__tests__/Util-test.ts Show resolved Hide resolved
packages/core/src/Util/CognitoIdentityClient.ts Outdated Show resolved Hide resolved
@haverchuck haverchuck marked this pull request as ready for review December 12, 2022 18:53
@haverchuck haverchuck requested review from a team as code owners December 12, 2022 18:53
cshfang
cshfang previously approved these changes Dec 12, 2022
AllanZhengYP
AllanZhengYP previously approved these changes Dec 12, 2022
jimblanc
jimblanc previously approved these changes Dec 12, 2022
stocaaro
stocaaro previously approved these changes Dec 12, 2022
@haverchuck haverchuck removed the request for review from elorzafe December 12, 2022 19:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants