summaryrefslogtreecommitdiffstats
path: root/modules/test
diff options
context:
space:
mode:
authorwxiaoguang <wxiaoguang@gmail.com>2023-06-18 15:59:09 +0800
committerGitHub <noreply@github.com>2023-06-18 09:59:09 +0200
commit4e2f1ee58d1aa49f85c3a28a4f96e915f12bdb21 (patch)
treee2751e1c4673f6a051ef04ca156f428a819fdb66 /modules/test
parentfc2115b494e9ba7e4cf7a1440404dce53738b514 (diff)
downloadgitea-4e2f1ee58d1aa49f85c3a28a4f96e915f12bdb21.tar.gz
gitea-4e2f1ee58d1aa49f85c3a28a4f96e915f12bdb21.zip
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 <teabot@gitea.io>
Diffstat (limited to 'modules/test')
-rw-r--r--modules/test/context_tests.go42
1 files changed, 22 insertions, 20 deletions
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.