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.

validurl_test.go 2.1KB

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