github.com/pensu/helm@v2.6.1+incompatible/pkg/repo/local_test.go (about)

     1  /*
     2  Copyright 2016 The Kubernetes Authors All rights reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package repo
    18  
    19  import (
    20  	"io/ioutil"
    21  	"net/http"
    22  	"strings"
    23  	"testing"
    24  )
    25  
    26  func TestRepositoryServer(t *testing.T) {
    27  	expectedIndexYAML, err := ioutil.ReadFile("testdata/server/index.yaml")
    28  	if err != nil {
    29  		t.Fatal(err)
    30  	}
    31  
    32  	tests := []struct {
    33  		name   string
    34  		path   string
    35  		expect string
    36  	}{
    37  		{"index YAML", "/charts/index.yaml", string(expectedIndexYAML)},
    38  		{"index HTML", "/charts/index.html", "<html>"},
    39  		{"charts root", "/charts/", "<html>"},
    40  		{"root", "/", "<html>"},
    41  		{"file", "/test.txt", "Hello World"},
    42  	}
    43  
    44  	s := &RepositoryServer{RepoPath: "testdata/server"}
    45  	srv, err := startLocalServerForTests(s)
    46  	if err != nil {
    47  		t.Fatal(err)
    48  	}
    49  	defer srv.Close()
    50  
    51  	for _, tt := range tests {
    52  		res, err := http.Get(srv.URL + tt.path)
    53  		if err != nil {
    54  			t.Errorf("%s: error getting %s: %s", tt.name, tt.path, err)
    55  			continue
    56  		}
    57  		body, err := ioutil.ReadAll(res.Body)
    58  		if err != nil {
    59  			t.Errorf("%s: error reading %s: %s", tt.name, tt.path, err)
    60  		}
    61  		res.Body.Close()
    62  		if !strings.Contains(string(body), tt.expect) {
    63  			t.Errorf("%s: expected to find %q in %q", tt.name, tt.expect, string(body))
    64  		}
    65  	}
    66  
    67  }