|
| 1 | +// Copyright 2024 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 | +// This is a copy of all the URL tests cases, plus additional ones to |
| 13 | +// account for multiple URLs |
| 14 | +var urlListValidationTestCases = []validationTestCase{ |
| 15 | + { |
| 16 | + description: "Empty URL", |
| 17 | + data: TestForm{ |
| 18 | + URLs: "", |
| 19 | + }, |
| 20 | + expectedErrors: binding.Errors{}, |
| 21 | + }, |
| 22 | + { |
| 23 | + description: "URL without port", |
| 24 | + data: TestForm{ |
| 25 | + URLs: "http://test.lan/", |
| 26 | + }, |
| 27 | + expectedErrors: binding.Errors{}, |
| 28 | + }, |
| 29 | + { |
| 30 | + description: "URL with port", |
| 31 | + data: TestForm{ |
| 32 | + URLs: "http://test.lan:3000/", |
| 33 | + }, |
| 34 | + expectedErrors: binding.Errors{}, |
| 35 | + }, |
| 36 | + { |
| 37 | + description: "URL with IPv6 address without port", |
| 38 | + data: TestForm{ |
| 39 | + URLs: "http://[::1]/", |
| 40 | + }, |
| 41 | + expectedErrors: binding.Errors{}, |
| 42 | + }, |
| 43 | + { |
| 44 | + description: "URL with IPv6 address with port", |
| 45 | + data: TestForm{ |
| 46 | + URLs: "http://[::1]:3000/", |
| 47 | + }, |
| 48 | + expectedErrors: binding.Errors{}, |
| 49 | + }, |
| 50 | + { |
| 51 | + description: "Invalid URL", |
| 52 | + data: TestForm{ |
| 53 | + URLs: "http//test.lan/", |
| 54 | + }, |
| 55 | + expectedErrors: binding.Errors{ |
| 56 | + binding.Error{ |
| 57 | + FieldNames: []string{"URLs"}, |
| 58 | + Classification: binding.ERR_URL, |
| 59 | + Message: "http//test.lan/", |
| 60 | + }, |
| 61 | + }, |
| 62 | + }, |
| 63 | + { |
| 64 | + description: "Invalid schema", |
| 65 | + data: TestForm{ |
| 66 | + URLs: "ftp://test.lan/", |
| 67 | + }, |
| 68 | + expectedErrors: binding.Errors{ |
| 69 | + binding.Error{ |
| 70 | + FieldNames: []string{"URLs"}, |
| 71 | + Classification: binding.ERR_URL, |
| 72 | + Message: "ftp://test.lan/", |
| 73 | + }, |
| 74 | + }, |
| 75 | + }, |
| 76 | + { |
| 77 | + description: "Invalid port", |
| 78 | + data: TestForm{ |
| 79 | + URLs: "http://test.lan:3x4/", |
| 80 | + }, |
| 81 | + expectedErrors: binding.Errors{ |
| 82 | + binding.Error{ |
| 83 | + FieldNames: []string{"URLs"}, |
| 84 | + Classification: binding.ERR_URL, |
| 85 | + Message: "http://test.lan:3x4/", |
| 86 | + }, |
| 87 | + }, |
| 88 | + }, |
| 89 | + { |
| 90 | + description: "Invalid port with IPv6 address", |
| 91 | + data: TestForm{ |
| 92 | + URLs: "http://[::1]:3x4/", |
| 93 | + }, |
| 94 | + expectedErrors: binding.Errors{ |
| 95 | + binding.Error{ |
| 96 | + FieldNames: []string{"URLs"}, |
| 97 | + Classification: binding.ERR_URL, |
| 98 | + Message: "http://[::1]:3x4/", |
| 99 | + }, |
| 100 | + }, |
| 101 | + }, |
| 102 | + { |
| 103 | + description: "Multi URLs", |
| 104 | + data: TestForm{ |
| 105 | + URLs: "http://test.lan:3000/\nhttp://test.local/", |
| 106 | + }, |
| 107 | + expectedErrors: binding.Errors{}, |
| 108 | + }, |
| 109 | + { |
| 110 | + description: "Multi URLs with newline", |
| 111 | + data: TestForm{ |
| 112 | + URLs: "http://test.lan:3000/\nhttp://test.local/\n", |
| 113 | + }, |
| 114 | + expectedErrors: binding.Errors{}, |
| 115 | + }, |
| 116 | + { |
| 117 | + description: "List with invalid entry", |
| 118 | + data: TestForm{ |
| 119 | + URLs: "http://test.lan:3000/\nhttp://[::1]:3x4/", |
| 120 | + }, |
| 121 | + expectedErrors: binding.Errors{ |
| 122 | + binding.Error{ |
| 123 | + FieldNames: []string{"URLs"}, |
| 124 | + Classification: binding.ERR_URL, |
| 125 | + Message: "http://[::1]:3x4/", |
| 126 | + }, |
| 127 | + }, |
| 128 | + }, |
| 129 | + { |
| 130 | + description: "List with two invalid entries", |
| 131 | + data: TestForm{ |
| 132 | + URLs: "ftp://test.lan:3000/\nhttp://[::1]:3x4/\n", |
| 133 | + }, |
| 134 | + expectedErrors: binding.Errors{ |
| 135 | + binding.Error{ |
| 136 | + FieldNames: []string{"URLs"}, |
| 137 | + Classification: binding.ERR_URL, |
| 138 | + Message: "ftp://test.lan:3000/", |
| 139 | + }, |
| 140 | + binding.Error{ |
| 141 | + FieldNames: []string{"URLs"}, |
| 142 | + Classification: binding.ERR_URL, |
| 143 | + Message: "http://[::1]:3x4/", |
| 144 | + }, |
| 145 | + }, |
| 146 | + }, |
| 147 | +} |
| 148 | + |
| 149 | +func Test_ValidURLListValidation(t *testing.T) { |
| 150 | + AddBindingRules() |
| 151 | + |
| 152 | + for _, testCase := range urlListValidationTestCases { |
| 153 | + t.Run(testCase.description, func(t *testing.T) { |
| 154 | + performValidationTest(t, testCase) |
| 155 | + }) |
| 156 | + } |
| 157 | +} |
0 commit comments