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.

178 lines
4.3 KiB

  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. "path"
  8. "testing"
  9. "github.com/stretchr/testify/assert"
  10. )
  11. func testSrcRouteRedirect(t *testing.T, session *TestSession, user, repo, route, expectedLocation string, expectedStatus int) {
  12. prefix := path.Join("/", user, repo, "src")
  13. // Make request
  14. req := NewRequest(t, "GET", path.Join(prefix, route))
  15. resp := session.MakeRequest(t, req, http.StatusFound)
  16. // Check Location header
  17. location := resp.HeaderMap.Get("Location")
  18. assert.Equal(t, path.Join(prefix, expectedLocation), location)
  19. // Perform redirect
  20. req = NewRequest(t, "GET", location)
  21. resp = session.MakeRequest(t, req, expectedStatus)
  22. }
  23. func setDefaultBranch(t *testing.T, session *TestSession, user, repo, branch string) {
  24. location := path.Join("/", user, repo, "settings/branches")
  25. csrf := GetCSRF(t, session, location)
  26. req := NewRequestWithValues(t, "POST", location, map[string]string{
  27. "_csrf": csrf,
  28. "action": "default_branch",
  29. "branch": branch,
  30. })
  31. session.MakeRequest(t, req, http.StatusFound)
  32. }
  33. func TestNonasciiBranches(t *testing.T) {
  34. testRedirects := []struct {
  35. from string
  36. to string
  37. status int
  38. }{
  39. // Branches
  40. {
  41. from: "master",
  42. to: "branch/master",
  43. status: http.StatusOK,
  44. },
  45. {
  46. from: "master/README.md",
  47. to: "branch/master/README.md",
  48. status: http.StatusOK,
  49. },
  50. {
  51. from: "master/badfile",
  52. to: "branch/master/badfile",
  53. status: http.StatusNotFound, // it does not exists
  54. },
  55. {
  56. from: "ГлавнаяВетка",
  57. 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",
  58. status: http.StatusOK,
  59. },
  60. {
  61. from: "а/б/в",
  62. to: "branch/%d0%b0/%d0%b1/%d0%b2",
  63. status: http.StatusOK,
  64. },
  65. {
  66. from: "Grüßen/README.md",
  67. to: "branch/Gr%c3%bc%c3%9fen/README.md",
  68. status: http.StatusOK,
  69. },
  70. {
  71. from: "Plus+Is+Not+Space",
  72. to: "branch/Plus+Is+Not+Space",
  73. status: http.StatusOK,
  74. },
  75. {
  76. from: "Plus+Is+Not+Space/Файл.md",
  77. to: "branch/Plus+Is+Not+Space/%d0%a4%d0%b0%d0%b9%d0%bb.md",
  78. status: http.StatusOK,
  79. },
  80. {
  81. from: "Plus+Is+Not+Space/and+it+is+valid.md",
  82. to: "branch/Plus+Is+Not+Space/and+it+is+valid.md",
  83. status: http.StatusOK,
  84. },
  85. {
  86. from: "ブランチ",
  87. to: "branch/%e3%83%96%e3%83%a9%e3%83%b3%e3%83%81",
  88. status: http.StatusOK,
  89. },
  90. // Tags
  91. {
  92. from: "Тэг",
  93. to: "tag/%d0%a2%d1%8d%d0%b3",
  94. status: http.StatusOK,
  95. },
  96. {
  97. from: "Ё/人",
  98. to: "tag/%d0%81/%e4%ba%ba",
  99. status: http.StatusOK,
  100. },
  101. {
  102. from: "タグ",
  103. to: "tag/%e3%82%bf%e3%82%b0",
  104. status: http.StatusOK,
  105. },
  106. {
  107. from: "タグ/ファイル.md",
  108. to: "tag/%e3%82%bf%e3%82%b0/%e3%83%95%e3%82%a1%e3%82%a4%e3%83%ab.md",
  109. status: http.StatusOK,
  110. },
  111. // Files
  112. {
  113. from: "README.md",
  114. to: "branch/Plus+Is+Not+Space/README.md",
  115. status: http.StatusOK,
  116. },
  117. {
  118. from: "Файл.md",
  119. to: "branch/Plus+Is+Not+Space/%d0%a4%d0%b0%d0%b9%d0%bb.md",
  120. status: http.StatusOK,
  121. },
  122. {
  123. from: "ファイル.md",
  124. to: "branch/Plus+Is+Not+Space/%e3%83%95%e3%82%a1%e3%82%a4%e3%83%ab.md",
  125. status: http.StatusNotFound, // it's not on default branch
  126. },
  127. // Same but url-encoded (few tests)
  128. {
  129. from: "%E3%83%96%E3%83%A9%E3%83%B3%E3%83%81",
  130. to: "branch/%e3%83%96%e3%83%a9%e3%83%b3%e3%83%81",
  131. status: http.StatusOK,
  132. },
  133. {
  134. from: "%E3%82%BF%E3%82%b0",
  135. to: "tag/%e3%82%bf%e3%82%b0",
  136. status: http.StatusOK,
  137. },
  138. {
  139. from: "%D0%A4%D0%B0%D0%B9%D0%BB.md",
  140. to: "branch/Plus+Is+Not+Space/%d0%a4%d0%b0%d0%b9%d0%bb.md",
  141. status: http.StatusOK,
  142. },
  143. {
  144. from: "%D0%81%2F%E4%BA%BA",
  145. to: "tag/%d0%81/%e4%ba%ba",
  146. status: http.StatusOK,
  147. },
  148. {
  149. from: "Ё%2F%E4%BA%BA",
  150. to: "tag/%d0%81/%e4%ba%ba",
  151. status: http.StatusOK,
  152. },
  153. }
  154. defer prepareTestEnv(t)()
  155. user := "user2"
  156. repo := "utf8"
  157. session := loginUser(t, user)
  158. setDefaultBranch(t, session, user, repo, "Plus+Is+Not+Space")
  159. for _, test := range testRedirects {
  160. testSrcRouteRedirect(t, session, user, repo, test.from, test.to, test.status)
  161. }
  162. setDefaultBranch(t, session, user, repo, "master")
  163. }