Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add Go SDK custom header support and README #1288

Merged
merged 7 commits into from
Apr 5, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion go/rtl/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func (s *AuthSession) Do(result interface{}, method, ver, path string, reqPars m
}

// set headers
req.Header.Add("Content-Type", contentTypeHeader)
req.Header.Set("Content-Type", contentTypeHeader)

if s.Config.AgentTag != "" {
req.Header.Set("User-Agent", s.Config.AgentTag)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you should also set x-looker-appid like the other SDKs do. There are some request routers that will modify the User-Agent header. Currently this is set in the existing auth.go and the intent is that AgentTag overwrites that default header value.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good callout, was unaware of the behavior of AgentTag in relation to x-looker-appid. Will have a separate ticket to tackle that.

Expand All @@ -151,6 +151,17 @@ func (s *AuthSession) Do(result interface{}, method, ver, path string, reqPars m
req.Header.Set("User-Agent", options.AgentTag)
}

if s.Config.Headers != nil {
for key, value := range s.Config.Headers {
req.Header.Set(key, value)
}
}
if options != nil && options.Headers != nil {
for key, value := range options.Headers {
req.Header.Set(key, value)
}
}

// set query params
setQuery(req.URL, reqPars)

Expand Down
190 changes: 125 additions & 65 deletions go/rtl/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,70 +125,6 @@ func TestAuthSession_Do_Authorization(t *testing.T) {
})
}

func TestAuthSession_Do_UserAgent(t *testing.T) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved these tests to header section.

const path = "/someMethod"
const apiVersion = "/4.0"

t.Run("Do() sets User-Agent header with AuthSession config's AgentTag", func(t *testing.T) {
mux := http.NewServeMux()
setupApi40Login(mux, foreverValidTestToken, http.StatusOK)
server := httptest.NewServer(mux)
defer server.Close()

mux.HandleFunc("/api"+apiVersion+path, func(w http.ResponseWriter, r *http.Request) {
userAgentHeader := r.Header.Get("User-Agent")
expectedHeader := "some-agent-tag"
if userAgentHeader != expectedHeader {
t.Errorf("User-Agent header not correct. got=%v want=%v", userAgentHeader, expectedHeader)
}
})

session := NewAuthSession(ApiSettings{
BaseUrl: server.URL,
ApiVersion: apiVersion,
AgentTag: "some-agent-tag",
})

var r string
err := session.Do(&r, "GET", apiVersion, path, nil, nil, nil)

if err != nil {
t.Errorf("Do() call failed: %v", err)
}
})

t.Run("Do() sets User-Agent header with Do's option's AgentTag, which will overwrite AuthSession config", func(t *testing.T) {
mux := http.NewServeMux()
setupApi40Login(mux, foreverValidTestToken, http.StatusOK)
server := httptest.NewServer(mux)
defer server.Close()

mux.HandleFunc("/api"+apiVersion+path, func(w http.ResponseWriter, r *http.Request) {
userAgentHeader := r.Header.Get("User-Agent")
expectedHeader := "new-agent-tag"
if userAgentHeader != expectedHeader {
t.Errorf("User-Agent header not correct. got=%v want=%v", userAgentHeader, expectedHeader)
}
})

session := NewAuthSession(ApiSettings{
BaseUrl: server.URL,
ApiVersion: apiVersion,
AgentTag: "some-agent-tag",
})

var r string
options := ApiSettings{
AgentTag: "new-agent-tag",
}
err := session.Do(&r, "GET", apiVersion, path, nil, nil, &options)

if err != nil {
t.Errorf("Do() call failed: %v", err)
}
})
}

func TestAuthSession_Do_Parse(t *testing.T) {
type stringStruct struct {
Field *string `json:"field"`
Expand Down Expand Up @@ -454,10 +390,75 @@ func TestAuthSession_Do_Parse(t *testing.T) {
})
}

