-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_http_batch_test.go
94 lines (75 loc) · 2.16 KB
/
client_http_batch_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
package kickbox_test
import (
"bytes"
"context"
"encoding/json"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"testing"
"github.com/wakumaku/kickbox"
"github.com/stretchr/testify/assert"
)
func TestVerifyBatch(t *testing.T) {
const csvFilePath = "./testdata/sample.csv"
expectedResponse := []byte(`{
"id":123,
"success":true,
"message":null
}`)
expectedFileNameHeader := "myfilename.response"
expectedCallbackHeader := "http://call.me.maybe"
expectedFileContent, err := ioutil.ReadFile(csvFilePath)
assert.Nil(t, err)
handler := func(rw http.ResponseWriter, r *http.Request) {
// Check headers
if r.Method != http.MethodPut {
t.Logf("incorrect http verb, expected PUT, got: %s", r.Method)
rw.WriteHeader(http.StatusInternalServerError)
return
}
if r.Header.Get("X-Kickbox-Filename") != expectedFileNameHeader {
t.Log("unexpected filename header content")
rw.WriteHeader(http.StatusInternalServerError)
return
}
if r.Header.Get("X-Kickbox-Callback") != expectedCallbackHeader {
t.Log("unexpected callback header content")
rw.WriteHeader(http.StatusInternalServerError)
return
}
// Check body content
content, errRead := ioutil.ReadAll(r.Body)
if errRead != nil {
t.Logf("cannot read body content: %v", err)
rw.WriteHeader(http.StatusInternalServerError)
return
}
defer r.Body.Close()
if !bytes.Equal(content, expectedFileContent) {
t.Log("content and expected content is different")
rw.WriteHeader(http.StatusInternalServerError)
return
}
rw.WriteHeader(http.StatusOK)
_, _ = rw.Write(expectedResponse)
}
svr := httptest.NewServer(http.HandlerFunc(handler))
defer svr.Close()
c, err := kickbox.New("myapikey", kickbox.OverrideBaseURL(svr.URL))
assert.Nil(t, err)
emailsFile, err := os.Open(csvFilePath)
assert.Nil(t, err)
defer emailsFile.Close()
resp, err := c.VerifyBatch(
context.TODO(),
emailsFile, kickbox.Filename(expectedFileNameHeader),
kickbox.Callback(expectedCallbackHeader),
)
assert.Nil(t, err)
var expectedResp kickbox.ResponseVerifyBatch
err = json.Unmarshal(expectedResponse, &expectedResp)
assert.Nil(t, err)
assert.Equal(t, &expectedResp, resp)
}