diff --git a/opensearch_internal_test.go b/opensearch_internal_test.go index f2c263531..8c051ea9e 100644 --- a/opensearch_internal_test.go +++ b/opensearch_internal_test.go @@ -37,13 +37,14 @@ import ( "os" "strings" "testing" + "testing/iotest" "github.com/opensearch-project/opensearch-go/v3/opensearchtransport" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) -var called bool +var called int type mockTransp struct { RoundTripFunc func(*http.Request) (*http.Response, error) @@ -61,7 +62,7 @@ var defaultRoundTripFunc = func(req *http.Request) (*http.Response, error) { }`)) response.Header.Add("Content-Type", "application/json") } else { - called = true + called = called + 1 } return response, nil @@ -206,14 +207,14 @@ func TestClientInterfe(t *testing.T) { c, err := NewClient(Config{Transport: &mockTransp{}}) require.NoError(t, err) - assert.False(t, called, "Unexpected call to transport by client") + call := called - res, err := c.Perform(&http.Request{URL: &url.URL{}, Header: make(http.Header)}) // errcheck ignore + res, err := c.Perform(&http.Request{URL: &url.URL{}, Header: make(http.Header)}) if err == nil && res != nil && res.Body != nil { res.Body.Close() } - assert.True(t, called, "Expected client to call transport") + assert.True(t, called-1 == call, "Expected client to call transport") }) t.Run("Do()", func(t *testing.T) { @@ -249,6 +250,28 @@ func TestClientInterfe(t *testing.T) { assert.ErrorIs(t, err, ErrJSONUnmarshalBody) assert.NotNil(t, resp) }) + + t.Run("Do() io read error", func(t *testing.T) { + c, err := NewClient( + Config{ + Transport: &mockTransp{ + RoundTripFunc: func(req *http.Request) (*http.Response, error) { + return &http.Response{StatusCode: http.StatusOK, Body: io.NopCloser(iotest.ErrReader(errors.New("io reader test")))}, nil + }, + }, + }, + ) + require.NoError(t, err) + + type failStr struct { + Version int `json:"version"` + } + req := testReq{} + resp, err := c.Do(nil, req, &failStr{}) + require.Error(t, err) + assert.ErrorIs(t, err, ErrReadBody) + assert.NotNil(t, resp) + }) } func TestAddrsToURLs(t *testing.T) {