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.

nonascii_branches_test.go 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 integrations
  5. import (
  6. "net/http"
  7. "net/url"
  8. "path"
  9. "testing"
  10. "github.com/stretchr/testify/assert"
  11. )
  12. func testSrcRouteRedirect(t *testing.T, session *TestSession, user, repo, route, expectedLocation string, expectedStatus int) {
  13. prefix := path.Join("/", user, repo, "src")
  14. // Make request
  15. req := NewRequest(t, "GET", path.Join(prefix, route))
  16. resp := session.MakeRequest(t, req, http.StatusSeeOther)
  17. // Check Location header
  18. location := resp.HeaderMap.Get("Location")
  19. assert.Equal(t, path.Join(prefix, expectedLocation), location)
  20. // Perform redirect
  21. req = NewRequest(t, "GET", location)
  22. resp = session.MakeRequest(t, req, expectedStatus)
  23. }
  24. func setDefaultBranch(t *testing.T, session *TestSession, user, repo, branch string) {
  25. location := path.Join("/", user, repo, "settings/branches")
  26. csrf := GetCSRF(t, session, location)
  27. req := NewRequestWithValues(t, "POST", location, map[string]string{
  28. "_csrf": csrf,
  29. "action": "default_branch",
  30. "branch": branch,
  31. })
  32. session.MakeRequest(t, req, http.StatusSeeOther)
  33. }
  34. func TestNonasciiBranches(t *testing.T) {
  35. testRedirects := []struct {
  36. from string
  37. to string
  38. status int
  39. }{
  40. // Branches
  41. {
  42. from: "master",
  43. to: "branch/master",
  44. status: http.StatusOK,
  45. },
  46. {
  47. from: "master/README.md",
  48. to: "branch/master/README.md",
  49. status: http.StatusOK,
  50. },
  51. {
  52. from: "master/badfile",
  53. to: "branch/master/badfile",
  54. status: http.StatusNotFound, // it does not exists
  55. },
  56. {
  57. from: "ГлавнаяВетка",
  58. to: "branch/%D0%93%D0%BB%D0%B0%D0%B2%D0%BD%D0%B0%D1%8F%D0%92%D0%B5%D1%82%D0%BA%D0%B0",
  59. status: http.StatusOK,
  60. },
  61. {
  62. from: "а/б/в",
  63. to: "branch/%D0%B0/%D0%B1/%D0%B2",
  64. status: http.StatusOK,
  65. },
  66. {
  67. from: "Grüßen/README.md",
  68. to: "branch/Gr%C3%BC%C3%9Fen/README.md",
  69. status: http.StatusOK,
  70. },
  71. {
  72. from: "Plus+Is+Not+Space",
  73. to: "branch/Plus+Is+Not+Space",
  74. status: http.StatusOK,
  75. },
  76. {
  77. from: "Plus+Is+Not+Space/Файл.md",
  78. to: "branch/Plus+Is+Not+Space/%D0%A4%D0%B0%D0%B9%D0%BB.md",
  79. status: http.StatusOK,
  80. },
  81. {
  82. from: "Plus+Is+Not+Space/and+it+is+valid.md",
  83. to: "branch/Plus+Is+Not+Space/and+it+is+valid.md",
  84. status: http.StatusOK,
  85. },
  86. {
  87. from: "ブランチ",
  88. to: "branch/%E3%83%96%E3%83%A9%E3%83%B3%E3%83%81",
  89. status: http.StatusOK,
  90. },
  91. // Tags
  92. {
  93. from: "Тэг",
  94. to: "tag/%D0%A2%D1%8D%D0%B3",
  95. status: http.StatusOK,
  96. },
  97. {
  98. from: "Ё/人",
  99. to: "tag/%D0%81/%E4%BA%BA",
  100. status: http.StatusOK,
  101. },
  102. {
  103. from: "タグ",
  104. to: "tag/%E3%82%BF%E3%82%B0",
  105. status: http.StatusOK,
  106. },
  107. {
  108. from: "タグ/ファイル.md",
  109. to: "tag/%E3%82%BF%E3%82%B0/%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB.md",
  110. status: http.StatusOK,
  111. },
  112. // Files
  113. {
  114. from: "README.md",
  115. to: "branch/Plus+Is+Not+Space/README.md",
  116. status: http.StatusOK,
  117. },
  118. {
  119. from: "Файл.md",
  120. to: "branch/Plus+Is+Not+Space/%D0%A4%D0%B0%D0%B9%D0%BB.md",
  121. status: http.StatusOK,
  122. },
  123. {
  124. from: "ファイル.md",
  125. to: "branch/Plus+Is+Not+Space/%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB.md",
  126. status: http.StatusNotFound, // it's not on default branch
  127. },
  128. // Same but url-encoded (few tests)
  129. {
  130. from: "%E3%83%96%E3%83%A9%E3%83%B3%E3%83%81",
  131. to: "branch/%E3%83%96%E3%83%A9%E3%83%B3%E3%83%81",
  132. status: http.StatusOK,
  133. },
  134. {
  135. from: "%E3%82%BF%E3%82%b0",
  136. to: "tag/%E3%82%BF%E3%82%B0",
  137. status: http.StatusOK,
  138. },
  139. {
  140. from: "%D0%A4%D0%B0%D0%B9%D0%BB.md",
  141. to: "branch/Plus+Is+Not+Space/%D0%A4%D0%B0%D0%B9%D0%BB.md",
  142. status: http.StatusOK,
  143. },
  144. {
  145. from: "%D0%81%2F%E4%BA%BA",
  146. to: "tag/%D0%81/%E4%BA%BA",
  147. status: http.StatusOK,
  148. },
  149. {
  150. from: "Ё%2F%E4%BA%BA",
  151. to: "tag/%D0%81/%E4%BA%BA",
  152. status: http.StatusOK,
  153. },
  154. {
  155. from: "Plus+Is+Not+Space/%25%252525mightnotplaywell",
  156. to: "branch/Plus+Is+Not+Space/%25%252525mightnotplaywell",
  157. status: http.StatusOK,
  158. },
  159. {
  160. from: "Plus+Is+Not+Space/%25253Fisnotaquestion%25253F",
  161. to: "branch/Plus+Is+Not+Space/%25253Fisnotaquestion%25253F",
  162. status: http.StatusOK,
  163. },
  164. {
  165. from: "Plus+Is+Not+Space/" + url.PathEscape("%3Fis?and#afile"),
  166. to: "branch/Plus+Is+Not+Space/" + url.PathEscape("%3Fis?and#afile"),
  167. status: http.StatusOK,
  168. },
  169. {
  170. from: "Plus+Is+Not+Space/10%25.md",
  171. to: "branch/Plus+Is+Not+Space/10%25.md",
  172. status: http.StatusOK,
  173. },
  174. {
  175. from: "Plus+Is+Not+Space/" + url.PathEscape("This+file%20has 1space"),
  176. to: "branch/Plus+Is+Not+Space/" + url.PathEscape("This+file%20has 1space"),
  177. status: http.StatusOK,
  178. },
  179. {
  180. from: "Plus+Is+Not+Space/" + url.PathEscape("This+file%2520has 2 spaces"),
  181. to: "branch/Plus+Is+Not+Space/" + url.PathEscape("This+file%2520has 2 spaces"),
  182. status: http.StatusOK,
  183. },
  184. {
  185. from: "Plus+Is+Not+Space/" + url.PathEscape("£15&$6.txt"),
  186. to: "branch/Plus+Is+Not+Space/" + url.PathEscape("£15&$6.txt"),
  187. status: http.StatusOK,
  188. },
  189. }
  190. defer prepareTestEnv(t)()
  191. user := "user2"
  192. repo := "utf8"
  193. session := loginUser(t, user)
  194. setDefaultBranch(t, session, user, repo, "Plus+Is+Not+Space")
  195. for _, test := range testRedirects {
  196. testSrcRouteRedirect(t, session, user, repo, test.from, test.to, test.status)
  197. }
  198. setDefaultBranch(t, session, user, repo, "master")
  199. }