github.com/x-helm/helm@v3.0.0-beta.3+incompatible/pkg/getter/httpgetter_test.go (about)

     1  /*
     2  Copyright The Helm Authors.
     3  Licensed under the Apache License, Version 2.0 (the "License");
     4  you may not use this file except in compliance with the License.
     5  You may obtain a copy of the License at
     6  
     7  http://www.apache.org/licenses/LICENSE-2.0
     8  
     9  Unless required by applicable law or agreed to in writing, software
    10  distributed under the License is distributed on an "AS IS" BASIS,
    11  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  See the License for the specific language governing permissions and
    13  limitations under the License.
    14  */
    15  
    16  package getter
    17  
    18  import (
    19  	"fmt"
    20  	"net/http"
    21  	"net/http/httptest"
    22  	"net/url"
    23  	"path/filepath"
    24  	"strings"
    25  	"testing"
    26  
    27  	"helm.sh/helm/internal/test"
    28  	"helm.sh/helm/internal/version"
    29  	"helm.sh/helm/pkg/cli"
    30  )
    31  
    32  func TestHTTPGetter(t *testing.T) {
    33  	g, err := NewHTTPGetter(WithURL("http://example.com"))
    34  	if err != nil {
    35  		t.Fatal(err)
    36  	}
    37  
    38  	if hg, ok := g.(*HTTPGetter); !ok {
    39  		t.Fatal("Expected NewHTTPGetter to produce an *HTTPGetter")
    40  	} else if hg.client != http.DefaultClient {
    41  		t.Fatal("Expected NewHTTPGetter to return a default HTTP client.")
    42  	}
    43  
    44  	// Test with SSL:
    45  	cd := "../../testdata"
    46  	join := filepath.Join
    47  	ca, pub, priv := join(cd, "ca.pem"), join(cd, "crt.pem"), join(cd, "key.pem")
    48  	g, err = NewHTTPGetter(
    49  		WithURL("http://example.com"),
    50  		WithTLSClientConfig(pub, priv, ca),
    51  	)
    52  	if err != nil {
    53  		t.Fatal(err)
    54  	}
    55  
    56  	hg, ok := g.(*HTTPGetter)
    57  	if !ok {
    58  		t.Fatal("Expected NewHTTPGetter to produce an *HTTPGetter")
    59  	}
    60  
    61  	transport, ok := hg.client.Transport.(*http.Transport)
    62  	if !ok {
    63  		t.Errorf("Expected NewHTTPGetter to set up an HTTP transport")
    64  	}
    65  
    66  	test.AssertGoldenString(t, transport.TLSClientConfig.ServerName, "output/httpgetter-servername.txt")
    67  
    68  	// Test other options
    69  	g, err = NewHTTPGetter(
    70  		WithBasicAuth("I", "Am"),
    71  		WithUserAgent("Groot"),
    72  	)
    73  	if err != nil {
    74  		t.Fatal(err)
    75  	}
    76  
    77  	hg, ok = g.(*HTTPGetter)
    78  	if !ok {
    79  		t.Fatal("expected NewHTTPGetter to produce an *HTTPGetter")
    80  	}
    81  
    82  	if hg.opts.username != "I" {
    83  		t.Errorf("Expected NewHTTPGetter to contain %q as the username, got %q", "I", hg.opts.username)
    84  	}
    85  
    86  	if hg.opts.password != "Am" {
    87  		t.Errorf("Expected NewHTTPGetter to contain %q as the password, got %q", "Am", hg.opts.password)
    88  	}
    89  
    90  	if hg.opts.userAgent != "Groot" {
    91  		t.Errorf("Expected NewHTTPGetter to contain %q as the user agent, got %q", "Groot", hg.opts.userAgent)
    92  	}
    93  }
    94  
    95  func TestDownload(t *testing.T) {
    96  	expect := "Call me Ishmael"
    97  	expectedUserAgent := "I am Groot"
    98  	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    99  		defaultUserAgent := "Helm/" + strings.TrimPrefix(version.GetVersion(), "v")
   100  		if r.UserAgent() != defaultUserAgent {
   101  			t.Errorf("Expected '%s', got '%s'", defaultUserAgent, r.UserAgent())
   102  		}
   103  		fmt.Fprint(w, expect)
   104  	}))
   105  	defer srv.Close()
   106  
   107  	g, err := All(new(cli.EnvSettings)).ByScheme("http")
   108  	if err != nil {
   109  		t.Fatal(err)
   110  	}
   111  	got, err := g.Get(srv.URL, WithURL(srv.URL))
   112  	if err != nil {
   113  		t.Fatal(err)
   114  	}
   115  
   116  	if got.String() != expect {
   117  		t.Errorf("Expected %q, got %q", expect, got.String())
   118  	}
   119  
   120  	// test with http server
   121  	basicAuthSrv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   122  		username, password, ok := r.BasicAuth()
   123  		if !ok || username != "username" || password != "password" {
   124  			t.Errorf("Expected request to use basic auth and for username == 'username' and password == 'password', got '%v', '%s', '%s'", ok, username, password)
   125  		}
   126  		if r.UserAgent() != expectedUserAgent {
   127  			t.Errorf("Expected '%s', got '%s'", expectedUserAgent, r.UserAgent())
   128  		}
   129  		fmt.Fprint(w, expect)
   130  	}))
   131  
   132  	defer basicAuthSrv.Close()
   133  
   134  	u, _ := url.ParseRequestURI(basicAuthSrv.URL)
   135  	httpgetter, err := NewHTTPGetter(
   136  		WithURL(u.String()),
   137  		WithBasicAuth("username", "password"),
   138  		WithUserAgent(expectedUserAgent),
   139  	)
   140  	if err != nil {
   141  		t.Fatal(err)
   142  	}
   143  	got, err = httpgetter.Get(u.String())
   144  	if err != nil {
   145  		t.Fatal(err)
   146  	}
   147  
   148  	if got.String() != expect {
   149  		t.Errorf("Expected %q, got %q", expect, got.String())
   150  	}
   151  }