-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_test.go
80 lines (70 loc) · 1.61 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
package yaddress
import (
"errors"
"fmt"
"github.com/stretchr/testify/assert"
"net/http"
"net/http/httptest"
"testing"
)
type mockClient struct {
errCode int
errMessage string
httpError bool
}
func (mc *mockClient) Get(url string) (*http.Response, error) {
w := httptest.NewRecorder()
w.Write([]byte(fmt.Sprintf(`{"ErrorCode":%d, "ErrorMessage":"%s"}`, mc.errCode, mc.errMessage)))
if mc.httpError {
return nil, errors.New("mock http error")
}
return w.Result(), nil
}
func TestYaddress(t *testing.T) {
type test struct {
name string
errCode int
errMessage string
addr1 string
addr2 string
shouldErr bool
errMsg string
}
tests := []test{
{
name: "HttpError",
shouldErr: true,
addr1: "",
addr2: "",
},
{
name: "EmptyFields",
errCode: 2,
errMessage: "Invalid address: missing City-State-Zip line",
addr1: "",
addr2: "",
shouldErr: true,
errMsg: "Should give an error if both address lines are empty",
},
{
name: "PresentAddress1AndAddress2",
errCode: 0,
addr1: "506 Fourth Avenue Unit 1",
addr2: "Asbury Prk, NJ",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
logger := DefaultLogger()
yd := NewClient("", WithLogger(logger))
yd.httpClient = &mockClient{errCode: tt.errCode, errMessage: tt.errMessage, httpError: tt.shouldErr}
request := Request{AddressLine1: tt.addr1, AddressLine2: tt.addr2}
_, err := yd.ProcessAddress(request)
if tt.shouldErr {
assert.Error(t, err, tt.errMsg)
} else {
assert.NoError(t, err)
}
})
}
}