aboutsummaryrefslogtreecommitdiffstats
path: root/modules/validation/validurllist_test.go
blob: c6f862a962b00837e94909a979b190636312b20b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// Copyright 2024 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package validation

import (
	"testing"

	"gitea.com/go-chi/binding"
)

// This is a copy of all the URL tests cases, plus additional ones to
// account for multiple URLs
var urlListValidationTestCases = []validationTestCase{
	{
		description: "Empty URL",
		data: TestForm{
			URLs: "",
		},
		expectedErrors: binding.Errors{},
	},
	{
		description: "URL without port",
		data: TestForm{
			URLs: "http://test.lan/",
		},
		expectedErrors: binding.Errors{},
	},
	{
		description: "URL with port",
		data: TestForm{
			URLs: "http://test.lan:3000/",
		},
		expectedErrors: binding.Errors{},
	},
	{
		description: "URL with IPv6 address without port",
		data: TestForm{
			URLs: "http://[::1]/",
		},
		expectedErrors: binding.Errors{},
	},
	{
		description: "URL with IPv6 address with port",
		data: TestForm{
			URLs: "http://[::1]:3000/",
		},
		expectedErrors: binding.Errors{},
	},
	{
		description: "Invalid URL",
		data: TestForm{
			URLs: "http//test.lan/",
		},
		expectedErrors: binding.Errors{
			binding.Error{
				FieldNames:     []string{"URLs"},
				Classification: binding.ERR_URL,
				Message:        "http//test.lan/",
			},
		},
	},
	{
		description: "Invalid schema",
		data: TestForm{
			URLs: "ftp://test.lan/",
		},
		expectedErrors: binding.Errors{
			binding.Error{
				FieldNames:     []string{"URLs"},
				Classification: binding.ERR_URL,
				Message:        "ftp://test.lan/",
			},
		},
	},
	{
		description: "Invalid port",
		data: TestForm{
			URLs: "http://test.lan:3x4/",
		},
		expectedErrors: binding.Errors{
			binding.Error{
				FieldNames:     []string{"URLs"},
				Classification: binding.ERR_URL,
				Message:        "http://test.lan:3x4/",
			},
		},
	},
	{
		description: "Invalid port with IPv6 address",
		data: TestForm{
			URLs: "http://[::1]:3x4/",
		},
		expectedErrors: binding.Errors{
			binding.Error{
				FieldNames:     []string{"URLs"},
				Classification: binding.ERR_URL,
				Message:        "http://[::1]:3x4/",
			},
		},
	},
	{
		description: "Multi URLs",
		data: TestForm{
			URLs: "http://test.lan:3000/\nhttp://test.local/",
		},
		expectedErrors: binding.Errors{},
	},
	{
		description: "Multi URLs with newline",
		data: TestForm{
			URLs: "http://test.lan:3000/\nhttp://test.local/\n",
		},
		expectedErrors: binding.Errors{},
	},
	{
		description: "List with invalid entry",
		data: TestForm{
			URLs: "http://test.lan:3000/\nhttp://[::1]:3x4/",
		},
		expectedErrors: binding.Errors{
			binding.Error{
				FieldNames:     []string{"URLs"},
				Classification: binding.ERR_URL,
				Message:        "http://[::1]:3x4/",
			},
		},
	},
	{
		description: "List with two invalid entries",
		data: TestForm{
			URLs: "ftp://test.lan:3000/\nhttp://[::1]:3x4/\n",
		},
		expectedErrors: binding.Errors{
			binding.Error{
				FieldNames:     []string{"URLs"},
				Classification: binding.ERR_URL,
				Message:        "ftp://test.lan:3000/",
			},
			binding.Error{
				FieldNames:     []string{"URLs"},
				Classification: binding.ERR_URL,
				Message:        "http://[::1]:3x4/",
			},
		},
	},
}

func Test_ValidURLListValidation(t *testing.T) {
	AddBindingRules()

	for _, testCase := range urlListValidationTestCases {
		t.Run(testCase.description, func(t *testing.T) {
			performValidationTest(t, testCase)
		})
	}
}