@@ -13,38 +13,61 @@ import (
1313
1414func TestSendToDevices (t * testing.T ) {
1515
16- // generate Response
17- srv := httptest .NewServer (http .HandlerFunc (func (rw http.ResponseWriter , req * http.Request ) {
18-
19- rw .WriteHeader (http .StatusOK )
20- rw .Header ().Set ("Content-Type" , "application/json" )
21- fmt .Fprint (rw , `{
22- "success": 1,
23- "failure": 0,
24- "results": [{
25- "message_id":"118218",
26- "registration_id": "abcd118218",
27- "error": ""
28- }]
29- }` )
30- }))
31- defer srv .Close ()
32-
33- app := initApp ()
34- firFCM := app .FCM ()
35- firFCM .NewFcmSendEndpoint (srv .URL )
36-
37- assert .NotNil (t , firFCM )
38-
39- response , err := firFCM .SendToDevice (context .Background (), "mydevicetoken" ,
40- admin.Message {Notification : admin.Notification {
41- Title : "Hello go firebase admin" ,
42- Body : "My little Big Notification" ,
43- Color : "#ffcc33" },
44- })
45-
46- assert .Nil (t , err , fmt .Sprintf ("unexpected error: %v" , err ))
47- assert .Equal (t , 1 , response .Success , fmt .Sprintf ("expected success: %v" , err ))
48- assert .Equal (t , 0 , response .Failure , fmt .Sprintf ("Not expected failure: %v" , err ))
16+ t .Run ("send=success" , func (t * testing.T ) {
17+ // generate Response
18+ srv := httptest .NewServer (http .HandlerFunc (func (rw http.ResponseWriter , req * http.Request ) {
19+
20+ rw .WriteHeader (http .StatusOK )
21+ rw .Header ().Set ("Content-Type" , "application/json" )
22+ fmt .Fprint (rw , `{"success": 1,"failure": 0,"results": [{"message_id":"118218","registration_id": "abcd118218","error": ""}]}` )
23+ }))
24+ defer srv .Close ()
25+
26+ app := initApp ()
27+ firFCM := app .FCM ()
28+ firFCM .NewFcmSendEndpoint (srv .URL )
29+
30+ assert .NotNil (t , app )
31+ assert .NotNil (t , firFCM )
32+
33+ response , err := firFCM .SendToDevice (context .Background (), "mydevicetoken" ,
34+ admin.Message {Notification : admin.Notification {
35+ Title : "Hello go firebase admin" ,
36+ Body : "My little Big Notification" ,
37+ Color : "#ffcc33" },
38+ })
39+
40+ assert .Nil (t , err )
41+ assert .NotNil (t , response )
42+ assert .Equal (t , 1 , response .Success )
43+ assert .Equal (t , 0 , response .Failure )
44+ })
45+
46+ t .Run ("send=failure" , func (t * testing.T ) {
47+ // generate Response
48+ srv := httptest .NewServer (http .HandlerFunc (func (rw http.ResponseWriter , req * http.Request ) {
49+
50+ rw .WriteHeader (http .StatusBadRequest )
51+ }))
52+ defer srv .Close ()
53+
54+ app := initApp ()
55+ firFCM := app .FCM ()
56+ firFCM .NewFcmSendEndpoint (srv .URL )
57+
58+ assert .NotNil (t , app )
59+ assert .NotNil (t , firFCM )
60+
61+ response , err := firFCM .SendToDevice (context .Background (), "mydevicetoken" ,
62+ admin.Message {Notification : admin.Notification {
63+ Title : "Hello go firebase admin" ,
64+ Body : "My little Big Notification" ,
65+ Color : "#ffcc33" },
66+ })
67+
68+ assert .NotNil (t , err )
69+ assert .Nil (t , response )
70+
71+ })
4972
5073}
0 commit comments