-
Notifications
You must be signed in to change notification settings - Fork 23
/
send_email_test.go
90 lines (77 loc) · 2.09 KB
/
send_email_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
package customerio_test
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"reflect"
"testing"
"time"
"github.com/customerio/go-customerio/v3"
)
func TestSendEmail(t *testing.T) {
emailRequest := &customerio.SendEmailRequest{
Identifiers: map[string]string{
"id": "customer_1",
},
To: "customer@example.com",
From: "business@example.com",
Subject: "hello, {{ trigger.name }}",
Body: "hello from the Customer.io {{ trigger.client }} client",
MessageData: map[string]interface{}{
"client": "Go",
"name": "gopher",
},
}
var verify = func(request []byte) {
var body customerio.SendEmailRequest
if err := json.Unmarshal(request, &body); err != nil {
t.Error(err)
}
if !reflect.DeepEqual(&body, emailRequest) {
t.Errorf("Request differed, want: %#v, got: %#v", request, body)
}
}
api, srv := transactionalServer(t, verify)
defer srv.Close()
resp, err := api.SendEmail(context.Background(), emailRequest)
if err != nil {
t.Error(err)
}
expect := &customerio.SendEmailResponse{
TransactionalResponse: customerio.TransactionalResponse{
DeliveryID: testDeliveryID,
QueuedAt: time.Unix(int64(testQueuedAt), 0),
},
}
if !reflect.DeepEqual(resp, expect) {
t.Errorf("Expect: %#v, Got: %#v", expect, resp)
}
}
func TestSendEmailError(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
w.WriteHeader(502)
}))
defer srv.Close()
api := customerio.NewAPIClient("myKey")
api.URL = srv.URL
resp, err := api.SendEmail(context.Background(), &customerio.SendEmailRequest{
Identifiers: map[string]string{
"id": "customer_1",
},
To: "customer@example.com",
From: "business@example.com",
Subject: "hello, {{ trigger.name }}",
Body: "hello from the Customer.io {{ trigger.client }} client",
MessageData: map[string]interface{}{
"client": "Go",
"name": "gopher",
},
})
if err == nil {
t.Errorf("Expected error, got: %#v", resp)
}
if e, ok := err.(*customerio.TransactionalError); !ok {
t.Errorf("Expected TransactionalError, got: %#v", e)
}
}