github.com/oam-dev/kubevela@v1.9.11/pkg/utils/url_test.go (about)

     1  /*
     2  Copyright 2021 The KubeVela 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 utils
    18  
    19  import (
    20  	"fmt"
    21  	"net/url"
    22  	"testing"
    23  
    24  	"github.com/stretchr/testify/assert"
    25  	"github.com/stretchr/testify/require"
    26  )
    27  
    28  func TestParseEndpoint(t *testing.T) {
    29  	testCases := []struct {
    30  		Input    string
    31  		Output   string
    32  		HasError bool
    33  	}{{
    34  		Input:  "127.0.0.1",
    35  		Output: "https://127.0.0.1:443",
    36  	}, {
    37  		Input:  "http://127.0.0.1",
    38  		Output: "http://127.0.0.1:80",
    39  	}, {
    40  		Input:  "127.0.0.1:6443",
    41  		Output: "https://127.0.0.1:6443",
    42  	}, {
    43  		Input:  "127.0.0.1:80",
    44  		Output: "http://127.0.0.1:80",
    45  	}, {
    46  		Input:  "localhost",
    47  		Output: "https://localhost:443",
    48  	}, {
    49  		Input:  "https://worker-control-plane:6443",
    50  		Output: "https://worker-control-plane:6443",
    51  	}, {
    52  		Input:    "invalid url",
    53  		HasError: true,
    54  	}}
    55  	r := require.New(t)
    56  	for _, testCase := range testCases {
    57  		output, err := ParseAPIServerEndpoint(testCase.Input)
    58  		if testCase.HasError {
    59  			r.Error(err)
    60  			continue
    61  		}
    62  		r.NoError(err)
    63  		r.Equal(testCase.Output, output)
    64  	}
    65  }
    66  
    67  func TestIsValidURL(t *testing.T) {
    68  	type args struct {
    69  		strURL string
    70  	}
    71  	tests := []struct {
    72  		name string
    73  		args args
    74  		want bool
    75  	}{
    76  		{
    77  			name: "empty url should valid error",
    78  			args: args{
    79  				strURL: "",
    80  			},
    81  			want: false,
    82  		},
    83  		{
    84  			name: "invalid url format should valid error",
    85  			args: args{
    86  				strURL: "invalid url",
    87  			},
    88  			want: false,
    89  		},
    90  		{
    91  			name: "invalid scheme should valid error",
    92  			args: args{
    93  				strURL: "http://",
    94  			},
    95  			want: false,
    96  		},
    97  		{
    98  			name: "invalid host should valid error",
    99  			args: args{
   100  				strURL: "http:// :8080",
   101  			},
   102  			want: false,
   103  		},
   104  		{
   105  			name: "normal url should valid",
   106  			args: args{
   107  				strURL: "http://localhost:8080",
   108  			},
   109  			want: true,
   110  		},
   111  	}
   112  	for _, tt := range tests {
   113  		t.Run(tt.name, func(t *testing.T) {
   114  			assert.Equalf(t, tt.want, IsValidURL(tt.args.strURL), "IsValidURL(%v)", tt.args.strURL)
   115  		})
   116  	}
   117  }
   118  
   119  func TestJoinURL(t *testing.T) {
   120  	testcase := []struct {
   121  		baseURL     string
   122  		subPath     string
   123  		expectedUrl string
   124  		err         error
   125  	}{
   126  		{
   127  			baseURL:     "https://www.kubevela.com",
   128  			subPath:     "index.yaml",
   129  			expectedUrl: "https://www.kubevela.com/index.yaml",
   130  			err:         nil,
   131  		},
   132  		{
   133  			baseURL:     "http://www.kubevela.com",
   134  			subPath:     "index.yaml",
   135  			expectedUrl: "http://www.kubevela.com/index.yaml",
   136  			err:         nil,
   137  		},
   138  		{
   139  			baseURL:     "0x7f:",
   140  			subPath:     "index.yaml",
   141  			expectedUrl: "",
   142  			err:         &url.Error{Op: "parse", URL: "0x7f:", Err: fmt.Errorf("first path segment in URL cannot contain colon")},
   143  		},
   144  	}
   145  
   146  	for _, tc := range testcase {
   147  		url, err := JoinURL(tc.baseURL, tc.subPath)
   148  		assert.Equal(t, tc.expectedUrl, url)
   149  		assert.Equal(t, tc.err, err)
   150  	}
   151  }