MCPcopy Index your code
hub / github.com/docker/docker-agent / TestHTTPRequestVerification

Function TestHTTPRequestVerification

pkg/telemetry/telemetry_test.go:587–685  ·  view source on GitHub ↗

TestHTTPRequestVerification tests that HTTP requests are made correctly when telemetry is enabled

(t *testing.T)

Source from the content-addressed store, hash-verified

585
586// TestHTTPRequestVerification tests that HTTP requests are made correctly when telemetry is enabled
587func TestHTTPRequestVerification(t *testing.T) {
588 t.Parallel()
589 logger := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelDebug}))
590 mockHTTP := NewMockHTTPClient()
591
592 client := newClient(t.Context(), logger, true, true, "test-version", mockHTTP.Client)
593
594 client.endpoint = "https://test-telemetry.example.com/api/events"
595 client.apiKey = "test-api-key"
596 client.header = "test-header"
597
598 ctx := t.Context()
599
600 t.Run("CommandEventHTTPRequest", func(t *testing.T) {
601 // Reset mock before test
602 mockHTTP = NewMockHTTPClient()
603 client.httpClient = mockHTTP
604
605 event := &CommandEvent{
606 Action: "run",
607 Args: []string{"config.yaml"},
608 Success: true,
609 }
610
611 assert.NotEmpty(t, client.endpoint, "Client endpoint should be set for this test")
612 assert.NotEmpty(t, client.apiKey, "Client API key should be set for this test")
613 assert.True(t, client.enabled, "Client should be enabled for this test")
614
615 t.Logf("Before Track: endpoint=%s, apiKey len=%d, enabled=%t", client.endpoint, len(client.apiKey), client.enabled)
616
617 client.Track(ctx, event)
618
619 require.Eventually(t, func() bool {
620 return mockHTTP.GetRequestCount() > 0
621 }, time.Second, 5*time.Millisecond, "Expected HTTP request to be made")
622
623 t.Logf("HTTP requests captured: %d", mockHTTP.GetRequestCount())
624
625 requests := mockHTTP.GetRequests()
626 req := requests[0]
627
628 assert.Equal(t, http.MethodPost, req.Method, "Expected POST request")
629 assert.Equal(t, "https://test-telemetry.example.com/api/events", req.URL.String(), "Expected correct URL")
630
631 assert.Equal(t, "application/json", req.Header.Get("Content-Type"), "Expected Content-Type application/json")
632 assert.Equal(t, "cagent/test-version", req.Header.Get("User-Agent"), "Expected User-Agent cagent/test-version")
633 assert.Equal(t, "test-api-key", req.Header.Get("test-header"), "Expected test-header test-api-key")
634
635 bodies := mockHTTP.GetBodies()
636 assert.NotEmpty(t, bodies, "Expected request body to be captured")
637
638 var requestBody map[string]any
639 require.NoError(t, json.Unmarshal(bodies[0], &requestBody), "Failed to unmarshal request body")
640
641 records, ok := requestBody["records"].([]any)
642 require.True(t, ok, "Expected 'records' array in request body")
643 assert.Len(t, records, 1, "Expected 1 record")
644

Callers

nothing calls this directly

Calls 12

GetRequestCountMethod · 0.95
GetRequestsMethod · 0.95
GetBodiesMethod · 0.95
NewMockHTTPClientFunction · 0.85
newClientFunction · 0.85
ContextMethod · 0.80
RunMethod · 0.65
GetMethod · 0.65
LenMethod · 0.65
NewMethod · 0.45
TrackMethod · 0.45
StringMethod · 0.45

Tested by

no test coverage detected