diff --git a/filter/response.go b/filter/response.go index f733bcc..b913585 100644 --- a/filter/response.go +++ b/filter/response.go @@ -142,6 +142,11 @@ func NewImmediateResponseBuilder() *ImmediateResponseWriter { // headerAction sets a header with the given key and value and the given append action func (irw *ImmediateResponseWriter) headerAction(key string, value string, appendAction corev3.HeaderValueOption_HeaderAppendAction) *ImmediateResponseWriter { + var shouldAppend *wrappers.BoolValue + if appendAction == corev3.HeaderValueOption_APPEND_IF_EXISTS_OR_ADD { + shouldAppend = &wrappers.BoolValue{Value: true} // FIXME: This is not the documented behavior but it seems to be the only way to append a header. + } + //nolint:staticcheck // Envoy still requires Append=true to preserve append semantics. irw.immediateResponse.ImmediateResponse.Headers.SetHeaders = append(irw.immediateResponse.ImmediateResponse.Headers.SetHeaders, &corev3.HeaderValueOption{ Header: &corev3.HeaderValue{ Key: key, @@ -152,6 +157,7 @@ func (irw *ImmediateResponseWriter) headerAction(key string, value string, appen RawValue: []byte(value), // FIXME: This depends on Envoy }, AppendAction: appendAction, + Append: shouldAppend, }) return irw } diff --git a/filter/response_test.go b/filter/response_test.go new file mode 100644 index 0000000..75b3c46 --- /dev/null +++ b/filter/response_test.go @@ -0,0 +1,39 @@ +package filter_test + +import ( + "testing" + + "github.com/getyourguide/extproc-go/filter" + "github.com/stretchr/testify/require" +) + +// Envoy's ext_proc only appends a header when the deprecated append field is true. When it is unset, +// Envoy overwrites, whatever append_action says, so every header but the last with that name is lost. +func TestImmediateResponseHeaderAppend(t *testing.T) { + for _, tt := range []struct { + name string + build func(irw *filter.ImmediateResponseWriter) *filter.ImmediateResponseWriter + wantAppend []bool + }{{ + name: "append header sets append", + build: func(irw *filter.ImmediateResponseWriter) *filter.ImmediateResponseWriter { + return irw.AppendHeader("set-cookie", "a=1").AppendHeader("set-cookie", "b=2") + }, + wantAppend: []bool{true, true}, + }, { + name: "set header does not set append", + build: func(irw *filter.ImmediateResponseWriter) *filter.ImmediateResponseWriter { + return irw.SetHeader("location", "/a") + }, + wantAppend: []bool{false}, + }} { + t.Run(tt.name, func(t *testing.T) { + res := tt.build(filter.NewImmediateResponseBuilder()).ImmediateResponse() + var gotAppend []bool + for _, h := range res.ImmediateResponse.GetHeaders().GetSetHeaders() { + gotAppend = append(gotAppend, h.GetAppend().GetValue()) //nolint:staticcheck // Append is what Envoy reads, see above. + } + require.Equal(t, tt.wantAppend, gotAppend) + }) + } +}