Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions messaging/messaging.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ import (
)

const (
messagingEndpoint = "https://fcm.googleapis.com/v1"
batchEndpoint = "https://fcm.googleapis.com/batch"
defaultMessagingEndpoint = "https://fcm.googleapis.com/v1"
defaultBatchEndpoint = "https://fcm.googleapis.com/batch"

firebaseClientHeader = "X-Firebase-Client"
apiFormatVersionHeader = "X-GOOG-API-FORMAT-VERSION"
Expand Down Expand Up @@ -862,17 +862,20 @@ func NewClient(ctx context.Context, c *internal.MessagingConfig) (*Client, error
return nil, errors.New("project ID is required to access Firebase Cloud Messaging client")
}

hc, endpoint, err := transport.NewHTTPClient(ctx, c.Opts...)
hc, messagingEndpoint, err := transport.NewHTTPClient(ctx, c.Opts...)
if err != nil {
return nil, err
}

if endpoint == "" {
endpoint = messagingEndpoint
batchEndpoint := messagingEndpoint

if messagingEndpoint == "" {
messagingEndpoint = defaultMessagingEndpoint
batchEndpoint = defaultBatchEndpoint
}

return &Client{
fcmClient: newFCMClient(hc, c, endpoint),
fcmClient: newFCMClient(hc, c, messagingEndpoint, batchEndpoint),
iidClient: newIIDClient(hc),
}, nil
}
Expand All @@ -885,7 +888,7 @@ type fcmClient struct {
httpClient *internal.HTTPClient
}

func newFCMClient(hc *http.Client, conf *internal.MessagingConfig, endpoint string) *fcmClient {
func newFCMClient(hc *http.Client, conf *internal.MessagingConfig, messagingEndpoint string, batchEndpoint string) *fcmClient {
client := internal.WithDefaultRetryConfig(hc)
client.CreateErrFn = handleFCMError

Expand All @@ -896,7 +899,7 @@ func newFCMClient(hc *http.Client, conf *internal.MessagingConfig, endpoint stri
}

return &fcmClient{
fcmEndpoint: endpoint,
fcmEndpoint: messagingEndpoint,
batchEndpoint: batchEndpoint,
project: conf.ProjectID,
version: version,
Expand Down
42 changes: 42 additions & 0 deletions messaging/messaging_batch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import (
"net/http/httptest"
"net/textproto"
"testing"

"google.golang.org/api/option"
)

var testMessages = []*Message{
Expand Down Expand Up @@ -520,6 +522,46 @@ func TestSendMulticast(t *testing.T) {
}
}

func TestSendMulticastWithCustomEndpoint(t *testing.T) {
resp, err := createMultipartResponse(testSuccessResponse, nil)
if err != nil {
t.Fatal(err)
}

var req []byte
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
req, _ = ioutil.ReadAll(r.Body)
w.Header().Set("Content-Type", wantMime)
w.Write(resp)
}))
defer ts.Close()

ctx := context.Background()

conf := *testMessagingConfig
customBatchEndpoint := fmt.Sprintf("%s/v1", ts.URL)
optEndpoint := option.WithEndpoint(customBatchEndpoint)
conf.Opts = append(conf.Opts, optEndpoint)

client, err := NewClient(ctx, &conf)
if err != nil {
t.Fatal(err)
}

if customBatchEndpoint != client.batchEndpoint {
t.Errorf("client.batchEndpoint = %q; want = %q", client.batchEndpoint, customBatchEndpoint)
}

br, err := client.SendMulticast(ctx, testMulticastMessage)
if err != nil {
t.Fatal(err)
}

if err := checkSuccessfulBatchResponse(br, req, false); err != nil {
t.Errorf("SendMulticast() = %v", err)
}
}

func TestSendMulticastDryRun(t *testing.T) {
resp, err := createMultipartResponse(testSuccessResponse, nil)
if err != nil {
Expand Down