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