Skip to content

Commit 13184d3

Browse files
fix(fcm): Updated messaging code to take into account the custom endpoint setting for the FCM batch endpoint (#385) (#393)
1 parent e921fe9 commit 13184d3

File tree

2 files changed

+53
-8
lines changed

2 files changed

+53
-8
lines changed

messaging/messaging.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ import (
3232
)
3333

3434
const (
35-
messagingEndpoint = "https://fcm.googleapis.com/v1"
36-
batchEndpoint = "https://fcm.googleapis.com/batch"
35+
defaultMessagingEndpoint = "https://fcm.googleapis.com/v1"
36+
defaultBatchEndpoint = "https://fcm.googleapis.com/batch"
3737

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

865-
hc, endpoint, err := transport.NewHTTPClient(ctx, c.Opts...)
865+
hc, messagingEndpoint, err := transport.NewHTTPClient(ctx, c.Opts...)
866866
if err != nil {
867867
return nil, err
868868
}
869869

870-
if endpoint == "" {
871-
endpoint = messagingEndpoint
870+
batchEndpoint := messagingEndpoint
871+
872+
if messagingEndpoint == "" {
873+
messagingEndpoint = defaultMessagingEndpoint
874+
batchEndpoint = defaultBatchEndpoint
872875
}
873876

874877
return &Client{
875-
fcmClient: newFCMClient(hc, c, endpoint),
878+
fcmClient: newFCMClient(hc, c, messagingEndpoint, batchEndpoint),
876879
iidClient: newIIDClient(hc),
877880
}, nil
878881
}
@@ -885,7 +888,7 @@ type fcmClient struct {
885888
httpClient *internal.HTTPClient
886889
}
887890

888-
func newFCMClient(hc *http.Client, conf *internal.MessagingConfig, endpoint string) *fcmClient {
891+
func newFCMClient(hc *http.Client, conf *internal.MessagingConfig, messagingEndpoint string, batchEndpoint string) *fcmClient {
889892
client := internal.WithDefaultRetryConfig(hc)
890893
client.CreateErrFn = handleFCMError
891894

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

898901
return &fcmClient{
899-
fcmEndpoint: endpoint,
902+
fcmEndpoint: messagingEndpoint,
900903
batchEndpoint: batchEndpoint,
901904
project: conf.ProjectID,
902905
version: version,

messaging/messaging_batch_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ import (
2727
"net/http/httptest"
2828
"net/textproto"
2929
"testing"
30+
31+
"google.golang.org/api/option"
3032
)
3133

3234
var testMessages = []*Message{
@@ -520,6 +522,46 @@ func TestSendMulticast(t *testing.T) {
520522
}
521523
}
522524

525+
func TestSendMulticastWithCustomEndpoint(t *testing.T) {
526+
resp, err := createMultipartResponse(testSuccessResponse, nil)
527+
if err != nil {
528+
t.Fatal(err)
529+
}
530+
531+
var req []byte
532+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
533+
req, _ = ioutil.ReadAll(r.Body)
534+
w.Header().Set("Content-Type", wantMime)
535+
w.Write(resp)
536+
}))
537+
defer ts.Close()
538+
539+
ctx := context.Background()
540+
541+
conf := *testMessagingConfig
542+
customBatchEndpoint := fmt.Sprintf("%s/v1", ts.URL)
543+
optEndpoint := option.WithEndpoint(customBatchEndpoint)
544+
conf.Opts = append(conf.Opts, optEndpoint)
545+
546+
client, err := NewClient(ctx, &conf)
547+
if err != nil {
548+
t.Fatal(err)
549+
}
550+
551+
if customBatchEndpoint != client.batchEndpoint {
552+
t.Errorf("client.batchEndpoint = %q; want = %q", client.batchEndpoint, customBatchEndpoint)
553+
}
554+
555+
br, err := client.SendMulticast(ctx, testMulticastMessage)
556+
if err != nil {
557+
t.Fatal(err)
558+
}
559+
560+
if err := checkSuccessfulBatchResponse(br, req, false); err != nil {
561+
t.Errorf("SendMulticast() = %v", err)
562+
}
563+
}
564+
523565
func TestSendMulticastDryRun(t *testing.T) {
524566
resp, err := createMultipartResponse(testSuccessResponse, nil)
525567
if err != nil {

0 commit comments

Comments
 (0)