This repository has been archived by the owner on Feb 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintegration_test.go
178 lines (145 loc) · 4.2 KB
/
integration_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
// +build integration
package gcmlib
import (
"flag"
"testing"
)
var apiKey = flag.String("key", "", "GCM API KEY")
var regID = flag.String("regid", "", "A valid registration id")
var changedRegID = flag.String("cregid", "", "A changed registration id")
var dryRun = flag.Bool("dry", true, "Dry run")
type badRequestTestCase struct {
message *Message
err *gcmError
}
var badRequestTestCases = []badRequestTestCase{
{
&Message{
RegistrationIDs: make([]string, 1001),
},
newError(ErrorBadRequest, "Number of messages on bulk (1001) exceeds maximum allowed (1000)\n"),
},
{
&Message{
To: "xx",
RegistrationIDs: []string{"id0"},
},
newError(ErrorBadRequest, "Must use either \"registration_ids\" field or \"to\", not both\n"),
},
// reserved "from" keyword
{
&Message{
To: "xx",
Data: map[string]string{"from": "reserved test"},
},
newError(ErrorBadRequest, "\"data\" key \"from\" is a reserved keyword\n"),
},
// TTL
{
&Message{
To: "JohnDoe",
TTL: maxTTL + 1,
},
newError(ErrorBadRequest, "Invalid value (2419201) for \"time_to_live\": must be between 0 and 2419200\n"),
},
// Missing registration_ids
{
&Message{},
newError(ErrorBadRequest, "Missing \"registration_ids\" field\n"),
},
// message too long
/*
{
&Message{
To: "xx",
Notification: &Notification{Body: strings.Repeat("a", 1024*1024)},
},
newError(RequestEntityTooLargeError, ""),
*/
}
func TestBadRequests(t *testing.T) {
client := NewClient(Config{APIKey: *apiKey})
for _, tc := range badRequestTestCases {
res, err := client.Send(tc.message)
if res != nil {
t.Errorf("Response: expected: %#v, actual: %#v", nil, res)
}
if err.Code() != tc.err.Code() {
t.Errorf("err.Code(): expected: %#v, actual: %#v", tc.err.Code(), err.Code())
}
if err.Error() != tc.err.Error() {
t.Errorf("err.Error(): expected: %#v, actual: %#v", tc.err.Error(), err.Error())
}
if err.ShouldRetry() != tc.err.ShouldRetry() {
t.Errorf("err.ShouldRetry: expected: %#v, actual: %#v", tc.err.ShouldRetry, err.ShouldRetry)
}
}
}
func TestAuthenticationError(t *testing.T) {
client := NewClient(Config{APIKey: "invalid-api-key"})
res, err := client.Send(&Message{})
expectedErr := newError(ErrorAuthentication, "")
if res != nil {
t.Errorf("Response: expected: %#v, actual: %#v", nil, res)
}
if err.Code() != expectedErr.Code() {
t.Errorf("err.Code(): expected: %#v, actual: %#v", expectedErr.Code(), err.Code())
}
if err.Error() != expectedErr.Error() {
t.Errorf("err.Error(): expected: %#v, actual: %#v", expectedErr.Error(), err.Error())
}
if err.ShouldRetry() != expectedErr.ShouldRetry() {
t.Errorf("err.ShouldRetry: expected: %#v, actual: %#v", expectedErr.ShouldRetry, err.ShouldRetry)
}
}
func TestSuccess(t *testing.T) {
if *regID == "" {
t.Skip("skipping success test since no 'regid' parameter provided")
}
client := NewClient(Config{APIKey: *apiKey})
msg := &Message{
To: *regID,
DryRun: *dryRun,
Notification: &Notification{Title: "gcm integration test message"},
}
t.Logf("Sending gcm message to: '%.40s...'", *regID)
res, err := client.Send(msg)
if err != nil {
t.Errorf("Couldn't send gcm message: %s", err)
return
}
if len(res.Results) != 1 {
t.Errorf("No results returned")
return
}
if !res.Results[0].Success() {
t.Errorf("Particular message delivery problem: %s", res.Results[0].Error)
}
}
func TestChangedRegistrationID(t *testing.T) {
if *changedRegID == "" {
t.Skip("skipping 'changed registration id' test since no 'cregid' parameter provided")
}
client := NewClient(Config{APIKey: *apiKey})
msg := &Message{
To: *changedRegID,
DryRun: *dryRun,
Notification: &Notification{Title: "gcm integration test message"},
}
t.Logf("Sending gcm message to: %.80s...", *changedRegID)
res, err := client.Send(msg)
if err != nil {
t.Errorf("Couldn't send gcm message: %s", err)
return
}
if len(res.Results) != 1 {
t.Errorf("No results returned")
return
}
if !res.Results[0].Success() {
t.Errorf("Particular message delivery problem: %s", res.Results[0].Error)
}
if !res.Results[0].TokenChanged() {
t.Errorf("Provided registration id is already canonical")
}
}