|
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"
|
@@ -77,3 +79,74 @@ func Test_healthHandler(t *testing.T) {
|
77 | 79 | assert.Contains(t, rec.Body.String(), "ok")
|
78 | 80 | }
|
79 | 81 | }
|
| 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