mirror of
				https://gitee.com/gitea/gitea
				synced 2025-11-04 08:30:25 +08:00 
			
		
		
		
	Backport #26075 by @KN4CK3R Fixes #25953 - Do not load full version information (v3) - Add pagination support (v2) Co-authored-by: KN4CK3R <admin@oldschoolhack.me>
This commit is contained in:
		@@ -12,6 +12,7 @@ import (
 | 
			
		||||
	"io"
 | 
			
		||||
	"net/http"
 | 
			
		||||
	"net/http/httptest"
 | 
			
		||||
	neturl "net/url"
 | 
			
		||||
	"strconv"
 | 
			
		||||
	"testing"
 | 
			
		||||
	"time"
 | 
			
		||||
@@ -68,10 +69,16 @@ func TestPackageNuGet(t *testing.T) {
 | 
			
		||||
		Content    string               `xml:",innerxml"`
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	type FeedEntryLink struct {
 | 
			
		||||
		Rel  string `xml:"rel,attr"`
 | 
			
		||||
		Href string `xml:"href,attr"`
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	type FeedResponse struct {
 | 
			
		||||
		XMLName xml.Name     `xml:"feed"`
 | 
			
		||||
		Entries []*FeedEntry `xml:"entry"`
 | 
			
		||||
		Count   int64        `xml:"count"`
 | 
			
		||||
		XMLName xml.Name        `xml:"feed"`
 | 
			
		||||
		Links   []FeedEntryLink `xml:"link"`
 | 
			
		||||
		Entries []*FeedEntry    `xml:"entry"`
 | 
			
		||||
		Count   int64           `xml:"count"`
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
 | 
			
		||||
@@ -373,6 +380,25 @@ AAAjQmxvYgAAAGm7ENm9SGxMtAFVvPUsPJTF6PbtAAAAAFcVogEJAAAAAQAAAA==`)
 | 
			
		||||
		})
 | 
			
		||||
	})
 | 
			
		||||
 | 
			
		||||
	containsOneNextLink := func(t *testing.T, links []FeedEntryLink) func() bool {
 | 
			
		||||
		return func() bool {
 | 
			
		||||
			found := 0
 | 
			
		||||
			for _, l := range links {
 | 
			
		||||
				if l.Rel == "next" {
 | 
			
		||||
					found++
 | 
			
		||||
					u, err := neturl.Parse(l.Href)
 | 
			
		||||
					assert.NoError(t, err)
 | 
			
		||||
					q := u.Query()
 | 
			
		||||
					assert.Contains(t, q, "$skip")
 | 
			
		||||
					assert.Contains(t, q, "$top")
 | 
			
		||||
					assert.Equal(t, "1", q.Get("$skip"))
 | 
			
		||||
					assert.Equal(t, "1", q.Get("$top"))
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
			return found == 1
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	t.Run("SearchService", func(t *testing.T) {
 | 
			
		||||
		cases := []struct {
 | 
			
		||||
			Query           string
 | 
			
		||||
@@ -393,7 +419,7 @@ AAAjQmxvYgAAAGm7ENm9SGxMtAFVvPUsPJTF6PbtAAAAAFcVogEJAAAAAQAAAA==`)
 | 
			
		||||
				defer tests.PrintCurrentTest(t)()
 | 
			
		||||
 | 
			
		||||
				for i, c := range cases {
 | 
			
		||||
					req := NewRequest(t, "GET", fmt.Sprintf("%s/Search()?searchTerm='%s'&skip=%d&take=%d", url, c.Query, c.Skip, c.Take))
 | 
			
		||||
					req := NewRequest(t, "GET", fmt.Sprintf("%s/Search()?searchTerm='%s'&$skip=%d&$top=%d", url, c.Query, c.Skip, c.Take))
 | 
			
		||||
					req = AddBasicAuthHeader(req, user.Name)
 | 
			
		||||
					resp := MakeRequest(t, req, http.StatusOK)
 | 
			
		||||
 | 
			
		||||
@@ -403,7 +429,7 @@ AAAjQmxvYgAAAGm7ENm9SGxMtAFVvPUsPJTF6PbtAAAAAFcVogEJAAAAAQAAAA==`)
 | 
			
		||||
					assert.Equal(t, c.ExpectedTotal, result.Count, "case %d: unexpected total hits", i)
 | 
			
		||||
					assert.Len(t, result.Entries, c.ExpectedResults, "case %d: unexpected result count", i)
 | 
			
		||||
 | 
			
		||||
					req = NewRequest(t, "GET", fmt.Sprintf("%s/Search()/$count?searchTerm='%s'&skip=%d&take=%d", url, c.Query, c.Skip, c.Take))
 | 
			
		||||
					req = NewRequest(t, "GET", fmt.Sprintf("%s/Search()/$count?searchTerm='%s'&$skip=%d&$top=%d", url, c.Query, c.Skip, c.Take))
 | 
			
		||||
					req = AddBasicAuthHeader(req, user.Name)
 | 
			
		||||
					resp = MakeRequest(t, req, http.StatusOK)
 | 
			
		||||
 | 
			
		||||
@@ -432,6 +458,17 @@ AAAjQmxvYgAAAGm7ENm9SGxMtAFVvPUsPJTF6PbtAAAAAFcVogEJAAAAAQAAAA==`)
 | 
			
		||||
					assert.Equal(t, strconv.FormatInt(c.ExpectedTotal, 10), resp.Body.String(), "case %d: unexpected total hits", i)
 | 
			
		||||
				}
 | 
			
		||||
			})
 | 
			
		||||
 | 
			
		||||
			t.Run("Next", func(t *testing.T) {
 | 
			
		||||
				req := NewRequest(t, "GET", fmt.Sprintf("%s/Search()?searchTerm='test'&$skip=0&$top=1", url))
 | 
			
		||||
				req = AddBasicAuthHeader(req, user.Name)
 | 
			
		||||
				resp := MakeRequest(t, req, http.StatusOK)
 | 
			
		||||
 | 
			
		||||
				var result FeedResponse
 | 
			
		||||
				decodeXML(t, resp, &result)
 | 
			
		||||
 | 
			
		||||
				assert.Condition(t, containsOneNextLink(t, result.Links))
 | 
			
		||||
			})
 | 
			
		||||
		})
 | 
			
		||||
 | 
			
		||||
		t.Run("v3", func(t *testing.T) {
 | 
			
		||||
@@ -558,7 +595,7 @@ AAAjQmxvYgAAAGm7ENm9SGxMtAFVvPUsPJTF6PbtAAAAAFcVogEJAAAAAQAAAA==`)
 | 
			
		||||
		t.Run("v2", func(t *testing.T) {
 | 
			
		||||
			defer tests.PrintCurrentTest(t)()
 | 
			
		||||
 | 
			
		||||
			req := NewRequest(t, "GET", fmt.Sprintf("%s/FindPackagesById()?id='%s'", url, packageName))
 | 
			
		||||
			req := NewRequest(t, "GET", fmt.Sprintf("%s/FindPackagesById()?id='%s'&$top=1", url, packageName))
 | 
			
		||||
			req = AddBasicAuthHeader(req, user.Name)
 | 
			
		||||
			resp := MakeRequest(t, req, http.StatusOK)
 | 
			
		||||
 | 
			
		||||
@@ -567,6 +604,7 @@ AAAjQmxvYgAAAGm7ENm9SGxMtAFVvPUsPJTF6PbtAAAAAFcVogEJAAAAAQAAAA==`)
 | 
			
		||||
 | 
			
		||||
			assert.Len(t, result.Entries, 1)
 | 
			
		||||
			assert.Equal(t, packageVersion, result.Entries[0].Properties.Version)
 | 
			
		||||
			assert.Condition(t, containsOneNextLink(t, result.Links))
 | 
			
		||||
 | 
			
		||||
			req = NewRequest(t, "GET", fmt.Sprintf("%s/FindPackagesById()/$count?id='%s'", url, packageName))
 | 
			
		||||
			req = AddBasicAuthHeader(req, user.Name)
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user