Skip to content

Commit

Permalink
Adds tests for GRAPHQL transport response headers.
Browse files Browse the repository at this point in the history
GRAPHQL transport (like GET, POST and MULTIPART transports) can
have specific response headers added.

This commit adds tests for it and changes doRequest() method
so that we can set inbound Content-Type. Graphql transport
uses 'application/graphql' content-type and not 'application/json'.
  • Loading branch information
RatkoR committed Mar 25, 2023
1 parent 8cac3b5 commit a270f35
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 25 deletions.
36 changes: 32 additions & 4 deletions graphql/handler/transport/headers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestHeadersWithPOST(t *testing.T) {
h := testserver.New()
h.AddTransport(transport.POST{})

resp := doRequest(h, "POST", "/graphql", `{"query":"{ name }"}`)
resp := doRequest(h, "POST", "/graphql", `{"query":"{ name }"}`, "application/json")
assert.Equal(t, http.StatusOK, resp.Code)
assert.Equal(t, 1, len(resp.Header()))
assert.Equal(t, "application/json", resp.Header().Get("Content-Type"))
Expand All @@ -36,7 +36,7 @@ func TestHeadersWithPOST(t *testing.T) {
h := testserver.New()
h.AddTransport(transport.POST{ResponseHeaders: headers})

resp := doRequest(h, "POST", "/graphql", `{"query":"{ name }"}`)
resp := doRequest(h, "POST", "/graphql", `{"query":"{ name }"}`, "application/json")
assert.Equal(t, http.StatusOK, resp.Code)
assert.Equal(t, 2, len(resp.Header()))
assert.Equal(t, "application/json; charset: utf8", resp.Header().Get("Content-Type"))
Expand All @@ -50,7 +50,7 @@ func TestHeadersWithGET(t *testing.T) {
h := testserver.New()
h.AddTransport(transport.GET{})

resp := doRequest(h, "GET", "/graphql?query={name}", "")
resp := doRequest(h, "GET", "/graphql?query={name}", "", "application/json")
assert.Equal(t, http.StatusOK, resp.Code)
assert.Equal(t, 1, len(resp.Header()))
assert.Equal(t, "application/json", resp.Header().Get("Content-Type"))
Expand All @@ -65,14 +65,42 @@ func TestHeadersWithGET(t *testing.T) {
h := testserver.New()
h.AddTransport(transport.GET{ResponseHeaders: headers})

resp := doRequest(h, "GET", "/graphql?query={name}", "")
resp := doRequest(h, "GET", "/graphql?query={name}", "", "application/json")
assert.Equal(t, http.StatusOK, resp.Code)
assert.Equal(t, 2, len(resp.Header()))
assert.Equal(t, "application/json; charset: utf8", resp.Header().Get("Content-Type"))
assert.Equal(t, "dummy-get", resp.Header().Get("Other-Header"))
})
}

func TestHeadersWithGRAPHQL(t *testing.T) {
t.Run("Headers not set", func(t *testing.T) {
h := testserver.New()
h.AddTransport(transport.GRAPHQL{})

resp := doRequest(h, "POST", "/graphql", `{ name }`, "application/graphql")
assert.Equal(t, http.StatusOK, resp.Code)
assert.Equal(t, 1, len(resp.Header()))
assert.Equal(t, "application/json", resp.Header().Get("Content-Type"))
})

t.Run("Headers set", func(t *testing.T) {
headers := map[string][]string{
"Content-Type": {"application/json; charset: utf8"},
"Other-Header": {"dummy-get-qraphql"},
}

h := testserver.New()
h.AddTransport(transport.GRAPHQL{ResponseHeaders: headers})

resp := doRequest(h, "POST", "/graphql", `{ name }`, "application/graphql")
assert.Equal(t, http.StatusOK, resp.Code)
assert.Equal(t, 2, len(resp.Header()))
assert.Equal(t, "application/json; charset: utf8", resp.Header().Get("Content-Type"))
assert.Equal(t, "dummy-get-qraphql", resp.Header().Get("Other-Header"))
})
}

func TestHeadersWithMULTIPART(t *testing.T) {
t.Run("Headers not set", func(t *testing.T) {
es := &graphql.ExecutableSchemaMock{
Expand Down
12 changes: 6 additions & 6 deletions graphql/handler/transport/http_get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,36 +14,36 @@ func TestGET(t *testing.T) {
h.AddTransport(transport.GET{})

t.Run("success", func(t *testing.T) {
resp := doRequest(h, "GET", "/graphql?query={name}", ``)
resp := doRequest(h, "GET", "/graphql?query={name}", ``, "application/json")
assert.Equal(t, http.StatusOK, resp.Code, resp.Body.String())
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}", ``)
resp := doRequest(h, "GET", "/graphql?query={name}", ``, "application/json")
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", "")
resp := doRequest(h, "GET", "/graphql?query={name}&variables=notjson", "", "application/json")
assert.Equal(t, http.StatusBadRequest, resp.Code, resp.Body.String())
assert.Equal(t, `{"errors":[{"message":"variables could not be decoded"}],"data":null}`, resp.Body.String())
})

t.Run("invalid variable", func(t *testing.T) {
resp := doRequest(h, "GET", `/graphql?query=query($id:Int!){find(id:$id)}&variables={"id":false}`, "")
resp := doRequest(h, "GET", `/graphql?query=query($id:Int!){find(id:$id)}&variables={"id":false}`, "", "application/json")
assert.Equal(t, http.StatusUnprocessableEntity, resp.Code, resp.Body.String())
assert.Equal(t, `{"errors":[{"message":"cannot use bool as Int","path":["variable","id"],"extensions":{"code":"GRAPHQL_VALIDATION_FAILED"}}],"data":null}`, resp.Body.String())
})

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

t.Run("no mutations", func(t *testing.T) {
resp := doRequest(h, "GET", "/graphql?query=mutation{name}", "")
resp := doRequest(h, "GET", "/graphql?query=mutation{name}", "", "application/json")
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())
})
Expand Down
20 changes: 8 additions & 12 deletions graphql/handler/transport/http_post_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,41 +17,41 @@ func TestPOST(t *testing.T) {
h.AddTransport(transport.POST{})

t.Run("success", func(t *testing.T) {
resp := doRequest(h, "POST", "/graphql", `{"query":"{ name }"}`)
resp := doRequest(h, "POST", "/graphql", `{"query":"{ name }"}`, "application/json")
assert.Equal(t, http.StatusOK, resp.Code)
assert.Equal(t, `{"data":{"name":"test"}}`, resp.Body.String())
})

t.Run("decode failure", func(t *testing.T) {
resp := doRequest(h, "POST", "/graphql", "notjson")
resp := doRequest(h, "POST", "/graphql", "notjson", "application/json")
assert.Equal(t, http.StatusBadRequest, resp.Code, resp.Body.String())
assert.Equal(t, resp.Header().Get("Content-Type"), "application/json")
assert.Equal(t, `{"errors":[{"message":"json request body could not be decoded: invalid character 'o' in literal null (expecting 'u') body:notjson"}],"data":null}`, resp.Body.String())
})

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

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

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

t.Run("execution failure", func(t *testing.T) {
resp := doRequest(h, "POST", "/graphql", `{"query": "mutation { name }"}`)
resp := doRequest(h, "POST", "/graphql", `{"query": "mutation { name }"}`, "application/json")
assert.Equal(t, http.StatusOK, resp.Code, resp.Body.String())
assert.Equal(t, resp.Header().Get("Content-Type"), "application/json")
assert.Equal(t, `{"errors":[{"message":"mutations are not supported"}],"data":null}`, resp.Body.String())
Expand Down Expand Up @@ -85,10 +85,6 @@ func TestPOST(t *testing.T) {
invalidContentTypes := []string{
"",
"text/plain",

// These content types are currently not supported, but they are supported by other GraphQL servers, like express-graphql.
"application/x-www-form-urlencoded",
"application/graphql",
}

for _, tc := range invalidContentTypes {
Expand All @@ -101,9 +97,9 @@ func TestPOST(t *testing.T) {
})
}

func doRequest(handler http.Handler, method string, target string, body string) *httptest.ResponseRecorder {
func doRequest(handler http.Handler, method string, target string, body string, contentType string) *httptest.ResponseRecorder {
r := httptest.NewRequest(method, target, strings.NewReader(body))
r.Header.Set("Content-Type", "application/json")
r.Header.Set("Content-Type", contentType)
w := httptest.NewRecorder()

handler.ServeHTTP(w, r)
Expand Down
6 changes: 3 additions & 3 deletions graphql/handler/transport/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func TestOptions(t *testing.T) {
t.Run("responds to options requests with default methods", func(t *testing.T) {
h := testserver.New()
h.AddTransport(transport.Options{})
resp := doRequest(h, "OPTIONS", "/graphql?query={me{name}}", ``)
resp := doRequest(h, "OPTIONS", "/graphql?query={me{name}}", ``, "application/json")
assert.Equal(t, http.StatusOK, resp.Code)
assert.Equal(t, "OPTIONS, GET, POST", resp.Header().Get("Allow"))
})
Expand All @@ -24,15 +24,15 @@ func TestOptions(t *testing.T) {
h.AddTransport(transport.Options{
AllowedMethods: []string{http.MethodOptions, http.MethodPost, http.MethodHead},
})
resp := doRequest(h, "OPTIONS", "/graphql?query={me{name}}", ``)
resp := doRequest(h, "OPTIONS", "/graphql?query={me{name}}", ``, "application/json")
assert.Equal(t, http.StatusOK, resp.Code)
assert.Equal(t, "OPTIONS, POST, HEAD", resp.Header().Get("Allow"))
})

t.Run("responds to head requests", func(t *testing.T) {
h := testserver.New()
h.AddTransport(transport.Options{})
resp := doRequest(h, "HEAD", "/graphql?query={me{name}}", ``)
resp := doRequest(h, "HEAD", "/graphql?query={me{name}}", ``, "application/json")
assert.Equal(t, http.StatusMethodNotAllowed, resp.Code)
})
}

0 comments on commit a270f35

Please sign in to comment.