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

chore: add unit tests to headerforwarder package #512

Merged
merged 5 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions headerforwarder/forwarder.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,14 @@ func (hf *HeaderForwarder) rememberHeaders(req *http.Request, resp *http.Respons

// Only remember interesting headers
for _, interestingHeader := range hf.interestingHeaders {
headersToRemember.Set(interestingHeader, resp.Header.Get(interestingHeader))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this line was overwriting each header value, not adding

values := resp.Header.Values(interestingHeader)

// Only remember the header if it is not empty
if len(values) > 0 {
for _, v := range values {
headersToRemember.Add(interestingHeader, v)
}
}
}

hf.incomingHeaderLock.Lock()
Expand Down Expand Up @@ -176,7 +183,7 @@ func (hf *HeaderForwarder) rememberMetadata(ctx context.Context, resp metadata.M

for _, interestingHeader := range hf.interestingHeaders {
for _, value := range resp.Get(strings.ToLower(interestingHeader)) {
headersToRemember.Set(interestingHeader, value)
headersToRemember.Add(interestingHeader, value)
}
}

Expand All @@ -187,7 +194,7 @@ func (hf *HeaderForwarder) rememberMetadata(ctx context.Context, resp metadata.M

// GetResponseHeaders returns any headers that should be returned to a rosetta response. These
// consist of native node response headers/metadata that were remembered for a request ID.
func (hf *HeaderForwarder) getResponseHeaders(rosettaRequestID string) (http.Header, bool) {
func (hf *HeaderForwarder) GetResponseHeaders(rosettaRequestID string) (http.Header, bool) {
hf.incomingHeaderLock.RLock()
headers, ok := hf.incomingHeaders[rosettaRequestID]
hf.incomingHeaderLock.RUnlock()
Expand Down Expand Up @@ -223,7 +230,7 @@ func (hf *HeaderForwarder) HeaderForwarderHandler(next http.Handler) http.Handle
wrappedResponseWriter := NewResponseWriter(
w,
RosettaIDFromRequest(requestWithID),
hf.getResponseHeaders,
hf.GetResponseHeaders,
)

// Serve the request
Expand Down Expand Up @@ -258,7 +265,7 @@ func (hf *HeaderForwarder) RoundTrip(req *http.Request) (*http.Response, error)

func (hf *HeaderForwarder) UnaryClientInterceptor(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
// Capture incoming headers from the grpc call
var header metadata.MD
header := make(metadata.MD)
opts = append(opts, grpc.Header(&header))

// Get outgoing headers from the request ID in context
Expand Down
Loading
Loading