Skip to content

Commit

Permalink
Adding more info to user-agent
Browse files Browse the repository at this point in the history
  • Loading branch information
soareswallace committed Jan 23, 2025
1 parent aef00a6 commit 81bcccb
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
20 changes: 20 additions & 0 deletions incognia.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"fmt"
"io/ioutil"
"net/http"
"runtime"
"runtime/debug"
"time"
)

Expand Down Expand Up @@ -384,7 +386,25 @@ func (c *Client) registerLogin(login *Login) (*TransactionAssessment, error) {
}

func (c *Client) doRequest(request *http.Request, response interface{}) error {
libVersion := "unknown"
if buildInfo, ok := debug.ReadBuildInfo(); ok {
for _, dep := range buildInfo.Deps {
if dep.Path == "repo.incognia.com/go/incognia" {
libVersion = dep.Version
}
}
}

userAgent := fmt.Sprintf(
"incognia-api-go/%s (%s %s) Go/%s",
libVersion,
runtime.GOOS,
runtime.GOARCH,
runtime.Version(),
)

request.Header.Add("Content-Type", "application/json")
request.Header.Add("user-agent", userAgent)

err := c.authorizeRequest(request)
if err != nil {
Expand Down
14 changes: 14 additions & 0 deletions incognia_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net/http"
"net/http/httptest"
"reflect"
"regexp"
"strconv"
"strings"
"testing"
Expand All @@ -21,6 +22,7 @@ const (
)

var (
userAgentRegex = regexp.MustCompile(`^incognia-api-go(/(v[0-9]+\.[0-9]+\.[0-9]+|unknown))? \([a-z]+ [a-z0-9]+\) Go/go[0-9]+\.[0-9]+\.[0-9]+$`)
now = time.Now()
nowMinusSeconds = now.Add(-1 * time.Second)
installationId = "installation-id"
Expand Down Expand Up @@ -957,6 +959,9 @@ func (suite *IncogniaTestSuite) mockFeedbackEndpoint(expectedToken string, expec
feedbackServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("content-type", "application/json")

userAgent := r.Header.Get("User-Agent")
suite.True(userAgentRegex.MatchString(userAgent), "User-Agent header does not match the expected format")

if !isRequestAuthorized(r, expectedToken) {
w.WriteHeader(http.StatusForbidden)
return
Expand Down Expand Up @@ -991,6 +996,9 @@ func (suite *IncogniaTestSuite) mockTokenEndpointUnauthorized() *httptest.Server
tokenServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("content-type", "application/json")
w.WriteHeader(http.StatusUnauthorized)

userAgent := r.Header.Get("User-Agent")
suite.True(userAgentRegex.MatchString(userAgent), "User-Agent header does not match the expected format")
}))

return tokenServer
Expand All @@ -1000,6 +1008,9 @@ func (suite *IncogniaTestSuite) mockPostTransactionsEndpoint(expectedToken strin
transactionsServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("content-type", "application/json")

userAgent := r.Header.Get("User-Agent")
suite.True(userAgentRegex.MatchString(userAgent), "User-Agent header does not match the expected format")

if !isRequestAuthorized(r, expectedToken) {
w.WriteHeader(http.StatusForbidden)
return
Expand Down Expand Up @@ -1038,6 +1049,9 @@ func (suite *IncogniaTestSuite) mockPostSignupsEndpoint(expectedToken string, ex
signupsServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("content-type", "application/json")

userAgent := r.Header.Get("User-Agent")
suite.True(userAgentRegex.MatchString(userAgent), "User-Agent header does not match the expected format")

if !isRequestAuthorized(r, expectedToken) {
w.WriteHeader(http.StatusForbidden)
return
Expand Down
21 changes: 21 additions & 0 deletions token_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ package incognia
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"runtime"
"runtime/debug"
"strconv"
"time"
)
Expand Down Expand Up @@ -51,8 +54,26 @@ func (tm TokenClient) requestToken() (Token, error) {
return nil, err
}

libVersion := "unknown"
if buildInfo, ok := debug.ReadBuildInfo(); ok {
for _, dep := range buildInfo.Deps {
if dep.Path == "repo.incognia.com/go/incognia" {
libVersion = dep.Version
}
}
}

userAgent := fmt.Sprintf(
"incognia-api-go/%s (%s %s) Go/%s",
libVersion,
runtime.GOOS,
runtime.GOARCH,
runtime.Version(),
)

req.SetBasicAuth(tm.ClientID, tm.ClientSecret)
req.Header.Add("content-type", "application/x-www-form-urlencoded")
req.Header.Add("user-agent", userAgent)

res, err := tm.netClient.Do(req)
if err != nil {
Expand Down

0 comments on commit 81bcccb

Please sign in to comment.