-
Notifications
You must be signed in to change notification settings - Fork 192
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
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7633c46
wip
jeremytchang 295df8c
wip
jeremytchang c538d38
wip
jeremytchang 36e8a02
wip
jeremytchang 0f60c79
Merge branch 'main' into customheadergo
jeremytchang fd2887f
fix readme typo
jeremytchang 513d940
fixed readme spaces
jeremytchang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -125,70 +125,6 @@ func TestAuthSession_Do_Authorization(t *testing.T) { | |
}) | ||
} | ||
|
||
func TestAuthSession_Do_UserAgent(t *testing.T) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"` | ||
|
@@ -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) | ||
|
@@ -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) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 theUser-Agent
header. Currently this is set in the existingauth.go
and the intent is that AgentTag overwrites that default header value.There was a problem hiding this comment.
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.