-
Notifications
You must be signed in to change notification settings - Fork 2
/
sender_test.go
230 lines (211 loc) · 7.72 KB
/
sender_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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package gcm
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
)
var data = map[string]string{"k": "v"}
var msg = &Message{Data: data}
var twoRecipients = []string{"1", "2"}
var topic = TopicPrefix + "global"
var success = response{Success: 1, Results: []result{{MessageID: "id"}}}
var fail = response{Failure: 1, Results: []result{{Err: ErrorUnavailable}}}
var partialDeviceGroup = response{Success: 1, Failure: 2, FailedRegistrationIDs: []string{"id1", "id2"}}
var partialMulticast = response{MulticastID: 1, Success: 1, Failure: 1, Results: []result{{MessageID: "id1"}, {Err: ErrorUnavailable}}}
func TestSendWithInvalidAPIKey(t *testing.T) {
server := startTestServer(t)
defer server.Close()
s := NewSender("")
_, err := s.SendNoRetry(msg, "1")
assert.EqualError(t, err, "missing API key")
_, err = s.SendWithRetries(msg, "1", 1)
assert.EqualError(t, err, "missing API key")
_, err = s.SendMulticastNoRetry(msg, twoRecipients)
assert.EqualError(t, err, "missing API key")
_, err = s.SendMulticastWithRetries(msg, twoRecipients, 1)
assert.EqualError(t, err, "missing API key")
}
func TestSendWithInvalidMessage(t *testing.T) {
server := startTestServer(t)
defer server.Close()
s := NewSender("test-api-key")
params := []struct {
msg *Message
err string
}{
{nil, "message cannot be nil"},
{&Message{TimeToLive: -1}, "TimeToLive should be non-negative and at most 4 weeks"},
{&Message{TimeToLive: 2419201}, "TimeToLive should be non-negative and at most 4 weeks"},
}
for _, param := range params {
_, err := s.SendNoRetry(param.msg, "1")
assert.EqualError(t, err, param.err)
_, err = s.SendWithRetries(param.msg, "1", 1)
assert.EqualError(t, err, param.err)
_, err = s.SendMulticastNoRetry(param.msg, twoRecipients)
assert.EqualError(t, err, param.err)
_, err = s.SendMulticastWithRetries(param.msg, twoRecipients, 1)
assert.EqualError(t, err, param.err)
}
}
func TestSendWithInvalidRetries(t *testing.T) {
server := startTestServer(t)
defer server.Close()
s := NewSender("test-api-key")
_, err := s.SendWithRetries(msg, "1", -1)
assert.EqualError(t, err, "retries cannot be negative")
_, err = s.SendMulticastWithRetries(msg, twoRecipients, -1)
assert.EqualError(t, err, "retries cannot be negative")
}
func TestSendWithInvalidRecipients(t *testing.T) {
server := startTestServer(t)
defer server.Close()
s := NewSender("test-api-key")
_, err := s.SendNoRetry(msg, "")
assert.EqualError(t, err, "missing recipient(s)")
_, err = s.SendWithRetries(msg, "", 0)
assert.EqualError(t, err, "missing recipient(s)")
_, err = s.SendMulticastNoRetry(msg, nil)
assert.EqualError(t, err, "missing recipient(s)")
_, err = s.SendMulticastNoRetry(msg, []string{})
assert.EqualError(t, err, "missing recipient(s)")
_, err = s.SendMulticastWithRetries(msg, nil, 0)
assert.EqualError(t, err, "missing recipient(s)")
_, err = s.SendMulticastWithRetries(msg, []string{}, 0)
assert.EqualError(t, err, "missing recipient(s)")
}
func TestSendRetryOk_DueToApiError(t *testing.T) {
server := startTestServer(t,
&testResponse{response: &fail},
&testResponse{response: &success},
)
defer server.Close()
s := NewSender("test-api-key")
result, err := s.SendWithRetries(msg, "regId", 1)
assert.NoError(t, err)
assert.Equal(t, Result{MessageID: "id"}, *result)
}
func TestSendRetryOk_DueToHttpError(t *testing.T) {
server := startTestServer(t,
&testResponse{statusCode: http.StatusInternalServerError},
&testResponse{response: &success},
)
defer server.Close()
s := NewSender("test-api-key")
result, err := s.SendWithRetries(msg, "regId", 1)
assert.NoError(t, err)
assert.Equal(t, Result{MessageID: "id"}, *result)
}
func TestSendRetryFail_DueToExceededRetries(t *testing.T) {
server := startTestServer(t,
&testResponse{response: &fail},
&testResponse{response: &fail},
)
defer server.Close()
s := NewSender("test-api-key")
result, err := s.SendWithRetries(msg, "regId", 1)
assert.NoError(t, err)
assert.Equal(t, Result{Error: ErrorUnavailable}, *result)
}
func TestSendRetryFail_DueToTopicRateExceeded(t *testing.T) {
server := startTestServer(t, &testResponse{response: &response{Err: ErrorTopicsMessageRateExceeded}})
defer server.Close()
s := NewSender("test-api-key")
result, err := s.SendWithRetries(msg, topic, 1)
assert.NoError(t, err)
assert.Equal(t, Result{Error: ErrorTopicsMessageRateExceeded}, *result)
}
func TestSendRetryFail_DueToDeviceGroupPartialFail(t *testing.T) {
server := startTestServer(t, &testResponse{response: &partialDeviceGroup})
defer server.Close()
s := NewSender("test-api-key")
result, err := s.SendWithRetries(msg, "group", 1)
assert.NoError(t, err)
assert.Equal(t, Result{Success: 1, Failure: 2, FailedRegistrationIDs: []string{"id1", "id2"}}, *result)
}
func TestSendRetryError_DueToUnrecoverableHttpError(t *testing.T) {
server := startTestServer(t, &testResponse{statusCode: http.StatusBadRequest})
defer server.Close()
s := NewSender("test-api-key")
_, err := s.SendWithRetries(msg, "regId", 1)
assert.EqualError(t, err, "400 error: 400 Bad Request")
}
func TestSendMulticastRetryError_DueToUnrecoverableHttpError(t *testing.T) {
server := startTestServer(t, &testResponse{statusCode: http.StatusBadRequest})
defer server.Close()
s := NewSender("test-api-key")
_, err := s.SendMulticastWithRetries(msg, twoRecipients, 1)
assert.EqualError(t, err, "400 error: 400 Bad Request")
}
func TestSendMulticastRetryOk(t *testing.T) {
server := startTestServer(t,
&testResponse{response: &partialMulticast},
&testResponse{response: &response{MulticastID: 2, Success: 1, Results: []result{{MessageID: "id2"}}}},
)
defer server.Close()
s := NewSender("test-api-key")
result, err := s.SendMulticastWithRetries(msg, twoRecipients, 1)
assert.NoError(t, err)
assert.Equal(t, MulticastResult{MulticastID: 1, Success: 2, RetryMulticastIDs: []int64{2}, Results: []Result{{MessageID: "id1"}, {MessageID: "id2"}}}, *result)
}
func TestSendMulticastRetryPartialFail_DueToExceededRetries(t *testing.T) {
server := startTestServer(t,
&testResponse{response: &partialMulticast},
&testResponse{response: &response{MulticastID: 2, Failure: 1, Results: []result{{Err: ErrorUnavailable}}}},
)
defer server.Close()
s := NewSender("test-api-key")
result, err := s.SendMulticastWithRetries(msg, twoRecipients, 1)
assert.NoError(t, err)
assert.Equal(t, MulticastResult{
MulticastID: 1,
Success: 1,
Failure: 1,
RetryMulticastIDs: []int64{2},
Results: []Result{{MessageID: "id1"}, {Error: ErrorUnavailable}},
}, *result)
}
func TestSendMulticastRetryPartialFail_DueToUnrecoverableError(t *testing.T) {
server := startTestServer(t,
&testResponse{response: &partialMulticast},
&testResponse{statusCode: http.StatusBadRequest},
)
defer server.Close()
s := NewSender("test-api-key")
result, err := s.SendMulticastWithRetries(msg, twoRecipients, 1)
assert.NoError(t, err)
assert.Equal(t, MulticastResult{
MulticastID: 1,
Success: 1,
Failure: 1,
Results: []Result{{MessageID: "id1"}, {Error: ErrorUnavailable}},
}, *result)
}
type testResponse struct {
statusCode int
response *response
}
func startTestServer(t *testing.T, responses ...*testResponse) *httptest.Server {
i := 0
handler := func(w http.ResponseWriter, r *http.Request) {
if i >= len(responses) {
t.Fatalf("server received %d requests, expected %d", i+1, len(responses))
}
resp := responses[i]
status := resp.statusCode
if status == 0 || status == http.StatusOK {
w.Header().Set("Content-Type", "application/json")
respBytes, _ := json.Marshal(resp.response)
fmt.Fprint(w, string(respBytes))
} else {
w.WriteHeader(status)
}
i++
}
server := httptest.NewServer(http.HandlerFunc(handler))
GCMEndpoint = server.URL
return server
}