Files
gitea/modules/validation/validurl_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

102 lines
1.9 KiB
Go

// Copyright 2017 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package validation
import (
"testing"
)
func Test_ValidURLValidation(t *testing.T) {
urlValidationTestCases := []validationTestCase{
{
description: "Empty URL",
data: &TestForm{
URL: "",
},
},
{
description: "URL without port",
data: &TestForm{
URL: "http://test.lan/",
},
},
{
description: "URL with port",
data: &TestForm{
URL: "http://test.lan:3000/",
},
},
{
description: "URL with IPv6 address without port",
data: &TestForm{
URL: "http://[::1]/",
},
},
{
description: "URL with IPv6 address with port",
data: &TestForm{
URL: "http://[::1]:3000/",
},
},
{
description: "Invalid URL",
data: &TestForm{
URL: "http//test.lan/",
},
expectedErrors: BindingErrors{
BindingError{
FieldNames: []string{"URL"},
Classification: "UrlError",
Message: "Url",
},
},
},
{
description: "Invalid schema",
data: &TestForm{
URL: "ftp://test.lan/",
},
expectedErrors: BindingErrors{
BindingError{
FieldNames: []string{"URL"},
Classification: "UrlError",
Message: "Url",
},
},
},
{
description: "Invalid port",
data: &TestForm{
URL: "http://test.lan:3x4/",
},
expectedErrors: BindingErrors{
BindingError{
FieldNames: []string{"URL"},
Classification: "UrlError",
Message: "Url",
},
},
},
{
description: "Invalid port with IPv6 address",
data: &TestForm{
URL: "http://[::1]:3x4/",
},
expectedErrors: BindingErrors{
BindingError{
FieldNames: []string{"URL"},
Classification: "UrlError",
Message: "Url",
},
},
},
}
for _, testCase := range urlValidationTestCases {
t.Run(testCase.description, func(t *testing.T) {
performValidationTest(t, testCase)
})
}
}