k8s.io/client-go@v0.31.1/rest/url_utils_test.go (about)

     1  /*
     2  Copyright 2016 The Kubernetes 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 rest
    18  
    19  import (
    20  	"path"
    21  	"testing"
    22  
    23  	"k8s.io/api/core/v1"
    24  )
    25  
    26  func TestValidatesHostParameter(t *testing.T) {
    27  	testCases := []struct {
    28  		Host    string
    29  		APIPath string
    30  
    31  		URL string
    32  		Err bool
    33  	}{
    34  		{"127.0.0.1", "", "http://127.0.0.1/" + v1.SchemeGroupVersion.Version, false},
    35  		{"127.0.0.1:8080", "", "http://127.0.0.1:8080/" + v1.SchemeGroupVersion.Version, false},
    36  		{"foo.bar.com", "", "http://foo.bar.com/" + v1.SchemeGroupVersion.Version, false},
    37  		{"http://host/prefix", "", "http://host/prefix/" + v1.SchemeGroupVersion.Version, false},
    38  		{"http://host", "", "http://host/" + v1.SchemeGroupVersion.Version, false},
    39  		{"http://host", "/", "http://host/" + v1.SchemeGroupVersion.Version, false},
    40  		{"http://host", "/other", "http://host/other/" + v1.SchemeGroupVersion.Version, false},
    41  		{"host/server", "", "", true},
    42  	}
    43  	for i, testCase := range testCases {
    44  		u, versionedAPIPath, err := DefaultServerURL(testCase.Host, testCase.APIPath, v1.SchemeGroupVersion, false)
    45  		switch {
    46  		case err == nil && testCase.Err:
    47  			t.Errorf("expected error but was nil")
    48  			continue
    49  		case err != nil && !testCase.Err:
    50  			t.Errorf("unexpected error %v", err)
    51  			continue
    52  		case err != nil:
    53  			continue
    54  		}
    55  		u.Path = path.Join(u.Path, versionedAPIPath)
    56  		if e, a := testCase.URL, u.String(); e != a {
    57  			t.Errorf("%d: expected host %s, got %s", i, e, a)
    58  			continue
    59  		}
    60  	}
    61  }