|
7 | 7 | "net/http" |
8 | 8 | "net/http/httptest" |
9 | 9 | "strconv" |
| 10 | + "strings" |
10 | 11 | "testing" |
11 | 12 |
|
12 | 13 | "sb-proxy/backend/models" |
@@ -171,3 +172,39 @@ func TestCheckNodeIPRejectsSocksFallback(t *testing.T) { |
171 | 172 | t.Fatalf("expected cleared status after fallback, got ip=%s location=%s country=%s latency=%d", ip, location, countryCode, latency) |
172 | 173 | } |
173 | 174 | } |
| 175 | + |
| 176 | +func TestUpdateNodeRemarkUpdatesRemark(t *testing.T) { |
| 177 | + gin.SetMode(gin.TestMode) |
| 178 | + |
| 179 | + handler := newTestHandler(t, func(proxyAddr, username, password string) (*services.IPInfo, error) { |
| 180 | + return nil, fmt.Errorf("not used") |
| 181 | + }) |
| 182 | + |
| 183 | + nodeID := insertTestNode(t, handler.db) |
| 184 | + |
| 185 | + rec := httptest.NewRecorder() |
| 186 | + ctx, _ := gin.CreateTestContext(rec) |
| 187 | + ctx.Params = gin.Params{gin.Param{Key: "id", Value: strconv.Itoa(nodeID)}} |
| 188 | + |
| 189 | + req, _ := http.NewRequest( |
| 190 | + http.MethodPut, |
| 191 | + "/api/nodes/"+strconv.Itoa(nodeID)+"/remark", |
| 192 | + strings.NewReader(`{"remark":"hello"}`), |
| 193 | + ) |
| 194 | + req.Header.Set("Content-Type", "application/json") |
| 195 | + ctx.Request = req |
| 196 | + |
| 197 | + handler.UpdateNodeRemark(ctx) |
| 198 | + |
| 199 | + if rec.Code != http.StatusOK { |
| 200 | + t.Fatalf("unexpected status %d", rec.Code) |
| 201 | + } |
| 202 | + |
| 203 | + var remark string |
| 204 | + if err := handler.db.QueryRow("SELECT remark FROM proxy_nodes WHERE id = ?", nodeID).Scan(&remark); err != nil { |
| 205 | + t.Fatalf("query node: %v", err) |
| 206 | + } |
| 207 | + if remark != "hello" { |
| 208 | + t.Fatalf("expected remark to be updated, got %q", remark) |
| 209 | + } |
| 210 | +} |
0 commit comments