loading up the forgejo repo on tangled to test page performance
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Fix install page context, make the install page tests really test (#24858)

Fix #24856

Rename "context.contextKey" to "context.WebContextKey", this context is
for web context only. But the Context itself is not renamed, otherwise
it would cause a lot of changes (if we really want to rename it, there
could be a separate PR).

The old test code doesn't really test, the "install page" gets broken
not only one time, so use new test code to make sure the "install page"
could work.

authored by

wxiaoguang and committed by
GitHub
abcf5a7b 5c0745c0

+43 -28
+1 -3
cmd/web.go
··· 142 142 return err 143 143 } 144 144 } 145 - installCtx, cancel := context.WithCancel(graceful.GetManager().HammerContext()) 146 - c := install.Routes(installCtx) 145 + c := install.Routes() 147 146 err := listen(c, false) 148 - cancel() 149 147 if err != nil { 150 148 log.Critical("Unable to open listener for installer. Is Gitea already running?") 151 149 graceful.GetManager().DoGracefulShutdown()
+6 -6
modules/context/context.go
··· 68 68 return ctx.Locale.Tr(msg, trArgs...) 69 69 } 70 70 71 - type contextKeyType struct{} 71 + type webContextKeyType struct{} 72 72 73 - var contextKey interface{} = contextKeyType{} 73 + var WebContextKey = webContextKeyType{} 74 74 75 - func GetContext(req *http.Request) *Context { 76 - ctx, _ := req.Context().Value(contextKey).(*Context) 75 + func GetWebContext(req *http.Request) *Context { 76 + ctx, _ := req.Context().Value(WebContextKey).(*Context) 77 77 return ctx 78 78 } 79 79 ··· 86 86 func GetValidateContext(req *http.Request) (ctx *ValidateContext) { 87 87 if ctxAPI, ok := req.Context().Value(apiContextKey).(*APIContext); ok { 88 88 ctx = &ValidateContext{Base: ctxAPI.Base} 89 - } else if ctxWeb, ok := req.Context().Value(contextKey).(*Context); ok { 89 + } else if ctxWeb, ok := req.Context().Value(WebContextKey).(*Context); ok { 90 90 ctx = &ValidateContext{Base: ctxWeb.Base} 91 91 } else { 92 92 panic("invalid context, expect either APIContext or Context") ··· 135 135 ctx.PageData = map[string]any{} 136 136 ctx.Data["PageData"] = ctx.PageData 137 137 138 - ctx.Base.AppendContextValue(contextKey, ctx) 138 + ctx.Base.AppendContextValue(WebContextKey, ctx) 139 139 ctx.Base.AppendContextValueFunc(git.RepositoryContextKey, func() any { return ctx.Repo.GitRepo }) 140 140 141 141 ctx.Csrf = PrepareCSRFProtector(csrfOpts, ctx)
+1 -1
modules/context/package.go
··· 150 150 } 151 151 defer baseCleanUp() 152 152 153 - ctx.Base.AppendContextValue(contextKey, ctx) 153 + ctx.Base.AppendContextValue(WebContextKey, ctx) 154 154 next.ServeHTTP(ctx.Resp, ctx.Req) 155 155 }) 156 156 }
+1 -1
modules/web/handler.go
··· 22 22 // TODO: decouple this from the context package, let the context package register these providers 23 23 var argTypeProvider = map[reflect.Type]func(req *http.Request) ResponseStatusProvider{ 24 24 reflect.TypeOf(&context.APIContext{}): func(req *http.Request) ResponseStatusProvider { return context.GetAPIContext(req) }, 25 - reflect.TypeOf(&context.Context{}): func(req *http.Request) ResponseStatusProvider { return context.GetContext(req) }, 25 + reflect.TypeOf(&context.Context{}): func(req *http.Request) ResponseStatusProvider { return context.GetWebContext(req) }, 26 26 reflect.TypeOf(&context.PrivateContext{}): func(req *http.Request) ResponseStatusProvider { return context.GetPrivateContext(req) }, 27 27 } 28 28
+2 -1
routers/install/install.go
··· 59 59 return func(next http.Handler) http.Handler { 60 60 return http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) { 61 61 base, baseCleanUp := context.NewBaseContext(resp, req) 62 - ctx := context.Context{ 62 + ctx := &context.Context{ 63 63 Base: base, 64 64 Flash: &middleware.Flash{}, 65 65 Render: rnd, ··· 67 67 } 68 68 defer baseCleanUp() 69 69 70 + ctx.AppendContextValue(context.WebContextKey, ctx) 70 71 ctx.Data.MergeFrom(middleware.CommonTemplateContextData()) 71 72 ctx.Data.MergeFrom(middleware.ContextData{ 72 73 "locale": ctx.Locale,
+1 -2
routers/install/routes.go
··· 4 4 package install 5 5 6 6 import ( 7 - goctx "context" 8 7 "fmt" 9 8 "html" 10 9 "net/http" ··· 18 17 ) 19 18 20 19 // Routes registers the installation routes 21 - func Routes(ctx goctx.Context) *web.Route { 20 + func Routes() *web.Route { 22 21 base := web.NewRoute() 23 22 base.Use(common.ProtocolMiddlewares()...) 24 23 base.RouteMethods("/assets/*", "GET, HEAD", public.AssetsHandlerFunc("/assets/"))
+29 -12
routers/install/routes_test.go
··· 1 - // Copyright 2021 The Gitea Authors. All rights reserved. 1 + // Copyright 2023 The Gitea Authors. All rights reserved. 2 2 // SPDX-License-Identifier: MIT 3 3 4 4 package install 5 5 6 6 import ( 7 - "context" 7 + "net/http/httptest" 8 + "path/filepath" 8 9 "testing" 9 10 11 + "code.gitea.io/gitea/models/unittest" 12 + 10 13 "github.com/stretchr/testify/assert" 11 14 ) 12 15 13 16 func TestRoutes(t *testing.T) { 14 - // TODO: this test seems not really testing the handlers 15 - ctx, cancel := context.WithCancel(context.Background()) 16 - defer cancel() 17 - base := Routes(ctx) 18 - assert.NotNil(t, base) 19 - r := base.R.Routes()[1] 20 - routes := r.SubRoutes.Routes()[0] 21 - assert.EqualValues(t, "/", routes.Pattern) 22 - assert.Nil(t, routes.SubRoutes) 23 - assert.Len(t, routes.Handlers, 2) 17 + r := Routes() 18 + assert.NotNil(t, r) 19 + 20 + w := httptest.NewRecorder() 21 + req := httptest.NewRequest("GET", "/", nil) 22 + r.ServeHTTP(w, req) 23 + assert.EqualValues(t, 200, w.Code) 24 + assert.Contains(t, w.Body.String(), `class="page-content install"`) 25 + 26 + w = httptest.NewRecorder() 27 + req = httptest.NewRequest("GET", "/no-such", nil) 28 + r.ServeHTTP(w, req) 29 + assert.EqualValues(t, 404, w.Code) 30 + 31 + w = httptest.NewRecorder() 32 + req = httptest.NewRequest("GET", "/assets/img/gitea.svg", nil) 33 + r.ServeHTTP(w, req) 34 + assert.EqualValues(t, 200, w.Code) 35 + } 36 + 37 + func TestMain(m *testing.M) { 38 + unittest.MainTest(m, &unittest.TestOptions{ 39 + GiteaRootPath: filepath.Join("..", ".."), 40 + }) 24 41 }
+1 -1
routers/web/web.go
··· 1405 1405 } 1406 1406 1407 1407 m.NotFound(func(w http.ResponseWriter, req *http.Request) { 1408 - ctx := context.GetContext(req) 1408 + ctx := context.GetWebContext(req) 1409 1409 ctx.NotFound("", nil) 1410 1410 }) 1411 1411 }
+1 -1
services/auth/auth.go
··· 92 92 middleware.SetLocaleCookie(resp, user.Language, 0) 93 93 94 94 // Clear whatever CSRF has right now, force to generate a new one 95 - if ctx := gitea_context.GetContext(req); ctx != nil { 95 + if ctx := gitea_context.GetWebContext(req); ctx != nil { 96 96 ctx.Csrf.DeleteCookie(ctx) 97 97 } 98 98 }