From 4a62f0121af3a730fa57bf9f9124beaf6a0809d7 Mon Sep 17 00:00:00 2001 From: David Douglas Date: Sun, 14 Jun 2020 04:53:02 +0000 Subject: [PATCH 1/2] Adding ContentType header to GET request responses --- graphql/handler/transport/http_get.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/graphql/handler/transport/http_get.go b/graphql/handler/transport/http_get.go index 35d9a58be4c..d97c89c63fd 100644 --- a/graphql/handler/transport/http_get.go +++ b/graphql/handler/transport/http_get.go @@ -27,6 +27,8 @@ func (h GET) Supports(r *http.Request) bool { } func (h GET) Do(w http.ResponseWriter, r *http.Request, exec graphql.GraphExecutor) { + w.Header().Set("Content-Type", "application/json") + raw := &graphql.RawParams{ Query: r.URL.Query().Get("query"), OperationName: r.URL.Query().Get("operationName"), From e19d43bcb46df36bf6d8d8699788bfd929a596f5 Mon Sep 17 00:00:00 2001 From: David Douglas Date: Sun, 14 Jun 2020 05:49:06 +0000 Subject: [PATCH 2/2] Adding test --- graphql/handler/transport/http_get_test.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/graphql/handler/transport/http_get_test.go b/graphql/handler/transport/http_get_test.go index 121dc3f6906..3e993c1e0b7 100644 --- a/graphql/handler/transport/http_get_test.go +++ b/graphql/handler/transport/http_get_test.go @@ -19,6 +19,11 @@ func TestGET(t *testing.T) { assert.Equal(t, `{"data":{"name":"test"}}`, resp.Body.String()) }) + t.Run("has json content-type header", func(t *testing.T) { + resp := doRequest(h, "GET", "/graphql?query={name}", ``) + assert.Equal(t, "application/json", resp.Header().Get("Content-Type")) + }) + t.Run("decode failure", func(t *testing.T) { resp := doRequest(h, "GET", "/graphql?query={name}&variables=notjson", "") assert.Equal(t, http.StatusBadRequest, resp.Code, resp.Body.String()) @@ -42,4 +47,5 @@ func TestGET(t *testing.T) { assert.Equal(t, http.StatusNotAcceptable, resp.Code, resp.Body.String()) assert.Equal(t, `{"errors":[{"message":"GET requests only allow query operations"}],"data":null}`, resp.Body.String()) }) + }