2018-09-06 19:37:02 -06:00
|
|
|
|
// Copyright 2018 The Gitea Authors. All rights reserved.
|
2022-11-27 11:20:29 -07:00
|
|
|
|
// SPDX-License-Identifier: MIT
|
2018-09-06 19:37:02 -06:00
|
|
|
|
|
2022-09-02 13:18:23 -06:00
|
|
|
|
package integration
|
2018-09-06 19:37:02 -06:00
|
|
|
|
|
|
|
|
|
import (
|
2024-06-17 17:28:47 -06:00
|
|
|
|
"fmt"
|
2018-09-06 19:37:02 -06:00
|
|
|
|
"net/http"
|
2021-12-16 10:40:18 -07:00
|
|
|
|
"net/url"
|
2018-09-06 19:37:02 -06:00
|
|
|
|
"path"
|
|
|
|
|
"testing"
|
|
|
|
|
|
2022-09-02 13:18:23 -06:00
|
|
|
|
"code.gitea.io/gitea/tests"
|
2022-09-05 00:04:18 -06:00
|
|
|
|
|
2018-09-06 19:37:02 -06:00
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func setDefaultBranch(t *testing.T, session *TestSession, user, repo, branch string) {
|
|
|
|
|
location := path.Join("/", user, repo, "settings/branches")
|
2024-10-09 21:48:21 -06:00
|
|
|
|
csrf := GetUserCSRFToken(t, session)
|
2018-09-06 19:37:02 -06:00
|
|
|
|
req := NewRequestWithValues(t, "POST", location, map[string]string{
|
|
|
|
|
"_csrf": csrf,
|
|
|
|
|
"action": "default_branch",
|
|
|
|
|
"branch": branch,
|
|
|
|
|
})
|
2022-03-22 22:54:07 -06:00
|
|
|
|
session.MakeRequest(t, req, http.StatusSeeOther)
|
2018-09-06 19:37:02 -06:00
|
|
|
|
}
|
|
|
|
|
|
2024-06-17 17:28:47 -06:00
|
|
|
|
func TestNonAsciiBranches(t *testing.T) {
|
2018-09-06 19:37:02 -06:00
|
|
|
|
testRedirects := []struct {
|
|
|
|
|
from string
|
|
|
|
|
to string
|
|
|
|
|
status int
|
|
|
|
|
}{
|
|
|
|
|
// Branches
|
|
|
|
|
{
|
|
|
|
|
from: "master",
|
|
|
|
|
to: "branch/master",
|
|
|
|
|
status: http.StatusOK,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
from: "master/README.md",
|
|
|
|
|
to: "branch/master/README.md",
|
|
|
|
|
status: http.StatusOK,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
from: "master/badfile",
|
|
|
|
|
to: "branch/master/badfile",
|
|
|
|
|
status: http.StatusNotFound, // it does not exists
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
from: "ГлавнаяВетка",
|
2021-11-16 11:18:25 -07:00
|
|
|
|
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",
|
2018-09-06 19:37:02 -06:00
|
|
|
|
status: http.StatusOK,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
from: "а/б/в",
|
2021-11-16 11:18:25 -07:00
|
|
|
|
to: "branch/%D0%B0/%D0%B1/%D0%B2",
|
2018-09-06 19:37:02 -06:00
|
|
|
|
status: http.StatusOK,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
from: "Grüßen/README.md",
|
2021-11-16 11:18:25 -07:00
|
|
|
|
to: "branch/Gr%C3%BC%C3%9Fen/README.md",
|
2018-09-06 19:37:02 -06:00
|
|
|
|
status: http.StatusOK,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
from: "Plus+Is+Not+Space",
|
|
|
|
|
to: "branch/Plus+Is+Not+Space",
|
|
|
|
|
status: http.StatusOK,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
from: "Plus+Is+Not+Space/Файл.md",
|
2021-11-16 11:18:25 -07:00
|
|
|
|
to: "branch/Plus+Is+Not+Space/%D0%A4%D0%B0%D0%B9%D0%BB.md",
|
2018-09-06 19:37:02 -06:00
|
|
|
|
status: http.StatusOK,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
from: "Plus+Is+Not+Space/and+it+is+valid.md",
|
|
|
|
|
to: "branch/Plus+Is+Not+Space/and+it+is+valid.md",
|
|
|
|
|
status: http.StatusOK,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
from: "ブランチ",
|
2021-11-16 11:18:25 -07:00
|
|
|
|
to: "branch/%E3%83%96%E3%83%A9%E3%83%B3%E3%83%81",
|
2018-09-06 19:37:02 -06:00
|
|
|
|
status: http.StatusOK,
|
|
|
|
|
},
|
2024-06-17 17:28:47 -06:00
|
|
|
|
|
2018-09-06 19:37:02 -06:00
|
|
|
|
// Tags
|
|
|
|
|
{
|
|
|
|
|
from: "Тэг",
|
2021-11-16 11:18:25 -07:00
|
|
|
|
to: "tag/%D0%A2%D1%8D%D0%B3",
|
2018-09-06 19:37:02 -06:00
|
|
|
|
status: http.StatusOK,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
from: "Ё/人",
|
2021-11-16 11:18:25 -07:00
|
|
|
|
to: "tag/%D0%81/%E4%BA%BA",
|
2018-09-06 19:37:02 -06:00
|
|
|
|
status: http.StatusOK,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
from: "タグ",
|
2021-11-16 11:18:25 -07:00
|
|
|
|
to: "tag/%E3%82%BF%E3%82%B0",
|
2018-09-06 19:37:02 -06:00
|
|
|
|
status: http.StatusOK,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
from: "タグ/ファイル.md",
|
2021-11-16 11:18:25 -07:00
|
|
|
|
to: "tag/%E3%82%BF%E3%82%B0/%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB.md",
|
2018-09-06 19:37:02 -06:00
|
|
|
|
status: http.StatusOK,
|
|
|
|
|
},
|
2024-06-17 17:28:47 -06:00
|
|
|
|
|
2018-09-06 19:37:02 -06:00
|
|
|
|
// Files
|
|
|
|
|
{
|
|
|
|
|
from: "README.md",
|
|
|
|
|
to: "branch/Plus+Is+Not+Space/README.md",
|
|
|
|
|
status: http.StatusOK,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
from: "Файл.md",
|
2021-11-16 11:18:25 -07:00
|
|
|
|
to: "branch/Plus+Is+Not+Space/%D0%A4%D0%B0%D0%B9%D0%BB.md",
|
2018-09-06 19:37:02 -06:00
|
|
|
|
status: http.StatusOK,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
from: "ファイル.md",
|
2021-11-16 11:18:25 -07:00
|
|
|
|
to: "branch/Plus+Is+Not+Space/%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB.md",
|
2018-09-06 19:37:02 -06:00
|
|
|
|
status: http.StatusNotFound, // it's not on default branch
|
|
|
|
|
},
|
2024-06-17 17:28:47 -06:00
|
|
|
|
|
2018-09-06 19:37:02 -06:00
|
|
|
|
// Same but url-encoded (few tests)
|
|
|
|
|
{
|
|
|
|
|
from: "%E3%83%96%E3%83%A9%E3%83%B3%E3%83%81",
|
2021-11-16 11:18:25 -07:00
|
|
|
|
to: "branch/%E3%83%96%E3%83%A9%E3%83%B3%E3%83%81",
|
2018-09-06 19:37:02 -06:00
|
|
|
|
status: http.StatusOK,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
from: "%E3%82%BF%E3%82%b0",
|
2021-11-16 11:18:25 -07:00
|
|
|
|
to: "tag/%E3%82%BF%E3%82%B0",
|
2018-09-06 19:37:02 -06:00
|
|
|
|
status: http.StatusOK,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
from: "%D0%A4%D0%B0%D0%B9%D0%BB.md",
|
2021-11-16 11:18:25 -07:00
|
|
|
|
to: "branch/Plus+Is+Not+Space/%D0%A4%D0%B0%D0%B9%D0%BB.md",
|
2018-09-06 19:37:02 -06:00
|
|
|
|
status: http.StatusOK,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
from: "%D0%81%2F%E4%BA%BA",
|
2021-11-16 11:18:25 -07:00
|
|
|
|
to: "tag/%D0%81/%E4%BA%BA",
|
2018-09-06 19:37:02 -06:00
|
|
|
|
status: http.StatusOK,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
from: "Ё%2F%E4%BA%BA",
|
2021-11-16 11:18:25 -07:00
|
|
|
|
to: "tag/%D0%81/%E4%BA%BA",
|
2018-09-06 19:37:02 -06:00
|
|
|
|
status: http.StatusOK,
|
|
|
|
|
},
|
2021-12-16 10:40:18 -07:00
|
|
|
|
{
|
|
|
|
|
from: "Plus+Is+Not+Space/%25%252525mightnotplaywell",
|
|
|
|
|
to: "branch/Plus+Is+Not+Space/%25%252525mightnotplaywell",
|
|
|
|
|
status: http.StatusOK,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
from: "Plus+Is+Not+Space/%25253Fisnotaquestion%25253F",
|
|
|
|
|
to: "branch/Plus+Is+Not+Space/%25253Fisnotaquestion%25253F",
|
|
|
|
|
status: http.StatusOK,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
from: "Plus+Is+Not+Space/" + url.PathEscape("%3Fis?and#afile"),
|
|
|
|
|
to: "branch/Plus+Is+Not+Space/" + url.PathEscape("%3Fis?and#afile"),
|
|
|
|
|
status: http.StatusOK,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
from: "Plus+Is+Not+Space/10%25.md",
|
|
|
|
|
to: "branch/Plus+Is+Not+Space/10%25.md",
|
|
|
|
|
status: http.StatusOK,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
from: "Plus+Is+Not+Space/" + url.PathEscape("This+file%20has 1space"),
|
|
|
|
|
to: "branch/Plus+Is+Not+Space/" + url.PathEscape("This+file%20has 1space"),
|
|
|
|
|
status: http.StatusOK,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
from: "Plus+Is+Not+Space/" + url.PathEscape("This+file%2520has 2 spaces"),
|
|
|
|
|
to: "branch/Plus+Is+Not+Space/" + url.PathEscape("This+file%2520has 2 spaces"),
|
|
|
|
|
status: http.StatusOK,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
from: "Plus+Is+Not+Space/" + url.PathEscape("£15&$6.txt"),
|
|
|
|
|
to: "branch/Plus+Is+Not+Space/" + url.PathEscape("£15&$6.txt"),
|
|
|
|
|
status: http.StatusOK,
|
|
|
|
|
},
|
2018-09-06 19:37:02 -06:00
|
|
|
|
}
|
|
|
|
|
|
2022-09-02 13:18:23 -06:00
|
|
|
|
defer tests.PrepareTestEnv(t)()
|
2018-09-06 19:37:02 -06:00
|
|
|
|
|
|
|
|
|
user := "user2"
|
|
|
|
|
repo := "utf8"
|
|
|
|
|
session := loginUser(t, user)
|
|
|
|
|
|
|
|
|
|
setDefaultBranch(t, session, user, repo, "Plus+Is+Not+Space")
|
2024-06-17 17:28:47 -06:00
|
|
|
|
defer setDefaultBranch(t, session, user, repo, "master")
|
2018-09-06 19:37:02 -06:00
|
|
|
|
|
|
|
|
|
for _, test := range testRedirects {
|
2024-06-17 17:28:47 -06:00
|
|
|
|
t.Run(test.from, func(t *testing.T) {
|
|
|
|
|
req := NewRequest(t, "GET", fmt.Sprintf("/%s/%s/src/%s", user, repo, test.from))
|
|
|
|
|
resp := session.MakeRequest(t, req, http.StatusSeeOther)
|
|
|
|
|
if resp.Code != http.StatusSeeOther {
|
|
|
|
|
return
|
|
|
|
|
}
|
2018-09-06 19:37:02 -06:00
|
|
|
|
|
2024-06-17 17:28:47 -06:00
|
|
|
|
redirectLocation := resp.Header().Get("Location")
|
|
|
|
|
if !assert.Equal(t, fmt.Sprintf("/%s/%s/src/%s", user, repo, test.to), redirectLocation) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
req = NewRequest(t, "GET", redirectLocation)
|
|
|
|
|
session.MakeRequest(t, req, test.status)
|
|
|
|
|
})
|
|
|
|
|
}
|
2018-09-06 19:37:02 -06:00
|
|
|
|
}
|