gitea源码

validurllist_test.go 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // Copyright 2024 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_ValidURLListValidation(t *testing.T) {
  9. AddBindingRules()
  10. // This is a copy of all the URL tests cases, plus additional ones to
  11. // account for multiple URLs
  12. urlListValidationTestCases := []validationTestCase{
  13. {
  14. description: "Empty URL",
  15. data: TestForm{
  16. URLs: "",
  17. },
  18. expectedErrors: binding.Errors{},
  19. },
  20. {
  21. description: "URL without port",
  22. data: TestForm{
  23. URLs: "http://test.lan/",
  24. },
  25. expectedErrors: binding.Errors{},
  26. },
  27. {
  28. description: "URL with port",
  29. data: TestForm{
  30. URLs: "http://test.lan:3000/",
  31. },
  32. expectedErrors: binding.Errors{},
  33. },
  34. {
  35. description: "URL with IPv6 address without port",
  36. data: TestForm{
  37. URLs: "http://[::1]/",
  38. },
  39. expectedErrors: binding.Errors{},
  40. },
  41. {
  42. description: "URL with IPv6 address with port",
  43. data: TestForm{
  44. URLs: "http://[::1]:3000/",
  45. },
  46. expectedErrors: binding.Errors{},
  47. },
  48. {
  49. description: "Invalid URL",
  50. data: TestForm{
  51. URLs: "http//test.lan/",
  52. },
  53. expectedErrors: binding.Errors{
  54. binding.Error{
  55. FieldNames: []string{"URLs"},
  56. Classification: binding.ERR_URL,
  57. Message: "http//test.lan/",
  58. },
  59. },
  60. },
  61. {
  62. description: "Invalid schema",
  63. data: TestForm{
  64. URLs: "ftp://test.lan/",
  65. },
  66. expectedErrors: binding.Errors{
  67. binding.Error{
  68. FieldNames: []string{"URLs"},
  69. Classification: binding.ERR_URL,
  70. Message: "ftp://test.lan/",
  71. },
  72. },
  73. },
  74. {
  75. description: "Invalid port",
  76. data: TestForm{
  77. URLs: "http://test.lan:3x4/",
  78. },
  79. expectedErrors: binding.Errors{
  80. binding.Error{
  81. FieldNames: []string{"URLs"},
  82. Classification: binding.ERR_URL,
  83. Message: "http://test.lan:3x4/",
  84. },
  85. },
  86. },
  87. {
  88. description: "Invalid port with IPv6 address",
  89. data: TestForm{
  90. URLs: "http://[::1]:3x4/",
  91. },
  92. expectedErrors: binding.Errors{
  93. binding.Error{
  94. FieldNames: []string{"URLs"},
  95. Classification: binding.ERR_URL,
  96. Message: "http://[::1]:3x4/",
  97. },
  98. },
  99. },
  100. {
  101. description: "Multi URLs",
  102. data: TestForm{
  103. URLs: "http://test.lan:3000/\nhttp://test.local/",
  104. },
  105. expectedErrors: binding.Errors{},
  106. },
  107. {
  108. description: "Multi URLs with newline",
  109. data: TestForm{
  110. URLs: "http://test.lan:3000/\nhttp://test.local/\n",
  111. },
  112. expectedErrors: binding.Errors{},
  113. },
  114. {
  115. description: "List with invalid entry",
  116. data: TestForm{
  117. URLs: "http://test.lan:3000/\nhttp://[::1]:3x4/",
  118. },
  119. expectedErrors: binding.Errors{
  120. binding.Error{
  121. FieldNames: []string{"URLs"},
  122. Classification: binding.ERR_URL,
  123. Message: "http://[::1]:3x4/",
  124. },
  125. },
  126. },
  127. {
  128. description: "List with two invalid entries",
  129. data: TestForm{
  130. URLs: "ftp://test.lan:3000/\nhttp://[::1]:3x4/\n",
  131. },
  132. expectedErrors: binding.Errors{
  133. binding.Error{
  134. FieldNames: []string{"URLs"},
  135. Classification: binding.ERR_URL,
  136. Message: "ftp://test.lan:3000/",
  137. },
  138. binding.Error{
  139. FieldNames: []string{"URLs"},
  140. Classification: binding.ERR_URL,
  141. Message: "http://[::1]:3x4/",
  142. },
  143. },
  144. },
  145. }
  146. for _, testCase := range urlListValidationTestCases {
  147. t.Run(testCase.description, func(t *testing.T) {
  148. performValidationTest(t, testCase)
  149. })
  150. }
  151. }