github.com/humans-group/gqlgen@v0.7.2/client/client_test.go (about)

     1  package client_test
     2  
     3  import (
     4  	"encoding/json"
     5  	"io/ioutil"
     6  	"net/http"
     7  	"net/http/httptest"
     8  	"testing"
     9  
    10  	"github.com/99designs/gqlgen/client"
    11  	"github.com/stretchr/testify/require"
    12  )
    13  
    14  func TestClient(t *testing.T) {
    15  	h := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    16  		b, err := ioutil.ReadAll(r.Body)
    17  		if err != nil {
    18  			panic(err)
    19  		}
    20  		require.Equal(t, `{"query":"user(id:$id){name}","variables":{"id":1}}`, string(b))
    21  
    22  		err = json.NewEncoder(w).Encode(map[string]interface{}{
    23  			"data": map[string]interface{}{
    24  				"name": "bob",
    25  			},
    26  		})
    27  		if err != nil {
    28  			panic(err)
    29  		}
    30  	}))
    31  
    32  	c := client.New(h.URL)
    33  
    34  	var resp struct {
    35  		Name string
    36  	}
    37  
    38  	c.MustPost("user(id:$id){name}", &resp, client.Var("id", 1))
    39  
    40  	require.Equal(t, "bob", resp.Name)
    41  }