diff options
author | wxiaoguang <wxiaoguang@gmail.com> | 2023-08-25 19:07:42 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-08-25 19:07:42 +0800 |
commit | 412e5c0946fc5b83456685bc2fb1aed682228f57 (patch) | |
tree | 1dc1a823618d554f740cff449e20c1538ad95ba3 /modules/test/context_tests.go | |
parent | ee9e83b230981437acf8c331e93a41cdc95af443 (diff) | |
download | gitea-412e5c0946fc5b83456685bc2fb1aed682228f57.tar.gz gitea-412e5c0946fc5b83456685bc2fb1aed682228f57.zip |
Make web context initialize correctly for different cases (#26726)
The web context (modules/context.Context) is quite complex, it's
difficult for the callers to initialize correctly.
This PR introduces a `NewWebContext` function, to make sure the web
context have the same behavior for different cases.
Diffstat (limited to 'modules/test/context_tests.go')
-rw-r--r-- | modules/test/context_tests.go | 16 |
1 files changed, 7 insertions, 9 deletions
diff --git a/modules/test/context_tests.go b/modules/test/context_tests.go index 92d7f8b22b..83e6117bcf 100644 --- a/modules/test/context_tests.go +++ b/modules/test/context_tests.go @@ -45,14 +45,12 @@ func MockContext(t *testing.T, reqPath string) (*context.Context, *httptest.Resp resp := httptest.NewRecorder() req := mockRequest(t, reqPath) base, baseCleanUp := context.NewBaseContext(resp, req) + _ = baseCleanUp // during test, it doesn't need to do clean up. TODO: this can be improved later base.Data = middleware.GetContextData(req.Context()) base.Locale = &translation.MockLocale{} - ctx := &context.Context{ - Base: base, - Render: &mockRender{}, - Flash: &middleware.Flash{Values: url.Values{}}, - } - _ = baseCleanUp // during test, it doesn't need to do clean up. TODO: this can be improved later + + ctx := context.NewWebContext(base, &MockRender{}, nil) + ctx.Flash = &middleware.Flash{Values: url.Values{}} chiCtx := chi.NewRouteContext() ctx.Base.AppendContextValue(chi.RouteCtxKey, chiCtx) @@ -148,13 +146,13 @@ func LoadGitRepo(t *testing.T, ctx *context.Context) { assert.NoError(t, err) } -type mockRender struct{} +type MockRender struct{} -func (tr *mockRender) TemplateLookup(tmpl string, _ gocontext.Context) (templates.TemplateExecutor, error) { +func (tr *MockRender) TemplateLookup(tmpl string, _ gocontext.Context) (templates.TemplateExecutor, error) { return nil, nil } -func (tr *mockRender) HTML(w io.Writer, status int, _ string, _ any, _ gocontext.Context) error { +func (tr *MockRender) HTML(w io.Writer, status int, _ string, _ any, _ gocontext.Context) error { if resp, ok := w.(http.ResponseWriter); ok { resp.WriteHeader(status) } |