Skip to content

Commit c7d36aa

Browse files
authored
Merge pull request #655 from hahwul/improve/server-testcodes
Enhance scan error handling and add tests for scan functionality
2 parents 4b3e781 + e8a268f commit c7d36aa

File tree

4 files changed

+109
-4
lines changed

4 files changed

+109
-4
lines changed

codecov.yml

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

go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ github.com/briandowns/spinner v1.23.2 h1:Zc6ecUnI+YzLmJniCfDNaMbW0Wid1d5+qcTq4L2
1616
github.com/briandowns/spinner v1.23.2/go.mod h1:LaZeM4wm2Ywy6vO571mvhQNRcWfRUnXOs0RcKV0wYKM=
1717
github.com/chromedp/cdproto v0.0.0-20250222051814-50c6cb17f10a h1:EnkQjhmp/MxhDB4KOTssv6xC20aQ9rhFRCfGHTsTqmE=
1818
github.com/chromedp/cdproto v0.0.0-20250222051814-50c6cb17f10a/go.mod h1:NItd7aLkcfOA/dcMXvl8p1u+lQqioRMq/SqDp71Pb/k=
19-
github.com/chromedp/chromedp v0.13.1 h1:FDh9CfaAt0w70gl69Hb69M/xgZrWuppH9AW22aGa+iU=
20-
github.com/chromedp/chromedp v0.13.1/go.mod h1:O3nO4Lno7iLoVX+7GdqQkehhKG7DtLf/zFRyJo0AhXY=
19+
github.com/chromedp/chromedp v0.13.0 h1:ydOqt7Y9LkwgutrX5C8bx49D+o63L6WcGUDyIoE0A5M=
20+
github.com/chromedp/chromedp v0.13.0/go.mod h1:O3nO4Lno7iLoVX+7GdqQkehhKG7DtLf/zFRyJo0AhXY=
2121
github.com/chromedp/sysutil v1.1.0 h1:PUFNv5EcprjqXZD9nJb9b/c9ibAbxiYo4exNWZyipwM=
2222
github.com/chromedp/sysutil v1.1.0/go.mod h1:WiThHUdltqCNKGc4gaU50XgYjwjYIhKWoHGPTUfWTJ8=
2323
github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=

pkg/server/scan.go

+6-1
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 for URL:", url, ": ", 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

+99
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package server
22

33
import (
4+
"bytes"
5+
"encoding/json"
46
"net/http"
57
"net/http/httptest"
68
"testing"
9+
"time"
710

811
"github.com/hahwul/dalfox/v2/pkg/model"
912
_ "github.com/hahwul/dalfox/v2/pkg/server/docs"
@@ -77,3 +80,99 @@ func Test_healthHandler(t *testing.T) {
7780
assert.Contains(t, rec.Body.String(), "ok")
7881
}
7982
}
83+
84+
func Test_postScanHandler(t *testing.T) {
85+
e := echo.New()
86+
rq := Req{
87+
URL: "http://example.com",
88+
Options: model.Options{
89+
Method: "GET",
90+
},
91+
}
92+
body, _ := json.Marshal(rq)
93+
req := httptest.NewRequest(http.MethodPost, "/scan", bytes.NewReader(body))
94+
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
95+
rec := httptest.NewRecorder()
96+
c := e.NewContext(req, rec)
97+
98+
scans := []string{}
99+
options := model.Options{
100+
Scan: map[string]model.Scan{},
101+
}
102+
103+
if assert.NoError(t, postScanHandler(c, &scans, options)) {
104+
assert.Equal(t, http.StatusOK, rec.Code)
105+
assert.Contains(t, rec.Body.String(), "code")
106+
assert.Contains(t, rec.Body.String(), "msg")
107+
assert.Contains(t, rec.Body.String(), "data")
108+
}
109+
}
110+
111+
func Test_GetScan(t *testing.T) {
112+
options := model.Options{
113+
Scan: map[string]model.Scan{
114+
"test-scan": {URL: "http://example.com", Results: []model.PoC{{Type: "finish"}}},
115+
},
116+
}
117+
scan := GetScan("test-scan", options)
118+
assert.Equal(t, "http://example.com", scan.URL)
119+
assert.Equal(t, "finish", scan.Results[0].Type)
120+
}
121+
122+
func Test_GetScans(t *testing.T) {
123+
options := model.Options{
124+
Scan: map[string]model.Scan{
125+
"test-scan1": {URL: "http://example1.com"},
126+
"test-scan2": {URL: "http://example2.com"},
127+
},
128+
}
129+
scans := GetScans(options)
130+
assert.Contains(t, scans, "test-scan1")
131+
assert.Contains(t, scans, "test-scan2")
132+
}
133+
134+
func Test_ScanFromAPI(t *testing.T) {
135+
options := model.Options{
136+
Debug: true,
137+
Scan: map[string]model.Scan{},
138+
}
139+
rqOptions := model.Options{
140+
Method: "GET",
141+
}
142+
sid := "test-scan-id"
143+
144+
t.Run("Successful Scan", func(t *testing.T) {
145+
ScanFromAPI("http://example.com", rqOptions, options, sid)
146+
// Add assertions to verify the scan was successful
147+
})
148+
149+
t.Run("Scan with Error", func(t *testing.T) {
150+
ScanFromAPI("http://invalid-url", rqOptions, options, sid)
151+
// Add assertions to verify error handling
152+
})
153+
}
154+
155+
func Test_setupEchoServer(t *testing.T) {
156+
options := model.Options{
157+
ServerHost: "localhost",
158+
ServerPort: 6664,
159+
}
160+
scans := []string{}
161+
e := setupEchoServer(options, &scans)
162+
163+
assert.NotNil(t, e)
164+
assert.Equal(t, "localhost:6664", e.Server.Addr)
165+
}
166+
167+
func Test_RunAPIServer(t *testing.T) {
168+
options := model.Options{
169+
ServerHost: "localhost",
170+
ServerPort: 6664,
171+
}
172+
go RunAPIServer(options)
173+
time.Sleep(1 * time.Second)
174+
175+
resp, err := http.Get("http://localhost:6664/health")
176+
assert.NoError(t, err)
177+
assert.Equal(t, http.StatusOK, resp.StatusCode)
178+
}

0 commit comments

Comments
 (0)