-
Notifications
You must be signed in to change notification settings - Fork 0
/
unione_test.go
105 lines (82 loc) · 2.39 KB
/
unione_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
package unione_test
import (
"bytes"
"errors"
"io/ioutil"
"log"
"net/http"
"reflect"
"testing"
"github.com/alexeyco/unione"
"github.com/alexeyco/unione/message"
"github.com/alexeyco/unione/utils"
)
func TestClient_LanguageEn(t *testing.T) {
}
func TestClient_LanguageRu(t *testing.T) {
}
func TestClient_Send(t *testing.T) {
responseMap := map[string]interface{}{
"status": "foo",
"job_id": "bar",
"emails": []string{
},
"failed_emails": map[string]string{
"[email protected]": "foo",
"[email protected]": "bar",
},
}
expectedResponseJson, _ := utils.ToJson(responseMap)
var givenReguestJson string
client := utils.NewTestHttpClient(func(req *http.Request) (res *http.Response, err error) {
b, err := ioutil.ReadAll(req.Body)
if err != nil {
t.Fatalf(`Error should be nil, "%s" given`, err)
}
givenReguestJson = string(b)
res = &http.Response{
StatusCode: http.StatusOK,
Body: ioutil.NopCloser(bytes.NewBufferString(expectedResponseJson)),
}
return
})
msg := message.NewMessage().
From("[email protected]", "John Doe").
To(message.NewRecipient("[email protected]")).
Subject("Lorem ipsum").
BodyHtml("Novus ordo seclorum")
success, failed, err := unione.New("foo", "bar").
Client(client).
Send(msg)
if err != nil {
t.Fatalf(`Error should be nil, "%s" given`, err)
}
expectedJson := `{"username":"foo","api_key":"bar","message":{"from_name":"John Doe","from_email":"[email protected]","recipients":[{"email":"[email protected]"}],"subject":"Lorem ipsum","body":{"html":"Novus ordo seclorum"}}}`
utils.JsonIsEqual(t, expectedJson, givenReguestJson)
if !reflect.DeepEqual(success, responseMap["emails"]) {
t.Fatal(`Success emails should be equal`)
}
failedEmails := map[string]error{}
for email, err := range responseMap["failed_emails"].(map[string]string) {
failedEmails[email] = errors.New(err)
}
if !reflect.DeepEqual(failed, failedEmails) {
t.Fatal(`Failed emails should be equal`)
}
}
func ExampleClient_Send() {
recipient := message.NewRecipient("[email protected]").
Name("John Doe")
msg := message.NewMessage().
From("[email protected]", "My site").
To(recipient).
Subject("Awesome news, buddy").
BodyPlainText("Return to my site and enjoy")
client := unione.New("username", "api-key")
_, _, err := client.Send(msg)
if err != nil {
log.Fatalln(err)
}
}