|
| 1 | +package admin_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | + "net/http/httptest" |
| 8 | + "testing" |
| 9 | + |
| 10 | + admin "github.com/acoshift/go-firebase-admin" |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | +) |
| 13 | + |
| 14 | +func TestSendToDevices(t *testing.T) { |
| 15 | + |
| 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 | + }) |
| 72 | + |
| 73 | +} |
0 commit comments