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