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

Prevent adding Accept header when already one. #81

Merged
merged 1 commit into from
Dec 16, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
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