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

feat(gensupport): pass in headers via context #2052

Merged
merged 6 commits into from
Jul 7, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
13 changes: 13 additions & 0 deletions internal/gensupport/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

"github.com/google/uuid"
"github.com/googleapis/gax-go/v2"
"github.com/googleapis/gax-go/v2/callctx"
)

// Use this error type to return an error which allows introspection of both
Expand Down Expand Up @@ -43,6 +44,12 @@ func (e wrappedCallErr) Is(target error) bool {
// req.WithContext, then calls any functions returned by the hooks in
// reverse order.
func SendRequest(ctx context.Context, client *http.Client, req *http.Request) (*http.Response, error) {
// Add headers set in context metadata.
headers := callctx.HeadersFromContext(ctx)
for k, v := range headers {
req.Header[k] = append(req.Header[k], v...)
tritone marked this conversation as resolved.
Show resolved Hide resolved
}

// Disallow Accept-Encoding because it interferes with the automatic gzip handling
// done by the default http.Transport. See https://github.com/google/google-api-go-client/issues/219.
if _, ok := req.Header["Accept-Encoding"]; ok {
Expand Down Expand Up @@ -77,6 +84,12 @@ func send(ctx context.Context, client *http.Client, req *http.Request) (*http.Re
// req.WithContext, then calls any functions returned by the hooks in
// reverse order.
func SendRequestWithRetry(ctx context.Context, client *http.Client, req *http.Request, retry *RetryConfig) (*http.Response, error) {
// Add headers set in context metadata.
headers := callctx.HeadersFromContext(ctx)
for k, v := range headers {
req.Header[k] = append(req.Header[k], v...)
tritone marked this conversation as resolved.
Show resolved Hide resolved
}

// Disallow Accept-Encoding because it interferes with the automatic gzip handling
// done by the default http.Transport. See https://github.com/google/google-api-go-client/issues/219.
if _, ok := req.Header["Accept-Encoding"]; ok {
Expand Down
36 changes: 36 additions & 0 deletions internal/gensupport/send_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ package gensupport
import (
"context"
"errors"
"fmt"
"github.com/google/go-cmp/cmp"
"github.com/googleapis/gax-go/v2/callctx"
"net/http"
"testing"
)
Expand All @@ -31,6 +34,39 @@ func TestSendRequestWithRetry(t *testing.T) {
}
}

type headerRoundTripper struct {
wantHeader http.Header
}

func (rt *headerRoundTripper) RoundTrip(r *http.Request) (*http.Response, error) {
// Ignore x-goog headers sent by SendRequestWithRetry
delete(r.Header, "X-Goog-Api-Client")
delete(r.Header, "X-Goog-Gcs-Idempotency-Token")
tritone marked this conversation as resolved.
Show resolved Hide resolved
if diff := cmp.Diff(r.Header, rt.wantHeader); diff != "" {
return nil, fmt.Errorf("headers don't match: %v", diff)
}
return &http.Response{StatusCode: 200}, nil
}

// Ensure that headers set via the context are passed through to the request as expected.
func TestSendRequestHeader(t *testing.T) {
ctx := context.Background()
ctx = callctx.SetHeaders(ctx, "foo", "100", "bar", "200")
client := http.Client{
Transport: &headerRoundTripper{
wantHeader: map[string][]string{"foo": {"100"}, "bar": {"200"}},
},
}
req, _ := http.NewRequest("GET", "url", nil)
if _, err := SendRequest(ctx, &client, req); err != nil {
t.Errorf("SendRequest: %v", err)
}
req2, _ := http.NewRequest("GET", "url", nil)
if _, err := SendRequestWithRetry(ctx, &client, req2, nil); err != nil {
t.Errorf("SendRequest: %v", err)
}
}

type brokenRoundTripper struct{}

func (t *brokenRoundTripper) RoundTrip(r *http.Request) (*http.Response, error) {
Expand Down