-
Notifications
You must be signed in to change notification settings - Fork 72
/
sender_test.go
162 lines (150 loc) · 5.58 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
package gcm
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"testing"
)
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))
gcmSendEndpoint = server.URL
return server
}
func TestSendNoRetryInvalidApiKey(t *testing.T) {
server := startTestServer(t)
defer server.Close()
sender := &Sender{ApiKey: ""}
if _, err := sender.SendNoRetry(&Message{RegistrationIDs: []string{"1"}}); err == nil {
t.Fatal("test should fail when sender's ApiKey is \"\"")
}
}
func TestSendInvalidApiKey(t *testing.T) {
server := startTestServer(t)
defer server.Close()
sender := &Sender{ApiKey: ""}
if _, err := sender.Send(&Message{RegistrationIDs: []string{"1"}}, 0); err == nil {
t.Fatal("test should fail when sender's ApiKey is \"\"")
}
}
func TestSendNoRetryInvalidMessage(t *testing.T) {
server := startTestServer(t)
defer server.Close()
sender := &Sender{ApiKey: "test"}
if _, err := sender.SendNoRetry(nil); err == nil {
t.Fatal("test should fail when message is nil")
}
if _, err := sender.SendNoRetry(&Message{}); err == nil {
t.Fatal("test should fail when message RegistrationIDs field is nil")
}
if _, err := sender.SendNoRetry(&Message{RegistrationIDs: []string{}}); err == nil {
t.Fatal("test should fail when message RegistrationIDs field is an empty slice")
}
if _, err := sender.SendNoRetry(&Message{RegistrationIDs: make([]string, 1001)}); err == nil {
t.Fatal("test should fail when more than 1000 RegistrationIDs are specified")
}
if _, err := sender.SendNoRetry(&Message{RegistrationIDs: []string{"1"}, TimeToLive: -1}); err == nil {
t.Fatal("test should fail when message TimeToLive field is negative")
}
if _, err := sender.SendNoRetry(&Message{RegistrationIDs: []string{"1"}, TimeToLive: 2419201}); err == nil {
t.Fatal("test should fail when message TimeToLive field is greater than 2419200")
}
}
func TestSendInvalidMessage(t *testing.T) {
server := startTestServer(t)
defer server.Close()
sender := &Sender{ApiKey: "test"}
if _, err := sender.Send(nil, 0); err == nil {
t.Fatal("test should fail when message is nil")
}
if _, err := sender.Send(&Message{}, 0); err == nil {
t.Fatal("test should fail when message RegistrationIDs field is nil")
}
if _, err := sender.Send(&Message{RegistrationIDs: []string{}}, 0); err == nil {
t.Fatal("test should fail when message RegistrationIDs field is an empty slice")
}
if _, err := sender.Send(&Message{RegistrationIDs: make([]string, 1001)}, 0); err == nil {
t.Fatal("test should fail when more than 1000 RegistrationIDs are specified")
}
if _, err := sender.Send(&Message{RegistrationIDs: []string{"1"}, TimeToLive: -1}, 0); err == nil {
t.Fatal("test should fail when message TimeToLive field is negative")
}
if _, err := sender.Send(&Message{RegistrationIDs: []string{"1"}, TimeToLive: 2419201}, 0); err == nil {
t.Fatal("test should fail when message TimeToLive field is greater than 2419200")
}
}
func TestSendNoRetrySuccess(t *testing.T) {
server := startTestServer(t, &testResponse{Response: &Response{}})
defer server.Close()
sender := &Sender{ApiKey: "test"}
msg := NewMessage(map[string]interface{}{"key": "value"}, "1")
if _, err := sender.SendNoRetry(msg); err != nil {
t.Fatalf("test failed with error: %s", err)
}
}
func TestSendNoRetryNonrecoverableFailure(t *testing.T) {
server := startTestServer(t, &testResponse{StatusCode: http.StatusBadRequest})
defer server.Close()
sender := &Sender{ApiKey: "test"}
msg := NewMessage(map[string]interface{}{"key": "value"}, "1")
if _, err := sender.SendNoRetry(msg); err == nil {
t.Fatal("test expected non-recoverable error")
}
}
func TestSendOneRetrySuccess(t *testing.T) {
server := startTestServer(t,
&testResponse{Response: &Response{Failure: 1, Results: []Result{{Error: "Unavailable"}}}},
&testResponse{Response: &Response{Success: 1, Results: []Result{{MessageID: "id"}}}},
)
defer server.Close()
sender := &Sender{ApiKey: "test"}
msg := NewMessage(map[string]interface{}{"key": "value"}, "1")
if _, err := sender.Send(msg, 1); err != nil {
t.Fatal("send should succeed after one retry")
}
}
func TestSendOneRetryFailure(t *testing.T) {
server := startTestServer(t,
&testResponse{Response: &Response{Failure: 1, Results: []Result{{Error: "Unavailable"}}}},
&testResponse{Response: &Response{Failure: 1, Results: []Result{{Error: "Unavailable"}}}},
)
defer server.Close()
sender := &Sender{ApiKey: "test"}
msg := NewMessage(map[string]interface{}{"key": "value"}, "1")
resp, err := sender.Send(msg, 1)
if err != nil || resp.Failure != 1 {
t.Fatal("send should return response with one failure")
}
}
func TestSendOneRetryNonrecoverableFailure(t *testing.T) {
server := startTestServer(t,
&testResponse{Response: &Response{Failure: 1, Results: []Result{{Error: "Unavailable"}}}},
&testResponse{StatusCode: http.StatusBadRequest},
)
defer server.Close()
sender := &Sender{ApiKey: "test"}
msg := NewMessage(map[string]interface{}{"key": "value"}, "1")
if _, err := sender.Send(msg, 1); err == nil {
t.Fatal("send should fail after one retry")
}
}