-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathiban_bic_test.go
166 lines (121 loc) · 3.6 KB
/
iban_bic_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
package finance_test
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/pieterclaerhout/go-finance"
)
func TestCheckIBAN(t *testing.T) {
type test struct {
number string
expectedBankName string
expectedIBAN string
expectedBIC string
expectsError bool
}
var tests = []test{
{"738-1202561-74", "KBC Bank", "BE16 7381 2025 6174", "KRED BE BB", false},
{"738120256174", "KBC Bank", "BE16 7381 2025 6174", "KRED BE BB", false},
{"7381202561-74", "KBC Bank", "BE16 7381 2025 6174", "KRED BE BB", false},
{"738-AAAAAAA-74", "", "", "", true},
{"738-AAAAAAA-AA", "", "", "", true},
{"", "", "", "", true},
}
for _, tc := range tests {
t.Run(tc.number, func(t *testing.T) {
info, err := finance.CheckIBAN(tc.number)
if tc.expectsError {
assert.Error(t, err, "error")
assert.Nil(t, info, "info")
} else {
assert.NoError(t, err, "error")
assert.NotNil(t, info, "info")
if info != nil {
assert.Equal(t, tc.expectedBankName, info.BankName, "bank-name")
assert.Equal(t, tc.expectedIBAN, info.IBAN, "IBAN")
assert.Equal(t, tc.expectedBIC, info.BIC, "BIC")
}
}
})
}
}
func TestCheckIBANInvalidURL(t *testing.T) {
finance.IBANBICServiceURL = "ht&@-tp://:aa"
defer func() {
finance.IBANBICServiceURL = finance.DefaultIBANBICServiceURL
}()
result, err := finance.CheckIBAN("738120256174")
assert.Nil(t, result, "result")
assert.Error(t, err, "error")
}
func TestCheckIBANTimeout(t *testing.T) {
s := httptest.NewServer(
http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
time.Sleep(500 * time.Millisecond)
w.Header().Set("Content-Type", "text/xml")
w.Write([]byte("hello"))
}),
)
defer s.Close()
finance.IBANBICTimeout = 250 * time.Millisecond
finance.IBANBICServiceURL = s.URL
defer func() {
finance.IBANBICTimeout = finance.DefaultIBANBICTimeout
finance.IBANBICServiceURL = finance.DefaultIBANBICServiceURL
}()
result, err := finance.CheckIBAN("738120256174")
assert.Nil(t, result, "result")
assert.Error(t, err, "error")
}
func TestCheckIBANReadBodyError(t *testing.T) {
s := httptest.NewServer(
http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.Write([]byte(`<string xmlns="http://tempuri.org/">KBC Bank</string>`))
}),
)
defer s.Close()
finance.IBANBICServiceURL = s.URL
defer func() {
finance.IBANBICServiceURL = finance.DefaultIBANBICServiceURL
}()
result, err := finance.CheckIBAN("738120256174")
assert.Nil(t, result, "result")
assert.Error(t, err, "error")
}
func TestCheckIBANInvalidBody(t *testing.T) {
s := httptest.NewServer(
http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Length", "1")
}),
)
defer s.Close()
finance.IBANBICServiceURL = s.URL
defer func() {
finance.IBANBICServiceURL = finance.DefaultIBANBICServiceURL
}()
result, err := finance.CheckIBAN("738120256174")
assert.Nil(t, result, "result")
assert.Error(t, err, "error")
}
func TestCheckIBANPartialFail(t *testing.T) {
s := httptest.NewServer(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if strings.Contains(r.RequestURI, "BBANtoIBANandBIC") {
w.Header().Set("Content-Length", "1")
return
}
w.Write([]byte(`<string xmlns="http://tempuri.org/">KBC Bank</string>`))
}),
)
defer s.Close()
finance.IBANBICServiceURL = s.URL
defer func() {
finance.IBANBICServiceURL = finance.DefaultIBANBICServiceURL
}()
result, err := finance.CheckIBAN("738120256174")
assert.Nil(t, result, "result")
assert.Error(t, err, "error")
}