From 553f1e34f54ce4c75ee10b53c441063d6303db3c Mon Sep 17 00:00:00 2001 From: Jakub Warczarek Date: Sat, 2 Oct 2021 14:48:36 +0200 Subject: [PATCH] Add test for User-Agent header setting and refactor Signed-off-by: Jakub Warczarek --- pkg/cli/environment_test.go | 17 +++++++++++++++++ pkg/getter/httpgetter_test.go | 4 ++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/pkg/cli/environment_test.go b/pkg/cli/environment_test.go index dbf056e3a..3de6fab4c 100644 --- a/pkg/cli/environment_test.go +++ b/pkg/cli/environment_test.go @@ -23,6 +23,8 @@ import ( "testing" "github.com/spf13/pflag" + + "helm.sh/helm/v3/internal/version" ) func TestSetNamespace(t *testing.T) { @@ -231,6 +233,21 @@ func TestEnvOrBool(t *testing.T) { } } +func TestUserAgentHeaderInK8sRESTClientConfig(t *testing.T) { + defer resetEnv()() + + settings := New() + restConfig, err := settings.RESTClientGetter().ToRESTConfig() + if err != nil { + t.Fatal(err) + } + + expectedUserAgent := version.GetUserAgent() + if restConfig.UserAgent != expectedUserAgent { + t.Errorf("expected User-Agent header %q in K8s REST client config, got %q", expectedUserAgent, restConfig.UserAgent) + } +} + func resetEnv() func() { origEnv := os.Environ() diff --git a/pkg/getter/httpgetter_test.go b/pkg/getter/httpgetter_test.go index 5a90d8b99..94baef3b8 100644 --- a/pkg/getter/httpgetter_test.go +++ b/pkg/getter/httpgetter_test.go @@ -155,9 +155,8 @@ func TestHTTPGetter(t *testing.T) { func TestDownload(t *testing.T) { expect := "Call me Ishmael" - expectedUserAgent := "I am Groot" srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - defaultUserAgent := "Helm/" + strings.TrimPrefix(version.GetVersion(), "v") + defaultUserAgent := version.GetUserAgent() if r.UserAgent() != defaultUserAgent { t.Errorf("Expected '%s', got '%s'", defaultUserAgent, r.UserAgent()) } @@ -179,6 +178,7 @@ func TestDownload(t *testing.T) { } // test with http server + const expectedUserAgent = "I am Groot" basicAuthSrv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { username, password, ok := r.BasicAuth() if !ok || username != "username" || password != "password" {