|
1 | 1 | package server
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "bytes" |
| 5 | + "encoding/json" |
4 | 6 | "net/http"
|
5 | 7 | "net/http/httptest"
|
6 | 8 | "testing"
|
| 9 | + "time" |
7 | 10 |
|
8 | 11 | "github.com/hahwul/dalfox/v2/pkg/model"
|
9 | 12 | _ "github.com/hahwul/dalfox/v2/pkg/server/docs"
|
@@ -77,3 +80,99 @@ func Test_healthHandler(t *testing.T) {
|
77 | 80 | assert.Contains(t, rec.Body.String(), "ok")
|
78 | 81 | }
|
79 | 82 | }
|
| 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