Skip to content

Commit 04a9ea6

Browse files
chore: Add X-Goog-Api-Client header to Auth and FCM requests (#648)
* chore: Add X-Goog-Api-Client header to Auth and FCM requests * fix lint errors * remove the prefix from version tag
1 parent 4093df9 commit 04a9ea6

File tree

5 files changed

+28
-0
lines changed

5 files changed

+28
-0
lines changed

auth/auth.go

Lines changed: 3 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

@@ -133,10 +134,12 @@ func NewClient(ctx context.Context, conf *internal.AuthConfig) (*Client, error)
133134
return nil, err
134135
}
135136

137+
goVersion := strings.TrimPrefix(runtime.Version(), "go")
136138
hc := internal.WithDefaultRetryConfig(transport)
137139
hc.CreateErrFn = handleHTTPError
138140
hc.Opts = []internal.HTTPOption{
139141
internal.WithHeader("X-Client-Version", fmt.Sprintf("Go/Admin/%s", conf.Version)),
142+
internal.WithHeader("x-goog-api-client", fmt.Sprintf("gl-go/%s fire-admin/%s", goVersion, conf.Version)),
140143
}
141144

142145
baseURL := defaultAuthURL

auth/auth_test.go

Lines changed: 8 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,13 @@ func checkBaseClient(client *Client, wantProjectID string) error {
14511452
return fmt.Errorf("version = %q; want = %q", version, wantVersion)
14521453
}
14531454

1455+
goVersion := strings.TrimPrefix(runtime.Version(), "go")
1456+
xGoogAPIClientHeader := req.Header.Get("x-goog-api-client")
1457+
wantXGoogAPIClientHeader := fmt.Sprintf("gl-go/%s fire-admin/%s", goVersion, testVersion)
1458+
if xGoogAPIClientHeader != wantXGoogAPIClientHeader {
1459+
return fmt.Errorf("x-goog-api-client header = %q; want = %q", xGoogAPIClientHeader, wantXGoogAPIClientHeader)
1460+
}
1461+
14541462
return nil
14551463
}
14561464

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", strings.TrimPrefix(runtime.Version(), "go"), 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: 3 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"
@@ -893,10 +894,12 @@ func newFCMClient(hc *http.Client, conf *internal.MessagingConfig, messagingEndp
893894
client := internal.WithDefaultRetryConfig(hc)
894895
client.CreateErrFn = handleFCMError
895896

897+
goVersion := strings.TrimPrefix(runtime.Version(), "go")
896898
version := fmt.Sprintf("fire-admin-go/%s", conf.Version)
897899
client.Opts = []internal.HTTPOption{
898900
internal.WithHeader(apiFormatVersionHeader, apiFormatVersion),
899901
internal.WithHeader(firebaseClientHeader, version),
902+
internal.WithHeader("x-goog-api-client", fmt.Sprintf("gl-go/%s fire-admin/%s", goVersion, conf.Version)),
900903
}
901904

902905
return &fcmClient{

messaging/messaging_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import (
2121
"net/http"
2222
"net/http/httptest"
2323
"reflect"
24+
"runtime"
25+
"strings"
2426
"testing"
2527
"time"
2628

@@ -1394,6 +1396,11 @@ func checkFCMRequest(t *testing.T, b []byte, tr *http.Request, want map[string]i
13941396
if h := tr.Header.Get("X-FIREBASE-CLIENT"); h != clientVersion {
13951397
t.Errorf("X-FIREBASE-CLIENT = %q; want = %q", h, clientVersion)
13961398
}
1399+
goVersion := strings.TrimPrefix(runtime.Version(), "go")
1400+
xGoogAPIClientHeader := "gl-go/" + goVersion + " fire-admin/" + testMessagingConfig.Version
1401+
if h := tr.Header.Get("x-goog-api-client"); h != xGoogAPIClientHeader {
1402+
t.Errorf("x-goog-api-client header = %q; want = %q", h, xGoogAPIClientHeader)
1403+
}
13971404
}
13981405

13991406
var httpErrors = []struct {

0 commit comments

Comments
 (0)