gitlab.com/ignitionrobotics/web/ign-go@v1.0.0-rc4/net/client_test.go (about)

     1  package net
     2  
     3  import (
     4  	"context"
     5  	"github.com/stretchr/testify/assert"
     6  	"github.com/stretchr/testify/require"
     7  	"gitlab.com/ignitionrobotics/web/ign-go/encoders"
     8  	"io/ioutil"
     9  	"net/http"
    10  	"net/http/httptest"
    11  	"net/url"
    12  	"testing"
    13  	"time"
    14  )
    15  
    16  type inputTest struct {
    17  	Data string `json:"data"`
    18  }
    19  
    20  type outputTest struct {
    21  	Result string `json:"result"`
    22  }
    23  
    24  func TestHTTPClient_CallWithIOErrors(t *testing.T) {
    25  	u, err := url.Parse("http://localhost")
    26  	require.NoError(t, err)
    27  
    28  	d := NewCallerHTTP(u, map[string]EndpointHTTP{
    29  		"TestEndpoint": {
    30  			Method: "GET",
    31  			Path:   "/test",
    32  		},
    33  	}, time.Second)
    34  
    35  	c := NewClient(d, encoders.JSON)
    36  
    37  	err = c.Call(context.Background(), "TestEndpoint", nil, nil)
    38  	assert.Error(t, err)
    39  	assert.Equal(t, ErrNilValuesIO, err)
    40  
    41  	err = c.Call(context.Background(), "TestEndpoint", &inputTest{}, nil)
    42  	assert.Error(t, err)
    43  	assert.Equal(t, ErrNilValuesIO, err)
    44  
    45  	err = c.Call(context.Background(), "TestEndpoint", nil, &outputTest{})
    46  	assert.Error(t, err)
    47  	assert.Equal(t, ErrNilValuesIO, err)
    48  }
    49  
    50  func TestHttpClient_Call(t *testing.T) {
    51  	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    52  		var in inputTest
    53  
    54  		assert.Equal(t, http.MethodPost, r.Method)
    55  		assert.Equal(t, "/test", r.URL.Path)
    56  
    57  		body, err := ioutil.ReadAll(r.Body)
    58  		if err != nil {
    59  			http.Error(w, err.Error(), http.StatusInternalServerError)
    60  			return
    61  		}
    62  
    63  		if err = encoders.JSON.Unmarshal(body, &in); err != nil {
    64  			http.Error(w, err.Error(), http.StatusInternalServerError)
    65  			return
    66  		}
    67  
    68  		out := outputTest{Result: in.Data}
    69  
    70  		body, err = encoders.JSON.Marshal(out)
    71  		if err != nil {
    72  			http.Error(w, err.Error(), http.StatusInternalServerError)
    73  			return
    74  		}
    75  
    76  		_, err = w.Write(body)
    77  		if err != nil {
    78  			http.Error(w, err.Error(), http.StatusInternalServerError)
    79  			return
    80  		}
    81  		w.WriteHeader(http.StatusOK)
    82  	}))
    83  	defer server.Close()
    84  
    85  	u, err := url.ParseRequestURI(server.URL)
    86  	require.NoError(t, err)
    87  
    88  	d := NewCallerHTTP(u, map[string]EndpointHTTP{
    89  		"CreateTest": {
    90  			Method: "POST",
    91  			Path:   "/test",
    92  		},
    93  	}, time.Second)
    94  
    95  	c := NewClient(d, encoders.JSON)
    96  
    97  	in := inputTest{Data: "test"}
    98  	var out outputTest
    99  
   100  	require.NoError(t, c.Call(context.Background(), "CreateTest", &in, &out))
   101  	assert.Equal(t, "test", out.Result)
   102  }