@@ -66,11 +66,11 @@ func TestLocalDirStoraget(t *testing.T) {
6666 },
6767 },
6868 {
69- name : "storing with query handler enabled should create indexes " ,
69+ name : "storing with metas handler enabled should create indices " ,
7070 setup : func (t * testing.T ) (* LocalDirV1 , fs.FS ) {
7171 s := & LocalDirV1 {
7272 RootDir : t .TempDir (),
73- EnableQueryHandler : true ,
73+ EnableMetasHandler : true ,
7474 }
7575 return s , createTestFS (t )
7676 },
@@ -106,7 +106,7 @@ func TestLocalDirStoraget(t *testing.T) {
106106 for i := 0 ; i < 10 ; i ++ {
107107 wg .Add (1 )
108108 go func () {
109- defer wg .Add ( - 1 )
109+ defer wg .Done ( )
110110 for j := 0 ; j < 100 ; j ++ {
111111 s .ContentExists (catalog )
112112 }
@@ -266,13 +266,13 @@ func TestLocalDirServerHandler(t *testing.T) {
266266 }
267267}
268268
269- // Tests to verify the behavior of the query endpoint, as described in
270- // https://docs.google.com/document/d/1s6_9IFEKGQLNh3ueH7SF4Yrx4PW9NSiNFqFIJx0pU-8/edit?usp=sharing
271- func TestQueryEndpoint (t * testing.T ) {
269+ // Tests to verify the behavior of the metas endpoint, as described in
270+ // https://docs.google.com/document/d/1s6_9IFEKGQLNh3ueH7SF4Yrx4PW9NSiNFqFIJx0pU-8/
271+ func TestMetasEndpoint (t * testing.T ) {
272272 store := & LocalDirV1 {
273273 RootDir : t .TempDir (),
274274 RootURL : & url.URL {Path : urlPrefix },
275- EnableQueryHandler : true ,
275+ EnableMetasHandler : true ,
276276 }
277277 if store .Store (context .Background (), "test-catalog" , createTestFS (t )) != nil {
278278 t .Fatal ("failed to store test catalog" )
@@ -281,7 +281,7 @@ func TestQueryEndpoint(t *testing.T) {
281281
282282 testCases := []struct {
283283 name string
284- setupStore func () ( * httptest. Server , error )
284+ initRequest func (req * http. Request ) error
285285 queryParams string
286286 expectedStatusCode int
287287 expectedContent string
@@ -329,27 +329,50 @@ func TestQueryEndpoint(t *testing.T) {
329329 expectedContent : "" ,
330330 },
331331 {
332- name : "cached response with If-Modified-Since" ,
333- queryParams : "?schema=olm.package" ,
332+ name : "valid query with packageName that returns multiple blobs" ,
333+ queryParams : "?package=webhook_operator_test" ,
334+ expectedStatusCode : http .StatusOK ,
335+ expectedContent : `{"image":"quaydock.io/namespace/bundle:0.0.3","name":"bundle.v0.0.1","package":"webhook_operator_test","properties":[{"type":"olm.bundle.object","value":{"data":"dW5pbXBvcnRhbnQK"}},{"type":"some.other","value":{"data":"arbitrary-info"}}],"relatedImages":[{"image":"testimage:latest","name":"test"}],"schema":"olm.bundle"}
336+ {"entries":[{"name":"bundle.v0.0.1"}],"name":"preview_test","package":"webhook_operator_test","schema":"olm.channel"}` ,
337+ },
338+ {
339+ name : "cached response with If-Modified-Since" ,
340+ queryParams : "?schema=olm.package" ,
341+ initRequest : func (req * http.Request ) error {
342+ resp , err := http .DefaultClient .Do (req )
343+ if err != nil {
344+ return err
345+ }
346+ resp .Body .Close ()
347+ req .Header .Set ("If-Modified-Since" , resp .Header .Get ("Last-Modified" ))
348+ return nil
349+ },
334350 expectedStatusCode : http .StatusNotModified ,
335351 expectedContent : "" ,
336352 },
353+ {
354+ name : "request with unknown parameters" ,
355+ queryParams : "?non-existent=foo" ,
356+ expectedStatusCode : http .StatusBadRequest ,
357+ expectedContent : "400 Bad Request" ,
358+ },
359+ {
360+ name : "request with duplicate parameters" ,
361+ queryParams : "?schema=olm.bundle&&schema=olm.bundle" ,
362+ expectedStatusCode : http .StatusOK ,
363+ expectedContent : `{"image":"quaydock.io/namespace/bundle:0.0.3","name":"bundle.v0.0.1","package":"webhook_operator_test","properties":[{"type":"olm.bundle.object","value":{"data":"dW5pbXBvcnRhbnQK"}},{"type":"some.other","value":{"data":"arbitrary-info"}}],"relatedImages":[{"image":"testimage:latest","name":"test"}],"schema":"olm.bundle"}` ,
364+ },
337365 }
338366
339367 for _ , tc := range testCases {
340368 t .Run (tc .name , func (t * testing.T ) {
341- req , err := http .NewRequest (http .MethodGet , fmt .Sprintf ("%s/catalogs/test-catalog/api/v1/metas%s" , testServer .URL , tc .queryParams ), nil )
369+ reqGet , err := http .NewRequest (http .MethodGet , fmt .Sprintf ("%s/catalogs/test-catalog/api/v1/metas%s" , testServer .URL , tc .queryParams ), nil )
342370 require .NoError (t , err )
343371
344- if strings .Contains (tc .name , "If-Modified-Since" ) {
345- // Do an initial request to get a Last-Modified timestamp
346- // for the actual request
347- resp , err := http .DefaultClient .Do (req )
348- require .NoError (t , err )
349- resp .Body .Close ()
350- req .Header .Set ("If-Modified-Since" , resp .Header .Get ("Last-Modified" ))
372+ if tc .initRequest != nil {
373+ require .NoError (t , tc .initRequest (reqGet ))
351374 }
352- resp , err := http .DefaultClient .Do (req )
375+ resp , err := http .DefaultClient .Do (reqGet )
353376 require .NoError (t , err )
354377 defer resp .Body .Close ()
355378
@@ -358,6 +381,30 @@ func TestQueryEndpoint(t *testing.T) {
358381 actualContent , err := io .ReadAll (resp .Body )
359382 require .NoError (t , err )
360383 require .Equal (t , tc .expectedContent , strings .TrimSpace (string (actualContent )))
384+
385+ // Also do a HEAD request
386+ reqHead , err := http .NewRequest (http .MethodHead , fmt .Sprintf ("%s/catalogs/test-catalog/api/v1/metas%s" , testServer .URL , tc .queryParams ), nil )
387+ require .NoError (t , err )
388+ if tc .initRequest != nil {
389+ require .NoError (t , tc .initRequest (reqHead ))
390+ }
391+ resp , err = http .DefaultClient .Do (reqHead )
392+ require .NoError (t , err )
393+ require .Equal (t , tc .expectedStatusCode , resp .StatusCode )
394+ actualContent , err = io .ReadAll (resp .Body )
395+ require .NoError (t , err )
396+ require .Equal (t , "" , string (actualContent )) // HEAD should not return a body
397+ resp .Body .Close ()
398+
399+ // And make sure any other method is not allowed
400+ for _ , method := range []string {http .MethodPost , http .MethodPut , http .MethodDelete } {
401+ reqPost , err := http .NewRequest (method , fmt .Sprintf ("%s/catalogs/test-catalog/api/v1/metas%s" , testServer .URL , tc .queryParams ), nil )
402+ require .NoError (t , err )
403+ resp , err = http .DefaultClient .Do (reqPost )
404+ require .NoError (t , err )
405+ require .Equal (t , http .StatusMethodNotAllowed , resp .StatusCode )
406+ resp .Body .Close ()
407+ }
361408 })
362409 }
363410}
@@ -366,7 +413,7 @@ func TestServerLoadHandling(t *testing.T) {
366413 store := & LocalDirV1 {
367414 RootDir : t .TempDir (),
368415 RootURL : & url.URL {Path : urlPrefix },
369- EnableQueryHandler : true ,
416+ EnableMetasHandler : true ,
370417 }
371418
372419 // Create large test data
@@ -443,20 +490,20 @@ func TestServerLoadHandling(t *testing.T) {
443490 },
444491 },
445492 {
446- name : "mixed all and query endpoints" ,
493+ name : "mixed all and metas endpoints" ,
447494 concurrent : 40 ,
448495 requests : func (baseURL string ) []* http.Request {
449496 var reqs []* http.Request
450497 for i := 0 ; i < 20 ; i ++ {
451498 allReq , _ := http .NewRequest (http .MethodGet ,
452499 fmt .Sprintf ("%s/catalogs/test-catalog/api/v1/all" , baseURL ),
453500 nil )
454- queryReq , _ := http .NewRequest (http .MethodGet ,
501+ metasReq , _ := http .NewRequest (http .MethodGet ,
455502 fmt .Sprintf ("%s/catalogs/test-catalog/api/v1/metas?schema=olm.bundle" , baseURL ),
456503 nil )
457504 allReq .Header .Set ("Accept" , "application/jsonl" )
458- queryReq .Header .Set ("Accept" , "application/jsonl" )
459- reqs = append (reqs , allReq , queryReq )
505+ metasReq .Header .Set ("Accept" , "application/jsonl" )
506+ reqs = append (reqs , allReq , metasReq )
460507 }
461508 return reqs
462509 },
0 commit comments