code.gitea.io/gitea@v1.19.3/modules/validation/validurl_test.go (about)

     1  // Copyright 2017 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package validation
     5  
     6  import (
     7  	"testing"
     8  
     9  	"gitea.com/go-chi/binding"
    10  )
    11  
    12  var urlValidationTestCases = []validationTestCase{
    13  	{
    14  		description: "Empty URL",
    15  		data: TestForm{
    16  			URL: "",
    17  		},
    18  		expectedErrors: binding.Errors{},
    19  	},
    20  	{
    21  		description: "URL without port",
    22  		data: TestForm{
    23  			URL: "http://test.lan/",
    24  		},
    25  		expectedErrors: binding.Errors{},
    26  	},
    27  	{
    28  		description: "URL with port",
    29  		data: TestForm{
    30  			URL: "http://test.lan:3000/",
    31  		},
    32  		expectedErrors: binding.Errors{},
    33  	},
    34  	{
    35  		description: "URL with IPv6 address without port",
    36  		data: TestForm{
    37  			URL: "http://[::1]/",
    38  		},
    39  		expectedErrors: binding.Errors{},
    40  	},
    41  	{
    42  		description: "URL with IPv6 address with port",
    43  		data: TestForm{
    44  			URL: "http://[::1]:3000/",
    45  		},
    46  		expectedErrors: binding.Errors{},
    47  	},
    48  	{
    49  		description: "Invalid URL",
    50  		data: TestForm{
    51  			URL: "http//test.lan/",
    52  		},
    53  		expectedErrors: binding.Errors{
    54  			binding.Error{
    55  				FieldNames:     []string{"URL"},
    56  				Classification: binding.ERR_URL,
    57  				Message:        "Url",
    58  			},
    59  		},
    60  	},
    61  	{
    62  		description: "Invalid schema",
    63  		data: TestForm{
    64  			URL: "ftp://test.lan/",
    65  		},
    66  		expectedErrors: binding.Errors{
    67  			binding.Error{
    68  				FieldNames:     []string{"URL"},
    69  				Classification: binding.ERR_URL,
    70  				Message:        "Url",
    71  			},
    72  		},
    73  	},
    74  	{
    75  		description: "Invalid port",
    76  		data: TestForm{
    77  			URL: "http://test.lan:3x4/",
    78  		},
    79  		expectedErrors: binding.Errors{
    80  			binding.Error{
    81  				FieldNames:     []string{"URL"},
    82  				Classification: binding.ERR_URL,
    83  				Message:        "Url",
    84  			},
    85  		},
    86  	},
    87  	{
    88  		description: "Invalid port with IPv6 address",
    89  		data: TestForm{
    90  			URL: "http://[::1]:3x4/",
    91  		},
    92  		expectedErrors: binding.Errors{
    93  			binding.Error{
    94  				FieldNames:     []string{"URL"},
    95  				Classification: binding.ERR_URL,
    96  				Message:        "Url",
    97  			},
    98  		},
    99  	},
   100  }
   101  
   102  func Test_ValidURLValidation(t *testing.T) {
   103  	AddBindingRules()
   104  
   105  	for _, testCase := range urlValidationTestCases {
   106  		t.Run(testCase.description, func(t *testing.T) {
   107  			performValidationTest(t, testCase)
   108  		})
   109  	}
   110  }