@@ -14,12 +14,10 @@ import (
1414func TestSendToDevices (t * testing.T ) {
1515
1616 t .Run ("send=success" , func (t * testing.T ) {
17- // generate Response
1817 srv := httptest .NewServer (http .HandlerFunc (func (rw http.ResponseWriter , req * http.Request ) {
19-
2018 rw .WriteHeader (http .StatusOK )
2119 rw .Header ().Set ("Content-Type" , "application/json" )
22- fmt .Fprint (rw , `{"success": 1,"failure": 0,"results": [{"message_id":"118218","registration_id": "abcd118218 ","error": ""}]}` )
20+ fmt .Fprint (rw , `{"multicast_id": 5438046884136077786," success": 1,"failure": 0,"results": [{"message_id":"118218","error": ""}]}` )
2321 }))
2422 defer srv .Close ()
2523
@@ -41,13 +39,47 @@ func TestSendToDevices(t *testing.T) {
4139 assert .NotNil (t , response )
4240 assert .Equal (t , 1 , response .Success )
4341 assert .Equal (t , 0 , response .Failure )
42+ assert .Equal (t , 0 , response .CanonicalIDs )
43+ assert .Equal (t , int64 (5438046884136077786 ), response .MulticastID )
44+ assert .Equal (t , []admin.Result {admin.Result {MessageID : "118218" }}, response .Results )
4445 })
4546
46- t .Run ("send=failure" , func (t * testing.T ) {
47- // generate Response
47+ t .Run ("send=BadToken" , func (t * testing.T ) {
4848 srv := httptest .NewServer (http .HandlerFunc (func (rw http.ResponseWriter , req * http.Request ) {
49+ rw .WriteHeader (http .StatusOK )
50+ rw .Header ().Set ("Content-Type" , "application/json" )
51+ fmt .Fprint (rw , `{"multicast_id": 5438046884136077786,"success": 0,"failure": 1,"results": [{"error": "InvalidRegistration"}]}` )
52+ }))
53+ defer srv .Close ()
54+
55+ app := initApp ()
56+ firFCM := app .FCM ()
57+ firFCM .NewFcmSendEndpoint (srv .URL )
58+
59+ assert .NotNil (t , app )
60+ assert .NotNil (t , firFCM )
4961
62+ response , err := firFCM .SendToDevice (context .Background (), "mydevicetoken" ,
63+ admin.Message {Notification : admin.Notification {
64+ Title : "Hello go firebase admin" ,
65+ Body : "My little Big Notification" ,
66+ Color : "#ffcc33" },
67+ })
68+
69+ assert .Nil (t , err )
70+ assert .NotNil (t , response )
71+ assert .Equal (t , 0 , response .Success )
72+ assert .Equal (t , 1 , response .Failure )
73+ assert .Equal (t , 0 , response .CanonicalIDs )
74+ assert .Equal (t , int64 (5438046884136077786 ), response .MulticastID )
75+ assert .Equal (t , []admin.Result {admin.Result {Error : admin .ErrInvalidRegistration }}, response .Results )
76+ })
77+
78+ t .Run ("send=missingDestination" , func (t * testing.T ) {
79+ srv := httptest .NewServer (http .HandlerFunc (func (rw http.ResponseWriter , req * http.Request ) {
5080 rw .WriteHeader (http .StatusBadRequest )
81+ rw .Header ().Set ("Content-Type" , "application/json" )
82+ fmt .Fprint (rw , `to` )
5183 }))
5284 defer srv .Close ()
5385
@@ -67,7 +99,199 @@ func TestSendToDevices(t *testing.T) {
6799
68100 assert .NotNil (t , err )
69101 assert .Nil (t , response )
102+ assert .Equal (t , fmt .Errorf ("StatusCode=%d, Desc=%s" , http .StatusBadRequest , admin .ErrInvalidParameters .Error ()), err )
103+ })
104+
105+ t .Run ("send=BadRequest" , func (t * testing.T ) {
106+ srv := httptest .NewServer (http .HandlerFunc (func (rw http.ResponseWriter , req * http.Request ) {
107+ rw .WriteHeader (http .StatusBadRequest )
108+ }))
109+ defer srv .Close ()
110+
111+ app := initApp ()
112+ firFCM := app .FCM ()
113+ firFCM .NewFcmSendEndpoint (srv .URL )
114+
115+ assert .NotNil (t , app )
116+ assert .NotNil (t , firFCM )
117+
118+ response , err := firFCM .SendToDevice (context .Background (), "mydevicetoken" ,
119+ admin.Message {Notification : admin.Notification {
120+ Title : "Hello go firebase admin" ,
121+ Body : "My little Big Notification" ,
122+ Color : "#ffcc33" },
123+ })
124+
125+ assert .NotNil (t , err )
126+ assert .Nil (t , response )
127+ assert .Equal (t , err , fmt .Errorf ("StatusCode=%d, Desc=%s" , http .StatusBadRequest , admin .ErrInvalidParameters .Error ()))
128+ })
129+
130+ t .Run ("send=Unauthorized" , func (t * testing.T ) {
131+ srv := httptest .NewServer (http .HandlerFunc (func (rw http.ResponseWriter , req * http.Request ) {
132+ rw .WriteHeader (http .StatusUnauthorized )
133+ }))
134+ defer srv .Close ()
135+
136+ app := initApp ()
137+ firFCM := app .FCM ()
138+ firFCM .NewFcmSendEndpoint (srv .URL )
139+
140+ assert .NotNil (t , app )
141+ assert .NotNil (t , firFCM )
142+
143+ response , err := firFCM .SendToDevice (context .Background (), "mydevicetoken" ,
144+ admin.Message {Notification : admin.Notification {
145+ Title : "Hello go firebase admin" ,
146+ Body : "My little Big Notification" ,
147+ Color : "#ffcc33" },
148+ })
149+
150+ assert .NotNil (t , err )
151+ assert .Nil (t , response )
152+ assert .Equal (t , err , fmt .Errorf ("StatusCode=%d, Desc=%s" , http .StatusUnauthorized , admin .ErrAuthentication .Error ()))
153+ })
154+ }
155+
156+ func TestSendToTopic (t * testing.T ) {
157+
158+ t .Run ("send=success" , func (t * testing.T ) {
159+ srv := httptest .NewServer (http .HandlerFunc (func (rw http.ResponseWriter , req * http.Request ) {
160+ rw .WriteHeader (http .StatusOK )
161+ rw .Header ().Set ("Content-Type" , "application/json" )
162+ fmt .Fprint (rw , `{"multicast_id": 5438046884136077786,"success": 1,"failure": 0,"results": [{"message_id":"118218","error": ""}]}` )
163+ }))
164+ defer srv .Close ()
165+
166+ app := initApp ()
167+ firFCM := app .FCM ()
168+ firFCM .NewFcmSendEndpoint (srv .URL )
169+
170+ assert .NotNil (t , app )
171+ assert .NotNil (t , firFCM )
172+
173+ response , err := firFCM .SendToTopic (context .Background (), "mytopic" ,
174+ admin.Message {Notification : admin.Notification {
175+ Title : "Hello go firebase admin" ,
176+ Body : "My little Big Notification" ,
177+ Color : "#ffcc33" },
178+ })
179+
180+ assert .Nil (t , err )
181+ assert .NotNil (t , response )
182+ assert .Equal (t , 1 , response .Success )
183+ assert .Equal (t , 0 , response .Failure )
184+ assert .Equal (t , 0 , response .CanonicalIDs )
185+ assert .Equal (t , int64 (5438046884136077786 ), response .MulticastID )
186+ assert .Equal (t , []admin.Result {admin.Result {MessageID : "118218" }}, response .Results )
187+ })
188+
189+ t .Run ("send=BadToken" , func (t * testing.T ) {
190+ srv := httptest .NewServer (http .HandlerFunc (func (rw http.ResponseWriter , req * http.Request ) {
191+ rw .WriteHeader (http .StatusOK )
192+ rw .Header ().Set ("Content-Type" , "application/json" )
193+ fmt .Fprint (rw , `{"multicast_id": 5438046884136077786,"success": 0,"failure": 1,"results": [{"error": "InvalidRegistration"}]}` )
194+ }))
195+ defer srv .Close ()
196+
197+ app := initApp ()
198+ firFCM := app .FCM ()
199+ firFCM .NewFcmSendEndpoint (srv .URL )
200+
201+ assert .NotNil (t , app )
202+ assert .NotNil (t , firFCM )
203+
204+ response , err := firFCM .SendToTopic (context .Background (), "mytopic" ,
205+ admin.Message {Notification : admin.Notification {
206+ Title : "Hello go firebase admin" ,
207+ Body : "My little Big Notification" ,
208+ Color : "#ffcc33" },
209+ })
210+
211+ assert .Nil (t , err )
212+ assert .NotNil (t , response )
213+ assert .Equal (t , 0 , response .Success )
214+ assert .Equal (t , 1 , response .Failure )
215+ assert .Equal (t , 0 , response .CanonicalIDs )
216+ assert .Equal (t , int64 (5438046884136077786 ), response .MulticastID )
217+ assert .Equal (t , []admin.Result {admin.Result {Error : admin .ErrInvalidRegistration }}, response .Results )
218+ })
219+
220+ t .Run ("send=missingDestination" , func (t * testing.T ) {
221+ srv := httptest .NewServer (http .HandlerFunc (func (rw http.ResponseWriter , req * http.Request ) {
222+ rw .WriteHeader (http .StatusBadRequest )
223+ rw .Header ().Set ("Content-Type" , "application/json" )
224+ fmt .Fprint (rw , `to` )
225+ }))
226+ defer srv .Close ()
227+
228+ app := initApp ()
229+ firFCM := app .FCM ()
230+ firFCM .NewFcmSendEndpoint (srv .URL )
231+
232+ assert .NotNil (t , app )
233+ assert .NotNil (t , firFCM )
234+
235+ response , err := firFCM .SendToTopic (context .Background (), "mytopic" ,
236+ admin.Message {Notification : admin.Notification {
237+ Title : "Hello go firebase admin" ,
238+ Body : "My little Big Notification" ,
239+ Color : "#ffcc33" },
240+ })
241+
242+ assert .NotNil (t , err )
243+ assert .Nil (t , response )
244+ assert .Equal (t , fmt .Errorf ("StatusCode=%d, Desc=%s" , http .StatusBadRequest , admin .ErrInvalidParameters .Error ()), err )
245+ })
246+
247+ t .Run ("send=BadRequest" , func (t * testing.T ) {
248+ srv := httptest .NewServer (http .HandlerFunc (func (rw http.ResponseWriter , req * http.Request ) {
249+ rw .WriteHeader (http .StatusBadRequest )
250+ }))
251+ defer srv .Close ()
252+
253+ app := initApp ()
254+ firFCM := app .FCM ()
255+ firFCM .NewFcmSendEndpoint (srv .URL )
256+
257+ assert .NotNil (t , app )
258+ assert .NotNil (t , firFCM )
259+
260+ response , err := firFCM .SendToTopic (context .Background (), "mytopic" ,
261+ admin.Message {Notification : admin.Notification {
262+ Title : "Hello go firebase admin" ,
263+ Body : "My little Big Notification" ,
264+ Color : "#ffcc33" },
265+ })
266+
267+ assert .NotNil (t , err )
268+ assert .Nil (t , response )
269+ assert .Equal (t , err , fmt .Errorf ("StatusCode=%d, Desc=%s" , http .StatusBadRequest , admin .ErrInvalidParameters .Error ()))
270+ })
271+
272+ t .Run ("send=Unauthorized" , func (t * testing.T ) {
273+ srv := httptest .NewServer (http .HandlerFunc (func (rw http.ResponseWriter , req * http.Request ) {
274+ rw .WriteHeader (http .StatusUnauthorized )
275+ }))
276+ defer srv .Close ()
277+
278+ app := initApp ()
279+ firFCM := app .FCM ()
280+ firFCM .NewFcmSendEndpoint (srv .URL )
70281
282+ assert .NotNil (t , app )
283+ assert .NotNil (t , firFCM )
284+
285+ response , err := firFCM .SendToTopic (context .Background (), "mytopic" ,
286+ admin.Message {Notification : admin.Notification {
287+ Title : "Hello go firebase admin" ,
288+ Body : "My little Big Notification" ,
289+ Color : "#ffcc33" },
290+ })
291+
292+ assert .NotNil (t , err )
293+ assert .Nil (t , response )
294+ assert .Equal (t , err , fmt .Errorf ("StatusCode=%d, Desc=%s" , http .StatusUnauthorized , admin .ErrAuthentication .Error ()))
71295 })
72296
73297}
0 commit comments