You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

111 lines
2.1 KiB

  1. // Copyright 2017 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package validation
  5. import (
  6. "testing"
  7. "github.com/go-macaron/binding"
  8. )
  9. var urlValidationTestCases = []validationTestCase{
  10. {
  11. description: "Empty URL",
  12. data: TestForm{
  13. URL: "",
  14. },
  15. expectedErrors: binding.Errors{},
  16. },
  17. {
  18. description: "URL without port",
  19. data: TestForm{
  20. URL: "http://test.lan/",
  21. },
  22. expectedErrors: binding.Errors{},
  23. },
  24. {
  25. description: "URL with port",
  26. data: TestForm{
  27. URL: "http://test.lan:3000/",
  28. },
  29. expectedErrors: binding.Errors{},
  30. },
  31. {
  32. description: "URL with IPv6 address without port",
  33. data: TestForm{
  34. URL: "http://[::1]/",
  35. },
  36. expectedErrors: binding.Errors{},
  37. },
  38. {
  39. description: "URL with IPv6 address with port",
  40. data: TestForm{
  41. URL: "http://[::1]:3000/",
  42. },
  43. expectedErrors: binding.Errors{},
  44. },
  45. {
  46. description: "Invalid URL",
  47. data: TestForm{
  48. URL: "http//test.lan/",
  49. },
  50. expectedErrors: binding.Errors{
  51. binding.Error{
  52. FieldNames: []string{"URL"},
  53. Classification: binding.ERR_URL,
  54. Message: "Url",
  55. },
  56. },
  57. },
  58. {
  59. description: "Invalid schema",
  60. data: TestForm{
  61. URL: "ftp://test.lan/",
  62. },
  63. expectedErrors: binding.Errors{
  64. binding.Error{
  65. FieldNames: []string{"URL"},
  66. Classification: binding.ERR_URL,
  67. Message: "Url",
  68. },
  69. },
  70. },
  71. {
  72. description: "Invalid port",
  73. data: TestForm{
  74. URL: "http://test.lan:3x4/",
  75. },
  76. expectedErrors: binding.Errors{
  77. binding.Error{
  78. FieldNames: []string{"URL"},
  79. Classification: binding.ERR_URL,
  80. Message: "Url",
  81. },
  82. },
  83. },
  84. {
  85. description: "Invalid port with IPv6 address",
  86. data: TestForm{
  87. URL: "http://[::1]:3x4/",
  88. },
  89. expectedErrors: binding.Errors{
  90. binding.Error{
  91. FieldNames: []string{"URL"},
  92. Classification: binding.ERR_URL,
  93. Message: "Url",
  94. },
  95. },
  96. },
  97. }
  98. func Test_ValidURLValidation(t *testing.T) {
  99. AddBindingRules()
  100. for _, testCase := range urlValidationTestCases {
  101. t.Run(testCase.description, func(t *testing.T) {
  102. performValidationTest(t, testCase)
  103. })
  104. }
  105. }