TestResponseFormat 测试响应格式
(t *testing.T)
| 502 | |
| 503 | // TestResponseFormat 测试响应格式 |
| 504 | func TestResponseFormat(t *testing.T) { |
| 505 | srv, cleanup := setupTestServer(t) |
| 506 | defer cleanup() |
| 507 | |
| 508 | t.Run("SuccessResponseFormat", func(t *testing.T) { |
| 509 | req := httptest.NewRequest(http.MethodGet, "/v1/rooms", nil) |
| 510 | w := httptest.NewRecorder() |
| 511 | |
| 512 | srv.Router().ServeHTTP(w, req) |
| 513 | |
| 514 | assert.Equal(t, http.StatusOK, w.Code) |
| 515 | |
| 516 | var resp map[string]any |
| 517 | err := json.Unmarshal(w.Body.Bytes(), &resp) |
| 518 | require.NoError(t, err) |
| 519 | |
| 520 | assert.Contains(t, resp, "success") |
| 521 | assert.True(t, resp["success"].(bool)) |
| 522 | assert.Contains(t, resp, "data") |
| 523 | }) |
| 524 | |
| 525 | t.Run("ErrorResponseFormat", func(t *testing.T) { |
| 526 | req := httptest.NewRequest(http.MethodGet, "/v1/agents/nonexistent", nil) |
| 527 | w := httptest.NewRecorder() |
| 528 | |
| 529 | srv.Router().ServeHTTP(w, req) |
| 530 | |
| 531 | assert.Equal(t, http.StatusNotFound, w.Code) |
| 532 | |
| 533 | var resp map[string]any |
| 534 | err := json.Unmarshal(w.Body.Bytes(), &resp) |
| 535 | require.NoError(t, err) |
| 536 | |
| 537 | assert.Contains(t, resp, "success") |
| 538 | assert.False(t, resp["success"].(bool)) |
| 539 | assert.Contains(t, resp, "error") |
| 540 | |
| 541 | errorObj := resp["error"].(map[string]any) |
| 542 | assert.Contains(t, errorObj, "code") |
| 543 | assert.Contains(t, errorObj, "message") |
| 544 | }) |
| 545 | } |
nothing calls this directly
no test coverage detected