2021-01-26 23:36:53 +08:00
|
|
|
// Copyright 2021 The Gitea Authors. All rights reserved.
|
2022-11-27 13:20:29 -05:00
|
|
|
// SPDX-License-Identifier: MIT
|
2021-01-26 23:36:53 +08:00
|
|
|
|
|
|
|
package web
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2024-12-25 12:03:14 +08:00
|
|
|
"strings"
|
2021-01-26 23:36:53 +08:00
|
|
|
"testing"
|
|
|
|
|
2024-06-18 07:28:47 +08:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
|
|
|
"code.gitea.io/gitea/modules/test"
|
2024-12-25 12:03:14 +08:00
|
|
|
"code.gitea.io/gitea/modules/util"
|
2024-06-18 07:28:47 +08:00
|
|
|
|
2024-12-25 12:03:14 +08:00
|
|
|
"github.com/go-chi/chi/v5"
|
2021-01-26 23:36:53 +08:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2024-12-25 12:03:14 +08:00
|
|
|
func chiURLParamsToMap(chiCtx *chi.Context) map[string]string {
|
|
|
|
pathParams := chiCtx.URLParams
|
|
|
|
m := make(map[string]string, len(pathParams.Keys))
|
|
|
|
for i, key := range pathParams.Keys {
|
|
|
|
if key == "*" && pathParams.Values[i] == "" {
|
|
|
|
continue // chi router will add an empty "*" key if there is a "Mount"
|
|
|
|
}
|
|
|
|
m[key] = pathParams.Values[i]
|
|
|
|
}
|
|
|
|
return m
|
|
|
|
}
|
2021-01-26 23:36:53 +08:00
|
|
|
|
2024-12-25 12:03:14 +08:00
|
|
|
func TestPathProcessor(t *testing.T) {
|
|
|
|
testProcess := func(pattern, uri string, expectedPathParams map[string]string) {
|
|
|
|
chiCtx := chi.NewRouteContext()
|
|
|
|
chiCtx.RouteMethod = "GET"
|
|
|
|
p := NewPathProcessor("GET", pattern)
|
|
|
|
assert.True(t, p.ProcessRequestPath(chiCtx, uri), "use pattern %s to process uri %s", pattern, uri)
|
|
|
|
assert.Equal(t, expectedPathParams, chiURLParamsToMap(chiCtx), "use pattern %s to process uri %s", pattern, uri)
|
|
|
|
}
|
|
|
|
testProcess("/<p1>/<p2>", "/a/b", map[string]string{"p1": "a", "p2": "b"})
|
|
|
|
testProcess("/<p1:*>", "", map[string]string{"p1": ""}) // this is a special case, because chi router could use empty path
|
|
|
|
testProcess("/<p1:*>", "/", map[string]string{"p1": ""})
|
|
|
|
testProcess("/<p1:*>/<p2>", "/a", map[string]string{"p1": "", "p2": "a"})
|
|
|
|
testProcess("/<p1:*>/<p2>", "/a/b", map[string]string{"p1": "a", "p2": "b"})
|
|
|
|
testProcess("/<p1:*>/<p2>", "/a/b/c", map[string]string{"p1": "a/b", "p2": "c"})
|
2021-01-26 23:36:53 +08:00
|
|
|
}
|
|
|
|
|
2024-12-25 12:03:14 +08:00
|
|
|
func TestRouter(t *testing.T) {
|
2021-01-26 23:36:53 +08:00
|
|
|
buff := bytes.NewBufferString("")
|
|
|
|
recorder := httptest.NewRecorder()
|
|
|
|
recorder.Body = buff
|
|
|
|
|
2024-12-25 12:03:14 +08:00
|
|
|
type resultStruct struct {
|
|
|
|
method string
|
|
|
|
pathParams map[string]string
|
|
|
|
handlerMark string
|
|
|
|
}
|
|
|
|
var res resultStruct
|
|
|
|
|
|
|
|
h := func(optMark ...string) func(resp http.ResponseWriter, req *http.Request) {
|
|
|
|
mark := util.OptionalArg(optMark, "")
|
|
|
|
return func(resp http.ResponseWriter, req *http.Request) {
|
|
|
|
res.method = req.Method
|
|
|
|
res.pathParams = chiURLParamsToMap(chi.RouteContext(req.Context()))
|
|
|
|
res.handlerMark = mark
|
|
|
|
}
|
|
|
|
}
|
2021-01-26 23:36:53 +08:00
|
|
|
|
2024-06-19 06:32:45 +08:00
|
|
|
r := NewRouter()
|
2024-12-25 12:03:14 +08:00
|
|
|
r.Get("/{username}/{reponame}/{type:issues|pulls}", h("list-issues-a")) // this one will never be called
|
2021-01-26 23:36:53 +08:00
|
|
|
r.Group("/{username}/{reponame}", func() {
|
2024-12-25 12:03:14 +08:00
|
|
|
r.Get("/{type:issues|pulls}", h("list-issues-b"))
|
2021-01-26 23:36:53 +08:00
|
|
|
r.Group("", func() {
|
2024-12-25 12:03:14 +08:00
|
|
|
r.Get("/{type:issues|pulls}/{index}", h("view-issue"))
|
2021-01-26 23:36:53 +08:00
|
|
|
}, func(resp http.ResponseWriter, req *http.Request) {
|
2024-12-25 12:03:14 +08:00
|
|
|
if stop := req.FormValue("stop"); stop != "" {
|
|
|
|
h(stop)(resp, req)
|
Refactor web route (#24080)
The old code is unnecessarily complex, and has many misuses.
Old code "wraps" a lot, wrap wrap wrap, it's difficult to understand
which kind of handler is used.
The new code uses a general approach, we do not need to write all kinds
of handlers into the "wrapper", do not need to wrap them again and
again.
New code, there are only 2 concepts:
1. HandlerProvider: `func (h any) (handlerProvider func (next)
http.Handler)`, it can be used as middleware
2. Use HandlerProvider to get the final HandlerFunc, and use it for
`r.Get()`
And we can decouple the route package from context package (see the
TODO).
# FAQ
## Is `reflect` safe?
Yes, all handlers are checked during startup, see the `preCheckHandler`
comment. If any handler is wrong, developers could know it in the first
time.
## Does `reflect` affect performance?
No. https://github.com/go-gitea/gitea/pull/24080#discussion_r1164825901
1. This reflect code only runs for each web handler call, handler is far
more slower: 10ms-50ms
2. The reflect is pretty fast (comparing to other code): 0.000265ms
3. XORM has more reflect operations already
2023-04-21 02:49:06 +08:00
|
|
|
resp.WriteHeader(http.StatusOK)
|
|
|
|
}
|
2021-01-26 23:36:53 +08:00
|
|
|
})
|
|
|
|
r.Group("/issues/{index}", func() {
|
2024-12-25 12:03:14 +08:00
|
|
|
r.Post("/update", h("update-issue"))
|
2021-01-26 23:36:53 +08:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2024-06-19 06:32:45 +08:00
|
|
|
m := NewRouter()
|
2021-01-26 23:36:53 +08:00
|
|
|
r.Mount("/api/v1", m)
|
|
|
|
m.Group("/repos", func() {
|
|
|
|
m.Group("/{username}/{reponame}", func() {
|
2024-12-25 12:03:14 +08:00
|
|
|
m.Group("/branches", func() {
|
|
|
|
m.Get("", h())
|
|
|
|
m.Post("", h())
|
2021-01-26 23:36:53 +08:00
|
|
|
m.Group("/{name}", func() {
|
2024-12-25 12:03:14 +08:00
|
|
|
m.Get("", h())
|
|
|
|
m.Patch("", h())
|
|
|
|
m.Delete("", h())
|
2021-01-26 23:36:53 +08:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2024-12-25 12:03:14 +08:00
|
|
|
testRoute := func(methodPath string, expected resultStruct) {
|
|
|
|
t.Run(methodPath, func(t *testing.T) {
|
|
|
|
res = resultStruct{}
|
|
|
|
methodPathFields := strings.Fields(methodPath)
|
|
|
|
req, err := http.NewRequest(methodPathFields[0], methodPathFields[1], nil)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
r.ServeHTTP(recorder, req)
|
|
|
|
assert.EqualValues(t, expected, res)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("Root Router", func(t *testing.T) {
|
|
|
|
testRoute("GET /the-user/the-repo/other", resultStruct{})
|
|
|
|
testRoute("GET /the-user/the-repo/pulls", resultStruct{
|
|
|
|
method: "GET",
|
|
|
|
pathParams: map[string]string{"username": "the-user", "reponame": "the-repo", "type": "pulls"},
|
|
|
|
handlerMark: "list-issues-b",
|
|
|
|
})
|
|
|
|
testRoute("GET /the-user/the-repo/issues/123", resultStruct{
|
|
|
|
method: "GET",
|
|
|
|
pathParams: map[string]string{"username": "the-user", "reponame": "the-repo", "type": "issues", "index": "123"},
|
|
|
|
handlerMark: "view-issue",
|
|
|
|
})
|
|
|
|
testRoute("GET /the-user/the-repo/issues/123?stop=hijack", resultStruct{
|
|
|
|
method: "GET",
|
|
|
|
pathParams: map[string]string{"username": "the-user", "reponame": "the-repo", "type": "issues", "index": "123"},
|
|
|
|
handlerMark: "hijack",
|
|
|
|
})
|
|
|
|
testRoute("POST /the-user/the-repo/issues/123/update", resultStruct{
|
|
|
|
method: "POST",
|
|
|
|
pathParams: map[string]string{"username": "the-user", "reponame": "the-repo", "index": "123"},
|
|
|
|
handlerMark: "update-issue",
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Sub Router", func(t *testing.T) {
|
|
|
|
testRoute("GET /api/v1/repos/the-user/the-repo/branches", resultStruct{
|
|
|
|
method: "GET",
|
|
|
|
pathParams: map[string]string{"username": "the-user", "reponame": "the-repo"},
|
|
|
|
})
|
|
|
|
|
|
|
|
testRoute("POST /api/v1/repos/the-user/the-repo/branches", resultStruct{
|
|
|
|
method: "POST",
|
|
|
|
pathParams: map[string]string{"username": "the-user", "reponame": "the-repo"},
|
|
|
|
})
|
|
|
|
|
|
|
|
testRoute("GET /api/v1/repos/the-user/the-repo/branches/master", resultStruct{
|
|
|
|
method: "GET",
|
|
|
|
pathParams: map[string]string{"username": "the-user", "reponame": "the-repo", "name": "master"},
|
|
|
|
})
|
|
|
|
|
|
|
|
testRoute("PATCH /api/v1/repos/the-user/the-repo/branches/master", resultStruct{
|
|
|
|
method: "PATCH",
|
|
|
|
pathParams: map[string]string{"username": "the-user", "reponame": "the-repo", "name": "master"},
|
|
|
|
})
|
|
|
|
|
|
|
|
testRoute("DELETE /api/v1/repos/the-user/the-repo/branches/master", resultStruct{
|
|
|
|
method: "DELETE",
|
|
|
|
pathParams: map[string]string{"username": "the-user", "reponame": "the-repo", "name": "master"},
|
|
|
|
})
|
|
|
|
})
|
2021-01-26 23:36:53 +08:00
|
|
|
}
|
2024-06-18 07:28:47 +08:00
|
|
|
|
|
|
|
func TestRouteNormalizePath(t *testing.T) {
|
|
|
|
type paths struct {
|
|
|
|
EscapedPath, RawPath, Path string
|
|
|
|
}
|
|
|
|
testPath := func(reqPath string, expectedPaths paths) {
|
|
|
|
recorder := httptest.NewRecorder()
|
|
|
|
recorder.Body = bytes.NewBuffer(nil)
|
|
|
|
|
|
|
|
actualPaths := paths{EscapedPath: "(none)", RawPath: "(none)", Path: "(none)"}
|
2024-06-19 06:32:45 +08:00
|
|
|
r := NewRouter()
|
2024-06-18 07:28:47 +08:00
|
|
|
r.Get("/*", func(resp http.ResponseWriter, req *http.Request) {
|
|
|
|
actualPaths.EscapedPath = req.URL.EscapedPath()
|
|
|
|
actualPaths.RawPath = req.URL.RawPath
|
|
|
|
actualPaths.Path = req.URL.Path
|
|
|
|
})
|
|
|
|
|
|
|
|
req, err := http.NewRequest("GET", reqPath, nil)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
r.ServeHTTP(recorder, req)
|
|
|
|
assert.Equal(t, expectedPaths, actualPaths, "req path = %q", reqPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RawPath could be empty if the EscapedPath is the same as escape(Path) and it is already normalized
|
|
|
|
testPath("/", paths{EscapedPath: "/", RawPath: "", Path: "/"})
|
|
|
|
testPath("//", paths{EscapedPath: "/", RawPath: "/", Path: "/"})
|
|
|
|
testPath("/%2f", paths{EscapedPath: "/%2f", RawPath: "/%2f", Path: "//"})
|
|
|
|
testPath("///a//b/", paths{EscapedPath: "/a/b", RawPath: "/a/b", Path: "/a/b"})
|
|
|
|
|
|
|
|
defer test.MockVariableValue(&setting.UseSubURLPath, true)()
|
|
|
|
defer test.MockVariableValue(&setting.AppSubURL, "/sub-path")()
|
|
|
|
testPath("/", paths{EscapedPath: "(none)", RawPath: "(none)", Path: "(none)"}) // 404
|
|
|
|
testPath("/sub-path", paths{EscapedPath: "/", RawPath: "/", Path: "/"})
|
|
|
|
testPath("/sub-path/", paths{EscapedPath: "/", RawPath: "/", Path: "/"})
|
|
|
|
testPath("/sub-path//a/b///", paths{EscapedPath: "/a/b", RawPath: "/a/b", Path: "/a/b"})
|
|
|
|
testPath("/sub-path/%2f/", paths{EscapedPath: "/%2f", RawPath: "/%2f", Path: "//"})
|
|
|
|
// "/v2" is special for OCI container registry, it should always be in the root of the site
|
|
|
|
testPath("/v2", paths{EscapedPath: "/v2", RawPath: "/v2", Path: "/v2"})
|
|
|
|
testPath("/v2/", paths{EscapedPath: "/v2", RawPath: "/v2", Path: "/v2"})
|
|
|
|
testPath("/v2/%2f", paths{EscapedPath: "/v2/%2f", RawPath: "/v2/%2f", Path: "/v2//"})
|
|
|
|
}
|