-
Notifications
You must be signed in to change notification settings - Fork 2
/
list_test.go
196 lines (183 loc) · 5.06 KB
/
list_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
190
191
192
193
194
195
196
package vtwilio
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestListMessages(t *testing.T) {
message := &Message{
SID: "sid",
DateCreated: time.Date(2017, time.January, 01, 01, 01, 01, 01, time.UTC).String(),
DateUpdated: time.Date(2017, time.January, 01, 01, 01, 01, 01, time.UTC).String(),
DateSent: time.Date(2017, time.January, 01, 01, 01, 01, 01, time.UTC).String(),
AccountSID: "account_sid",
To: "+123445678910",
From: "+10987654321",
MessagingServiceSID: "messaging_sid",
Body: "message",
Status: "200",
NumSegments: "1",
NumMedia: "0",
Direction: "",
APIVersion: "2010-04-01",
Price: "0.00",
PriceUnit: "1",
ErrorCode: "",
ErrorMessage: "",
URI: "uri",
SubresourceURIs: Media{
Media: "media",
},
}
expected := &List{
FirstPageURI: "http://pageuri.com",
End: 10,
PreviousPageURI: "http://pageuri.com/prev",
Messages: []*Message{message, message},
}
tests := []struct {
name string
in []ListOption
expected *List
expectedError bool
expectedPath string
expectedQuery string
failServer bool
checkResult bool
requestURL string
}{
{
name: "no options",
in: []ListOption{},
expectedPath: "/sid/Messages.json",
expectedQuery: "PageSize=10&Page=0",
},
{
name: "to",
in: []ListOption{To("+12345678910")},
expectedPath: "/sid/Messages.json",
expectedQuery: "PageSize=10&Page=0&To=+12345678910",
},
{
name: "to",
in: []ListOption{From("+12345678910")},
expectedPath: "/sid/Messages.json",
expectedQuery: "PageSize=10&Page=0&From=+12345678910",
},
{
name: "on date",
in: []ListOption{OnDate(time.Date(2017, time.January, 01, 01, 01, 01, 01, time.UTC))},
expectedPath: "/sid/Messages.json",
expectedQuery: "PageSize=10&Page=0&DateSent=2017-01-01",
},
{
name: "on and before date",
in: []ListOption{OnAndBeforeDate(time.Date(2017, time.January, 01, 01, 01, 01, 01, time.UTC))},
expectedPath: "/sid/Messages.json",
expectedQuery: "PageSize=10&Page=0&DateSent<=2017-01-01",
},
{
name: "on and after date",
in: []ListOption{OnAndAfterDate(time.Date(2017, time.January, 01, 01, 01, 01, 01, time.UTC))},
expectedPath: "/sid/Messages.json",
expectedQuery: "PageSize=10&Page=0&DateSent>=2017-01-01",
},
{
name: "page size",
in: []ListOption{PageSize(20)},
expectedPath: "/sid/Messages.json",
expectedQuery: "PageSize=20&Page=0",
},
{
name: "page size",
in: []ListOption{Page(2)},
expectedPath: "/sid/Messages.json",
expectedQuery: "PageSize=10&Page=2",
},
{
name: "valid request",
in: []ListOption{},
expected: expected,
expectedError: false,
expectedPath: "/sid/Messages.json",
expectedQuery: "PageSize=10&Page=0",
checkResult: true,
},
{
name: "bad request url",
in: []ListOption{},
expectedPath: "/sid/Messages.json",
expectedQuery: "PageSize=10&Page=0",
expected: nil,
checkResult: true,
expectedError: true,
requestURL: "http://192.168.0.%31/",
},
{
name: "bad request",
in: []ListOption{},
expectedPath: "/sid/Messages.json",
expected: nil,
checkResult: true,
expectedError: true,
failServer: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if tt.failServer {
resp := struct {
Status int
Message string
}{
Status: 400,
Message: "invalid request",
}
w.WriteHeader(http.StatusBadRequest)
bytes, err := json.Marshal(&resp)
if err != nil {
t.Error(err)
}
w.Write(bytes)
return
}
w.WriteHeader(http.StatusOK)
if r.Method != "GET" {
t.Errorf("Expected 'GET' got %v", r.Method)
}
assert.Equal(t, tt.expectedPath, r.URL.Path)
assert.Equal(t, tt.expectedQuery, r.URL.RawQuery)
bytes, err := json.Marshal(expected)
if err != nil {
t.Fatal(err)
}
w.Write(bytes)
}))
defer ts.Close()
requestURL := fmt.Sprintf("%s/", ts.URL)
if tt.requestURL != "" {
requestURL = tt.requestURL
}
v := &VTwilio{
accountSID: "sid",
authToken: "token",
twilioNumber: "+12345678910",
baseAPI: requestURL,
}
actual, err := v.ListMessages(tt.in...)
if tt.checkResult {
assert.Equal(t, tt.expected, actual)
if err == nil && tt.expectedError {
t.Error("expected error got nil")
} else if err != nil && !tt.expectedError {
t.Errorf("did not expect error, got: %v", err)
}
}
})
}
}