Skip to content

Commit 1839e85

Browse files
test(envoy): add unit tests for GenerateHeadersMutation and SortSetHeadersInResponses (kubernetes-sigs#2529)
These envoy util functions lacked test coverage. Add 13 table-driven test cases covering edge cases like nil safety, empty inputs, map iteration non-determinism, and mixed response types. Signed-off-by: Yehudit Kerido <yehudit1987@gmail.com>
1 parent 85c398c commit 1839e85

File tree

3 files changed

+441
-0
lines changed

3 files changed

+441
-0
lines changed

pkg/common/envoy/headers_test.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,13 @@ limitations under the License.
1717
package envoy
1818

1919
import (
20+
"sort"
2021
"testing"
2122

2223
corev3 "github.com/envoyproxy/go-control-plane/envoy/config/core/v3"
2324
extProcPb "github.com/envoyproxy/go-control-plane/envoy/service/ext_proc/v3"
25+
"github.com/google/go-cmp/cmp"
26+
"google.golang.org/protobuf/testing/protocmp"
2427
)
2528

2629
func TestExtractHeaderValue(t *testing.T) {
@@ -82,6 +85,89 @@ func TestExtractHeaderValue(t *testing.T) {
8285
}
8386
}
8487

88+
func TestGenerateHeadersMutation(t *testing.T) {
89+
tests := []struct {
90+
name string
91+
headers map[string]string
92+
want []*corev3.HeaderValueOption
93+
}{
94+
{
95+
name: "empty map returns empty slice",
96+
headers: map[string]string{},
97+
want: []*corev3.HeaderValueOption{},
98+
},
99+
{
100+
name: "single header",
101+
headers: map[string]string{"x-api-key": "secret-123"},
102+
want: []*corev3.HeaderValueOption{
103+
{
104+
Header: &corev3.HeaderValue{
105+
Key: "x-api-key",
106+
RawValue: []byte("secret-123"),
107+
},
108+
},
109+
},
110+
},
111+
{
112+
name: "multiple headers",
113+
headers: map[string]string{
114+
"x-api-key": "key-val",
115+
"x-request-id": "req-456",
116+
"authorization": "Bearer tok",
117+
},
118+
want: []*corev3.HeaderValueOption{
119+
{
120+
Header: &corev3.HeaderValue{
121+
Key: "authorization",
122+
RawValue: []byte("Bearer tok"),
123+
},
124+
},
125+
{
126+
Header: &corev3.HeaderValue{
127+
Key: "x-api-key",
128+
RawValue: []byte("key-val"),
129+
},
130+
},
131+
{
132+
Header: &corev3.HeaderValue{
133+
Key: "x-request-id",
134+
RawValue: []byte("req-456"),
135+
},
136+
},
137+
},
138+
},
139+
{
140+
name: "header with empty value",
141+
headers: map[string]string{"x-empty": ""},
142+
want: []*corev3.HeaderValueOption{
143+
{
144+
Header: &corev3.HeaderValue{
145+
Key: "x-empty",
146+
RawValue: []byte(""),
147+
},
148+
},
149+
},
150+
},
151+
}
152+
153+
sortByKey := func(opts []*corev3.HeaderValueOption) {
154+
sort.Slice(opts, func(i, j int) bool {
155+
return opts[i].GetHeader().GetKey() < opts[j].GetHeader().GetKey()
156+
})
157+
}
158+
159+
for _, tt := range tests {
160+
t.Run(tt.name, func(t *testing.T) {
161+
got := GenerateHeadersMutation(tt.headers)
162+
sortByKey(got)
163+
sortByKey(tt.want)
164+
if diff := cmp.Diff(tt.want, got, protocmp.Transform()); diff != "" {
165+
t.Errorf("GenerateHeadersMutation() mismatch (-want +got):\n%s", diff)
166+
}
167+
})
168+
}
169+
}
170+
85171
func TestGetHeaderValue(t *testing.T) {
86172
tests := []struct {
87173
name string

pkg/common/envoy/test/utils.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@ func SortSetHeadersInResponses(responses []*extProcPb.ProcessingResponse) {
3939
if rr.RequestBody != nil && rr.RequestBody.Response != nil {
4040
common = rr.RequestBody.Response
4141
}
42+
case *extProcPb.ProcessingResponse_ResponseHeaders:
43+
if rr.ResponseHeaders != nil && rr.ResponseHeaders.Response != nil {
44+
common = rr.ResponseHeaders.Response
45+
}
46+
case *extProcPb.ProcessingResponse_ResponseBody:
47+
if rr.ResponseBody != nil && rr.ResponseBody.Response != nil {
48+
common = rr.ResponseBody.Response
49+
}
4250
default:
4351
continue
4452
}

0 commit comments

Comments
 (0)