Files
Atay-Makhzan/modules/validation/validurl_test.go
T

111 lines
2.2 KiB
Go
Raw Normal View History

2017-04-19 06:02:20 +03:00
// Copyright 2017 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
2017-04-19 06:02:20 +03:00
package validation
import (
"testing"
2021-01-26 23:36:53 +08:00
"gitea.com/go-chi/binding"
2017-04-19 06:02:20 +03:00
)
func Test_ValidURLValidation(t *testing.T) {
AddBindingRules()
urlValidationTestCases := []validationTestCase{
{
description: "Empty URL",
data: TestForm{
URL: "",
},
expectedErrors: binding.Errors{},
2017-04-19 06:02:20 +03:00
},
{
description: "URL without port",
data: TestForm{
URL: "http://test.lan/",
},
expectedErrors: binding.Errors{},
2017-04-19 06:02:20 +03:00
},
{
description: "URL with port",
data: TestForm{
URL: "http://test.lan:3000/",
2017-04-19 06:02:20 +03:00
},
expectedErrors: binding.Errors{},
2017-04-19 06:02:20 +03:00
},
{
description: "URL with IPv6 address without port",
data: TestForm{
URL: "http://[::1]/",
},
expectedErrors: binding.Errors{},
2017-04-19 06:02:20 +03:00
},
{
description: "URL with IPv6 address with port",
data: TestForm{
URL: "http://[::1]:3000/",
2017-04-19 06:02:20 +03:00
},
expectedErrors: binding.Errors{},
2017-04-19 06:02:20 +03:00
},
{
description: "Invalid URL",
data: TestForm{
URL: "http//test.lan/",
},
expectedErrors: binding.Errors{
binding.Error{
FieldNames: []string{"URL"},
Classification: binding.ERR_URL,
Message: "Url",
},
},
2017-04-19 06:02:20 +03:00
},
{
description: "Invalid schema",
data: TestForm{
URL: "ftp://test.lan/",
},
expectedErrors: binding.Errors{
binding.Error{
FieldNames: []string{"URL"},
Classification: binding.ERR_URL,
Message: "Url",
},
2017-04-19 06:02:20 +03:00
},
},
{
description: "Invalid port",
data: TestForm{
URL: "http://test.lan:3x4/",
},
expectedErrors: binding.Errors{
binding.Error{
FieldNames: []string{"URL"},
Classification: binding.ERR_URL,
Message: "Url",
},
},
2017-04-19 06:02:20 +03:00
},
{
description: "Invalid port with IPv6 address",
data: TestForm{
URL: "http://[::1]:3x4/",
},
expectedErrors: binding.Errors{
binding.Error{
FieldNames: []string{"URL"},
Classification: binding.ERR_URL,
Message: "Url",
},
2017-04-19 06:02:20 +03:00
},
},
}
2017-04-19 06:02:20 +03:00
for _, testCase := range urlValidationTestCases {
t.Run(testCase.description, func(t *testing.T) {
performValidationTest(t, testCase)
})
}
}