66 "context"
77 "net/http"
88 "net/http/httptest"
9+ "strconv"
910 "strings"
1011 "testing"
1112 "time"
@@ -36,6 +37,48 @@ const sampleContentList = `{
3637 ]
3738}`
3839
40+ // sampleMultiContentList has two items: one matching "database", one not.
41+ const sampleMultiContentList = `{
42+ "items": [
43+ {
44+ "contentId": 2888,
45+ "publisher": "Mendix",
46+ "type": "Module",
47+ "latestVersion": {
48+ "name": "Database Connector",
49+ "versionId": "aaaa",
50+ "versionNumber": "3.1.0",
51+ "minSupportedMendixVersion": "9.0.0",
52+ "publicationDate": "2025-06-01T00:00:00Z"
53+ }
54+ },
55+ {
56+ "contentId": 170,
57+ "publisher": "Mendix",
58+ "type": "Module",
59+ "latestVersion": {
60+ "name": "Community Commons",
61+ "versionId": "bbbb",
62+ "versionNumber": "11.5.0",
63+ "minSupportedMendixVersion": "10.24.0",
64+ "publicationDate": "2026-01-13T00:00:00Z"
65+ }
66+ },
67+ {
68+ "contentId": 999,
69+ "publisher": "ACME",
70+ "type": "Module",
71+ "latestVersion": {
72+ "name": "Advanced Database Tools",
73+ "versionId": "cccc",
74+ "versionNumber": "1.0.0",
75+ "minSupportedMendixVersion": "9.0.0",
76+ "publicationDate": "2024-01-01T00:00:00Z"
77+ }
78+ }
79+ ]
80+ }`
81+
3982const sampleContent = `{
4083 "contentId": 170,
4184 "publisher": "Mendix",
@@ -79,9 +122,11 @@ func TestSearch_PassesQueryAndLimit(t *testing.T) {
79122 gotPath = r .URL .Path
80123 gotQuery = r .URL .RawQuery
81124 w .Header ().Set ("Content-Type" , "application/json" )
82- _ , _ = w .Write ([]byte (sampleContentList ))
125+ _ , _ = w .Write ([]byte (sampleMultiContentList ))
83126 })
84127
128+ // User requests limit=3 but because the API ignores ?search=, we fetch
129+ // searchFetchLimit items and apply client-side filtering.
85130 result , err := client .Search (context .Background (), "database" , 3 )
86131 if err != nil {
87132 t .Fatalf ("Search: %v" , err )
@@ -90,13 +135,72 @@ func TestSearch_PassesQueryAndLimit(t *testing.T) {
90135 t .Errorf ("path: got %q, want /v1/content" , gotPath )
91136 }
92137 if ! strings .Contains (gotQuery , "search=database" ) {
93- t .Errorf ("query missing search: %q" , gotQuery )
138+ t .Errorf ("query missing search param: %q" , gotQuery )
139+ }
140+ // API receives searchFetchLimit, not the user's limit=3
141+ if ! strings .Contains (gotQuery , "limit=" + strconv .Itoa (searchFetchLimit )) {
142+ t .Errorf ("expected API to receive limit=%d, got query %q" , searchFetchLimit , gotQuery )
94143 }
95- if ! strings .Contains (gotQuery , "limit=3" ) {
96- t .Errorf ("query missing limit: %q" , gotQuery )
144+ // Client-side filter: only items whose name contains "database"
145+ if len (result .Items ) != 2 {
146+ t .Errorf ("expected 2 filtered items (Database Connector + Advanced Database Tools), got %d" , len (result .Items ))
147+ }
148+ }
149+
150+ func TestSearch_ClientSideFiltering (t * testing.T ) {
151+ client , _ := newMockServer (t , func (w http.ResponseWriter , r * http.Request ) {
152+ _ , _ = w .Write ([]byte (sampleMultiContentList ))
153+ })
154+
155+ result , err := client .Search (context .Background (), "community" , 20 )
156+ if err != nil {
157+ t .Fatal (err )
97158 }
98159 if len (result .Items ) != 1 || result .Items [0 ].ContentID != 170 {
99- t .Errorf ("unexpected result: %+v" , result )
160+ t .Errorf ("expected only Community Commons (170), got %+v" , result .Items )
161+ }
162+ }
163+
164+ func TestSearch_ClientSideFiltering_NoMatch (t * testing.T ) {
165+ client , _ := newMockServer (t , func (w http.ResponseWriter , r * http.Request ) {
166+ _ , _ = w .Write ([]byte (sampleMultiContentList ))
167+ })
168+
169+ result , err := client .Search (context .Background (), "zzzznonexistent" , 20 )
170+ if err != nil {
171+ t .Fatal (err )
172+ }
173+ if len (result .Items ) != 0 {
174+ t .Errorf ("expected 0 results for nonexistent query, got %d" , len (result .Items ))
175+ }
176+ }
177+
178+ func TestSearch_ClientSideFiltering_LimitApplied (t * testing.T ) {
179+ client , _ := newMockServer (t , func (w http.ResponseWriter , r * http.Request ) {
180+ _ , _ = w .Write ([]byte (sampleMultiContentList ))
181+ })
182+
183+ // 2 items match "database", but user wants only 1
184+ result , err := client .Search (context .Background (), "database" , 1 )
185+ if err != nil {
186+ t .Fatal (err )
187+ }
188+ if len (result .Items ) != 1 {
189+ t .Errorf ("expected limit of 1 applied after filtering, got %d items" , len (result .Items ))
190+ }
191+ }
192+
193+ func TestSearch_PublisherFiltering (t * testing.T ) {
194+ client , _ := newMockServer (t , func (w http.ResponseWriter , r * http.Request ) {
195+ _ , _ = w .Write ([]byte (sampleMultiContentList ))
196+ })
197+
198+ result , err := client .Search (context .Background (), "acme" , 20 )
199+ if err != nil {
200+ t .Fatal (err )
201+ }
202+ if len (result .Items ) != 1 || result .Items [0 ].ContentID != 999 {
203+ t .Errorf ("expected ACME item (999), got %+v" , result .Items )
100204 }
101205}
102206
0 commit comments