Skip to content

Commit

Permalink
prevents adding Accept header when already one.
Browse files Browse the repository at this point in the history
  • Loading branch information
Eclion authored and wlynch committed Dec 16, 2022
1 parent 7a488cb commit 185b910
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
7 changes: 5 additions & 2 deletions transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,18 @@ func (t *Transport) RoundTrip(req *http.Request) (*http.Response, error) {
}
}()
}

token, err := t.Token(req.Context())
if err != nil {
return nil, err
}

creq := cloneRequest(req) // per RoundTripper contract
creq.Header.Set("Authorization", "token "+token)
creq.Header.Add("Accept", acceptHeader) // We add to "Accept" header to avoid overwriting existing req headers.

if creq.Header.Get("Accept") == "" { // We only add an "Accept" header to avoid overwriting the expected behavior.
creq.Header.Add("Accept", acceptHeader)
}
reqBodyClosed = true // req.Body is assumed to be closed by the tr RoundTripper.
resp, err := t.tr.RoundTrip(creq)
return resp, err
Expand Down
12 changes: 12 additions & 0 deletions transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,18 @@ func TestNew_appendHeader(t *testing.T) {
if !found {
t.Errorf("could not find %v in request's accept headers: %v", myheader, headers["Accept"])
}

// Here we test that there isn't a second Accept header.
// Though the Accept header 'application/vnd.github.v3+json' is used for most
// interactions with the GitHub API, having this header will force the
// GitHub API response as JSON, which we don't want when downloading a
// release (octet-stream)
for _, v := range headers["Accept"] {
if v == acceptHeader {
t.Errorf("accept header '%s' should not be present when accept header '%s' is set: %v", acceptHeader, myheader, headers["Accept"])
break
}
}
}

func TestRefreshTokenWithParameters(t *testing.T) {
Expand Down

0 comments on commit 185b910

Please sign in to comment.