MCPcopy
hub / github.com/netdata/netdata / TestHandler

Function TestHandler

packaging/tools/agent-events/server_test.go:101–658  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

99}
100
101func TestHandler(t *testing.T) {
102 setupTest(t) // Setup once for all sub-tests
103
104 // --- Test Cases ---
105
106 t.Run("FirstValidRequest", func(t *testing.T) {
107 t.Cleanup(resetDedupState) // Reset map for isolation
108 jsonBody := `{"id": "uuid-1", "data": "value1"}`
109 req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(jsonBody))
110 rr := httptest.NewRecorder()
111 stdout, _ := captureOutput(t, func() { handler(rr, req) })
112 if status := rr.Code; status != http.StatusOK {
113 t.Errorf("status: got %v want %v", status, http.StatusOK)
114 }
115 if rr.Body.String() != "OK" {
116 t.Errorf("body: got %v want %v", rr.Body.String(), "OK")
117 }
118 if !strings.Contains(stdout, `"id":"uuid-1"`) {
119 t.Errorf("stdout missing id: %q", stdout)
120 }
121 mapMutex.Lock()
122 mapLen := len(seenIDs)
123 mapMutex.Unlock()
124 if mapLen != 1 {
125 t.Errorf("map size: got %d want 1", mapLen)
126 }
127 })
128
129 t.Run("DuplicateRequestWithinWindow", func(t *testing.T) {
130 t.Cleanup(resetDedupState)
131 firstJsonBody := `{"id": "uuid-2", "data": "value2"}`
132 firstReq := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(firstJsonBody))
133 firstRr := httptest.NewRecorder()
134 captureOutput(t, func() { handler(firstRr, firstReq) })
135 if firstRr.Code != http.StatusOK {
136 t.Fatalf("Setup failed")
137 }
138 req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(firstJsonBody))
139 rr := httptest.NewRecorder()
140 stdout, _ := captureOutput(t, func() { handler(rr, req) })
141 if status := rr.Code; status != http.StatusOK {
142 t.Errorf("status: got %v want %v", status, http.StatusOK)
143 }
144 if rr.Body.String() != "OK" {
145 t.Errorf("body: got %v want %v", rr.Body.String(), "OK")
146 }
147 if stdout != "" {
148 t.Errorf("stdout not empty: %q", stdout)
149 }
150 mapMutex.Lock()
151 mapLen := len(seenIDs)
152 mapMutex.Unlock()
153 if mapLen != 1 {
154 t.Errorf("map size: got %d want 1", mapLen)
155 }
156 })
157
158 t.Run("InvalidJSON", func(t *testing.T) {

Callers

nothing calls this directly

Calls 15

setupTestFunction · 0.85
captureOutputFunction · 0.85
handlerFunction · 0.85
healthHandlerFunction · 0.85
processInterface · 0.85
LockMethod · 0.80
UnlockMethod · 0.80
SubMethod · 0.80
RunMethod · 0.65
CleanupMethod · 0.65
ErrorfMethod · 0.65
ContainsMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…