|
| 1 | +/* |
| 2 | +Copyright 2025 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package handlers |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "testing" |
| 22 | + |
| 23 | + basepb "github.com/envoyproxy/go-control-plane/envoy/config/core/v3" |
| 24 | + eppb "github.com/envoyproxy/go-control-plane/envoy/service/ext_proc/v3" |
| 25 | + extProcPb "github.com/envoyproxy/go-control-plane/envoy/service/ext_proc/v3" |
| 26 | + "github.com/google/go-cmp/cmp" |
| 27 | + "google.golang.org/protobuf/testing/protocmp" |
| 28 | + "sigs.k8s.io/controller-runtime/pkg/log" |
| 29 | + logutil "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/util/logging" |
| 30 | +) |
| 31 | + |
| 32 | +func TestProcessRequestBody(t *testing.T) { |
| 33 | + ctx := logutil.NewTestLoggerIntoContext(context.Background()) |
| 34 | + |
| 35 | + cases := []struct { |
| 36 | + desc string |
| 37 | + streaming bool |
| 38 | + bodys []*extProcPb.HttpBody |
| 39 | + want []*extProcPb.ProcessingResponse |
| 40 | + }{ |
| 41 | + { |
| 42 | + desc: "no-streaming", |
| 43 | + bodys: []*extProcPb.HttpBody{ |
| 44 | + { |
| 45 | + Body: mapToBytes(t, map[string]any{ |
| 46 | + "model": "foo", |
| 47 | + }), |
| 48 | + }, |
| 49 | + }, |
| 50 | + want: []*extProcPb.ProcessingResponse{ |
| 51 | + { |
| 52 | + Response: &extProcPb.ProcessingResponse_RequestBody{ |
| 53 | + RequestBody: &extProcPb.BodyResponse{ |
| 54 | + Response: &extProcPb.CommonResponse{ |
| 55 | + // Necessary so that the new headers are used in the routing decision. |
| 56 | + ClearRouteCache: true, |
| 57 | + HeaderMutation: &extProcPb.HeaderMutation{ |
| 58 | + SetHeaders: []*basepb.HeaderValueOption{ |
| 59 | + { |
| 60 | + Header: &basepb.HeaderValue{ |
| 61 | + Key: modelHeader, |
| 62 | + RawValue: []byte("foo"), |
| 63 | + }, |
| 64 | + }, |
| 65 | + }, |
| 66 | + }, |
| 67 | + }, |
| 68 | + }, |
| 69 | + }, |
| 70 | + }, |
| 71 | + }, |
| 72 | + }, |
| 73 | + { |
| 74 | + desc: "streaming", |
| 75 | + streaming: true, |
| 76 | + bodys: []*extProcPb.HttpBody{ |
| 77 | + { |
| 78 | + Body: mapToBytes(t, map[string]any{ |
| 79 | + "model": "foo", |
| 80 | + }), |
| 81 | + }, |
| 82 | + { |
| 83 | + EndOfStream: true, |
| 84 | + }, |
| 85 | + }, |
| 86 | + want: []*extProcPb.ProcessingResponse{ |
| 87 | + { |
| 88 | + Response: &eppb.ProcessingResponse_RequestHeaders{ |
| 89 | + RequestHeaders: &eppb.HeadersResponse{ |
| 90 | + Response: &eppb.CommonResponse{ |
| 91 | + ClearRouteCache: true, |
| 92 | + HeaderMutation: &eppb.HeaderMutation{ |
| 93 | + SetHeaders: []*basepb.HeaderValueOption{ |
| 94 | + { |
| 95 | + Header: &basepb.HeaderValue{ |
| 96 | + Key: modelHeader, |
| 97 | + RawValue: []byte("foo"), |
| 98 | + }, |
| 99 | + }, |
| 100 | + }, |
| 101 | + }, |
| 102 | + }, |
| 103 | + }, |
| 104 | + }, |
| 105 | + }, |
| 106 | + { |
| 107 | + Response: &extProcPb.ProcessingResponse_RequestBody{ |
| 108 | + RequestBody: &extProcPb.BodyResponse{ |
| 109 | + Response: &extProcPb.CommonResponse{ |
| 110 | + BodyMutation: &extProcPb.BodyMutation{ |
| 111 | + Mutation: &extProcPb.BodyMutation_StreamedResponse{ |
| 112 | + StreamedResponse: &extProcPb.StreamedBodyResponse{ |
| 113 | + Body: mapToBytes(t, map[string]any{ |
| 114 | + "model": "foo", |
| 115 | + }), |
| 116 | + EndOfStream: true, |
| 117 | + }, |
| 118 | + }, |
| 119 | + }, |
| 120 | + }, |
| 121 | + }, |
| 122 | + }, |
| 123 | + }, |
| 124 | + }, |
| 125 | + }, |
| 126 | + } |
| 127 | + |
| 128 | + for _, tc := range cases { |
| 129 | + t.Run(tc.desc, func(t *testing.T) { |
| 130 | + srv := NewServer(tc.streaming) |
| 131 | + streamedBody := &streamedBody{} |
| 132 | + for i, body := range tc.bodys { |
| 133 | + got, err := srv.processRequestBody(context.Background(), body, streamedBody, log.FromContext(ctx)) |
| 134 | + if err != nil { |
| 135 | + t.Fatalf("processRequestBody(): %v", err) |
| 136 | + } |
| 137 | + |
| 138 | + if i == len(tc.bodys)-1 { |
| 139 | + if diff := cmp.Diff(tc.want, got, protocmp.Transform()); diff != "" { |
| 140 | + t.Errorf("processRequestBody returned unexpected response, diff(-want, +got): %v", diff) |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + }) |
| 145 | + } |
| 146 | +} |
0 commit comments