Skip to content

Commit

Permalink
adding in softer check for content type (#14097)
Browse files Browse the repository at this point in the history
  • Loading branch information
james-prysm authored Jun 10, 2024
1 parent b7866be commit dfe31c9
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 3 deletions.
2 changes: 1 addition & 1 deletion api/server/middleware/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func ContentTypeHandler(acceptedMediaTypes []string) mux.MiddlewareFunc {

accepted := false
for _, acceptedType := range acceptedMediaTypes {
if strings.TrimSpace(contentType) == strings.TrimSpace(acceptedType) {
if strings.Contains(strings.TrimSpace(contentType), strings.TrimSpace(acceptedType)) {
accepted = true
break
}
Expand Down
5 changes: 5 additions & 0 deletions api/server/middleware/middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ func TestContentTypeHandler(t *testing.T) {
expectedStatusCode: http.StatusOK,
isGet: true,
},
{
name: "Content type contains charset is ok",
contentType: "application/json; charset=utf-8",
expectedStatusCode: http.StatusOK,
},
}

for _, tt := range tests {
Expand Down
2 changes: 1 addition & 1 deletion validator/client/beacon-api/json_rest_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 10 additions & 1 deletion validator/client/beacon-api/json_rest_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down

0 comments on commit dfe31c9

Please sign in to comment.