@@ -431,7 +431,12 @@ func TestAPIDB_Check_WithCommit(t *testing.T) {
431431 mux := http .NewServeMux ()
432432
433433 mux .HandleFunc ("/querybatch" , func (w http.ResponseWriter , r * http.Request ) {
434- expectRequestPayload (t , r , []apiQuery {{Commit : "abc123" }})
434+ expectRequestPayload (t , r , []apiQuery {
435+ {
436+ Version : "1.0.0" ,
437+ Package : apiPackage {Name : "my-package" , Ecosystem : lockfile .NpmEcosystem },
438+ },
439+ })
435440
436441 jsonData := jsonMarshalQueryBatchResponse (t , []objectsWithIDs {{}})
437442
@@ -464,6 +469,80 @@ func TestAPIDB_Check_WithCommit(t *testing.T) {
464469 }
465470}
466471
472+ func TestAPIDB_Check_WithCommitOnly (t * testing.T ) {
473+ t .Parallel ()
474+
475+ mux := http .NewServeMux ()
476+
477+ mux .HandleFunc ("/querybatch" , func (w http.ResponseWriter , r * http.Request ) {
478+ expectRequestPayload (t , r , []apiQuery {{Commit : "abc123" }})
479+
480+ jsonData := jsonMarshalQueryBatchResponse (t , []objectsWithIDs {{}})
481+
482+ _ , _ = w .Write (jsonData )
483+ })
484+
485+ ts := httptest .NewServer (mux )
486+ t .Cleanup (ts .Close )
487+
488+ db , err := database .NewAPIDB (database.Config {URL : ts .URL }, false , 1 )
489+
490+ if err != nil {
491+ t .Fatalf ("Check() unexpected error \" %v\" " , err )
492+ }
493+
494+ vulns , err := db .Check ([]internal.PackageDetails {{Commit : "abc123" }})
495+
496+ if err != nil {
497+ t .Fatalf ("unexpected error \" %v\" " , err )
498+ }
499+
500+ if len (vulns ) != 1 {
501+ t .Fatalf ("expected to get 1 package but got %d" , len (vulns ))
502+ }
503+
504+ if len (vulns [0 ]) != 0 {
505+ t .Fatalf ("expected to get 0 vulnerabilities but got %d" , len (vulns [0 ]))
506+ }
507+ }
508+
509+ func TestAPIDB_Check_WithCommitAndSomeFields (t * testing.T ) {
510+ t .Parallel ()
511+
512+ mux := http .NewServeMux ()
513+
514+ mux .HandleFunc ("/querybatch" , func (w http.ResponseWriter , r * http.Request ) {
515+ expectRequestPayload (t , r , []apiQuery {{Commit : "abc123" }})
516+
517+ jsonData := jsonMarshalQueryBatchResponse (t , []objectsWithIDs {{}})
518+
519+ _ , _ = w .Write (jsonData )
520+ })
521+
522+ ts := httptest .NewServer (mux )
523+ t .Cleanup (ts .Close )
524+
525+ db , err := database .NewAPIDB (database.Config {URL : ts .URL }, false , 1 )
526+
527+ if err != nil {
528+ t .Fatalf ("Check() unexpected error \" %v\" " , err )
529+ }
530+
531+ vulns , err := db .Check ([]internal.PackageDetails {{Commit : "abc123" , Ecosystem : "npm" }})
532+
533+ if err != nil {
534+ t .Fatalf ("unexpected error \" %v\" " , err )
535+ }
536+
537+ if len (vulns ) != 1 {
538+ t .Fatalf ("expected to get 1 package but got %d" , len (vulns ))
539+ }
540+
541+ if len (vulns [0 ]) != 0 {
542+ t .Fatalf ("expected to get 0 vulnerabilities but got %d" , len (vulns [0 ]))
543+ }
544+ }
545+
467546func TestAPIDB_Check_Batches (t * testing.T ) {
468547 t .Parallel ()
469548
0 commit comments