Skip to content

Commit

Permalink
chore: Add X-Goog-Api-Client header to Auth and FCM requests
Browse files Browse the repository at this point in the history
  • Loading branch information
lahirumaramba committed Oct 23, 2024
1 parent 4093df9 commit 4489f91
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 0 deletions.
2 changes: 2 additions & 0 deletions auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"errors"
"fmt"
"os"
"runtime"
"strings"
"time"

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

baseURL := defaultAuthURL
Expand Down
7 changes: 7 additions & 0 deletions auth/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"log"
"net/http"
"os"
"runtime"
"strings"
"syscall"
"testing"
Expand Down Expand Up @@ -1451,6 +1452,12 @@ func checkBaseClient(client *Client, wantProjectID string) error {
return fmt.Errorf("version = %q; want = %q", version, wantVersion)
}

xGoogApiClientHeader := req.Header.Get("x-goog-api-client")

Check failure on line 1455 in auth/auth_test.go

View workflow job for this annotation

GitHub Actions / stage_release

var xGoogApiClientHeader should be xGoogAPIClientHeader

Check failure on line 1455 in auth/auth_test.go

View workflow job for this annotation

GitHub Actions / Module build (1.21)

var xGoogApiClientHeader should be xGoogAPIClientHeader

Check failure on line 1455 in auth/auth_test.go

View workflow job for this annotation

GitHub Actions / Module build (1.22)

var xGoogApiClientHeader should be xGoogAPIClientHeader

Check failure on line 1455 in auth/auth_test.go

View workflow job for this annotation

GitHub Actions / Module build (1.23)

var xGoogApiClientHeader should be xGoogAPIClientHeader
wantXGoogApiClientHeader := fmt.Sprintf("gl-go/%s fire-admin/%s", runtime.Version(), testVersion)

Check failure on line 1456 in auth/auth_test.go

View workflow job for this annotation

GitHub Actions / stage_release

var wantXGoogApiClientHeader should be wantXGoogAPIClientHeader

Check failure on line 1456 in auth/auth_test.go

View workflow job for this annotation

GitHub Actions / Module build (1.21)

var wantXGoogApiClientHeader should be wantXGoogAPIClientHeader

Check failure on line 1456 in auth/auth_test.go

View workflow job for this annotation

GitHub Actions / Module build (1.22)

var wantXGoogApiClientHeader should be wantXGoogAPIClientHeader

Check failure on line 1456 in auth/auth_test.go

View workflow job for this annotation

GitHub Actions / Module build (1.23)

var wantXGoogApiClientHeader should be wantXGoogAPIClientHeader
if xGoogApiClientHeader != wantXGoogApiClientHeader {
return fmt.Errorf("x-goog-api-client header = %q; want = %q", xGoogApiClientHeader, wantXGoogApiClientHeader)
}

return nil
}

Expand Down
7 changes: 7 additions & 0 deletions auth/user_mgt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"net/http"
"net/http/httptest"
"reflect"
"runtime"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -2315,6 +2316,12 @@ func echoServer(resp interface{}, t *testing.T) *mockAuthServer {
t.Errorf("X-Client-Version header = %q; want: %q", gh, wh)
}

gh = r.Header.Get("x-goog-api-client")
wh = fmt.Sprintf("gl-go/%s fire-admin/%s", runtime.Version(), testVersion)
if gh != wh {
t.Errorf("x-goog-api-client header = %q; want: %q", gh, wh)
}

for k, v := range s.Header {
w.Header().Set(k, v)
}
Expand Down
2 changes: 2 additions & 0 deletions messaging/messaging.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"fmt"
"net/http"
"regexp"
"runtime"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -897,6 +898,7 @@ func newFCMClient(hc *http.Client, conf *internal.MessagingConfig, messagingEndp
client.Opts = []internal.HTTPOption{
internal.WithHeader(apiFormatVersionHeader, apiFormatVersion),
internal.WithHeader(firebaseClientHeader, version),
internal.WithHeader("x-goog-api-client", fmt.Sprintf("gl-go/%s fire-admin/%s", runtime.Version(), conf.Version)),
}

return &fcmClient{
Expand Down
5 changes: 5 additions & 0 deletions messaging/messaging_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"net/http"
"net/http/httptest"
"reflect"
"runtime"
"testing"
"time"

Expand Down Expand Up @@ -1394,6 +1395,10 @@ func checkFCMRequest(t *testing.T, b []byte, tr *http.Request, want map[string]i
if h := tr.Header.Get("X-FIREBASE-CLIENT"); h != clientVersion {
t.Errorf("X-FIREBASE-CLIENT = %q; want = %q", h, clientVersion)
}
xGoogApiClientHeader := "gl-go/" + runtime.Version() + " fire-admin/" + testMessagingConfig.Version

Check failure on line 1398 in messaging/messaging_test.go

View workflow job for this annotation

GitHub Actions / stage_release

var xGoogApiClientHeader should be xGoogAPIClientHeader

Check failure on line 1398 in messaging/messaging_test.go

View workflow job for this annotation

GitHub Actions / Module build (1.21)

var xGoogApiClientHeader should be xGoogAPIClientHeader

Check failure on line 1398 in messaging/messaging_test.go

View workflow job for this annotation

GitHub Actions / Module build (1.22)

var xGoogApiClientHeader should be xGoogAPIClientHeader

Check failure on line 1398 in messaging/messaging_test.go

View workflow job for this annotation

GitHub Actions / Module build (1.23)

var xGoogApiClientHeader should be xGoogAPIClientHeader
if h := tr.Header.Get("x-goog-api-client"); h != xGoogApiClientHeader {
t.Errorf("x-goog-api-client header = %q; want = %q", h, xGoogApiClientHeader)
}
}

var httpErrors = []struct {
Expand Down

0 comments on commit 4489f91

Please sign in to comment.