mirror of
https://github.com/go-gitea/gitea.git
synced 2026-08-20 04:53:55 +02:00
Better than before, still not good enough (more work can be done in the future) And add the missing error handling in the PrivateContext "bind" middleware. By the way, picked some "TrimSpace" changes from "fix: trim whitespace from SMTP address and port - #38934" (fix #38926)
54 lines
1.0 KiB
Go
54 lines
1.0 KiB
Go
// Copyright 2021 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package validation
|
|
|
|
import (
|
|
"regexp"
|
|
"testing"
|
|
)
|
|
|
|
func getRegexPatternErrorString(pattern string) string {
|
|
if _, err := regexp.Compile(pattern); err != nil {
|
|
return err.Error()
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func Test_RegexPatternValidation(t *testing.T) {
|
|
regexValidationTestCases := []validationTestCase{
|
|
{
|
|
description: "Empty regex pattern",
|
|
data: &TestForm{
|
|
RegexPattern: "",
|
|
},
|
|
},
|
|
{
|
|
description: "Valid regex",
|
|
data: &TestForm{
|
|
RegexPattern: `(\d{1,3})+`,
|
|
},
|
|
},
|
|
|
|
{
|
|
description: "Invalid regex",
|
|
data: &TestForm{
|
|
RegexPattern: "[a-",
|
|
},
|
|
expectedErrors: BindingErrors{
|
|
BindingError{
|
|
FieldNames: []string{"RegexPattern"},
|
|
Classification: ErrRegexPattern,
|
|
Message: getRegexPatternErrorString("[a-"),
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
for _, testCase := range regexValidationTestCases {
|
|
t.Run(testCase.description, func(t *testing.T) {
|
|
performValidationTest(t, testCase)
|
|
})
|
|
}
|
|
}
|