MCPcopy Create free account
hub / github.com/cronitorio/cronitor-cli / TestFetchAllPages_StopsOnEmptyItems

Function TestFetchAllPages_StopsOnEmptyItems

cmd/ui_test.go:88–133  ·  view source on GitHub ↗

--------------------------------------------------------------------------- FetchAllPages ---------------------------------------------------------------------------

(t *testing.T)

Source from the content-addressed store, hash-verified

86// ---------------------------------------------------------------------------
87
88func TestFetchAllPages_StopsOnEmptyItems(t *testing.T) {
89 viper.Set("CRONITOR_API_KEY", "test-key")
90 defer viper.Set("CRONITOR_API_KEY", "")
91
92 var requestCount atomic.Int32
93
94 server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
95 requestCount.Add(1)
96 page := r.URL.Query().Get("page")
97 var body string
98 switch page {
99 case "", "1":
100 body = `{"items":[{"id":1}]}`
101 case "2":
102 body = `{"items":[{"id":2}]}`
103 default:
104 body = `{"items":[]}`
105 }
106 w.Header().Set("Content-Type", "application/json")
107 w.WriteHeader(200)
108 w.Write([]byte(body))
109 }))
110 defer server.Close()
111
112 client := &lib.APIClient{
113 BaseURL: server.URL,
114 ApiKey: "test-key",
115 UserAgent: "test",
116 }
117
118 pages, err := FetchAllPages(client, "/monitors", nil, "items")
119 if err != nil {
120 t.Fatalf("unexpected error: %v", err)
121 }
122
123 // Pages 1, 2, and 3 are fetched; page 3 is the empty one that stops iteration.
124 // The empty page body is still included in the returned slice.
125 if len(pages) != 3 {
126 t.Fatalf("expected 3 pages, got %d", len(pages))
127 }
128
129 // Verify that request count matches: pages 1, 2, 3
130 if int(requestCount.Load()) != 3 {
131 t.Errorf("expected 3 requests, got %d", requestCount.Load())
132 }
133}
134
135func TestFetchAllPages_PageQueryParamsIncrement(t *testing.T) {
136 viper.Set("CRONITOR_API_KEY", "test-key")

Callers

nothing calls this directly

Calls 4

FetchAllPagesFunction · 0.85
GetMethod · 0.80
CloseMethod · 0.80
WriteMethod · 0.45

Tested by

no test coverage detected