Merge pull request #13034 from mattclegg/issue-9507

ISSUE-9507: ADD `application/gzip,application/octet-stream` accept header
pull/13213/head
Robert Sirchia 11 months ago committed by GitHub
commit 5ae91e11ae
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -97,6 +97,8 @@ func (c *ChartDownloader) DownloadTo(ref, version, dest string) (string, *proven
return "", nil, err
}
c.Options = append(c.Options, getter.WithAcceptHeader("application/gzip,application/octet-stream"))
data, err := g.Get(u.String(), c.Options...)
if err != nil {
return "", nil, err

@ -38,6 +38,7 @@ type options struct {
unTar bool
insecureSkipVerifyTLS bool
plainHTTP bool
acceptHeader string
username string
password string
passCredentialsAll bool
@ -60,6 +61,13 @@ func WithURL(url string) Option {
}
}
// WithAcceptHeader sets the request's Accept header as some REST APIs serve multiple content types
func WithAcceptHeader(header string) Option {
return func(opts *options) {
opts.acceptHeader = header
}
}
// WithBasicAuth sets the request's Authorization header to use the provided credentials
func WithBasicAuth(username, password string) Option {
return func(opts *options) {

@ -53,6 +53,10 @@ func (g *HTTPGetter) get(href string) (*bytes.Buffer, error) {
return nil, err
}
if g.opts.acceptHeader != "" {
req.Header.Set("Accept", g.opts.acceptHeader)
}
req.Header.Set("User-Agent", version.GetUserAgent())
if g.opts.userAgent != "" {
req.Header.Set("User-Agent", g.opts.userAgent)

@ -280,6 +280,29 @@ func TestDownload(t *testing.T) {
if got.String() != expect {
t.Errorf("Expected %q, got %q", expect, got.String())
}
// test server with varied Accept Header
const expectedAcceptHeader = "application/gzip,application/octet-stream"
acceptHeaderSrv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("Accept") != expectedAcceptHeader {
t.Errorf("Expected '%s', got '%s'", expectedAcceptHeader, r.Header.Get("Accept"))
}
fmt.Fprint(w, expect)
}))
defer acceptHeaderSrv.Close()
u, _ = url.ParseRequestURI(acceptHeaderSrv.URL)
httpgetter, err = NewHTTPGetter(
WithAcceptHeader(expectedAcceptHeader),
)
if err != nil {
t.Fatal(err)
}
_, err = httpgetter.Get(u.String())
if err != nil {
t.Fatal(err)
}
}
func TestDownloadTLS(t *testing.T) {

Loading…
Cancel
Save