Skip to content

Commit e536a9d

Browse files
committed
add unit tests and fix failing cases
1 parent 57958d2 commit e536a9d

File tree

4 files changed

+163
-3
lines changed

4 files changed

+163
-3
lines changed

modules/util/truncate.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ func SplitStringAtByteN(input string, n int) (left, right string) {
4141

4242
// SplitTrimSpace splits the string at given separator and trims leading and trailing space
4343
func SplitTrimSpace(input, sep string) []string {
44+
// Trim initial leading & trailing space
45+
input = strings.TrimSpace(input)
4446
// replace CRLF with LF
4547
input = strings.ReplaceAll(input, "\r\n", "\n")
4648

modules/validation/binding.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func addValidURLListBindingRule() {
6464
// URL validation rule
6565
binding.AddRule(&binding.Rule{
6666
IsMatch: func(rule string) bool {
67-
return strings.HasPrefix(rule, "ValidUrlList")
67+
return strings.EqualFold(rule, "ValidUrlList")
6868
},
6969
IsValid: func(errs binding.Errors, name string, val any) (bool, binding.Errors) {
7070
str := fmt.Sprintf("%v", val)
@@ -78,7 +78,7 @@ func addValidURLListBindingRule() {
7878
for _, u := range urls {
7979
if !IsValidURL(u) {
8080
ok = false
81-
errs.Add([]string{"RedirectURIs"}, binding.ERR_URL, u)
81+
errs.Add([]string{name}, binding.ERR_URL, u)
8282
}
8383
}
8484

@@ -91,7 +91,7 @@ func addValidURLBindingRule() {
9191
// URL validation rule
9292
binding.AddRule(&binding.Rule{
9393
IsMatch: func(rule string) bool {
94-
return strings.HasPrefix(rule, "ValidUrl")
94+
return strings.EqualFold(rule, "ValidUrl")
9595
},
9696
IsValid: func(errs binding.Errors, name string, val any) (bool, binding.Errors) {
9797
str := fmt.Sprintf("%v", val)

modules/validation/binding_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ type (
2727
TestForm struct {
2828
BranchName string `form:"BranchName" binding:"GitRefName"`
2929
URL string `form:"ValidUrl" binding:"ValidUrl"`
30+
URLs string `form:"ValidUrls" binding:"ValidUrlList"`
3031
GlobPattern string `form:"GlobPattern" binding:"GlobPattern"`
3132
RegexPattern string `form:"RegexPattern" binding:"RegexPattern"`
3233
}
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
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

Comments
 (0)