Skip to content

Commit 94df33b

Browse files
committed
Enhance scan error handling and add tests for scan functionality
1 parent 4b3e781 commit 94df33b

File tree

3 files changed

+81
-2
lines changed

3 files changed

+81
-2
lines changed

codecov.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
ignore:
2-
- cmd
2+
- cmd
3+
- pkg/server/docs/docs.go

pkg/server/scan.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,12 @@ func ScanFromAPI(url string, rqOptions model.Options, options model.Options, sid
3434
escapedURL := cleanURL(url)
3535
vLog.WithField("data1", sid).Debug(escapedURL)
3636
vLog.WithField("data1", sid).Debug(newOptions)
37-
_, _ = scan.Scan(url, newOptions, sid)
37+
_, err := scan.Scan(url, newOptions, sid)
38+
if err != nil {
39+
vLog.WithField("data1", sid).Error("Scan failed: ", err)
40+
return
41+
}
42+
vLog.WithField("data1", sid).Info("Scan completed successfully")
3843
}
3944

4045
// GetScan is get scan information

pkg/server/server_test.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package server
22

33
import (
4+
"bytes"
5+
"encoding/json"
46
"net/http"
57
"net/http/httptest"
68
"testing"
@@ -77,3 +79,74 @@ func Test_healthHandler(t *testing.T) {
7779
assert.Contains(t, rec.Body.String(), "ok")
7880
}
7981
}
82+
83+
func Test_postScanHandler(t *testing.T) {
84+
e := echo.New()
85+
rq := Req{
86+
URL: "http://example.com",
87+
Options: model.Options{
88+
Method: "GET",
89+
},
90+
}
91+
body, _ := json.Marshal(rq)
92+
req := httptest.NewRequest(http.MethodPost, "/scan", bytes.NewReader(body))
93+
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
94+
rec := httptest.NewRecorder()
95+
c := e.NewContext(req, rec)
96+
97+
scans := []string{}
98+
options := model.Options{
99+
Scan: map[string]model.Scan{},
100+
}
101+
102+
if assert.NoError(t, postScanHandler(c, &scans, options)) {
103+
assert.Equal(t, http.StatusOK, rec.Code)
104+
assert.Contains(t, rec.Body.String(), "code")
105+
assert.Contains(t, rec.Body.String(), "msg")
106+
assert.Contains(t, rec.Body.String(), "data")
107+
}
108+
}
109+
110+
func Test_GetScan(t *testing.T) {
111+
options := model.Options{
112+
Scan: map[string]model.Scan{
113+
"test-scan": {URL: "http://example.com", Results: []model.PoC{{Type: "finish"}}},
114+
},
115+
}
116+
scan := GetScan("test-scan", options)
117+
assert.Equal(t, "http://example.com", scan.URL)
118+
assert.Equal(t, "finish", scan.Results[0].Type)
119+
}
120+
121+
func Test_GetScans(t *testing.T) {
122+
options := model.Options{
123+
Scan: map[string]model.Scan{
124+
"test-scan1": {URL: "http://example1.com"},
125+
"test-scan2": {URL: "http://example2.com"},
126+
},
127+
}
128+
scans := GetScans(options)
129+
assert.Contains(t, scans, "test-scan1")
130+
assert.Contains(t, scans, "test-scan2")
131+
}
132+
133+
func Test_ScanFromAPI(t *testing.T) {
134+
options := model.Options{
135+
Debug: true,
136+
Scan: map[string]model.Scan{},
137+
}
138+
rqOptions := model.Options{
139+
Method: "GET",
140+
}
141+
sid := "test-scan-id"
142+
143+
t.Run("Successful Scan", func(t *testing.T) {
144+
ScanFromAPI("http://example.com", rqOptions, options, sid)
145+
// Add assertions to verify the scan was successful
146+
})
147+
148+
t.Run("Scan with Error", func(t *testing.T) {
149+
ScanFromAPI("http://invalid-url", rqOptions, options, sid)
150+
// Add assertions to verify error handling
151+
})
152+
}

0 commit comments

Comments
 (0)