-
Notifications
You must be signed in to change notification settings - Fork 32
/
core_test.go
151 lines (140 loc) · 3.66 KB
/
core_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
package mailjet
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"runtime"
"strings"
"testing"
)
func TestCreateRequest(t *testing.T) {
req, err := createRequest("GET", apiBase, nil, nil)
if err != nil {
t.Fatal("Unexpected error:", err)
}
if req.Method != "GET" {
t.Fatal("Wrong method:", req.Method)
}
if req.URL.String() != apiBase {
t.Fatal("Wrong URL:", req.URL.String())
}
ua := fmt.Sprintf("%s/%s;%s",
UserAgentBase,
UserAgentVersion,
runtime.Version(),
)
if req.Header["User-Agent"] == nil || req.Header["User-Agent"][0] != ua {
t.Fatal("Wrong User-agent:", req.Header["User-Agent"][0])
}
}
func TestConvertPayload(t *testing.T) {
type Test struct {
ID int64 `mailjet:"read_only"`
Name string
Email string
Address string `json:",omitempty" mailjet:"read_only"`
TextPart string `json:"Text-Part,omitempty"`
Header map[string]string `json:",omitempty"`
MjCampaignID int64 `json:"Mj-CampaignID,omitempty" mailjet:"read_only"`
}
test := &Test{
ID: -42,
Email: "ex@mple.com",
TextPart: "This is text",
}
resMap := make(map[string]interface{})
resMap["Email"] = "ex@mple.com"
resMap["Text-Part"] = "This is text"
body, err := convertPayload(test, []string{"Email", "TextPart"})
if err != nil {
t.Fatal("Unexpected error:", err)
}
res, _ := json.Marshal(resMap)
if !bytes.Equal(body, res) {
t.Fatal("Wrong body:", string(body), string(res))
}
resMap["Name"] = ""
body, err = convertPayload(&test, nil)
if err != nil {
t.Fatal("Unexpected error:", err)
}
res, _ = json.Marshal(resMap)
if !bytes.Equal(body, res) {
t.Fatal("Wrong body:", string(body), string(res))
}
}
func TestBuildUrl(t *testing.T) {
info := &Request{
Resource: "contactslist",
ID: 1,
Action: "managemanycontacts",
ActionID: 5,
}
expected := "https://api.mailjet.com/v3/REST/contactslist/1/managemanycontacts/5"
res := buildURL(apiBase, info)
if res != expected {
t.Fatal("Fail to build URL:", res)
}
}
func TestReadJsonEmptyResult(t *testing.T) {
type TestStruct struct {
Email string
}
var data []TestStruct
body := `{"Count":0,"Data":[],"Total":0}`
_, _, err := readJSONResult(strings.NewReader(body), &data)
if err != nil {
t.Fatal("Unexpected error:", err)
}
}
func TestReadJsonResult(t *testing.T) {
type TestStruct struct {
Email string
}
var data []TestStruct
body := `{"Count":2,"Data":[{"Email":"qwe@qwe.com"},{"Email":"aze@aze.com"}],"Total":1}`
count, total, err := readJSONResult(strings.NewReader(body), &data)
if err != nil {
t.Fatal("Unexpected error:", err)
}
if count != 2 {
t.Fatalf("Wrong count: %d != %d", count, 2)
}
if total != 1 {
t.Fatalf("Wrong total: %d != %d", total, 2)
}
if data != nil {
if data[0].Email != "qwe@qwe.com" {
t.Fatalf("Fail to unmarshal JSON: %s != %s", data[0].Email, "qwe@qwe.com")
}
if data[1].Email != "aze@aze.com" {
t.Fatalf("Fail to unmarshal JSON: %s != %s", data[1].Email, "aze@aze.com")
}
} else {
t.Fatal("Fail to unmarshal JSON: empty res")
}
}
func Test_checkResponseError(t *testing.T) {
t.Run("4xx", func(t *testing.T) {
const statusCode = 404
resp := &http.Response{
StatusCode: statusCode,
Body: io.NopCloser(strings.NewReader(`{"ErrorMessage":"foo not found"}`)),
}
err := checkResponseError(resp)
if err == nil {
t.Fatal("Expected error")
}
var mailjetErr RequestError
if errors.As(err, &mailjetErr) {
if mailjetErr.StatusCode != statusCode {
t.Fatalf("Status code: exptected(%d) but got(%d)", statusCode, mailjetErr.StatusCode)
}
} else {
t.Fatalf("err(%v) must be a RequestError type", err)
}
})
}