github.com/maeglindeveloper/gqlgen@v0.13.1-0.20210413081235-57808b12a0a0/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("has json content-type header", func(t *testing.T) {
    23  		resp := doRequest(h, "GET", "/graphql?query={name}", ``)
    24  		assert.Equal(t, "application/json", resp.Header().Get("Content-Type"))
    25  	})
    26  
    27  	t.Run("decode failure", func(t *testing.T) {
    28  		resp := doRequest(h, "GET", "/graphql?query={name}&variables=notjson", "")
    29  		assert.Equal(t, http.StatusBadRequest, resp.Code, resp.Body.String())
    30  		assert.Equal(t, `{"errors":[{"message":"variables could not be decoded"}],"data":null}`, resp.Body.String())
    31  	})
    32  
    33  	t.Run("invalid variable", func(t *testing.T) {
    34  		resp := doRequest(h, "GET", `/graphql?query=query($id:Int!){find(id:$id)}&variables={"id":false}`, "")
    35  		assert.Equal(t, http.StatusUnprocessableEntity, resp.Code, resp.Body.String())
    36  		assert.Equal(t, `{"errors":[{"message":"cannot use bool as Int","path":["variable","id"],"extensions":{"code":"GRAPHQL_VALIDATION_FAILED"}}],"data":null}`, resp.Body.String())
    37  	})
    38  
    39  	t.Run("parse failure", func(t *testing.T) {
    40  		resp := doRequest(h, "GET", "/graphql?query=!", "")
    41  		assert.Equal(t, http.StatusUnprocessableEntity, resp.Code, resp.Body.String())
    42  		assert.Equal(t, `{"errors":[{"message":"Unexpected !","locations":[{"line":1,"column":1}],"extensions":{"code":"GRAPHQL_PARSE_FAILED"}}],"data":null}`, resp.Body.String())
    43  	})
    44  
    45  	t.Run("no mutations", func(t *testing.T) {
    46  		resp := doRequest(h, "GET", "/graphql?query=mutation{name}", "")
    47  		assert.Equal(t, http.StatusNotAcceptable, resp.Code, resp.Body.String())
    48  		assert.Equal(t, `{"errors":[{"message":"GET requests only allow query operations"}],"data":null}`, resp.Body.String())
    49  	})
    50  
    51  }