1
0
Fork 0
forked from forgejo/forgejo

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.
This commit is contained in:
wxiaoguang 2023-05-23 09:29:15 +08:00 committed by GitHub
parent 5c0745c034
commit abcf5a7b5e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 43 additions and 28 deletions

View file

@ -68,12 +68,12 @@ func (ctx *Context) TrHTMLEscapeArgs(msg string, args ...string) string {
return ctx.Locale.Tr(msg, trArgs...)
}
type contextKeyType struct{}
type webContextKeyType struct{}
var contextKey interface{} = contextKeyType{}
var WebContextKey = webContextKeyType{}
func GetContext(req *http.Request) *Context {
ctx, _ := req.Context().Value(contextKey).(*Context)
func GetWebContext(req *http.Request) *Context {
ctx, _ := req.Context().Value(WebContextKey).(*Context)
return ctx
}
@ -86,7 +86,7 @@ type ValidateContext struct {
func GetValidateContext(req *http.Request) (ctx *ValidateContext) {
if ctxAPI, ok := req.Context().Value(apiContextKey).(*APIContext); ok {
ctx = &ValidateContext{Base: ctxAPI.Base}
} else if ctxWeb, ok := req.Context().Value(contextKey).(*Context); ok {
} else if ctxWeb, ok := req.Context().Value(WebContextKey).(*Context); ok {
ctx = &ValidateContext{Base: ctxWeb.Base}
} else {
panic("invalid context, expect either APIContext or Context")
@ -135,7 +135,7 @@ func Contexter() func(next http.Handler) http.Handler {
ctx.PageData = map[string]any{}
ctx.Data["PageData"] = ctx.PageData
ctx.Base.AppendContextValue(contextKey, ctx)
ctx.Base.AppendContextValue(WebContextKey, ctx)
ctx.Base.AppendContextValueFunc(git.RepositoryContextKey, func() any { return ctx.Repo.GitRepo })
ctx.Csrf = PrepareCSRFProtector(csrfOpts, ctx)