Skip to content

Commit

Permalink
opensearch: add io read error test, adjust Perform test
Browse files Browse the repository at this point in the history
Signed-off-by: Jakob Hahn <jakob.hahn@hetzner.com>
  • Loading branch information
Jakob3xD committed Apr 8, 2024
1 parent fb59828 commit e3e39da
Showing 1 changed file with 28 additions and 5 deletions.
33 changes: 28 additions & 5 deletions opensearch_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit e3e39da

Please sign in to comment.