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

     1  package graphql
     2  
     3  import (
     4  	"context"
     5  	"io"
     6  	"io/ioutil"
     7  	"net/http"
     8  	"net/http/httptest"
     9  	"strings"
    10  	"testing"
    11  	"time"
    12  
    13  	"github.com/matryer/is"
    14  )
    15  
    16  func TestWithClient(t *testing.T) {
    17  	is := is.New(t)
    18  	var calls int
    19  	testClient := &http.Client{
    20  		Transport: roundTripperFunc(func(req *http.Request) (*http.Response, error) {
    21  			calls++
    22  			resp := &http.Response{
    23  				Body: ioutil.NopCloser(strings.NewReader(`{"data":{"key":"value"}}`)),
    24  			}
    25  			return resp, nil
    26  		}),
    27  	}
    28  
    29  	ctx := context.Background()
    30  	client := NewClient("", WithHTTPClient(testClient), UseMultipartForm())
    31  
    32  	req := NewRequest(``)
    33  	client.Run(ctx, req, nil)
    34  
    35  	is.Equal(calls, 1) // calls
    36  }
    37  
    38  func TestDoUseMultipartForm(t *testing.T) {
    39  	is := is.New(t)
    40  	var calls int
    41  	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    42  		calls++
    43  		is.Equal(r.Method, http.MethodPost)
    44  		query := r.FormValue("query")
    45  		is.Equal(query, `query {}`)
    46  		io.WriteString(w, `{
    47  			"data": {
    48  				"something": "yes"
    49  			}
    50  		}`)
    51  	}))
    52  	defer srv.Close()
    53  
    54  	ctx := context.Background()
    55  	client := NewClient(srv.URL, UseMultipartForm())
    56  
    57  	ctx, cancel := context.WithTimeout(ctx, 1*time.Second)
    58  	defer cancel()
    59  	var responseData map[string]interface{}
    60  	err := client.Run(ctx, &Request{q: "query {}"}, &responseData)
    61  	is.NoErr(err)
    62  	is.Equal(calls, 1) // calls
    63  	is.Equal(responseData["something"], "yes")
    64  }
    65  func TestImmediatelyCloseReqBody(t *testing.T) {
    66  	is := is.New(t)
    67  	var calls int
    68  	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    69  		calls++
    70  		is.Equal(r.Method, http.MethodPost)
    71  		query := r.FormValue("query")
    72  		is.Equal(query, `query {}`)
    73  		io.WriteString(w, `{
    74  			"data": {
    75  				"something": "yes"
    76  			}
    77  		}`)
    78  	}))
    79  	defer srv.Close()
    80  
    81  	ctx := context.Background()
    82  	client := NewClient(srv.URL, ImmediatelyCloseReqBody(), UseMultipartForm())
    83  
    84  	ctx, cancel := context.WithTimeout(ctx, 1*time.Second)
    85  	defer cancel()
    86  	var responseData map[string]interface{}
    87  	err := client.Run(ctx, &Request{q: "query {}"}, &responseData)
    88  	is.NoErr(err)
    89  	is.Equal(calls, 1) // calls
    90  	is.Equal(responseData["something"], "yes")
    91  }
    92  
    93  func TestDoErr(t *testing.T) {
    94  	is := is.New(t)
    95  	var calls int
    96  	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    97  		calls++
    98  		is.Equal(r.Method, http.MethodPost)
    99  		query := r.FormValue("query")
   100  		is.Equal(query, `query {}`)
   101  		io.WriteString(w, `{
   102  			"errors": [{
   103  				"message": "Something went wrong"
   104  			}]
   105  		}`)
   106  	}))
   107  	defer srv.Close()
   108  
   109  	ctx := context.Background()
   110  	client := NewClient(srv.URL, UseMultipartForm())
   111  
   112  	ctx, cancel := context.WithTimeout(ctx, 1*time.Second)
   113  	defer cancel()
   114  	var responseData map[string]interface{}
   115  	err := client.Run(ctx, &Request{q: "query {}"}, &responseData)
   116  	is.True(err != nil)
   117  	is.Equal(err.Error(), "graphql: Something went wrong")
   118  }
   119  
   120  func TestDoServerErr(t *testing.T) {
   121  	is := is.New(t)
   122  	var calls int
   123  	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   124  		calls++
   125  		is.Equal(r.Method, http.MethodPost)
   126  		query := r.FormValue("query")
   127  		is.Equal(query, `query {}`)
   128  		w.WriteHeader(http.StatusInternalServerError)
   129  		io.WriteString(w, `Internal Server Error`)
   130  	}))
   131  	defer srv.Close()
   132  
   133  	ctx := context.Background()
   134  	client := NewClient(srv.URL, UseMultipartForm())
   135  
   136  	ctx, cancel := context.WithTimeout(ctx, 1*time.Second)
   137  	defer cancel()
   138  	var responseData map[string]interface{}
   139  	err := client.Run(ctx, &Request{q: "query {}"}, &responseData)
   140  	is.Equal(err.Error(), "graphql: server returned a non-200 status code: 500")
   141  }
   142  
   143  func TestDoBadRequestErr(t *testing.T) {
   144  	is := is.New(t)
   145  	var calls int
   146  	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   147  		calls++
   148  		is.Equal(r.Method, http.MethodPost)
   149  		query := r.FormValue("query")
   150  		is.Equal(query, `query {}`)
   151  		w.WriteHeader(http.StatusBadRequest)
   152  		io.WriteString(w, `{
   153  			"errors": [{
   154  				"message": "miscellaneous message as to why the the request was bad"
   155  			}]
   156  		}`)
   157  	}))
   158  	defer srv.Close()
   159  
   160  	ctx := context.Background()
   161  	client := NewClient(srv.URL, UseMultipartForm())
   162  
   163  	ctx, cancel := context.WithTimeout(ctx, 1*time.Second)
   164  	defer cancel()
   165  	var responseData map[string]interface{}
   166  	err := client.Run(ctx, &Request{q: "query {}"}, &responseData)
   167  	is.Equal(err.Error(), "graphql: miscellaneous message as to why the the request was bad")
   168  }
   169  
   170  func TestDoNoResponse(t *testing.T) {
   171  	is := is.New(t)
   172  	var calls int
   173  	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   174  		calls++
   175  		is.Equal(r.Method, http.MethodPost)
   176  		query := r.FormValue("query")
   177  		is.Equal(query, `query {}`)
   178  		io.WriteString(w, `{
   179  			"data": {
   180  				"something": "yes"
   181  			}
   182  		}`)
   183  	}))
   184  	defer srv.Close()
   185  
   186  	ctx := context.Background()
   187  	client := NewClient(srv.URL, UseMultipartForm())
   188  
   189  	ctx, cancel := context.WithTimeout(ctx, 1*time.Second)
   190  	defer cancel()
   191  	err := client.Run(ctx, &Request{q: "query {}"}, nil)
   192  	is.NoErr(err)
   193  	is.Equal(calls, 1) // calls
   194  }
   195  
   196  func TestQuery(t *testing.T) {
   197  	is := is.New(t)
   198  
   199  	var calls int
   200  	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   201  		calls++
   202  		query := r.FormValue("query")
   203  		is.Equal(query, "query {}")
   204  		is.Equal(r.FormValue("variables"), `{"username":"matryer"}`+"\n")
   205  		_, err := io.WriteString(w, `{"data":{"value":"some data"}}`)
   206  		is.NoErr(err)
   207  	}))
   208  	defer srv.Close()
   209  	ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
   210  	defer cancel()
   211  
   212  	client := NewClient(srv.URL, UseMultipartForm())
   213  
   214  	req := NewRequest("query {}")
   215  	req.Var("username", "matryer")
   216  
   217  	// check variables
   218  	is.True(req != nil)
   219  	is.Equal(req.vars["username"], "matryer")
   220  
   221  	var resp struct {
   222  		Value string
   223  	}
   224  	err := client.Run(ctx, req, &resp)
   225  	is.NoErr(err)
   226  	is.Equal(calls, 1)
   227  
   228  	is.Equal(resp.Value, "some data")
   229  
   230  }
   231  
   232  func TestFile(t *testing.T) {
   233  	is := is.New(t)
   234  
   235  	var calls int
   236  	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   237  		calls++
   238  		file, header, err := r.FormFile("file")
   239  		is.NoErr(err)
   240  		defer file.Close()
   241  		is.Equal(header.Filename, "filename.txt")
   242  
   243  		b, err := ioutil.ReadAll(file)
   244  		is.NoErr(err)
   245  		is.Equal(string(b), `This is a file`)
   246  
   247  		_, err = io.WriteString(w, `{"data":{"value":"some data"}}`)
   248  		is.NoErr(err)
   249  	}))
   250  	defer srv.Close()
   251  	ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
   252  	defer cancel()
   253  	client := NewClient(srv.URL, UseMultipartForm())
   254  	f := strings.NewReader(`This is a file`)
   255  	req := NewRequest("query {}")
   256  	req.File("file", "filename.txt", f)
   257  	err := client.Run(ctx, req, nil)
   258  	is.NoErr(err)
   259  }
   260  
   261  type roundTripperFunc func(req *http.Request) (*http.Response, error)
   262  
   263  func (fn roundTripperFunc) RoundTrip(req *http.Request) (*http.Response, error) {
   264  	return fn(req)
   265  }