Skip to content

Commit 9982d37

Browse files
committed
test: add cases
1 parent 2d94678 commit 9982d37

File tree

2 files changed

+100
-0
lines changed

2 files changed

+100
-0
lines changed

__snapshots__/main_test.snap

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,44 @@ You must provide at least one path to either a lockfile or a directory containin
5858

5959
---
6060

61+
[TestRun_APIError/#00 - 1]
62+
Loaded the following OSV databases:
63+
api#http://127.0.0.1:42805 (using batches of 1000)
64+
RubyGems (%% vulnerabilities, including withdrawn - last updated %%)
65+
66+
/tmp/TestRun_APIError#003124218549/001/Gemfile.lock: found 1 package
67+
Using config at /tmp/TestRun_APIError#003124218549/001/.osv-detector.yml (0 ignores)
68+
Using db api#http://127.0.0.1:42805 (using batches of 1000)
69+
Using db RubyGems (%% vulnerabilities, including withdrawn - last updated %%)
70+
71+
no known vulnerabilities found
72+
73+
---
74+
75+
[TestRun_APIError/#00 - 2]
76+
an api error occurred while trying to check the packages listed in /tmp/TestRun_APIError#003124218549/001/Gemfile.lock: api returned unexpected status (POST http://127.0.0.1:42805/querybatch 400)
77+
78+
---
79+
80+
[TestRun_APIError/#01 - 1]
81+
Loaded the following OSV databases:
82+
api#http://127.0.0.1:36893 (using batches of 1000)
83+
RubyGems (%% vulnerabilities, including withdrawn - last updated %%)
84+
85+
/tmp/TestRun_APIError#01238228472/001/Gemfile.lock: found 1 package
86+
Using config at /tmp/TestRun_APIError#01238228472/001/.osv-detector.yml (0 ignores)
87+
Using db api#http://127.0.0.1:36893 (using batches of 1000)
88+
Using db RubyGems (%% vulnerabilities, including withdrawn - last updated %%)
89+
90+
no known vulnerabilities found
91+
92+
---
93+
94+
[TestRun_APIError/#01 - 2]
95+
an api error occurred while trying to check the packages listed in /tmp/TestRun_APIError#01238228472/001/Gemfile.lock: api response could not be parsed as json (POST http://127.0.0.1:36893/querybatch): invalid character '<' looking for beginning of value
96+
97+
---
98+
6199
[TestRun_Configs/#00 - 1]
62100
Loaded the following OSV databases:
63101

main_test.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package main
33
import (
44
"bytes"
55
"fmt"
6+
"net/http"
7+
"net/http/httptest"
68
"os"
79
"path/filepath"
810
"strings"
@@ -836,3 +838,63 @@ func TestRun_EndToEnd(t *testing.T) {
836838
})
837839
}
838840
}
841+
842+
func TestRun_APIError(t *testing.T) {
843+
t.Parallel()
844+
845+
tests := []struct{ handler http.HandlerFunc }{
846+
{
847+
handler: func(w http.ResponseWriter, _ *http.Request) {
848+
w.WriteHeader(http.StatusBadRequest)
849+
_, _ = w.Write([]byte("{}"))
850+
},
851+
},
852+
{
853+
handler: func(w http.ResponseWriter, _ *http.Request) {
854+
_, _ = w.Write([]byte("<html></html>"))
855+
},
856+
},
857+
}
858+
for _, tt := range tests {
859+
t.Run("", func(t *testing.T) {
860+
t.Parallel()
861+
862+
tmp := t.TempDir()
863+
864+
var err error
865+
866+
// create a file for scanning
867+
err = os.WriteFile(filepath.Join(tmp, "Gemfile.lock"), []byte(`
868+
GEM
869+
remote: https://rubygems.org/
870+
specs:
871+
ast (2.4.2)
872+
`), 0600)
873+
874+
if err != nil {
875+
t.Fatal(err)
876+
}
877+
878+
// setup a fake api server
879+
ts := httptest.NewServer(tt.handler)
880+
t.Cleanup(ts.Close)
881+
882+
// create a config file setting up our api server
883+
err = os.WriteFile(filepath.Join(tmp, ".osv-detector.yml"), []byte(`
884+
extra-databases:
885+
- url: `+ts.URL,
886+
), 0600)
887+
888+
if err != nil {
889+
t.Fatal(err)
890+
}
891+
892+
// run the cli in our tmp directory
893+
testCli(t, cliTestCase{
894+
name: "",
895+
args: []string{tmp},
896+
exit: 127,
897+
})
898+
})
899+
}
900+
}

0 commit comments

Comments
 (0)