Skip to content

Commit cda6478

Browse files
committed
fix test
1 parent 516ff5b commit cda6478

File tree

4 files changed

+50
-30
lines changed

4 files changed

+50
-30
lines changed

routers/common/middleware.go

+14
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,24 @@ import (
1919

2020
"gitea.com/go-chi/session"
2121
"github.com/chi-middleware/proxy"
22+
"github.com/go-chi/chi/v5"
2223
)
2324

2425
// ProtocolMiddlewares returns HTTP protocol related middlewares, and it provides a global panic recovery
2526
func ProtocolMiddlewares() (handlers []any) {
27+
// make sure chi uses EscapedPath(RawPath) as RoutePath, then "%2f" could be handled correctly
28+
handlers = append(handlers, func(next http.Handler) http.Handler {
29+
return http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
30+
ctx := chi.RouteContext(req.Context())
31+
if req.URL.RawPath == "" {
32+
ctx.RoutePath = req.URL.EscapedPath()
33+
} else {
34+
ctx.RoutePath = req.URL.RawPath
35+
}
36+
next.ServeHTTP(resp, req)
37+
})
38+
})
39+
2640
// prepare the ContextData and panic recovery
2741
handlers = append(handlers, func(next http.Handler) http.Handler {
2842
return http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {

services/context/base.go

+12-7
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"code.gitea.io/gitea/modules/json"
1919
"code.gitea.io/gitea/modules/log"
2020
"code.gitea.io/gitea/modules/optional"
21+
"code.gitea.io/gitea/modules/setting"
2122
"code.gitea.io/gitea/modules/translation"
2223
"code.gitea.io/gitea/modules/web/middleware"
2324

@@ -142,23 +143,27 @@ func (b *Base) RemoteAddr() string {
142143
return b.Req.RemoteAddr
143144
}
144145

145-
// Params returns the param on route
146-
func (b *Base) Params(p string) string {
147-
s, _ := url.PathUnescape(chi.URLParam(b.Req, strings.TrimPrefix(p, ":")))
146+
// Params returns the param in request path, eg: "/{var}" => "/a%2fb", then `var == "a/b"`
147+
func (b *Base) Params(name string) string {
148+
s, err := url.PathUnescape(b.PathParamRaw(name))
149+
if err != nil && !setting.IsProd {
150+
panic("Failed to unescape path param: " + err.Error() + ", there seems to be a double-unescaping bug")
151+
}
148152
return s
149153
}
150154

151-
func (b *Base) PathParamRaw(p string) string {
152-
return chi.URLParam(b.Req, strings.TrimPrefix(p, ":"))
155+
// PathParamRaw returns the raw param in request path, eg: "/{var}" => "/a%2fb", then `var == "a%2fb"`
156+
func (b *Base) PathParamRaw(name string) string {
157+
return chi.URLParam(b.Req, strings.TrimPrefix(name, ":"))
153158
}
154159

155-
// ParamsInt64 returns the param on route as int64
160+
// ParamsInt64 returns the param in request path as int64
156161
func (b *Base) ParamsInt64(p string) int64 {
157162
v, _ := strconv.ParseInt(b.Params(p), 10, 64)
158163
return v
159164
}
160165

161-
// SetParams set params into routes
166+
// SetParams set request path params into routes
162167
func (b *Base) SetParams(k, v string) {
163168
chiCtx := chi.RouteContext(b)
164169
chiCtx.URLParams.Add(strings.TrimPrefix(k, ":"), url.PathEscape(v))

services/context/repo.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -1006,12 +1006,12 @@ func RepoRefByType(refType RepoRefType, ignoreNotExistErr ...bool) func(*Context
10061006
if refType == RepoRefLegacy {
10071007
// redirect from old URL scheme to new URL scheme
10081008
prefix := strings.TrimPrefix(setting.AppSubURL+strings.ToLower(strings.TrimSuffix(ctx.Req.URL.Path, ctx.Params("*"))), strings.ToLower(ctx.Repo.RepoLink))
1009-
1010-
ctx.Redirect(path.Join(
1009+
redirect := path.Join(
10111010
ctx.Repo.RepoLink,
10121011
util.PathEscapeSegments(prefix),
10131012
ctx.Repo.BranchNameSubURL(),
1014-
util.PathEscapeSegments(ctx.Repo.TreePath)))
1013+
util.PathEscapeSegments(ctx.Repo.TreePath))
1014+
ctx.Redirect(redirect)
10151015
return cancel
10161016
}
10171017
}

tests/integration/nonascii_branches_test.go

+21-20
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package integration
55

66
import (
7+
"fmt"
78
"net/http"
89
"net/url"
910
"path"
@@ -14,22 +15,6 @@ import (
1415
"github.com/stretchr/testify/assert"
1516
)
1617

17-
func testSrcRouteRedirect(t *testing.T, session *TestSession, user, repo, route, expectedLocation string, expectedStatus int) {
18-
prefix := path.Join("/", user, repo, "src")
19-
20-
// Make request
21-
req := NewRequest(t, "GET", path.Join(prefix, route))
22-
resp := session.MakeRequest(t, req, http.StatusSeeOther)
23-
24-
// Check Location header
25-
location := resp.Header().Get("Location")
26-
assert.Equal(t, path.Join(prefix, expectedLocation), location)
27-
28-
// Perform redirect
29-
req = NewRequest(t, "GET", location)
30-
session.MakeRequest(t, req, expectedStatus)
31-
}
32-
3318
func setDefaultBranch(t *testing.T, session *TestSession, user, repo, branch string) {
3419
location := path.Join("/", user, repo, "settings/branches")
3520
csrf := GetCSRF(t, session, location)
@@ -41,7 +26,7 @@ func setDefaultBranch(t *testing.T, session *TestSession, user, repo, branch str
4126
session.MakeRequest(t, req, http.StatusSeeOther)
4227
}
4328

44-
func TestNonasciiBranches(t *testing.T) {
29+
func TestNonAsciiBranches(t *testing.T) {
4530
testRedirects := []struct {
4631
from string
4732
to string
@@ -98,6 +83,7 @@ func TestNonasciiBranches(t *testing.T) {
9883
to: "branch/%E3%83%96%E3%83%A9%E3%83%B3%E3%83%81",
9984
status: http.StatusOK,
10085
},
86+
10187
// Tags
10288
{
10389
from: "Тэг",
@@ -119,6 +105,7 @@ func TestNonasciiBranches(t *testing.T) {
119105
to: "tag/%E3%82%BF%E3%82%B0/%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB.md",
120106
status: http.StatusOK,
121107
},
108+
122109
// Files
123110
{
124111
from: "README.md",
@@ -135,6 +122,7 @@ func TestNonasciiBranches(t *testing.T) {
135122
to: "branch/Plus+Is+Not+Space/%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB.md",
136123
status: http.StatusNotFound, // it's not on default branch
137124
},
125+
138126
// Same but url-encoded (few tests)
139127
{
140128
from: "%E3%83%96%E3%83%A9%E3%83%B3%E3%83%81",
@@ -205,10 +193,23 @@ func TestNonasciiBranches(t *testing.T) {
205193
session := loginUser(t, user)
206194

207195
setDefaultBranch(t, session, user, repo, "Plus+Is+Not+Space")
196+
defer setDefaultBranch(t, session, user, repo, "master")
208197

209198
for _, test := range testRedirects {
210-
testSrcRouteRedirect(t, session, user, repo, test.from, test.to, test.status)
211-
}
199+
t.Run(test.from, func(t *testing.T) {
200+
req := NewRequest(t, "GET", fmt.Sprintf("/%s/%s/src/%s", user, repo, test.from))
201+
resp := session.MakeRequest(t, req, http.StatusSeeOther)
202+
if resp.Code != http.StatusSeeOther {
203+
return
204+
}
212205

213-
setDefaultBranch(t, session, user, repo, "master")
206+
redirectLocation := resp.Header().Get("Location")
207+
if !assert.Equal(t, fmt.Sprintf("/%s/%s/src/%s", user, repo, test.to), redirectLocation) {
208+
return
209+
}
210+
211+
req = NewRequest(t, "GET", redirectLocation)
212+
session.MakeRequest(t, req, test.status)
213+
})
214+
}
214215
}

0 commit comments

Comments
 (0)