From b31db3f0d2a55e8d71366f683acb1c46e10e7f1e Mon Sep 17 00:00:00 2001 From: Saloni Date: Mon, 29 Apr 2024 17:44:16 -0600 Subject: [PATCH] address nit comments and add unit tests --- .../mode/static/nginx/config/servers_test.go | 68 +++++++++++++++++++ .../mode/static/state/graph/httproute_test.go | 4 +- 2 files changed, 70 insertions(+), 2 deletions(-) diff --git a/internal/mode/static/nginx/config/servers_test.go b/internal/mode/static/nginx/config/servers_test.go index b7f52a35a4..b84aee43a4 100644 --- a/internal/mode/static/nginx/config/servers_test.go +++ b/internal/mode/static/nginx/config/servers_test.go @@ -1948,3 +1948,71 @@ func TestConvertBackendTLSFromGroup(t *testing.T) { }) } } + +func TestGenerateResponseHeaders(t *testing.T) { + tests := []struct { + filters *dataplane.HTTPFilters + msg string + expectedHeaders http.ResponseHeaders + }{ + { + msg: "no filter set", + filters: &dataplane.HTTPFilters{ + RequestHeaderModifiers: &dataplane.HTTPHeaderFilter{}, + }, + expectedHeaders: http.ResponseHeaders{}, + }, + { + msg: "set filters correctly", + filters: &dataplane.HTTPFilters{ + ResponseHeaderModifiers: &dataplane.HTTPHeaderFilter{ + Add: []dataplane.HTTPHeader{ + { + Name: "Accept-Encoding", + Value: "gzip", + }, + { + Name: "Authorization", + Value: "my-auth", + }, + }, + Set: []dataplane.HTTPHeader{ + { + Name: "Accept-Encoding", + Value: "my-new-overwritten-value", + }, + }, + Remove: []string{"Authorization"}, + }, + }, + expectedHeaders: http.ResponseHeaders{ + Add: []http.Header{ + { + Name: "Accept-Encoding", + Value: "gzip", + }, + { + Name: "Authorization", + Value: "my-auth", + }, + }, + Set: []http.Header{ + { + Name: "Accept-Encoding", + Value: "my-new-overwritten-value", + }, + }, + Remove: []string{"Authorization"}, + }, + }, + } + + for _, tc := range tests { + t.Run(tc.msg, func(t *testing.T) { + g := NewWithT(t) + + headers := generateResponseHeaders(tc.filters) + g.Expect(headers).To(Equal(tc.expectedHeaders)) + }) + } +} diff --git a/internal/mode/static/state/graph/httproute_test.go b/internal/mode/static/state/graph/httproute_test.go index fe90d6c5b4..3d797adec5 100644 --- a/internal/mode/static/state/graph/httproute_test.go +++ b/internal/mode/static/state/graph/httproute_test.go @@ -2563,7 +2563,7 @@ func TestValidateFilterResponseHeaderModifier(t *testing.T) { }, }, expectErrCount: 3, - name: "invalid response header modifier filter", + name: "response header modifier filter with disallowed header name", }, { validator: createAllValidValidator(), @@ -2580,7 +2580,7 @@ func TestValidateFilterResponseHeaderModifier(t *testing.T) { }, }, expectErrCount: 3, - name: "invalid response header modifier filter", + name: "response header modifier filter with disallowed header name prefix", }, }