As we reevaluate how to best support and maintain Staging Ref in the future, we encourage development teams using this environment to highlight their use cases in the following issue: https://gitlab.com/gitlab-com/gl-infra/software-delivery/framework/software-delivery-framework-issue-tracker/-/issues/36.

Skip to content
Snippets Groups Projects
Commit d44dd84c authored by Nick Thomas's avatar Nick Thomas
Browse files

Merge branch 'zj-override-user-agent' into 'master'

client: Allow User-Agent header to be overridden

See merge request gitlab-org/gitlab-shell!418
parents d5fdca30 3f031273
No related branches found
No related tags found
No related merge requests found
Loading
Loading
@@ -19,6 +19,7 @@ import (
const (
internalApiPath = "/api/v4/internal"
secretHeaderName = "Gitlab-Shared-Secret"
defaultUserAgent = "GitLab-Shell"
)
type ErrorResponse struct {
Loading
Loading
@@ -26,8 +27,11 @@ type ErrorResponse struct {
}
type GitlabNetClient struct {
httpClient *HttpClient
user, password, secret string
httpClient *HttpClient
user string
password string
secret string
userAgent string
}
func NewGitlabNetClient(
Loading
Loading
@@ -46,9 +50,16 @@ func NewGitlabNetClient(
user: user,
password: password,
secret: secret,
userAgent: defaultUserAgent,
}, nil
}
// SetUserAgent overrides the default user agent for the User-Agent header field
// for subsequent requests for the GitlabNetClient
func (c *GitlabNetClient) SetUserAgent(ua string) {
c.userAgent = ua
}
func normalizePath(path string) string {
if !strings.HasPrefix(path, "/") {
path = "/" + path
Loading
Loading
@@ -119,6 +130,7 @@ func (c *GitlabNetClient) DoRequest(ctx context.Context, method, path string, da
request.Header.Set(secretHeaderName, encodedSecret)
request.Header.Add("Content-Type", "application/json")
request.Header.Add("User-Agent", c.userAgent)
request.Close = true
start := time.Now()
Loading
Loading
Loading
Loading
@@ -94,6 +94,35 @@ func TestEmptyBasicAuthSettings(t *testing.T) {
require.NoError(t, err)
}
func TestRequestWithUserAgent(t *testing.T) {
const gitalyUserAgent = "gitaly/13.5.0"
requests := []testserver.TestRequestHandler{
{
Path: "/api/v4/internal/default_user_agent",
Handler: func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, defaultUserAgent, r.UserAgent())
},
},
{
Path: "/api/v4/internal/override_user_agent",
Handler: func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, gitalyUserAgent, r.UserAgent())
},
},
}
client, cleanup := setup(t, "", "", requests)
defer cleanup()
_, err := client.Get(context.Background(), "/default_user_agent")
require.NoError(t, err)
client.SetUserAgent(gitalyUserAgent)
_, err = client.Get(context.Background(), "/override_user_agent")
require.NoError(t, err)
}
func setup(t *testing.T, username, password string, requests []testserver.TestRequestHandler) (*GitlabNetClient, func()) {
url, cleanup := testserver.StartHttpServer(t, requests)
Loading
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment