forked from guilhermebr/go-stone-openbank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_test.go
233 lines (203 loc) · 5.72 KB
/
client_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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package openbank
import (
"bytes"
"encoding/json"
"io"
"net/http"
"net/url"
"reflect"
"testing"
)
type data struct {
Field string `json:"field,omitempty"`
Detail string `json:"detail,omitempty"`
}
type responseBody struct {
Data []data `json:"data,omitempty"`
}
func testClientDefaultURLs(t *testing.T, c *Client) {
if c.ApiBaseURL == nil || c.ApiBaseURL.String() != prodAPIBaseURL {
t.Errorf("NewClient ApiBaseURL = %v, expected %v", c.ApiBaseURL, prodAPIBaseURL)
}
if c.AccountURL == nil || c.AccountURL.String() != prodAccountURL {
t.Errorf("NewClient AccountURL = %v, expected %v", c.AccountURL, prodAccountURL)
}
if c.SiteURL == nil || c.SiteURL.String() != prodSiteURL {
t.Errorf("NewClient SiteURL = %v, expected %v", c.AccountURL, prodAccountURL)
}
}
func testClientDefaults(t *testing.T, c *Client) {
testClientDefaultURLs(t, c)
}
func TestNewClient(t *testing.T) {
c, _ := NewClient()
testClientDefaults(t, c)
}
func TestClientWithSandboxURLs(t *testing.T) {
c, _ := NewClient(UseSandbox())
if c.ApiBaseURL == nil || c.ApiBaseURL.String() != sandboxAPIBaseURL {
t.Errorf("NewClient ApiBaseURL = %v, expected %v", c.ApiBaseURL, sandboxAPIBaseURL)
}
if c.AccountURL == nil || c.AccountURL.String() != sandboxAccountURL {
t.Errorf("NewClient AccountURL = %v, expected %v", c.AccountURL, sandboxAccountURL)
}
if c.SiteURL == nil || c.SiteURL.String() != sandboxSiteURL {
t.Errorf("NewClient SiteURL = %v, expected %v", c.AccountURL, sandboxAccountURL)
}
}
func TestNewAPIRequest(t *testing.T) {
c, _ := NewClient()
inURL, outURL := "/test", prodAPIBaseURL+"/test"
inBody, outBody := struct {
AccountID string `json:"account_id"`
Items []struct{} `json:"items"`
Customer struct {
Name string `json:"name"`
} `json:"customer"`
Closed bool `json:"closed"`
Payments []struct{} `json:"payments"`
}{AccountID: "abc123"},
`{"account_id":"abc123","items":null,"customer":{"name":""},"closed":false,"payments":null}`+"\n"
req, _ := c.NewAPIRequest(http.MethodPost, inURL, inBody)
if req.URL.String() != outURL {
t.Errorf("NewAPIRequest(%v) URL = %v, expected %v", inURL, req.URL, outURL)
}
body, _ := io.ReadAll(req.Body)
if string(body) != outBody {
t.Errorf("NewAPIRequest(%v)Body = %v, expected %v", inBody, string(body), outBody)
}
userAgent := req.Header.Get("User-Agent")
if c.UserAgent != userAgent {
t.Errorf("NewAPIRequest() User-Agent = %v, expected %v", userAgent, c.UserAgent)
}
}
func TestCheckResponse(t *testing.T) {
testCases := []struct {
Name string
ResponseBody responseBody
StatusCode int
ExpectedError bool
ErrorResponse *responseBody
RequestMethod string
RequestUrl string
}{
{
Name: "Should return error for unsuccessful status code",
ResponseBody: responseBody{
Data: []data{
{
Field: "Test",
Detail: "Error",
},
},
},
StatusCode: 400,
ExpectedError: true,
ErrorResponse: new(responseBody),
RequestUrl: "http://127.0.0.1:3001/test",
RequestMethod: http.MethodGet,
},
{
Name: "Should not return error for successful status code",
ResponseBody: responseBody{
Data: []data{
{
Field: "Test",
Detail: "Success",
},
},
},
StatusCode: 200,
ExpectedError: false,
ErrorResponse: new(responseBody),
RequestUrl: "http://127.0.0.1:3001/test",
RequestMethod: http.MethodGet,
},
}
for _, testCase := range testCases {
t.Run(testCase.Name, func(t *testing.T) {
// Arrange
jsonBody, _ := json.Marshal(testCase.ResponseBody)
reqUrl, _ := url.Parse(testCase.RequestUrl)
response := &http.Response{
StatusCode: testCase.StatusCode,
Body: io.NopCloser(bytes.NewReader(jsonBody)),
Request: &http.Request{
Method: testCase.RequestMethod,
URL: reqUrl,
},
}
// Act
err := CheckResponse(response, testCase.ErrorResponse)
// Asserts
if testCase.ExpectedError && err == nil {
t.Error("expected err got nil")
}
if testCase.ExpectedError && !reflect.DeepEqual(testCase.ErrorResponse, &testCase.ResponseBody) {
t.Errorf("expected error response: %+v, got %+v", &testCase.ResponseBody, testCase.ErrorResponse)
}
})
}
}
func TestDoMethod(t *testing.T) {
testCases := []struct {
Name string
ResponseBody responseBody
ExpectedError bool
Method string
Path string
}{
{
Name: "Should return error for unsuccessful status code",
ResponseBody: responseBody{
Data: []data{
{
Field: "Test field",
Detail: "Error",
},
},
},
ExpectedError: true,
Method: http.MethodGet,
Path: "http://127.0.0.1:3001/error-test",
},
{
Name: "Should not return error for successful status code",
ResponseBody: responseBody{
Data: []data{
{
Field: "Test field",
Detail: "Success",
},
},
},
ExpectedError: false,
Method: http.MethodGet,
Path: "http://127.0.0.1:3001/success-test",
},
}
for _, testCase := range testCases {
t.Run(testCase.Name, func(t *testing.T) {
// Arrange
c, _ := NewClient()
reqUrl, err := url.Parse(testCase.Path)
if err != nil {
t.Fatalf("error converting string to URL: %v", err)
}
request := &http.Request{
Method: testCase.Method,
URL: reqUrl,
}
successResponse, errorResponse := new(responseBody), new(responseBody)
// Act
response, err := c.Do(request, successResponse, errorResponse)
// Asserts
if err != nil && response == nil {
t.Fatalf("unable to execute request: %v", err)
}
if err != nil && !reflect.DeepEqual(errorResponse, &testCase.ResponseBody) {
t.Errorf("expected error response: %+v, got %+v", &testCase.ResponseBody, errorResponse)
}
})
}
}