github.com/kruglovmax/helm@v3.0.0-beta.3+incompatible/internal/urlutil/urlutil_test.go (about)

     1  /*
     2  Copyright The Helm Authors.
     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 urlutil
    18  
    19  import "testing"
    20  
    21  func TestURLJoin(t *testing.T) {
    22  	tests := []struct {
    23  		name, url, expect string
    24  		paths             []string
    25  	}{
    26  		{name: "URL, one path", url: "http://example.com", paths: []string{"hello"}, expect: "http://example.com/hello"},
    27  		{name: "Long URL, one path", url: "http://example.com/but/first", paths: []string{"slurm"}, expect: "http://example.com/but/first/slurm"},
    28  		{name: "URL, two paths", url: "http://example.com", paths: []string{"hello", "world"}, expect: "http://example.com/hello/world"},
    29  		{name: "URL, no paths", url: "http://example.com", paths: []string{}, expect: "http://example.com"},
    30  		{name: "basepath, two paths", url: "../example.com", paths: []string{"hello", "world"}, expect: "../example.com/hello/world"},
    31  	}
    32  
    33  	for _, tt := range tests {
    34  		if got, err := URLJoin(tt.url, tt.paths...); err != nil {
    35  			t.Errorf("%s: error %q", tt.name, err)
    36  		} else if got != tt.expect {
    37  			t.Errorf("%s: expected %q, got %q", tt.name, tt.expect, got)
    38  		}
    39  	}
    40  }
    41  
    42  func TestEqual(t *testing.T) {
    43  	for _, tt := range []struct {
    44  		a, b  string
    45  		match bool
    46  	}{
    47  		{"http://example.com", "http://example.com", true},
    48  		{"http://example.com", "http://another.example.com", false},
    49  		{"https://example.com", "https://example.com", true},
    50  		{"http://example.com/", "http://example.com", true},
    51  		{"https://example.com", "http://example.com", false},
    52  		{"http://example.com/foo", "http://example.com/foo/", true},
    53  		{"http://example.com/foo//", "http://example.com/foo/", true},
    54  		{"http://example.com/./foo/", "http://example.com/foo/", true},
    55  		{"http://example.com/bar/../foo/", "http://example.com/foo/", true},
    56  		{"/foo", "/foo", true},
    57  		{"/foo", "/foo/", true},
    58  		{"/foo/.", "/foo/", true},
    59  	} {
    60  		if tt.match != Equal(tt.a, tt.b) {
    61  			t.Errorf("Expected %q==%q to be %t", tt.a, tt.b, tt.match)
    62  		}
    63  	}
    64  }
    65  
    66  func TestExtractHostname(t *testing.T) {
    67  	tests := map[string]string{
    68  		"http://example.com":      "example.com",
    69  		"https://example.com/foo": "example.com",
    70  
    71  		"https://example.com:31337/not/with/a/bang/but/a/whimper": "example.com",
    72  	}
    73  	for start, expect := range tests {
    74  		if got, _ := ExtractHostname(start); got != expect {
    75  			t.Errorf("Got %q, expected %q", got, expect)
    76  		}
    77  	}
    78  }