Skip to content

Commit

Permalink
fix bug: POST Insufficient rigorous judgment leads to invalid SSE (#3153
Browse files Browse the repository at this point in the history
)

Co-authored-by: alex <[email protected]>
  • Loading branch information
o8x and alex authored Jun 22, 2024
1 parent 7c8bc50 commit feab5f5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
4 changes: 4 additions & 0 deletions graphql/handler/transport/http_post.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ func (h POST) Supports(r *http.Request) bool {
return false
}

if strings.Contains(r.Header.Get("Accept"), "text/event-stream") {
return false
}

mediaType, _, err := mime.ParseMediaType(r.Header.Get("Content-Type"))
if err != nil {
return false
Expand Down
16 changes: 16 additions & 0 deletions graphql/handler/transport/http_post_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,22 @@ func TestPOST(t *testing.T) {
})
}
})

t.Run("validate SSE", func(t *testing.T) {
doReq := func(handler http.Handler, method string, target string, body string) *httptest.ResponseRecorder {
r := httptest.NewRequest(method, target, strings.NewReader(body))
r.Header.Set("Content-Type", "application/json")
r.Header.Set("Accept", "text/event-stream")
w := httptest.NewRecorder()

handler.ServeHTTP(w, r)
return w
}

resp := doReq(h, "POST", "/graphql", `{"query":"{ name }"}`)
assert.Equal(t, http.StatusBadRequest, resp.Code, resp.Body.String())
assert.Equal(t, `{"errors":[{"message":"transport not supported"}],"data":null}`, resp.Body.String())
})
}

func doRequest(handler http.Handler, method, target, body, contentType string) *httptest.ResponseRecorder {
Expand Down

0 comments on commit feab5f5

Please sign in to comment.