func TestAuthSession_Do_Content_Type(t *testing.T) {
func TestAuthSession_Do_Headers(t *testing.T) {
const path = "/someMethod"
const apiVersion = "/4.0"

t.Run("Do() sets custom headers if Headers is set in the AuthSession's api settings", func(t *testing.T) {
mux := http.NewServeMux()
setupApi40Login(mux, foreverValidTestToken, http.StatusOK)
server := httptest.NewServer(mux)
defer server.Close()

mux.HandleFunc("/api"+apiVersion+path, func(w http.ResponseWriter, r *http.Request) {
headerValue1 := r.Header.Get("Key1")
headerValue2 := r.Header.Get("Key2")

expectedHeaderValue1 := "Value1"
expectedHeaderValue2 := "Value2"
if headerValue1 != expectedHeaderValue1 || headerValue2 != expectedHeaderValue2 {
t.Errorf("Custom headers not set correctly. got=%v and %v want=%v and %v", headerValue1, headerValue2, expectedHeaderValue1, expectedHeaderValue2)
}
})

s := NewAuthSession(ApiSettings{
BaseUrl: server.URL,
ApiVersion: apiVersion,
Headers: map[string]string{"Key1":"Value1","Key2":"Value2"},
})

var r string
err := s.Do(&r, "GET", apiVersion, path, nil, nil, nil)

if err != nil {
t.Errorf("Do() call failed: %v", err)
}
})

t.Run("Do()'s options.Headers will overwrite the Headers in the AuthSession's api settings", func(t *testing.T) {
mux := http.NewServeMux()
setupApi40Login(mux, foreverValidTestToken, http.StatusOK)
server := httptest.NewServer(mux)
defer server.Close()

mux.HandleFunc("/api"+apiVersion+path, func(w http.ResponseWriter, r *http.Request) {
headerValue1 := r.Header.Get("Key1")
headerValue2 := r.Header.Get("Key2")

expectedHeaderValue1 := "Value1"
expectedHeaderValue2 := "OverwriteValue2"
if headerValue1 != expectedHeaderValue1 || headerValue2 != expectedHeaderValue2 {
t.Errorf("Custom headers not set correctly. got=%v and %v want=%v and %v", headerValue1, headerValue2, expectedHeaderValue1, expectedHeaderValue2)
}
})

s := NewAuthSession(ApiSettings{
BaseUrl: server.URL,
ApiVersion: apiVersion,
Headers: map[string]string{"Key1":"Value1","Key2":"Value2"},
})

options := ApiSettings{
Headers: map[string]string{"Key1":"Value1","Key2":"OverwriteValue2"},
}
var r string
err := s.Do(&r, "GET", apiVersion, path, nil, nil, &options)

if err != nil {
t.Errorf("Do() call failed: %v", err)
}
})

t.Run("Do() sets Content-Type header to 'application/json' if body is json", func(t *testing.T) {
mux := http.NewServeMux()
setupApi40Login(mux, foreverValidTestToken, http.StatusOK)
Expand Down Expand Up @@ -517,6 +518,65 @@ func TestAuthSession_Do_Content_Type(t *testing.T) {
t.Errorf("Do() call failed: %v", err)
}
})

t.Run("Do() sets User-Agent header with AuthSession config's AgentTag", func(t *testing.T) {
mux := http.NewServeMux()
setupApi40Login(mux, foreverValidTestToken, http.StatusOK)
server := httptest.NewServer(mux)
defer server.Close()

mux.HandleFunc("/api"+apiVersion+path, func(w http.ResponseWriter, r *http.Request) {
userAgentHeader := r.Header.Get("User-Agent")
expectedHeader := "some-agent-tag"
if userAgentHeader != expectedHeader {
t.Errorf("User-Agent header not correct. got=%v want=%v", userAgentHeader, expectedHeader)
}
})

session := NewAuthSession(ApiSettings{
BaseUrl: server.URL,
ApiVersion: apiVersion,
AgentTag: "some-agent-tag",
})

var r string
err := session.Do(&r, "GET", apiVersion, path, nil, nil, nil)

if err != nil {
t.Errorf("Do() call failed: %v", err)
}
})

t.Run("Do() sets User-Agent header with Do's option's AgentTag, which will overwrite AuthSession config", func(t *testing.T) {
mux := http.NewServeMux()
setupApi40Login(mux, foreverValidTestToken, http.StatusOK)
server := httptest.NewServer(mux)
defer server.Close()

mux.HandleFunc("/api"+apiVersion+path, func(w http.ResponseWriter, r *http.Request) {
userAgentHeader := r.Header.Get("User-Agent")
expectedHeader := "new-agent-tag"
if userAgentHeader != expectedHeader {
t.Errorf("User-Agent header not correct. got=%v want=%v", userAgentHeader, expectedHeader)
}
})

session := NewAuthSession(ApiSettings{
BaseUrl: server.URL,
ApiVersion: apiVersion,
AgentTag: "some-agent-tag",
})

var r string
options := ApiSettings{
AgentTag: "new-agent-tag",
}
err := session.Do(&r, "GET", apiVersion, path, nil, nil, &options)

if err != nil {
t.Errorf("Do() call failed: %v", err)
}
})
}

func TestAuthSession_Do_Timeout(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions go/rtl/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type ApiSettings struct {
ClientId string `ini:"client_id"`
ClientSecret string `ini:"client_secret"`
ApiVersion string `ini:"api_version"`
Headers map[string]string
}

var defaultSettings ApiSettings = ApiSettings{
Expand Down