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(router): Handle repeated headers in response #1537

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

cmtm
Copy link

@cmtm cmtm commented Jan 27, 2025

When multiple headers with the same name were sent in the response from
a subgraph, only one would be included in the response to the client
when forwarding headers. Fix it so that they're all forwarded.

@StarpTech
Copy link
Contributor

Hi @cmtm, thanks for the PR. Please add an integration test to verify the correct behaviour. Example: https://github.com/wundergraph/cosmo/blob/main/router-tests/header_set_test.go

@StarpTech StarpTech added the internally-reviewed The issue has been reviewed internally. label Jan 28, 2025
@cmtm cmtm force-pushed the forward-multiple-headers branch from 0f48af5 to 32268e5 Compare January 28, 2025 20:10
@cmtm
Copy link
Author

cmtm commented Jan 28, 2025

I've added a unit test. I've been unable to run any integration tests locally.
https://discord.com/channels/738739428314316823/1333891899551121438

Is the unit test sufficient?

I've added the integration tests.

@cmtm cmtm force-pushed the forward-multiple-headers branch from 32268e5 to 32c5307 Compare January 29, 2025 23:35
return testenv.SubgraphsConfig{
Employees: testenv.SubgraphConfig{
Middleware: func(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set(header, valA)
w.Header()[header] = valA
Copy link
Member

Choose a reason for hiding this comment

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

Hi @cmtm

Manually modifying the underlying map is not fully equal to header.Set, because the wrapper always uses canonical form of the header name

The simplest fix will be

w.Header()[textproto.CanonicalMIMEHeaderKey(header)] = valA

Copy link
Contributor

Choose a reason for hiding this comment

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

We should write a test that verify it.

Copy link
Author

Choose a reason for hiding this comment

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

Done. I wrote a test for it, but I'm not sure how the http.Header type handles canonicalization internally. If it canonicalizes when deserializing an http request, we might not be able to properly test the canonicalization with it. We'd need to use a lower level construct.

Copy link
Author

Choose a reason for hiding this comment

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

Note that I didn't change the behavior of this handler, since it's okay if it doesn't do canonicalization.

When multiple headers with the same name were sent in the response from
a subgraph, only one would be included in the response to the client
when forwarding headers. Fix it so that they're all forwarded.
@cmtm cmtm force-pushed the forward-multiple-headers branch from 7f78495 to 3807826 Compare January 31, 2025 04:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
internally-reviewed The issue has been reviewed internally. router
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants