1 package transport_test
2
3 import (
4 "net/http"
5 "testing"
6
7 "github.com/stretchr/testify/assert"
8
9 "github.com/99designs/gqlgen/graphql/handler/testserver"
10 "github.com/99designs/gqlgen/graphql/handler/transport"
11 )
12
13 func TestGET(t *testing.T) {
14 h := testserver.New()
15 h.AddTransport(transport.GET{})
16
17 t.Run("success", func(t *testing.T) {
18 resp := doRequest(h, "GET", "/graphql?query={name}", ``, "application/json")
19 assert.Equal(t, http.StatusOK, resp.Code, resp.Body.String())
20 assert.Equal(t, `{"data":{"name":"test"}}`, resp.Body.String())
21 })
22
23 t.Run("has json content-type header", func(t *testing.T) {
24 resp := doRequest(h, "GET", "/graphql?query={name}", ``, "application/json")
25 assert.Equal(t, "application/json", resp.Header().Get("Content-Type"))
26 })
27
28 t.Run("decode failure", func(t *testing.T) {
29 resp := doRequest(h, "GET", "/graphql?query={name}&variables=notjson", "", "application/json")
30 assert.Equal(t, http.StatusBadRequest, resp.Code, resp.Body.String())
31 assert.Equal(t, `{"errors":[{"message":"variables could not be decoded"}],"data":null}`, resp.Body.String())
32 })
33
34 t.Run("invalid variable", func(t *testing.T) {
35 resp := doRequest(h, "GET", `/graphql?query=query($id:Int!){find(id:$id)}&variables={"id":false}`, "", "application/json")
36 assert.Equal(t, http.StatusUnprocessableEntity, resp.Code, resp.Body.String())
37 assert.Equal(t, `{"errors":[{"message":"cannot use bool as Int","path":["variable","id"],"extensions":{"code":"GRAPHQL_VALIDATION_FAILED"}}],"data":null}`, resp.Body.String())
38 })
39
40 t.Run("parse failure", func(t *testing.T) {
41 resp := doRequest(h, "GET", "/graphql?query=!", "", "application/json")
42 assert.Equal(t, http.StatusUnprocessableEntity, resp.Code, resp.Body.String())
43 assert.Equal(t, `{"errors":[{"message":"Unexpected !","locations":[{"line":1,"column":1}],"extensions":{"code":"GRAPHQL_PARSE_FAILED"}}],"data":null}`, resp.Body.String())
44 })
45
46 t.Run("no mutations", func(t *testing.T) {
47 resp := doRequest(h, "GET", "/graphql?query=mutation{name}", "", "application/json")
48 assert.Equal(t, http.StatusNotAcceptable, resp.Code, resp.Body.String())
49 assert.Equal(t, `{"errors":[{"message":"GET requests only allow query operations"}],"data":null}`, resp.Body.String())
50 })
51 }
52
View as plain text