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.

helpers_test.go 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // Copyright 2018 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. "code.gitea.io/gitea/modules/setting"
  8. "github.com/stretchr/testify/assert"
  9. )
  10. func Test_IsValidURL(t *testing.T) {
  11. cases := []struct {
  12. description string
  13. url string
  14. valid bool
  15. }{
  16. {
  17. description: "Empty URL",
  18. url: "",
  19. valid: false,
  20. },
  21. {
  22. description: "Loobpack IPv4 URL",
  23. url: "http://127.0.1.1:5678/",
  24. valid: true,
  25. },
  26. {
  27. description: "Loobpack IPv6 URL",
  28. url: "https://[::1]/",
  29. valid: true,
  30. },
  31. {
  32. description: "Missing semicolon after schema",
  33. url: "http//meh/",
  34. valid: false,
  35. },
  36. }
  37. for _, testCase := range cases {
  38. t.Run(testCase.description, func(t *testing.T) {
  39. assert.Equal(t, testCase.valid, IsValidURL(testCase.url))
  40. })
  41. }
  42. }
  43. func Test_IsValidExternalURL(t *testing.T) {
  44. setting.AppURL = "https://try.gitea.io/"
  45. cases := []struct {
  46. description string
  47. url string
  48. valid bool
  49. }{
  50. {
  51. description: "Current instance URL",
  52. url: "https://try.gitea.io/test",
  53. valid: true,
  54. },
  55. {
  56. description: "Loobpack IPv4 URL",
  57. url: "http://127.0.1.1:5678/",
  58. valid: false,
  59. },
  60. {
  61. description: "Current instance API URL",
  62. url: "https://try.gitea.io/api/v1/user/follow",
  63. valid: false,
  64. },
  65. {
  66. description: "Local network URL",
  67. url: "http://192.168.1.2/api/v1/user/follow",
  68. valid: true,
  69. },
  70. {
  71. description: "Local URL",
  72. url: "http://LOCALHOST:1234/whatever",
  73. valid: false,
  74. },
  75. }
  76. for _, testCase := range cases {
  77. t.Run(testCase.description, func(t *testing.T) {
  78. assert.Equal(t, testCase.valid, IsValidExternalURL(testCase.url))
  79. })
  80. }
  81. }
  82. func Test_IsValidExternalTrackerURLFormat(t *testing.T) {
  83. setting.AppURL = "https://try.gitea.io/"
  84. cases := []struct {
  85. description string
  86. url string
  87. valid bool
  88. }{
  89. {
  90. description: "Correct external tracker URL with all placeholders",
  91. url: "https://github.com/{user}/{repo}/issues/{index}",
  92. valid: true,
  93. },
  94. {
  95. description: "Local external tracker URL with all placeholders",
  96. url: "https://127.0.0.1/{user}/{repo}/issues/{index}",
  97. valid: false,
  98. },
  99. {
  100. description: "External tracker URL with typo placeholder",
  101. url: "https://github.com/{user}/{repo/issues/{index}",
  102. valid: false,
  103. },
  104. {
  105. description: "External tracker URL with typo placeholder",
  106. url: "https://github.com/[user}/{repo/issues/{index}",
  107. valid: false,
  108. },
  109. {
  110. description: "External tracker URL with typo placeholder",
  111. url: "https://github.com/{user}/repo}/issues/{index}",
  112. valid: false,
  113. },
  114. {
  115. description: "External tracker URL missing optional placeholder",
  116. url: "https://github.com/{user}/issues/{index}",
  117. valid: true,
  118. },
  119. {
  120. description: "External tracker URL missing optional placeholder",
  121. url: "https://github.com/{repo}/issues/{index}",
  122. valid: true,
  123. },
  124. {
  125. description: "External tracker URL missing optional placeholder",
  126. url: "https://github.com/issues/{index}",
  127. valid: true,
  128. },
  129. {
  130. description: "External tracker URL missing optional placeholder",
  131. url: "https://github.com/issues/{user}",
  132. valid: true,
  133. },
  134. {
  135. description: "External tracker URL with similar placeholder names test",
  136. url: "https://github.com/user/repo/issues/{index}",
  137. valid: true,
  138. },
  139. }
  140. for _, testCase := range cases {
  141. t.Run(testCase.description, func(t *testing.T) {
  142. assert.Equal(t, testCase.valid, IsValidExternalTrackerURLFormat(testCase.url))
  143. })
  144. }
  145. }