Decouple the different contexts from each other (#24786)

Replace #16455

Close #21803

Mixing different Gitea contexts together causes some problems:

1. Unable to respond proper content when error occurs, eg: Web should
respond HTML while API should respond JSON
2. Unclear dependency, eg: it's unclear when Context is used in
APIContext, which fields should be initialized, which methods are
necessary.


To make things clear, this PR introduces a Base context, it only
provides basic Req/Resp/Data features.

This PR mainly moves code. There are still many legacy problems and
TODOs in code, leave unrelated changes to future PRs.
This commit is contained in:
wxiaoguang 2023-05-21 09:50:53 +08:00 committed by GitHub
parent 6ba4f89723
commit 6b33152b7d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
57 changed files with 885 additions and 781 deletions

View file

@ -16,7 +16,6 @@ import (
"code.gitea.io/gitea/modules/markup"
"code.gitea.io/gitea/modules/setting"
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/templates"
"code.gitea.io/gitea/modules/util"
"code.gitea.io/gitea/modules/web"
"code.gitea.io/gitea/modules/web/middleware"
@ -30,26 +29,16 @@ const (
AppSubURL = AppURL + Repo + "/"
)
func createContext(req *http.Request) (*context.Context, *httptest.ResponseRecorder) {
rnd := templates.HTMLRenderer()
func createAPIContext(req *http.Request) (*context.APIContext, *httptest.ResponseRecorder) {
resp := httptest.NewRecorder()
c := &context.Context{
Req: req,
Resp: context.NewResponse(resp),
Render: rnd,
Data: make(middleware.ContextData),
}
defer c.Close()
base, baseCleanUp := context.NewBaseContext(resp, req)
base.Data = middleware.ContextData{}
c := &context.APIContext{Base: base}
_ = baseCleanUp // during test, it doesn't need to do clean up. TODO: this can be improved later
return c, resp
}
func wrap(ctx *context.Context) *context.APIContext {
return &context.APIContext{
Context: ctx,
}
}
func testRenderMarkup(t *testing.T, mode, filePath, text, responseBody string, responseCode int) {
setting.AppURL = AppURL
@ -65,8 +54,7 @@ func testRenderMarkup(t *testing.T, mode, filePath, text, responseBody string, r
Method: "POST",
URL: requrl,
}
m, resp := createContext(req)
ctx := wrap(m)
ctx, resp := createAPIContext(req)
options.Text = text
web.SetForm(ctx, &options)
@ -90,8 +78,7 @@ func testRenderMarkdown(t *testing.T, mode, text, responseBody string, responseC
Method: "POST",
URL: requrl,
}
m, resp := createContext(req)
ctx := wrap(m)
ctx, resp := createAPIContext(req)
options.Text = text
web.SetForm(ctx, &options)
@ -211,8 +198,7 @@ func TestAPI_RenderSimple(t *testing.T) {
Method: "POST",
URL: requrl,
}
m, resp := createContext(req)
ctx := wrap(m)
ctx, resp := createAPIContext(req)
for i := 0; i < len(simpleCases); i += 2 {
options.Text = simpleCases[i]
@ -231,8 +217,7 @@ func TestAPI_RenderRaw(t *testing.T) {
Method: "POST",
URL: requrl,
}
m, resp := createContext(req)
ctx := wrap(m)
ctx, resp := createAPIContext(req)
for i := 0; i < len(simpleCases); i += 2 {
ctx.Req.Body = io.NopCloser(strings.NewReader(simpleCases[i]))