diff --git a/services/context/context.go b/services/context/context.go index a6a861ecaa6f9..de607cd2bc794 100644 --- a/services/context/context.go +++ b/services/context/context.go @@ -165,6 +165,7 @@ func Contexter() func(next http.Handler) http.Handler { base := NewBaseContext(resp, req) ctx := NewWebContext(base, rnd, session.GetContextSession(req)) ctx.Data.MergeFrom(middleware.CommonTemplateContextData()) + ctx.Data["CurrentURL"] = setting.AppSubURL + req.URL.RequestURI() ctx.Data["Link"] = ctx.Link // PageData is passed by reference, and it will be rendered to `window.config.pageData` in `head.tmpl` for JavaScript modules diff --git a/tests/integration/view_test.go b/tests/integration/view_test.go index 9ed3e30857522..aa6e69e047b5c 100644 --- a/tests/integration/view_test.go +++ b/tests/integration/view_test.go @@ -7,6 +7,10 @@ import ( "net/http" "testing" + "code.gitea.io/gitea/modules/setting" + "code.gitea.io/gitea/modules/test" + "code.gitea.io/gitea/modules/web" + "code.gitea.io/gitea/services/context" "code.gitea.io/gitea/tests" "github.com/stretchr/testify/assert" @@ -65,3 +69,15 @@ func TestCommitListActions(t *testing.T) { AssertHTMLElement(t, htmlDoc, `.commit-list .view-commit-path`, true) }) } + +func TestViewPageCurrentURL(t *testing.T) { + defer test.MockVariableValue(&setting.AppSubURL, "/subpath")() + var currentURL string + web.RouteMock(web.MockAfterMiddlewares, func(ctx *context.Context) { + // Some custom template users need this template variable to construct links in their templates + currentURL, _ = ctx.Data["CurrentURL"].(string) + }) + defer web.RouteMockReset() + MakeRequest(t, NewRequest(t, "GET", "/any-page?k=v"), http.StatusNotFound) + assert.Equal(t, "/subpath/any-page?k=v", currentURL) +}