Add test for User-Agent header setting and refactor

Signed-off-by: Jakub Warczarek <jakub.warczarek@gmail.com>
pull/11226/head
Jakub Warczarek 3 years ago
parent 2fa7b3d1b7
commit 553f1e34f5

@ -23,6 +23,8 @@ import (
"testing" "testing"
"github.com/spf13/pflag" "github.com/spf13/pflag"
"helm.sh/helm/v3/internal/version"
) )
func TestSetNamespace(t *testing.T) { 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() { func resetEnv() func() {
origEnv := os.Environ() origEnv := os.Environ()

@ -155,9 +155,8 @@ func TestHTTPGetter(t *testing.T) {
func TestDownload(t *testing.T) { func TestDownload(t *testing.T) {
expect := "Call me Ishmael" expect := "Call me Ishmael"
expectedUserAgent := "I am Groot"
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 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 { if r.UserAgent() != defaultUserAgent {
t.Errorf("Expected '%s', got '%s'", defaultUserAgent, r.UserAgent()) t.Errorf("Expected '%s', got '%s'", defaultUserAgent, r.UserAgent())
} }
@ -179,6 +178,7 @@ func TestDownload(t *testing.T) {
} }
// test with http server // test with http server
const expectedUserAgent = "I am Groot"
basicAuthSrv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { basicAuthSrv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
username, password, ok := r.BasicAuth() username, password, ok := r.BasicAuth()
if !ok || username != "username" || password != "password" { if !ok || username != "username" || password != "password" {

Loading…
Cancel
Save