From 4e2f1ee58d1aa49f85c3a28a4f96e915f12bdb21 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Sun, 18 Jun 2023 15:59:09 +0800 Subject: Refactor web package and context package (#25298) 1. The "web" package shouldn't depends on "modules/context" package, instead, let each "web context" register themselves to the "web" package. 2. The old Init/Free doesn't make sense, so simplify it * The ctx in "Init(ctx)" is never used, and shouldn't be used that way * The "Free" is never called and shouldn't be called because the SSPI instance is shared --------- Co-authored-by: Giteabot --- modules/test/context_tests.go | 42 ++++++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 20 deletions(-) (limited to 'modules/test') diff --git a/modules/test/context_tests.go b/modules/test/context_tests.go index 349c7e3e80..cf8af32fc6 100644 --- a/modules/test/context_tests.go +++ b/modules/test/context_tests.go @@ -9,6 +9,7 @@ import ( "net/http" "net/http/httptest" "net/url" + "strings" "testing" access_model "code.gitea.io/gitea/models/perm/access" @@ -25,19 +26,26 @@ import ( "github.com/stretchr/testify/assert" ) -// MockContext mock context for unit tests -// TODO: move this function to other packages, because it depends on "models" package -func MockContext(t *testing.T, path string) *context.Context { - resp := httptest.NewRecorder() +func mockRequest(t *testing.T, reqPath string) *http.Request { + method, path, found := strings.Cut(reqPath, " ") + if !found { + method = "GET" + path = reqPath + } requestURL, err := url.Parse(path) assert.NoError(t, err) - req := &http.Request{ - URL: requestURL, - Form: url.Values{}, - } + req := &http.Request{Method: method, URL: requestURL, Form: url.Values{}} + req = req.WithContext(middleware.WithContextData(req.Context())) + return req +} +// MockContext mock context for unit tests +// TODO: move this function to other packages, because it depends on "models" package +func MockContext(t *testing.T, reqPath string) (*context.Context, *httptest.ResponseRecorder) { + resp := httptest.NewRecorder() + req := mockRequest(t, reqPath) base, baseCleanUp := context.NewBaseContext(resp, req) - base.Data = middleware.ContextData{} + base.Data = middleware.GetContextData(req.Context()) base.Locale = &translation.MockLocale{} ctx := &context.Context{ Base: base, @@ -48,29 +56,23 @@ func MockContext(t *testing.T, path string) *context.Context { chiCtx := chi.NewRouteContext() ctx.Base.AppendContextValue(chi.RouteCtxKey, chiCtx) - return ctx + return ctx, resp } // MockAPIContext mock context for unit tests // TODO: move this function to other packages, because it depends on "models" package -func MockAPIContext(t *testing.T, path string) *context.APIContext { +func MockAPIContext(t *testing.T, reqPath string) (*context.APIContext, *httptest.ResponseRecorder) { resp := httptest.NewRecorder() - requestURL, err := url.Parse(path) - assert.NoError(t, err) - req := &http.Request{ - URL: requestURL, - Form: url.Values{}, - } - + req := mockRequest(t, reqPath) base, baseCleanUp := context.NewBaseContext(resp, req) - base.Data = middleware.ContextData{} + base.Data = middleware.GetContextData(req.Context()) base.Locale = &translation.MockLocale{} ctx := &context.APIContext{Base: base} _ = baseCleanUp // during test, it doesn't need to do clean up. TODO: this can be improved later chiCtx := chi.NewRouteContext() ctx.Base.AppendContextValue(chi.RouteCtxKey, chiCtx) - return ctx + return ctx, resp } // LoadRepo load a repo into a test context. -- cgit v1.2.3