gitea源码

validurl_test.go 2.2KB

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