-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_http_verify_test.go
173 lines (145 loc) · 4.54 KB
/
client_http_verify_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
package kickbox_test
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"sync"
"testing"
"time"
"github.com/wakumaku/kickbox"
"github.com/stretchr/testify/assert"
)
func TestVerifyMaxTimeout(t *testing.T) {
client, err := kickbox.New("apikey")
if err != nil {
assert.NotNil(t, err, "unexpected error")
}
_, _, err = client.Verify(context.TODO(), "[email protected]", kickbox.Timeout(60*time.Second))
assert.NotNil(t, err)
assert.EqualError(t, err, "timeout not valid, must be less than 30 sec: 1m0s")
_, _, err = client.Verify(context.TODO(), "[email protected]", kickbox.Timeout(0))
assert.NotNil(t, err)
assert.EqualError(t, err, "timeout not valid, must be less than 30 sec: 0s")
}
func TestVerifyRateLimitError(t *testing.T) {
client, err := kickbox.New("apikey")
assert.Nil(t, err)
ctx, cancel := context.WithCancel(context.Background())
cancel() // cancel the context!
_, _, err = client.Verify(ctx, "[email protected]")
assert.NotNil(t, err)
assert.EqualError(t, err, "rate limiting requests: context canceled")
}
func TestVerifyBuildingRequestError(t *testing.T) {
client, err := kickbox.New("apikey", kickbox.OverrideBaseURL(":::"))
assert.Nil(t, err)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
_, _, err = client.Verify(ctx, "[email protected]")
assert.NotNil(t, err)
assert.EqualError(t, err, "building request: parse \":::/v2/verify\": missing protocol scheme")
}
func TestVerifyRequestError(t *testing.T) {
client, err := kickbox.New("apikey",
kickbox.OverrideBaseURL("http://nonexistinghost.test.me"),
)
assert.Nil(t, err)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
_, _, err = client.Verify(ctx, "[email protected]")
assert.NotNil(t, err)
assert.EqualError(t, err,
"doing request: Get \"http://nonexistinghost.test.me/v2/verify?apikey=apikey&email=email%40example.com&timeout=6000\": "+
"dial tcp: lookup nonexistinghost.test.me: no such host")
}
func TestVerifyRequestBodyBroken(t *testing.T) {
handler := func(rw http.ResponseWriter, r *http.Request) {
rw.WriteHeader(http.StatusOK)
_, _ = rw.Write([]byte(`{broken json`))
}
svr := httptest.NewServer(http.HandlerFunc(handler))
defer svr.Close()
client, err := kickbox.New("apikey",
kickbox.OverrideBaseURL(svr.URL),
)
assert.Nil(t, err)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
_, _, err = client.Verify(ctx, "[email protected]")
assert.NotNil(t, err)
assert.EqualError(t, err, "decoding response: invalid character 'b' looking for beginning of object key string")
}
func TestVerifyMockResponse(t *testing.T) {
body := []byte(`{
"result":"undeliverable",
"reason":"rejected_email",
"role":false,
"free":false,
"disposable":false,
"accept_all":false,
"did_you_mean":"[email protected]",
"sendex":0.23,
"email":"[email protected]",
"user":"bill.lumbergh",
"domain":"gamil.com",
"success":true,
"message":null
}`)
handler := func(rw http.ResponseWriter, r *http.Request) {
rw.WriteHeader(http.StatusOK)
_, _ = rw.Write(body)
}
svr := httptest.NewServer(http.HandlerFunc(handler))
defer svr.Close()
client, err := kickbox.New("apikey", kickbox.OverrideBaseURL(svr.URL))
assert.Nil(t, err, "unexpected error")
_, resp, err := client.Verify(context.TODO(), "[email protected]")
assert.Nil(t, err, "unexpected error")
var expectedResponse kickbox.ResponseVerify
err = json.Unmarshal(body, &expectedResponse)
assert.Nil(t, err)
assert.Equal(t, &expectedResponse, resp, "must be equals")
}
func TestVerifyMaxConcurrentConnections(t *testing.T) {
handler := func(rw http.ResponseWriter, r *http.Request) {
// delay the response
time.Sleep(3 * time.Second)
rw.WriteHeader(http.StatusOK)
_, _ = rw.Write([]byte(`{}`))
}
svr := httptest.NewServer(http.HandlerFunc(handler))
defer svr.Close()
client, err := kickbox.New("apikey",
kickbox.OverrideBaseURL(svr.URL),
kickbox.MaxConcurrentConnections(25),
)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
errCtrl := make(chan struct{})
totalErrors := 0
go func() {
for range errCtrl {
totalErrors++
}
}()
wg := sync.WaitGroup{}
// Launch 26 concurrent request, only 1 must fail
for i := 0; i < 26; i++ {
wg.Add(1)
go func() {
defer wg.Done()
_, _, err := client.Verify(context.TODO(), "[email protected]")
if err != nil {
// on error send a signal to count the total number
errCtrl <- struct{}{}
}
}()
}
wg.Wait()
close(errCtrl)
if totalErrors != 1 {
t.Errorf("expecting only 1 error, got: %d", totalErrors)
}
}