Skip to content
This repository has been archived by the owner on Mar 31, 2019. It is now read-only.

Commit

Permalink
adding in a basic test for messaging
Browse files Browse the repository at this point in the history
  • Loading branch information
zabawaba99 committed Nov 28, 2016
1 parent 8bdf33d commit a45ac51
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
5 changes: 3 additions & 2 deletions messaging/fcm.go → messaging/messaging.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const Endpoint = "https://fcm.googleapis.com/fcm/send"
// Messaing represents the Firebase Cloud Messaging service.
type Messaing struct {
apiKey string
apiURL string
client *http.Client
}

Expand All @@ -24,6 +25,7 @@ func New(apiKey string, client *http.Client) *Messaing {

return &Messaing{
apiKey: apiKey,
apiURL: Endpoint,
client: client,
}
}
Expand All @@ -34,7 +36,7 @@ func (f *Messaing) Send(msg Message) (*Response, error) {
if err != nil {
return nil, err
}
req, err := http.NewRequest("POST", Endpoint, bytes.NewReader(b))
req, err := http.NewRequest("POST", f.apiURL, bytes.NewReader(b))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -115,7 +117,6 @@ type Message struct {
// This means a FCM connection server can simultaneously store 4 different send-to-sync
// messages per client app. If you exceed this number, there is no guarantee which 4 collapse
// keys the FCM connection server will keep.

CollapseKey string `json:"collapse_key,omitempty"`

// Priority sets the priority of the message. Valid values are "normal" and "high."
Expand Down
36 changes: 36 additions & 0 deletions messaging/messaging_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package messaging

import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestSend(t *testing.T) {
var serverCalled bool
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
serverCalled = true
err := json.NewEncoder(w).Encode(&Response{Failure: 0})
require.NoError(t, err)
}))
defer server.Close()

serverKey := "hello-world"
fcm := New(serverKey, nil)
fcm.apiURL = server.URL

msg := Message{
Token: "foo",
Priority: "asdas",
}
resp, err := fcm.Send(msg)
require.NoError(t, err)
require.NotNil(t, resp)

assert.Equal(t, 0, resp.Failure)
assert.True(t, serverCalled)
}

0 comments on commit a45ac51

Please sign in to comment.