|
| 1 | +package extproc |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + core "github.com/envoyproxy/go-control-plane/envoy/config/core/v3" |
| 7 | + ext_proc "github.com/envoyproxy/go-control-plane/envoy/service/ext_proc/v3" |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | +) |
| 10 | + |
| 11 | +func TestVSRHeadersAddedOnSuccessfulNonCachedResponse(t *testing.T) { |
| 12 | + // Create a mock router |
| 13 | + router := &OpenAIRouter{} |
| 14 | + |
| 15 | + // Create request context with VSR decision information |
| 16 | + ctx := &RequestContext{ |
| 17 | + VSRSelectedCategory: "math", |
| 18 | + VSRReasoningMode: "on", |
| 19 | + VSRSelectedModel: "deepseek-v31", |
| 20 | + VSRCacheHit: false, // Not a cache hit |
| 21 | + } |
| 22 | + |
| 23 | + // Create response headers with successful status (200) |
| 24 | + responseHeaders := &ext_proc.ProcessingRequest_ResponseHeaders{ |
| 25 | + ResponseHeaders: &ext_proc.HttpHeaders{ |
| 26 | + Headers: &core.HeaderMap{ |
| 27 | + Headers: []*core.HeaderValue{ |
| 28 | + {Key: ":status", Value: "200"}, |
| 29 | + {Key: "content-type", Value: "application/json"}, |
| 30 | + }, |
| 31 | + }, |
| 32 | + }, |
| 33 | + } |
| 34 | + |
| 35 | + // Call handleResponseHeaders |
| 36 | + response, err := router.handleResponseHeaders(responseHeaders, ctx) |
| 37 | + |
| 38 | + // Verify no error occurred |
| 39 | + assert.NoError(t, err) |
| 40 | + assert.NotNil(t, response) |
| 41 | + |
| 42 | + // Verify response structure |
| 43 | + assert.NotNil(t, response.GetResponseHeaders()) |
| 44 | + assert.NotNil(t, response.GetResponseHeaders().GetResponse()) |
| 45 | + |
| 46 | + // Verify VSR headers were added |
| 47 | + headerMutation := response.GetResponseHeaders().GetResponse().GetHeaderMutation() |
| 48 | + assert.NotNil(t, headerMutation, "HeaderMutation should not be nil for successful non-cached response") |
| 49 | + |
| 50 | + setHeaders := headerMutation.GetSetHeaders() |
| 51 | + assert.Len(t, setHeaders, 3, "Should have 3 VSR headers") |
| 52 | + |
| 53 | + // Verify each header |
| 54 | + headerMap := make(map[string]string) |
| 55 | + for _, header := range setHeaders { |
| 56 | + headerMap[header.Header.Key] = string(header.Header.RawValue) |
| 57 | + } |
| 58 | + |
| 59 | + assert.Equal(t, "math", headerMap["x-vsr-selected-category"]) |
| 60 | + assert.Equal(t, "on", headerMap["x-vsr-selected-reasoning"]) |
| 61 | + assert.Equal(t, "deepseek-v31", headerMap["x-vsr-selected-model"]) |
| 62 | +} |
| 63 | + |
| 64 | +func TestVSRHeadersNotAddedOnCacheHit(t *testing.T) { |
| 65 | + // Create a mock router |
| 66 | + router := &OpenAIRouter{} |
| 67 | + |
| 68 | + // Create request context with cache hit |
| 69 | + ctx := &RequestContext{ |
| 70 | + VSRSelectedCategory: "math", |
| 71 | + VSRReasoningMode: "on", |
| 72 | + VSRSelectedModel: "deepseek-v31", |
| 73 | + VSRCacheHit: true, // Cache hit - headers should not be added |
| 74 | + } |
| 75 | + |
| 76 | + // Create response headers with successful status (200) |
| 77 | + responseHeaders := &ext_proc.ProcessingRequest_ResponseHeaders{ |
| 78 | + ResponseHeaders: &ext_proc.HttpHeaders{ |
| 79 | + Headers: &core.HeaderMap{ |
| 80 | + Headers: []*core.HeaderValue{ |
| 81 | + {Key: ":status", Value: "200"}, |
| 82 | + {Key: "content-type", Value: "application/json"}, |
| 83 | + }, |
| 84 | + }, |
| 85 | + }, |
| 86 | + } |
| 87 | + |
| 88 | + // Call handleResponseHeaders |
| 89 | + response, err := router.handleResponseHeaders(responseHeaders, ctx) |
| 90 | + |
| 91 | + // Verify no error occurred |
| 92 | + assert.NoError(t, err) |
| 93 | + assert.NotNil(t, response) |
| 94 | + |
| 95 | + // Verify VSR headers were NOT added due to cache hit |
| 96 | + headerMutation := response.GetResponseHeaders().GetResponse().GetHeaderMutation() |
| 97 | + assert.Nil(t, headerMutation, "HeaderMutation should be nil for cache hit") |
| 98 | +} |
| 99 | + |
| 100 | +func TestVSRHeadersNotAddedOnErrorResponse(t *testing.T) { |
| 101 | + // Create a mock router |
| 102 | + router := &OpenAIRouter{} |
| 103 | + |
| 104 | + // Create request context with VSR decision information |
| 105 | + ctx := &RequestContext{ |
| 106 | + VSRSelectedCategory: "math", |
| 107 | + VSRReasoningMode: "on", |
| 108 | + VSRSelectedModel: "deepseek-v31", |
| 109 | + VSRCacheHit: false, // Not a cache hit |
| 110 | + } |
| 111 | + |
| 112 | + // Create response headers with error status (500) |
| 113 | + responseHeaders := &ext_proc.ProcessingRequest_ResponseHeaders{ |
| 114 | + ResponseHeaders: &ext_proc.HttpHeaders{ |
| 115 | + Headers: &core.HeaderMap{ |
| 116 | + Headers: []*core.HeaderValue{ |
| 117 | + {Key: ":status", Value: "500"}, |
| 118 | + {Key: "content-type", Value: "application/json"}, |
| 119 | + }, |
| 120 | + }, |
| 121 | + }, |
| 122 | + } |
| 123 | + |
| 124 | + // Call handleResponseHeaders |
| 125 | + response, err := router.handleResponseHeaders(responseHeaders, ctx) |
| 126 | + |
| 127 | + // Verify no error occurred |
| 128 | + assert.NoError(t, err) |
| 129 | + assert.NotNil(t, response) |
| 130 | + |
| 131 | + // Verify VSR headers were NOT added due to error status |
| 132 | + headerMutation := response.GetResponseHeaders().GetResponse().GetHeaderMutation() |
| 133 | + assert.Nil(t, headerMutation, "HeaderMutation should be nil for error response") |
| 134 | +} |
| 135 | + |
| 136 | +func TestVSRHeadersPartialInformation(t *testing.T) { |
| 137 | + // Create a mock router |
| 138 | + router := &OpenAIRouter{} |
| 139 | + |
| 140 | + // Create request context with partial VSR information |
| 141 | + ctx := &RequestContext{ |
| 142 | + VSRSelectedCategory: "math", |
| 143 | + VSRReasoningMode: "", // Empty reasoning mode |
| 144 | + VSRSelectedModel: "deepseek-v31", |
| 145 | + VSRCacheHit: false, |
| 146 | + } |
| 147 | + |
| 148 | + // Create response headers with successful status (200) |
| 149 | + responseHeaders := &ext_proc.ProcessingRequest_ResponseHeaders{ |
| 150 | + ResponseHeaders: &ext_proc.HttpHeaders{ |
| 151 | + Headers: &core.HeaderMap{ |
| 152 | + Headers: []*core.HeaderValue{ |
| 153 | + {Key: ":status", Value: "200"}, |
| 154 | + {Key: "content-type", Value: "application/json"}, |
| 155 | + }, |
| 156 | + }, |
| 157 | + }, |
| 158 | + } |
| 159 | + |
| 160 | + // Call handleResponseHeaders |
| 161 | + response, err := router.handleResponseHeaders(responseHeaders, ctx) |
| 162 | + |
| 163 | + // Verify no error occurred |
| 164 | + assert.NoError(t, err) |
| 165 | + assert.NotNil(t, response) |
| 166 | + |
| 167 | + // Verify only non-empty headers were added |
| 168 | + headerMutation := response.GetResponseHeaders().GetResponse().GetHeaderMutation() |
| 169 | + assert.NotNil(t, headerMutation) |
| 170 | + |
| 171 | + setHeaders := headerMutation.GetSetHeaders() |
| 172 | + assert.Len(t, setHeaders, 2, "Should have 2 VSR headers (excluding empty reasoning mode)") |
| 173 | + |
| 174 | + // Verify each header |
| 175 | + headerMap := make(map[string]string) |
| 176 | + for _, header := range setHeaders { |
| 177 | + headerMap[header.Header.Key] = string(header.Header.RawValue) |
| 178 | + } |
| 179 | + |
| 180 | + assert.Equal(t, "math", headerMap["x-vsr-selected-category"]) |
| 181 | + assert.Equal(t, "deepseek-v31", headerMap["x-vsr-selected-model"]) |
| 182 | + assert.NotContains(t, headerMap, "x-vsr-selected-reasoning", "Empty reasoning mode should not be added") |
| 183 | +} |
0 commit comments