forked from mailjet/mailjet-apiv3-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore_test.go
123 lines (116 loc) · 3.03 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
package mailjet
import (
"bytes"
"encoding/json"
"fmt"
"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: "[email protected]",
TextPart: "This is text",
}
resMap := make(map[string]interface{})
resMap["Email"] = "[email protected]"
resMap["Text-Part"] = "This is text"
body, err := convertPayload(test, []string{"Email", "TextPart"})
if err != nil {
t.Fatal("Unexpected error:", err)
}
res, err := 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, err = 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":"[email protected]"},{"Email":"[email protected]"}],"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 != "[email protected]" {
t.Fatalf("Fail to unmarshal JSON: %s != %s", data[0].Email, "[email protected]")
}
if data[1].Email != "[email protected]" {
t.Fatalf("Fail to unmarshal JSON: %s != %s", data[1].Email, "[email protected]")
}
} else {
t.Fatal("Fail to unmarshal JSON: empty res")
}
}