hono icon indicating copy to clipboard operation
hono copied to clipboard

fix: avoid duplicating already set headers

Open Amorim33 opened this issue 1 year ago • 1 comments

fix #2550

Description

This pull request avoids duplicating already set headers; this was happening because API Gateway V1 event has "multiValueHeaders" for headers with multiple values.

Currently, Hono AWS Lambda adapter appends any value from these "multiValueHeaders" in the current Header instance. This leads to duplicated values when a key is already defined.

How

  • Added a condition to only append a value from "multiValueHeaders" when the current key is not already defined or the current value is not already included;
  • added unit tests for the createRequest method from EventProcessor to make sure that the Request is adapted correctly, headers are not duplicated and to avoid future regressions;

Tests are following the arrange / act / assert pattern.

  • getProcessor function was exported for unit testing purposes;
  • Added a comment because the conditions became a little complex.

References

https://docs.aws.amazon.com/lambda/latest/dg/services-apigateway.html https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html

Author should do the followings, if applicable

  • [x] Add tests
  • [x] Run tests
  • [x] bun denoify to generate files for Deno

Extra

Thanks to @jean-leonco that found the bug in @BemteviSeguros API, @kbrandwijk that opened the issue and @ShaneYu that suggested the idea of the fix.

Please let me know if this aligns with the library goals and if the code follows the repository recommendations.

Thanks in advance.

Amorim33 avatar May 01 '24 15:05 Amorim33

Thanks @Amorim33 !

@watany-dev Could you review this?

yusukebe avatar May 01 '24 23:05 yusukebe

I have tried to fix the problem at hand for a short time, but it appears that the header rewrite does not work if I just change the createRequest. In fact, I can see that the test is still failing with this fix.

watany-dev avatar May 04 '24 01:05 watany-dev

Hello @watany-dev!

Sorry for this, I didn't realize that tests were not passing when running test:all.

This was happening because of other LambdaEvent related global definitions, the "content-type" header was missing in my test. Just fixed it!

Now, I believe that the tests are running properly and headers are not getting duplicated anymore.

May you please, run the workflow one more time?

Amorim33 avatar May 05 '24 20:05 Amorim33

Hi @Amorim33

Thanks! Can you run bun run format:fix && bun run lint:fix to format the code?

yusukebe avatar May 06 '24 00:05 yusukebe

Sure, thank you @yusukebe !

Amorim33 avatar May 06 '24 18:05 Amorim33

@Amorim33

Thanks! I'll merge this now.

yusukebe avatar May 06 '24 20:05 yusukebe