Skip to content

Commit 4489f91

Browse files
committed
chore: Add X-Goog-Api-Client header to Auth and FCM requests
1 parent 4093df9 commit 4489f91

File tree

5 files changed

+23
-0
lines changed

5 files changed

+23
-0
lines changed

auth/auth.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"errors"
2222
"fmt"
2323
"os"
24+
"runtime"
2425
"strings"
2526
"time"
2627

@@ -137,6 +138,7 @@ func NewClient(ctx context.Context, conf *internal.AuthConfig) (*Client, error)
137138
hc.CreateErrFn = handleHTTPError
138139
hc.Opts = []internal.HTTPOption{
139140
internal.WithHeader("X-Client-Version", fmt.Sprintf("Go/Admin/%s", conf.Version)),
141+
internal.WithHeader("x-goog-api-client", fmt.Sprintf("gl-go/%s fire-admin/%s", runtime.Version(), conf.Version)),
140142
}
141143

142144
baseURL := defaultAuthURL

auth/auth_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"log"
2424
"net/http"
2525
"os"
26+
"runtime"
2627
"strings"
2728
"syscall"
2829
"testing"
@@ -1451,6 +1452,12 @@ func checkBaseClient(client *Client, wantProjectID string) error {
14511452
return fmt.Errorf("version = %q; want = %q", version, wantVersion)
14521453
}
14531454

1455+
xGoogApiClientHeader := req.Header.Get("x-goog-api-client")
1456+
wantXGoogApiClientHeader := fmt.Sprintf("gl-go/%s fire-admin/%s", runtime.Version(), testVersion)
1457+
if xGoogApiClientHeader != wantXGoogApiClientHeader {
1458+
return fmt.Errorf("x-goog-api-client header = %q; want = %q", xGoogApiClientHeader, wantXGoogApiClientHeader)
1459+
}
1460+
14541461
return nil
14551462
}
14561463

auth/user_mgt_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"net/http"
2525
"net/http/httptest"
2626
"reflect"
27+
"runtime"
2728
"sort"
2829
"strconv"
2930
"strings"
@@ -2315,6 +2316,12 @@ func echoServer(resp interface{}, t *testing.T) *mockAuthServer {
23152316
t.Errorf("X-Client-Version header = %q; want: %q", gh, wh)
23162317
}
23172318

2319+
gh = r.Header.Get("x-goog-api-client")
2320+
wh = fmt.Sprintf("gl-go/%s fire-admin/%s", runtime.Version(), testVersion)
2321+
if gh != wh {
2322+
t.Errorf("x-goog-api-client header = %q; want: %q", gh, wh)
2323+
}
2324+
23182325
for k, v := range s.Header {
23192326
w.Header().Set(k, v)
23202327
}

messaging/messaging.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"fmt"
2424
"net/http"
2525
"regexp"
26+
"runtime"
2627
"strconv"
2728
"strings"
2829
"time"
@@ -897,6 +898,7 @@ func newFCMClient(hc *http.Client, conf *internal.MessagingConfig, messagingEndp
897898
client.Opts = []internal.HTTPOption{
898899
internal.WithHeader(apiFormatVersionHeader, apiFormatVersion),
899900
internal.WithHeader(firebaseClientHeader, version),
901+
internal.WithHeader("x-goog-api-client", fmt.Sprintf("gl-go/%s fire-admin/%s", runtime.Version(), conf.Version)),
900902
}
901903

902904
return &fcmClient{

messaging/messaging_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"net/http"
2222
"net/http/httptest"
2323
"reflect"
24+
"runtime"
2425
"testing"
2526
"time"
2627

@@ -1394,6 +1395,10 @@ func checkFCMRequest(t *testing.T, b []byte, tr *http.Request, want map[string]i
13941395
if h := tr.Header.Get("X-FIREBASE-CLIENT"); h != clientVersion {
13951396
t.Errorf("X-FIREBASE-CLIENT = %q; want = %q", h, clientVersion)
13961397
}
1398+
xGoogApiClientHeader := "gl-go/" + runtime.Version() + " fire-admin/" + testMessagingConfig.Version
1399+
if h := tr.Header.Get("x-goog-api-client"); h != xGoogApiClientHeader {
1400+
t.Errorf("x-goog-api-client header = %q; want = %q", h, xGoogApiClientHeader)
1401+
}
13971402
}
13981403

13991404
var httpErrors = []struct {

0 commit comments

Comments
 (0)