diff --git a/routers/web/explore/code.go b/routers/web/explore/code.go index f0b12e9142..6697755c22 100644 --- a/routers/web/explore/code.go +++ b/routers/web/explore/code.go @@ -147,6 +147,7 @@ func Code(ctx *context.Context) { pager := context.NewPagination(total, setting.UI.RepoSearchPagingNum, page, 5) pager.SetDefaultParams(ctx) pager.AddParam(ctx, "l", "Language") + pager.AddParam(ctx, "mode", "CodeSearchMode") ctx.Data["Page"] = pager ctx.HTML(http.StatusOK, tplExploreCode) diff --git a/routers/web/repo/search.go b/routers/web/repo/search.go index 1671378a3b..ad10542c01 100644 --- a/routers/web/repo/search.go +++ b/routers/web/repo/search.go @@ -165,6 +165,8 @@ func Search(ctx *context.Context) { pager := context.NewPagination(total, setting.UI.RepoSearchPagingNum, page, 5) pager.SetDefaultParams(ctx) pager.AddParam(ctx, "l", "Language") + pager.AddParam(ctx, "mode", "CodeSearchMode") + pager.AddParam(ctx, "path", "CodeSearchPath") ctx.Data["Page"] = pager ctx.HTML(http.StatusOK, tplSearch) diff --git a/routers/web/user/code.go b/routers/web/user/code.go index ac1852e410..b5c5e54953 100644 --- a/routers/web/user/code.go +++ b/routers/web/user/code.go @@ -131,6 +131,7 @@ func CodeSearch(ctx *context.Context) { pager := context.NewPagination(total, setting.UI.RepoSearchPagingNum, page, 5) pager.SetDefaultParams(ctx) pager.AddParam(ctx, "l", "Language") + pager.AddParam(ctx, "mode", "CodeSearchMode") ctx.Data["Page"] = pager ctx.HTML(http.StatusOK, tplUserCode) diff --git a/tests/integration/repo_search_test.go b/tests/integration/repo_search_test.go index aaaa4618cf..b145d9f427 100644 --- a/tests/integration/repo_search_test.go +++ b/tests/integration/repo_search_test.go @@ -5,6 +5,7 @@ package integration import ( "net/http" + "net/url" "testing" "forgejo.org/models/db" @@ -92,8 +93,8 @@ func testSearchRepo(t *testing.T, indexer bool) { testSearch(t, "/user2/glob/search?q=file5&page=1&mode=exact", []string{}, indexer) } -func testSearch(t *testing.T, url string, expected []string, indexer bool) { - req := NewRequest(t, "GET", url) +func testSearch(t *testing.T, rawURL string, expected []string, indexer bool) { + req := NewRequest(t, "GET", rawURL) resp := MakeRequest(t, req, http.StatusOK) doc := NewHTMLParser(t, resp.Body) @@ -119,4 +120,41 @@ func testSearch(t *testing.T, url string, expected []string, indexer bool) { filenames := resultFilenames(t, doc) assert.ElementsMatch(t, expected, filenames) + + testSearchPagination(t, rawURL, doc) +} + +// Tests that the variables set in the url persist for all the paginated links +func testSearchPagination(t *testing.T, rawURL string, doc *HTMLDoc) { + original, err := queryFromStr(rawURL) + require.NoError(t, err) + + hrefs := doc. + Find(".pagination.menu a[href]:not(.disabled)"). + Map(func(i int, el *goquery.Selection) string { + attr, ok := el.Attr("href") + require.True(t, ok) + return attr + }) + query := make([]url.Values, len(hrefs)) + for i, href := range hrefs { + query[i], err = queryFromStr(href) + require.NoError(t, err) + } + + for key := range original { + for i, q := range query { + assert.Equal(t, original.Get(key), q.Get(key), + "failed at index '%d' with url '%v'", i, hrefs[i]) + } + } +} + +func queryFromStr(rawURL string) (url.Values, error) { + u, err := url.Parse(rawURL) + if err != nil { + return nil, err + } + + return url.ParseQuery(u.RawQuery) }