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 3f031273 authored by Zeger-Jan van de Weg's avatar Zeger-Jan van de Weg
Browse files

client: Allow User-Agent header to be overridden

The user agent for requests to the internal API endpoints used the
default Go provided user agent. This change updates that to always set
something else, by default `GitLab-Shell`.

Than for others importing the package, there's a new API to set it to
something else. This has been done with new method, a setter, to
maintain backwards compatibility in the API.
parent d5fdca30
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