adding in softer check for content type (#14097)

This commit is contained in:
james-prysm
2024-06-10 12:15:23 -05:00
committed by GitHub
parent b7866be3a9
commit dfe31c9242
4 changed files with 17 additions and 3 deletions

View File

@@ -109,7 +109,7 @@ func decodeResp(httpResp *http.Response, resp interface{}) error {
return errors.Wrapf(err, "failed to read response body for %s", httpResp.Request.URL)
}
if httpResp.Header.Get("Content-Type") != api.JsonMediaType {
if !strings.Contains(httpResp.Header.Get("Content-Type"), api.JsonMediaType) {
// 2XX codes are a success
if strings.HasPrefix(httpResp.Status, "2") {
return nil

View File

@@ -99,7 +99,16 @@ func Test_decodeResp(t *testing.T) {
type j struct {
Foo string `json:"foo"`
}
t.Run("200 JSON with charset", func(t *testing.T) {
body := bytes.Buffer{}
r := &http.Response{
Status: "200",
StatusCode: http.StatusOK,
Body: io.NopCloser(&body),
Header: map[string][]string{"Content-Type": {"application/json; charset=utf-8"}},
}
require.NoError(t, decodeResp(r, nil))
})
t.Run("200 non-JSON", func(t *testing.T) {
body := bytes.Buffer{}
r := &http.Response{