-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_test.go
106 lines (78 loc) · 3.62 KB
/
test_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package clearbank_test
import (
"bytes"
"context"
_ "embed"
"errors"
"io"
"net/http"
"testing"
"github.com/brokeyourbike/clearbank-api-client-go"
"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"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
)
//go:embed testdata/bad-request.json
var badRequest []byte
func TestTest(t *testing.T) {
mockSigner := clearbank.NewMockSigner(t)
mockHttpClient := clearbank.NewMockHttpClient(t)
logger, hook := logrustest.NewNullLogger()
logger.SetLevel(logrus.DebugLevel)
client := clearbank.NewClient("token", mockSigner, clearbank.WithHTTPClient(mockHttpClient), clearbank.WithLogger(logger))
ctx := clearbank.RequestIdContext(context.TODO(), "123")
mockSigner.On("Sign", ctx, mock.Anything).Return([]byte("signed"), nil).Once()
resp := &http.Response{StatusCode: 200, Body: io.NopCloser(bytes.NewReader(nil))}
mockHttpClient.On("Do", mock.AnythingOfType("*http.Request")).Return(resp, nil).Once()
assert.NoError(t, client.Test(ctx, "hello!"))
require.Equal(t, 2, len(hook.Entries))
require.Contains(t, hook.Entries[0].Data, "http.request.body.content")
require.Contains(t, hook.Entries[0].Data, "http.request.headers.request_id")
require.Contains(t, hook.Entries[1].Data, "http.response.status_code")
require.Contains(t, hook.Entries[1].Data, "http.response.body.content")
require.Contains(t, hook.Entries[1].Data, "http.response.headers")
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(nil, errors.New("cannot do")).Once()
assert.Error(t, client.Test(context.TODO(), "hello!"))
}
func TestFailedSign(t *testing.T) {
mockSigner := clearbank.NewMockSigner(t)
mockHttpClient := clearbank.NewMockHttpClient(t)
client := clearbank.NewClient("token", mockSigner, clearbank.WithHTTPClient(mockHttpClient))
ctx := context.TODO()
mockSigner.On("Sign", ctx, mock.Anything).Return(nil, errors.New("failed to sign")).Once()
assert.Error(t, client.Test(ctx, "hello!"))
}
func TestUnexpectedStatus(t *testing.T) {
mockSigner := clearbank.NewMockSigner(t)
mockHttpClient := clearbank.NewMockHttpClient(t)
client := clearbank.NewClient("token", mockSigner, clearbank.WithHTTPClient(mockHttpClient))
ctx := context.TODO()
mockSigner.On("Sign", ctx, mock.Anything).Return([]byte("signed"), nil).Once()
resp := &http.Response{StatusCode: 500, Body: io.NopCloser(bytes.NewReader(nil))}
mockHttpClient.On("Do", mock.AnythingOfType("*http.Request")).Return(resp, nil).Once()
err := client.Test(ctx, "hello!")
require.Error(t, err)
require.ErrorIs(t, err, clearbank.UnexpectedResponse{Status: 500})
}
func TestBadRequest(t *testing.T) {
mockSigner := clearbank.NewMockSigner(t)
mockHttpClient := clearbank.NewMockHttpClient(t)
client := clearbank.NewClient("token", mockSigner, clearbank.WithHTTPClient(mockHttpClient))
ctx := context.TODO()
mockSigner.On("Sign", ctx, mock.Anything).Return([]byte("signed"), nil).Once()
resp := &http.Response{StatusCode: 400, Body: io.NopCloser(bytes.NewReader(badRequest))}
mockHttpClient.On("Do", mock.AnythingOfType("*http.Request")).Return(resp, nil).Once()
err := client.Test(ctx, "hello!")
require.Error(t, err)
got, ok := err.(clearbank.ErrResponse)
require.True(t, ok)
require.Equal(t, 123, got.Status)
}