github.com/kyma-project/kyma-environment-broker@v0.0.1/internal/third_party/machinebox/graphql/graphql_json_test.go (about)

     1  package graphql
     2  
     3  import (
     4  	"context"
     5  	"io"
     6  	"io/ioutil"
     7  	"net/http"
     8  	"net/http/httptest"
     9  	"testing"
    10  	"time"
    11  
    12  	"github.com/matryer/is"
    13  )
    14  
    15  func TestDoJSON(t *testing.T) {
    16  	is := is.New(t)
    17  	var calls int
    18  	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    19  		calls++
    20  		is.Equal(r.Method, http.MethodPost)
    21  		b, err := ioutil.ReadAll(r.Body)
    22  		is.NoErr(err)
    23  		is.Equal(string(b), `{"query":"query {}","variables":null}`+"\n")
    24  		io.WriteString(w, `{
    25  			"data": {
    26  				"something": "yes"
    27  			}
    28  		}`)
    29  	}))
    30  	defer srv.Close()
    31  
    32  	ctx := context.Background()
    33  	client := NewClient(srv.URL)
    34  
    35  	ctx, cancel := context.WithTimeout(ctx, 1*time.Second)
    36  	defer cancel()
    37  	var responseData map[string]interface{}
    38  	err := client.Run(ctx, &Request{q: "query {}"}, &responseData)
    39  	is.NoErr(err)
    40  	is.Equal(calls, 1) // calls
    41  	is.Equal(responseData["something"], "yes")
    42  }
    43  
    44  func TestDoJSONServerError(t *testing.T) {
    45  	is := is.New(t)
    46  	var calls int
    47  	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    48  		calls++
    49  		is.Equal(r.Method, http.MethodPost)
    50  		b, err := ioutil.ReadAll(r.Body)
    51  		is.NoErr(err)
    52  		is.Equal(string(b), `{"query":"query {}","variables":null}`+"\n")
    53  		w.WriteHeader(http.StatusInternalServerError)
    54  		io.WriteString(w, `Internal Server Error`)
    55  	}))
    56  	defer srv.Close()
    57  
    58  	ctx := context.Background()
    59  	client := NewClient(srv.URL)
    60  
    61  	ctx, cancel := context.WithTimeout(ctx, 1*time.Second)
    62  	defer cancel()
    63  	var responseData map[string]interface{}
    64  	err := client.Run(ctx, &Request{q: "query {}"}, &responseData)
    65  	is.Equal(calls, 1) // calls
    66  	is.Equal(err.Error(), "graphql: server returned a non-200 status code: 500")
    67  }
    68  
    69  func TestDoJSONBadRequestErr(t *testing.T) {
    70  	is := is.New(t)
    71  	var calls int
    72  	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    73  		calls++
    74  		is.Equal(r.Method, http.MethodPost)
    75  		b, err := ioutil.ReadAll(r.Body)
    76  		is.NoErr(err)
    77  		is.Equal(string(b), `{"query":"query {}","variables":null}`+"\n")
    78  		w.WriteHeader(http.StatusBadRequest)
    79  		io.WriteString(w, `{
    80  			"errors": [{
    81  				"message": "miscellaneous message as to why the the request was bad"
    82  			}]
    83  		}`)
    84  	}))
    85  	defer srv.Close()
    86  
    87  	ctx := context.Background()
    88  	client := NewClient(srv.URL)
    89  
    90  	ctx, cancel := context.WithTimeout(ctx, 1*time.Second)
    91  	defer cancel()
    92  	var responseData map[string]interface{}
    93  	err := client.Run(ctx, &Request{q: "query {}"}, &responseData)
    94  	is.Equal(calls, 1) // calls
    95  	is.Equal(err.Error(), "graphql: miscellaneous message as to why the the request was bad")
    96  }
    97  
    98  func TestDoJSONErrWithExtensions(t *testing.T) {
    99  	is := is.New(t)
   100  	var calls int
   101  	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   102  		calls++
   103  		is.Equal(r.Method, http.MethodPost)
   104  		b, err := ioutil.ReadAll(r.Body)
   105  		is.NoErr(err)
   106  		is.Equal(string(b), `{"query":"query {}","variables":null}`+"\n")
   107  		w.WriteHeader(http.StatusOK)
   108  		io.WriteString(w, `{
   109  			"errors": [{
   110  				"message": "miscellaneous message as to why the the request was bad",
   111  				"extensions": {
   112  					"code": "400"
   113  				}
   114  			}]
   115  		}`)
   116  	}))
   117  	defer srv.Close()
   118  
   119  	ctx := context.Background()
   120  	client := NewClient(srv.URL)
   121  
   122  	ctx, cancel := context.WithTimeout(ctx, 1*time.Second)
   123  	defer cancel()
   124  	var responseData map[string]interface{}
   125  	err := client.Run(ctx, &Request{q: "query {}"}, &responseData)
   126  	is.Equal(calls, 1) // calls
   127  	is.Equal(err.Error(), "graphql: miscellaneous message as to why the the request was bad")
   128  	is.Equal(err.(ExtendedError).Extensions()["code"], "400")
   129  }
   130  
   131  func TestQueryJSON(t *testing.T) {
   132  	is := is.New(t)
   133  
   134  	var calls int
   135  	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   136  		calls++
   137  		b, err := ioutil.ReadAll(r.Body)
   138  		is.NoErr(err)
   139  		is.Equal(string(b), `{"query":"query {}","variables":{"username":"matryer"}}`+"\n")
   140  		_, err = io.WriteString(w, `{"data":{"value":"some data"}}`)
   141  		is.NoErr(err)
   142  	}))
   143  	defer srv.Close()
   144  	ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
   145  	defer cancel()
   146  
   147  	client := NewClient(srv.URL)
   148  
   149  	req := NewRequest("query {}")
   150  	req.Var("username", "matryer")
   151  
   152  	// check variables
   153  	is.True(req != nil)
   154  	is.Equal(req.vars["username"], "matryer")
   155  
   156  	var resp struct {
   157  		Value string
   158  	}
   159  	err := client.Run(ctx, req, &resp)
   160  	is.NoErr(err)
   161  	is.Equal(calls, 1)
   162  
   163  	is.Equal(resp.Value, "some data")
   164  }
   165  
   166  func TestHeader(t *testing.T) {
   167  	is := is.New(t)
   168  
   169  	var calls int
   170  	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   171  		calls++
   172  		is.Equal(r.Header.Get("X-Custom-Header"), "123")
   173  
   174  		_, err := io.WriteString(w, `{"data":{"value":"some data"}}`)
   175  		is.NoErr(err)
   176  	}))
   177  	defer srv.Close()
   178  	ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
   179  	defer cancel()
   180  
   181  	client := NewClient(srv.URL)
   182  
   183  	req := NewRequest("query {}")
   184  	req.Header.Set("X-Custom-Header", "123")
   185  
   186  	var resp struct {
   187  		Value string
   188  	}
   189  	err := client.Run(ctx, req, &resp)
   190  	is.NoErr(err)
   191  	is.Equal(calls, 1)
   192  
   193  	is.Equal(resp.Value, "some data")
   194  }