Skip to content

Commit

Permalink
Merge pull request #304 from Kuadrant/add-headers-test
Browse files Browse the repository at this point in the history
add basic unit test to see passed headers are added to the request
  • Loading branch information
maleck13 authored Nov 14, 2024
2 parents ddc5ed8 + 5e8e930 commit ee206a5
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions internal/probes/worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package probes_test

import (
"context"
"fmt"
"net/http"
"sync"
"testing"
Expand Down Expand Up @@ -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)
Expand All @@ -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))
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit ee206a5

Please sign in to comment.