Files
gitea/modules/validation/regex_pattern_test.go
T
wxiaoguangandGitHub 6904f6480c refactor: http request binding (#38971)
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)
2026-08-19 14:15:42 +08:00

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)
})
}
}