istio.io/istio@v0.0.0-20240520182934-d79c90f27776/operator/pkg/util/common_test.go (about)

     1  // Copyright Istio Authors
     2  //
     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  package util
    16  
    17  import (
    18  	"errors"
    19  	"testing"
    20  )
    21  
    22  func TestIsFilePath(t *testing.T) {
    23  	tests := []struct {
    24  		desc string
    25  		in   string
    26  		want bool
    27  	}{
    28  		{
    29  			desc: "empty",
    30  			in:   "",
    31  			want: false,
    32  		},
    33  		{
    34  			desc: "no-markers",
    35  			in:   "foobar",
    36  			want: false,
    37  		},
    38  		{
    39  			desc: "with-slash",
    40  			in:   "/home/bobby/go_rocks/main",
    41  			want: true,
    42  		},
    43  		{
    44  			desc: "with-period",
    45  			in:   "istio.go",
    46  			want: true,
    47  		},
    48  	}
    49  	for _, tt := range tests {
    50  		t.Run(tt.desc, func(t *testing.T) {
    51  			if got, want := IsFilePath(tt.in), tt.want; !(got == want) {
    52  				t.Errorf("%s: got %v, want: %v", tt.desc, got, want)
    53  			}
    54  		})
    55  	}
    56  }
    57  
    58  func TestIsHTTPURL(t *testing.T) {
    59  	tests := []struct {
    60  		desc string
    61  		in   string
    62  		want bool
    63  		err  error
    64  	}{
    65  		{
    66  			desc: "empty",
    67  			in:   "",
    68  			want: false,
    69  			err:  nil,
    70  		},
    71  		{
    72  			desc: "http-standard-url",
    73  			in:   "http://localhost:3000",
    74  			want: true,
    75  			err:  nil,
    76  		},
    77  		{
    78  			desc: "https-standard-url",
    79  			in:   "https://golang.org/",
    80  			want: true,
    81  			err:  nil,
    82  		},
    83  		{
    84  			desc: "ftp-url",
    85  			in:   "ftp://gopher:gopwd@localhost:3000/go",
    86  			want: false,
    87  			err:  nil,
    88  		},
    89  		{
    90  			desc: "tcp-discovery-url",
    91  			in:   "tcp://127.0.0.1:80",
    92  			want: false,
    93  			err:  nil,
    94  		},
    95  		{
    96  			desc: "empty-http",
    97  			in:   "http://",
    98  			want: false,
    99  			err:  errors.New(""),
   100  		},
   101  	}
   102  	for _, tt := range tests {
   103  		t.Run(tt.desc, func(t *testing.T) {
   104  			got, err := IsHTTPURL(tt.in)
   105  			if want, wantErr := tt.want, tt.err; !(got == want) || ((err == nil && wantErr != nil) || (err != nil && wantErr == nil)) {
   106  				t.Errorf("%s: got :%v, wanted output: %v, got error: %v, wanted error: %v", tt.desc, got, want, err, wantErr)
   107  			}
   108  		})
   109  	}
   110  }