forked from saintpete/twilio-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
messages_test.go
189 lines (180 loc) · 4.7 KB
/
messages_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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package twilio
import (
"context"
"encoding/json"
"fmt"
"net/url"
"os"
"strings"
"testing"
"time"
)
func TestGet(t *testing.T) {
if testing.Short() {
t.Skip("skipping HTTP request in short mode")
}
t.Parallel()
sid := "SM26b3b00f8def53be77c5697183bfe95e"
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
msg, err := envClient.Messages.Get(ctx, sid)
if err != nil {
t.Fatal(err)
}
if msg.Sid != sid {
t.Errorf("expected Sid to equal %s, got %s", sid, msg.Sid)
}
}
func TestGetPage(t *testing.T) {
if testing.Short() {
t.Skip("skipping HTTP request in short mode")
}
t.Parallel()
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
page, err := envClient.Messages.GetPage(ctx, url.Values{"PageSize": []string{"5"}})
if err != nil {
t.Fatal(err)
}
if len(page.Messages) != 5 {
t.Fatalf("expected len(messages) to be 5, got %d", len(page.Messages))
}
}
func TestSendMessage(t *testing.T) {
t.Parallel()
client, s := getServer(sendMessageResponse)
defer s.Close()
msg, err := client.Messages.SendMessage(from, to, "twilio-go testing!", nil)
if err != nil {
t.Fatal(err)
}
if msg.From != from {
t.Errorf("expected From to be from, got error")
}
if msg.Body != "twilio-go testing!" {
t.Errorf("expected Body to be twilio-go testing, got %s", msg.Body)
}
if msg.NumSegments != 1 {
t.Errorf("expected NumSegments to be 1, got %d", msg.NumSegments)
}
if msg.Status != StatusQueued {
t.Errorf("expected Status to be StatusQueued, got %s", msg.Status)
}
}
func TestGetMessage(t *testing.T) {
if testing.Short() {
t.Skip("skipping HTTP request in short mode")
}
t.Parallel()
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
msg, err := envClient.Messages.Get(ctx, "SM5a52bc49b2354703bfdea7e92b44b385")
if err != nil {
t.Fatal(err)
}
if msg.ErrorCode != CodeUnknownDestination {
t.Errorf("expected Code to be %d, got %d", CodeUnknownDestination, msg.ErrorCode)
}
if msg.ErrorMessage == "" {
t.Errorf(`expected ErrorMessage to be non-empty, got ""`)
}
}
func TestIterateAll(t *testing.T) {
if testing.Short() {
t.Skip("skipping HTTP request in short mode")
}
t.Parallel()
iter := envClient.Messages.GetPageIterator(url.Values{"PageSize": []string{"500"}})
count := 0
start := uint(0)
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute)
defer cancel()
for {
page, err := iter.Next(ctx)
if err == NoMoreResults {
break
}
if err != nil {
t.Fatal(err)
return
}
if count > 0 && (page.Start <= start || page.Start-start > 500) {
t.Fatalf("expected page.Start to be greater than previous, got %d, previous %d", page.Start, start)
return
} else {
start = page.Start
}
if err != nil {
t.Fatal(err)
break
}
count++
if count > 15 {
fmt.Println("count > 15")
t.Fail()
break
}
}
if count < 10 {
t.Errorf("Too small of a count - expected at least 10, got %d", count)
}
}
func TestGetMediaURLs(t *testing.T) {
if testing.Short() {
t.Skip("skipping HTTP request in short mode")
}
t.Parallel()
sid := os.Getenv("TWILIO_ACCOUNT_SID")
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
urls, err := envClient.Messages.GetMediaURLs(ctx, "MM89a8c4a6891c53054e9cd604922bfb61", nil)
if err != nil {
t.Fatal(err)
}
if len(urls) != 1 {
t.Errorf("Wrong number of URLs returned: %d", len(urls))
}
if !strings.HasPrefix(urls[0].String(), "https://s3-external-1.amazonaws.com/media.twiliocdn.com/"+sid) {
t.Errorf("wrong url: %s", urls[0].String())
}
}
func TestDecode(t *testing.T) {
t.Parallel()
msg := new(Message)
if err := json.Unmarshal(getMessageResponse, &msg); err != nil {
t.Fatal(err)
}
if msg.Sid != "SM26b3b00f8def53be77c5697183bfe95e" {
t.Errorf("wrong sid")
}
got := msg.DateCreated.Time.Format(time.RFC3339)
want := "2016-09-20T22:59:57Z"
if got != want {
t.Errorf("msg.DateCreated: got %s, want %s", got, want)
}
if msg.Direction != DirectionOutboundReply {
t.Errorf("wrong direction")
}
if msg.Status != StatusDelivered {
t.Errorf("wrong status")
}
if msg.Body != "Welcome to ZomboCom." {
t.Errorf("wrong body")
}
if msg.From != PhoneNumber("+19253920364") {
t.Errorf("wrong from")
}
if msg.FriendlyPrice() != "$0.0075" {
t.Errorf("wrong friendly price %v, want %v", msg.FriendlyPrice(), "$0.00750")
}
}
func TestStatusFriendly(t *testing.T) {
t.Parallel()
if StatusQueued.Friendly() != "Queued" {
t.Errorf("expected StatusQueued.Friendly to equal Queued, got %s", StatusQueued.Friendly())
}
s := Status("in-progress")
if f := s.Friendly(); f != "In Progress" {
t.Errorf("expected In Progress.Friendly to equal In Progress, got %s", f)
}
}