From 26cdec290222c951330179c39d24a96b94faf617 Mon Sep 17 00:00:00 2001 From: Ivan Stasiuk Date: Wed, 30 Aug 2023 12:35:36 +0100 Subject: [PATCH] test: http do --- test_test.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/test_test.go b/test_test.go index 0a47112..52d96ef 100644 --- a/test_test.go +++ b/test_test.go @@ -11,6 +11,7 @@ import ( "github.com/brokeyourbike/clearbank-api-client-go" "github.com/brokeyourbike/clearbank-api-client-go/signature" + "github.com/brokeyourbike/clearbank-api-client-go/signature/local" "github.com/sirupsen/logrus" logrustest "github.com/sirupsen/logrus/hooks/test" "github.com/stretchr/testify/assert" @@ -49,6 +50,26 @@ func TestTest(t *testing.T) { assert.Equal(t, "123", hook.Entries[0].Data["http.request.headers.request_id"]) } +func TestFailedHttpRequest(t *testing.T) { + mockHttpClient := clearbank.NewMockHttpClient(t) + client := clearbank.NewClient("token", local.NewNilSigner(), clearbank.WithHTTPClient(mockHttpClient)) + + mockHttpClient.On("Do", mock.AnythingOfType("*http.Request")).Return(&http.Response{}, errors.New("cannot do")).Once() + + assert.Error(t, client.Test(context.TODO(), "hello!")) +} + +func TestFailedHttpRequestBodyDecode(t *testing.T) { + mockHttpClient := clearbank.NewMockHttpClient(t) + client := clearbank.NewClient("token", local.NewNilSigner(), clearbank.WithHTTPClient(mockHttpClient)) + + body := io.NopCloser(bytes.NewReader(nil)) + require.NoError(t, body.Close()) + mockHttpClient.On("Do", mock.AnythingOfType("*http.Request")).Return(&http.Response{Body: body}, nil).Once() + + assert.Error(t, client.Test(context.TODO(), "hello!")) +} + func TestFailedSign(t *testing.T) { mockSigner := signature.NewMockSigner(t) mockHttpClient := clearbank.NewMockHttpClient(t)