您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

43 行
1.1KB

  1. package handlers
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "net/http/httptest"
  6. "testing"
  7. "time"
  8. "qctextbuilder/internal/logging"
  9. )
  10. func TestListLogsHonorsLimit(t *testing.T) {
  11. t.Parallel()
  12. recent := logging.NewRecentStore(10)
  13. recent.Add(logging.Entry{Timestamp: time.Now().UTC(), Level: "INFO", Message: "older"})
  14. recent.Add(logging.Entry{Timestamp: time.Now().UTC(), Level: "INFO", Message: "newer"})
  15. api := NewAPI(nil, nil, nil, nil, recent)
  16. req := httptest.NewRequest(http.MethodGet, "/api/logs?limit=1", nil)
  17. w := httptest.NewRecorder()
  18. api.ListLogs(w, req)
  19. if w.Code != http.StatusOK {
  20. t.Fatalf("unexpected status: %d", w.Code)
  21. }
  22. var payload struct {
  23. Count int `json:"count"`
  24. Logs []logging.Entry `json:"logs"`
  25. }
  26. if err := json.Unmarshal(w.Body.Bytes(), &payload); err != nil {
  27. t.Fatalf("decode response: %v", err)
  28. }
  29. if payload.Count != 1 || len(payload.Logs) != 1 {
  30. t.Fatalf("unexpected payload: %+v", payload)
  31. }
  32. if payload.Logs[0].Message != "newer" {
  33. t.Fatalf("expected newest log entry, got %q", payload.Logs[0].Message)
  34. }
  35. }