github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/pkg/inputvalidation/validators_url_test.go (about)

     1  package inputvalidation_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	validation "github.com/go-ozzo/ozzo-validation/v4"
     7  	"github.com/kyma-incubator/compass/components/director/pkg/inputvalidation"
     8  	"github.com/kyma-incubator/compass/components/director/pkg/str"
     9  	"github.com/stretchr/testify/require"
    10  )
    11  
    12  func TestValidateURL(t *testing.T) {
    13  	testCases := []struct {
    14  		Name  string
    15  		Input interface{}
    16  		Valid bool
    17  	}{
    18  		{
    19  			Name:  "Valid https",
    20  			Input: "https://kyma-project.io",
    21  			Valid: true,
    22  		},
    23  		{
    24  			Name:  "Valid http",
    25  			Input: "http://kyma-project.io",
    26  			Valid: true,
    27  		},
    28  		{
    29  			Name:  "URL without protocol",
    30  			Input: "kyma-project.io",
    31  			Valid: false,
    32  		},
    33  		{
    34  			Name:  "Valid",
    35  			Input: str.Ptr("https://kyma-project.io"),
    36  			Valid: true,
    37  		},
    38  		{
    39  			Name:  "Not string",
    40  			Input: 123,
    41  			Valid: false,
    42  		},
    43  		{
    44  			Name:  "nil",
    45  			Input: nil,
    46  			Valid: false,
    47  		},
    48  	}
    49  
    50  	for _, testCase := range testCases {
    51  		t.Run(testCase.Name, func(t *testing.T) {
    52  			//GIVEN
    53  			urlValidator := inputvalidation.IsURL
    54  
    55  			// WHEN
    56  			err := validation.Validate(testCase.Input, urlValidator)
    57  
    58  			// THEN
    59  			if testCase.Valid {
    60  				require.NoError(t, err)
    61  			} else {
    62  				require.Error(t, err)
    63  			}
    64  		})
    65  	}
    66  }