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

Fix set header to JSON earlier in GraphQL response #532

Merged
merged 1 commit into from
Feb 5, 2019
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
2 changes: 1 addition & 1 deletion handler/graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@ func (gh *graphqlHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}

w.Header().Set("Content-Type", "application/json")
var reqParams params
switch r.Method {
case http.MethodGet:
Expand All @@ -332,7 +333,6 @@ func (gh *graphqlHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
w.Header().Set("Content-Type", "application/json")

ctx := r.Context()

Expand Down
5 changes: 5 additions & 0 deletions handler/graphql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,30 +45,35 @@ func TestHandlerPOST(t *testing.T) {
t.Run("decode failure", func(t *testing.T) {
resp := doRequest(h, "POST", "/graphql", "notjson")
assert.Equal(t, http.StatusBadRequest, resp.Code)
assert.Equal(t, resp.HeaderMap.Get("Content-Type"), "application/json")
assert.Equal(t, `{"errors":[{"message":"json body could not be decoded: invalid character 'o' in literal null (expecting 'u')"}],"data":null}`, resp.Body.String())
})

t.Run("parse failure", func(t *testing.T) {
resp := doRequest(h, "POST", "/graphql", `{"query": "!"}`)
assert.Equal(t, http.StatusUnprocessableEntity, resp.Code)
assert.Equal(t, resp.HeaderMap.Get("Content-Type"), "application/json")
assert.Equal(t, `{"errors":[{"message":"Unexpected !","locations":[{"line":1,"column":1}]}],"data":null}`, resp.Body.String())
})

t.Run("validation failure", func(t *testing.T) {
resp := doRequest(h, "POST", "/graphql", `{"query": "{ me { title }}"}`)
assert.Equal(t, http.StatusUnprocessableEntity, resp.Code)
assert.Equal(t, resp.HeaderMap.Get("Content-Type"), "application/json")
assert.Equal(t, `{"errors":[{"message":"Cannot query field \"title\" on type \"User\".","locations":[{"line":1,"column":8}]}],"data":null}`, resp.Body.String())
})

t.Run("invalid variable", func(t *testing.T) {
resp := doRequest(h, "POST", "/graphql", `{"query": "query($id:Int!){user(id:$id){name}}","variables":{"id":false}}`)
assert.Equal(t, http.StatusUnprocessableEntity, resp.Code)
assert.Equal(t, resp.HeaderMap.Get("Content-Type"), "application/json")
assert.Equal(t, `{"errors":[{"message":"cannot use bool as Int","path":["variable","id"]}],"data":null}`, resp.Body.String())
})

t.Run("execution failure", func(t *testing.T) {
resp := doRequest(h, "POST", "/graphql", `{"query": "mutation { me { name } }"}`)
assert.Equal(t, http.StatusOK, resp.Code)
assert.Equal(t, resp.HeaderMap.Get("Content-Type"), "application/json")
assert.Equal(t, `{"errors":[{"message":"mutations are not supported"}],"data":null}`, resp.Body.String())
})
}
Expand Down