From 5e8e930c27e9290988d0a9f13f9bc475bab7e816 Mon Sep 17 00:00:00 2001 From: craig Date: Thu, 14 Nov 2024 10:16:57 +0000 Subject: [PATCH] add basic unit test to see passed headers are added to the request Signed-off-by: craig rh-pre-commit.version: 2.2.0 rh-pre-commit.check-secrets: ENABLED --- internal/probes/worker_test.go | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/internal/probes/worker_test.go b/internal/probes/worker_test.go index 0bf33d0..22f421a 100644 --- a/internal/probes/worker_test.go +++ b/internal/probes/worker_test.go @@ -2,6 +2,7 @@ package probes_test import ( "context" + "fmt" "net/http" "sync" "testing" @@ -42,9 +43,24 @@ func TestWorker_ProbeSuccess(t *testing.T) { Status: v1alpha1.DNSHealthCheckProbeStatus{}, } + var testHeaders = []v1alpha1.AdditionalHeader{ + {Name: "first", Value: "test"}, + {Name: "second", Value: "test"}, + } + + var validateHeaders = func(r *http.Request, headers v1alpha1.AdditionalHeaders) error { + for _, kv := range headers { + header := r.Header.Get(kv.Name) + if header != kv.Value { + return fmt.Errorf("expected header %s with value %s but got value %s ", kv.Name, kv.Value, header) + } + } + return nil + } + testCases := []struct { Name string - Transport func() probes.RoundTripperFunc + Transport func(headers v1alpha1.AdditionalHeaders) probes.RoundTripperFunc ProbeConfig func() *v1alpha1.DNSHealthCheckProbe ProbeHeaders v1alpha1.AdditionalHeaders Validate func(t *testing.T, results []probes.ProbeResult, tt *testTransport, expectedCalls int) @@ -53,17 +69,18 @@ func TestWorker_ProbeSuccess(t *testing.T) { }{ { Name: "test health check success", - Transport: func() probes.RoundTripperFunc { + Transport: func(expectedHeaders v1alpha1.AdditionalHeaders) probes.RoundTripperFunc { return func(r *http.Request) (*http.Response, error) { return &http.Response{ StatusCode: 200, - }, nil + }, validateHeaders(r, expectedHeaders) } }, ExpectedProbeCalls: 5, ProbeConfig: func() *v1alpha1.DNSHealthCheckProbe { return testProbe.DeepCopy() }, + ProbeHeaders: testHeaders, Validate: func(t *testing.T, results []probes.ProbeResult, tt *testTransport, expectedCalls int) { if len(results) != expectedCalls { t.Fatalf("expected %v results got %v", expectedCalls, len(results)) @@ -86,7 +103,7 @@ func TestWorker_ProbeSuccess(t *testing.T) { }, { Name: "test healthcheck failure ", - Transport: func() probes.RoundTripperFunc { + Transport: func(headers v1alpha1.AdditionalHeaders) probes.RoundTripperFunc { return func(r *http.Request) (*http.Response, error) { return &http.Response{ StatusCode: 503, @@ -117,7 +134,7 @@ func TestWorker_ProbeSuccess(t *testing.T) { probeConfig := test.ProbeConfig() w := probes.NewProbe(test.ProbeHeaders) tTransport := &testTransport{} - tTransport.f = test.Transport() + tTransport.f = test.Transport(test.ProbeHeaders) w.Transport = tTransport.countedHTTP ctx, cancel := context.WithCancel(context.TODO()) //setup our channel