Skip to content

Commit 0e4edef

Browse files
slirxhiranya911
andauthored
fix(fcm): Add ability to override default FCM endpoint via ClientOptions (#373)
* Add FIREBASE_MESSAGING_ENDPOINT environment variable to override default FCM endpoint * Use endpoint while creating new messaging client * Add tests for custom endpoint * Simplify setting default endpoint * Remove redundant code * Fix formatting * Fix imports order Co-authored-by: Hiranya Jayathilaka <hiranya911@gmail.com>
1 parent 0d63b01 commit 0e4edef

File tree

3 files changed

+83
-4
lines changed

3 files changed

+83
-4
lines changed

firebase_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"testing"
3030
"time"
3131

32+
"firebase.google.com/go/messaging"
3233
"golang.org/x/oauth2"
3334
"golang.org/x/oauth2/google"
3435
"google.golang.org/api/option"
@@ -361,6 +362,43 @@ func TestMessaging(t *testing.T) {
361362
}
362363
}
363364

365+
func TestMessagingSendWithCustomEndpoint(t *testing.T) {
366+
name := "custom-endpoint-ok"
367+
368+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
369+
w.Header().Set("Content-Type", "application/json")
370+
w.Write([]byte("{ \"name\":\"" + name + "\" }"))
371+
}))
372+
defer ts.Close()
373+
374+
ctx := context.Background()
375+
376+
tokenSource := &testTokenSource{AccessToken: "mock-token-from-custom"}
377+
app, err := NewApp(
378+
ctx,
379+
nil,
380+
option.WithCredentialsFile("testdata/service_account.json"),
381+
option.WithTokenSource(tokenSource),
382+
option.WithEndpoint(ts.URL),
383+
)
384+
if err != nil {
385+
t.Fatal(err)
386+
}
387+
388+
c, err := app.Messaging(ctx)
389+
if c == nil || err != nil {
390+
t.Fatalf("Messaging() = (%v, %v); want (iid, nil)", c, err)
391+
}
392+
393+
msg := &messaging.Message{
394+
Token: "...",
395+
}
396+
n, err := c.Send(ctx, msg)
397+
if n != name || err != nil {
398+
t.Errorf("Send() = (%q, %v); want (%q, nil)", n, err, name)
399+
}
400+
}
401+
364402
func TestCustomTokenSource(t *testing.T) {
365403
ctx := context.Background()
366404
ts := &testTokenSource{AccessToken: "mock-token-from-custom"}

messaging/messaging.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -913,13 +913,17 @@ func NewClient(ctx context.Context, c *internal.MessagingConfig) (*Client, error
913913
return nil, errors.New("project ID is required to access Firebase Cloud Messaging client")
914914
}
915915

916-
hc, _, err := transport.NewHTTPClient(ctx, c.Opts...)
916+
hc, endpoint, err := transport.NewHTTPClient(ctx, c.Opts...)
917917
if err != nil {
918918
return nil, err
919919
}
920920

921+
if endpoint == "" {
922+
endpoint = messagingEndpoint
923+
}
924+
921925
return &Client{
922-
fcmClient: newFCMClient(hc, c),
926+
fcmClient: newFCMClient(hc, c, endpoint),
923927
iidClient: newIIDClient(hc),
924928
}, nil
925929
}
@@ -932,7 +936,7 @@ type fcmClient struct {
932936
httpClient *internal.HTTPClient
933937
}
934938

935-
func newFCMClient(hc *http.Client, conf *internal.MessagingConfig) *fcmClient {
939+
func newFCMClient(hc *http.Client, conf *internal.MessagingConfig, endpoint string) *fcmClient {
936940
client := internal.WithDefaultRetryConfig(hc)
937941
client.CreateErrFn = handleFCMError
938942
client.SuccessFn = internal.HasSuccessStatus
@@ -944,7 +948,7 @@ func newFCMClient(hc *http.Client, conf *internal.MessagingConfig) *fcmClient {
944948
}
945949

946950
return &fcmClient{
947-
fcmEndpoint: messagingEndpoint,
951+
fcmEndpoint: endpoint,
948952
batchEndpoint: batchEndpoint,
949953
project: conf.ProjectID,
950954
version: version,

messaging/messaging_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1126,6 +1126,43 @@ func TestSend(t *testing.T) {
11261126
}
11271127
}
11281128

1129+
func TestSendWithCustomEndpoint(t *testing.T) {
1130+
var tr *http.Request
1131+
var b []byte
1132+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
1133+
tr = r
1134+
b, _ = ioutil.ReadAll(r.Body)
1135+
w.Header().Set("Content-Type", "application/json")
1136+
w.Write([]byte("{ \"name\":\"" + testMessageID + "\" }"))
1137+
}))
1138+
defer ts.Close()
1139+
1140+
ctx := context.Background()
1141+
1142+
conf := *testMessagingConfig
1143+
optEndpoint := option.WithEndpoint(ts.URL)
1144+
conf.Opts = append(conf.Opts, optEndpoint)
1145+
1146+
client, err := NewClient(ctx, &conf)
1147+
if err != nil {
1148+
t.Fatal(err)
1149+
}
1150+
1151+
if ts.URL != client.fcmEndpoint {
1152+
t.Errorf("client.fcmEndpoint = %q; want = %q", client.fcmEndpoint, ts.URL)
1153+
}
1154+
1155+
for _, tc := range validMessages {
1156+
t.Run(tc.name, func(t *testing.T) {
1157+
name, err := client.Send(ctx, tc.req)
1158+
if name != testMessageID || err != nil {
1159+
t.Errorf("Send(%s) = (%q, %v); want = (%q, nil)", tc.name, name, err, testMessageID)
1160+
}
1161+
checkFCMRequest(t, b, tr, tc.want, false)
1162+
})
1163+
}
1164+
}
1165+
11291166
func TestSendDryRun(t *testing.T) {
11301167
var tr *http.Request
11311168
var b []byte

0 commit comments

Comments
 (0)