-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors_test.go
More file actions
457 lines (435 loc) · 15.3 KB
/
Copy patherrors_test.go
File metadata and controls
457 lines (435 loc) · 15.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
package main
import (
"bytes"
"encoding/json"
"io"
"log/slog"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
)
// parseErrorBody parses the proxy's structured error response into a typed
// struct so individual tests don't repeat boilerplate.
type errorBody struct {
Error struct {
Message string `json:"message"`
Type string `json:"type"`
Code string `json:"code"`
RequestID string `json:"request_id"`
} `json:"error"`
}
func parseErrorBody(t *testing.T, w *httptest.ResponseRecorder) errorBody {
t.Helper()
if ct := w.Header().Get("Content-Type"); !strings.HasPrefix(ct, "application/json") {
t.Fatalf("error response Content-Type must be application/json, got %q", ct)
}
var body errorBody
if err := json.Unmarshal(w.Body.Bytes(), &body); err != nil {
t.Fatalf("error response is not valid JSON: %v\nbody: %s", err, w.Body.String())
}
if body.Error.Type == "" {
t.Errorf("error.type must not be empty (body: %s)", w.Body.String())
}
if body.Error.Code == "" {
t.Errorf("error.code must not be empty (body: %s)", w.Body.String())
}
if body.Error.RequestID == "" {
t.Errorf("error.request_id must not be empty (body: %s)", w.Body.String())
}
if body.Error.Message == "" {
t.Errorf("error.message must not be empty (body: %s)", w.Body.String())
}
return body
}
// auditEntryFromBuf reads the inbound audit log entry from a buffer that has
// captured one or more log lines. Returns the first entry with direction=inbound.
func auditEntryFromBuf(t *testing.T, buf *bytes.Buffer) map[string]any {
t.Helper()
if buf.Len() == 0 {
t.Fatal("no audit log lines emitted")
}
for _, line := range strings.Split(strings.TrimSpace(buf.String()), "\n") {
var entry map[string]any
if err := json.Unmarshal([]byte(line), &entry); err != nil {
t.Fatalf("audit line is not JSON: %v\nline: %s", err, line)
}
if entry["direction"] == "inbound" {
return entry
}
}
t.Fatalf("no inbound audit entry found in:\n%s", buf.String())
return nil
}
// withAuditLogger attaches a slog logger writing to buf as the proxy's
// audit logger.
func withAuditLogger(p *Proxy, buf *bytes.Buffer) {
p.auditLogger = slog.New(slog.NewJSONHandler(buf, nil))
}
// proxyForErrors builds a minimal proxy with a working Philter mock. Callers
// override fields (e.g. keyIndex, openaiTarget) for the specific scenario.
func proxyForErrors(t *testing.T) (*Proxy, func()) {
t.Helper()
philterSrv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write(explainJSON("hi", "doc-id", nil))
}))
providerSrv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.Write([]byte(`{"id":"x","choices":[{"message":{"role":"assistant","content":"ok"}}]}`))
}))
u, _ := url.Parse(providerSrv.URL)
p := &Proxy{
config: testConfig(philterSrv.URL),
philter: testPhilterClient(philterSrv.URL),
openaiTarget: u,
openaiClient: http.DefaultClient,
}
return p, func() {
philterSrv.Close()
providerSrv.Close()
}
}
// --- The error-contract table ----------------------------------------------
//
// Each entry pins the (status, type, code) the proxy is contractually committed
// to. Changing any value here is a breaking API change.
type contractCase struct {
name string
wantStatus int
wantType string
wantCode string
wantHeader map[string]string // additional response headers to assert
drive func(t *testing.T) *httptest.ResponseRecorder
}
func TestErrorContract(t *testing.T) {
cases := []contractCase{
{
name: "invalid_request/bad_json",
wantStatus: http.StatusBadRequest,
wantType: "invalid_request",
wantCode: "bad_json",
drive: func(t *testing.T) *httptest.ResponseRecorder {
p, cleanup := proxyForErrors(t)
defer cleanup()
req := httptest.NewRequest("POST", "/v1/chat/completions", strings.NewReader(`{not json`))
w := httptest.NewRecorder()
p.ServeHTTP(w, req)
return w
},
},
{
name: "unauthorized/missing_api_key",
wantStatus: http.StatusUnauthorized,
wantType: "unauthorized",
wantCode: "missing_api_key",
drive: func(t *testing.T) *httptest.ResponseRecorder {
p, cleanup := proxyForErrors(t)
defer cleanup()
p.keyStore = testKeyStore(map[string]string{"valid-key": ""})
req := httptest.NewRequest("POST", "/v1/chat/completions", strings.NewReader(openAIBody()))
w := httptest.NewRecorder()
p.ServeHTTP(w, req)
return w
},
},
{
name: "unauthorized/invalid_api_key",
wantStatus: http.StatusUnauthorized,
wantType: "unauthorized",
wantCode: "invalid_api_key",
drive: func(t *testing.T) *httptest.ResponseRecorder {
p, cleanup := proxyForErrors(t)
defer cleanup()
p.keyStore = testKeyStore(map[string]string{"valid-key": ""})
req := httptest.NewRequest("POST", "/v1/chat/completions", strings.NewReader(openAIBody()))
req.Header.Set("x-philter-proxy-key", "wrong-key")
w := httptest.NewRecorder()
p.ServeHTTP(w, req)
return w
},
},
{
name: "capacity/concurrency_exceeded",
wantStatus: http.StatusServiceUnavailable,
wantType: "capacity",
wantCode: "concurrency_exceeded",
wantHeader: map[string]string{"Retry-After": "1"},
drive: func(t *testing.T) *httptest.ResponseRecorder {
p, cleanup := proxyForErrors(t)
defer cleanup()
// Fill the only slot synthetically by giving the limiter no headroom.
p.concurrency = newConcurrencyLimiter(1)
p.concurrency.global <- struct{}{} // hold the slot
req := httptest.NewRequest("POST", "/v1/chat/completions", strings.NewReader(openAIBody()))
w := httptest.NewRecorder()
p.ServeHTTP(w, req)
<-p.concurrency.global // release
return w
},
},
{
name: "pii_blocked/outbound_stream_unscannable",
wantStatus: http.StatusForbidden,
wantType: "pii_blocked",
wantCode: "outbound_stream_unscannable",
drive: func(t *testing.T) *httptest.ResponseRecorder {
philter := philterRedact("[REDACTED]")
defer philter.Close()
provider := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/event-stream")
w.WriteHeader(http.StatusOK)
io.WriteString(w, "data: {\"choices\":[{\"delta\":{\"content\":\"SSN 123-45-6789\"}}]}\n\n")
}))
defer provider.Close()
p := newOutboundProxy(philter.URL, provider.URL, "openai", "block")
req := httptest.NewRequest("POST", "/v1/chat/completions",
strings.NewReader(`{"model":"gpt-4","messages":[{"role":"user","content":"hi"}],"stream":true}`))
w := httptest.NewRecorder()
p.ServeHTTP(w, req)
return w
},
},
{
name: "not_found/bedrock_disabled",
wantStatus: http.StatusNotFound,
wantType: "not_found",
wantCode: "bedrock_disabled",
drive: func(t *testing.T) *httptest.ResponseRecorder {
p, cleanup := proxyForErrors(t)
defer cleanup()
// bedrockRegion is "" by default → bedrock path returns 404.
req := httptest.NewRequest("POST", "/model/foo/converse", strings.NewReader(`{"messages":[]}`))
w := httptest.NewRecorder()
p.ServeHTTP(w, req)
return w
},
},
{
name: "circuit_open/philter_unavailable",
wantStatus: http.StatusServiceUnavailable,
wantType: "circuit_open",
wantCode: "philter_unavailable",
drive: func(t *testing.T) *httptest.ResponseRecorder {
p, cleanup := proxyForErrors(t)
defer cleanup()
// Force the circuit open: 1-attempt retry, 1-failure threshold, block fallback.
p.philter = newPhilterClient(http.DefaultClient, "http://127.0.0.1:1",
RetryConfig{MaxAttempts: 1},
CircuitBreakerConfig{Enabled: true, Threshold: 1, TimeoutSeconds: 60, Fallback: "block"},
)
// First request trips the breaker.
sendRequest(p, "/v1/chat/completions", openAIBody(), nil)
// Second request gets the structured 503.
req := httptest.NewRequest("POST", "/v1/chat/completions", strings.NewReader(openAIBody()))
w := httptest.NewRecorder()
p.ServeHTTP(w, req)
return w
},
},
{
name: "philter_error/request_failed",
wantStatus: http.StatusBadGateway,
wantType: "philter_error",
wantCode: "request_failed",
drive: func(t *testing.T) *httptest.ResponseRecorder {
p, cleanup := proxyForErrors(t)
defer cleanup()
// Point Philter at an unreachable address; no circuit breaker.
p.philter = newPhilterClient(http.DefaultClient, "http://127.0.0.1:1",
RetryConfig{MaxAttempts: 1},
CircuitBreakerConfig{Enabled: false},
)
req := httptest.NewRequest("POST", "/v1/chat/completions", strings.NewReader(openAIBody()))
w := httptest.NewRecorder()
p.ServeHTTP(w, req)
return w
},
},
{
name: "provider_error/unreachable",
wantStatus: http.StatusBadGateway,
wantType: "provider_error",
wantCode: "unreachable",
drive: func(t *testing.T) *httptest.ResponseRecorder {
p, cleanup := proxyForErrors(t)
defer cleanup()
// Point provider at an unreachable address.
u, _ := url.Parse("http://127.0.0.1:1")
p.openaiTarget = u
req := httptest.NewRequest("POST", "/v1/chat/completions", strings.NewReader(openAIBody()))
w := httptest.NewRecorder()
p.ServeHTTP(w, req)
return w
},
},
{
name: "pii_blocked/outbound_blocked",
wantStatus: http.StatusForbidden,
wantType: "pii_blocked",
wantCode: "outbound_blocked",
drive: func(t *testing.T) *httptest.ResponseRecorder {
philter := philterRedact("[REDACTED]")
t.Cleanup(philter.Close)
provider := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.Write([]byte(`{"choices":[{"message":{"role":"assistant","content":"SSN here"}}]}`))
}))
t.Cleanup(provider.Close)
p := newOutboundProxy(philter.URL, provider.URL, "openai", "block")
req := httptest.NewRequest("POST", "/v1/chat/completions", strings.NewReader(openAIBody()))
w := httptest.NewRecorder()
p.ServeHTTP(w, req)
return w
},
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
w := tc.drive(t)
if w.Code != tc.wantStatus {
t.Errorf("status: want %d, got %d (body: %s)", tc.wantStatus, w.Code, w.Body.String())
}
body := parseErrorBody(t, w)
if body.Error.Type != tc.wantType {
t.Errorf("error.type: want %q, got %q", tc.wantType, body.Error.Type)
}
if body.Error.Code != tc.wantCode {
t.Errorf("error.code: want %q, got %q", tc.wantCode, body.Error.Code)
}
if got := w.Header().Get("X-Request-Id"); got == "" {
t.Error("X-Request-Id header must be set on every response")
} else if got != body.Error.RequestID {
t.Errorf("X-Request-Id header (%q) must match error.request_id (%q)", got, body.Error.RequestID)
}
for header, wantVal := range tc.wantHeader {
got := w.Header().Get(header)
if got == "" {
t.Errorf("header %s must be set", header)
} else if wantVal != "" && got != wantVal {
t.Errorf("header %s: want %q, got %q", header, wantVal, got)
}
}
})
}
}
// TestXRequestIdHonored verifies that an inbound X-Request-Id is propagated
// rather than overwritten, so callers can correlate across hops.
func TestXRequestIdHonored(t *testing.T) {
p, cleanup := proxyForErrors(t)
defer cleanup()
req := httptest.NewRequest("POST", "/v1/chat/completions", strings.NewReader(openAIBody()))
req.Header.Set("X-Request-Id", "client-supplied-id-123")
w := httptest.NewRecorder()
p.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Fatalf("expected 200, got %d", w.Code)
}
if got := w.Header().Get("X-Request-Id"); got != "client-supplied-id-123" {
t.Errorf("X-Request-Id must echo the inbound value, got %q", got)
}
}
// TestXRequestIdOnSuccess verifies that a request_id is generated and exposed
// even when the client did not send one and the request succeeds.
func TestXRequestIdOnSuccess(t *testing.T) {
p, cleanup := proxyForErrors(t)
defer cleanup()
req := httptest.NewRequest("POST", "/v1/chat/completions", strings.NewReader(openAIBody()))
w := httptest.NewRecorder()
p.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Fatalf("expected 200, got %d", w.Code)
}
id := w.Header().Get("X-Request-Id")
if id == "" {
t.Fatal("X-Request-Id must be set on success")
}
if len(id) != 36 {
t.Errorf("X-Request-Id should look like a UUID; got %q (len %d)", id, len(id))
}
}
// TestAuditCarriesErrorCode verifies that audit entries reference the same
// (type, code, request_id) the client saw — the AC's correlation guarantee.
// Runs across multiple error paths so a regression in any one handler is
// caught, not just the one we happened to pick.
func TestAuditCarriesErrorCode(t *testing.T) {
type setup struct {
name string
drive func(t *testing.T, auditBuf *bytes.Buffer) *httptest.ResponseRecorder
}
setups := []setup{
{
name: "unauthorized",
drive: func(t *testing.T, buf *bytes.Buffer) *httptest.ResponseRecorder {
p, cleanup := proxyForErrors(t)
defer cleanup()
p.keyStore = testKeyStore(map[string]string{"valid-key": ""})
withAuditLogger(p, buf)
req := httptest.NewRequest("POST", "/v1/chat/completions", strings.NewReader(openAIBody()))
w := httptest.NewRecorder()
p.ServeHTTP(w, req)
return w
},
},
{
name: "invalid_request",
drive: func(t *testing.T, buf *bytes.Buffer) *httptest.ResponseRecorder {
p, cleanup := proxyForErrors(t)
defer cleanup()
withAuditLogger(p, buf)
req := httptest.NewRequest("POST", "/v1/chat/completions", strings.NewReader(`{not json`))
w := httptest.NewRecorder()
p.ServeHTTP(w, req)
return w
},
},
{
name: "provider_error",
drive: func(t *testing.T, buf *bytes.Buffer) *httptest.ResponseRecorder {
p, cleanup := proxyForErrors(t)
defer cleanup()
u, _ := url.Parse("http://127.0.0.1:1")
p.openaiTarget = u
withAuditLogger(p, buf)
req := httptest.NewRequest("POST", "/v1/chat/completions", strings.NewReader(openAIBody()))
w := httptest.NewRecorder()
p.ServeHTTP(w, req)
return w
},
},
}
for _, s := range setups {
t.Run(s.name, func(t *testing.T) {
var buf bytes.Buffer
w := s.drive(t, &buf)
body := parseErrorBody(t, w)
entry := auditEntryFromBuf(t, &buf)
if entry["error_type"] != body.Error.Type {
t.Errorf("audit.error_type (%v) must equal error.type (%q)", entry["error_type"], body.Error.Type)
}
if entry["error_code"] != body.Error.Code {
t.Errorf("audit.error_code (%v) must equal error.code (%q)", entry["error_code"], body.Error.Code)
}
if entry["request_id"] != body.Error.RequestID {
t.Errorf("audit.request_id (%v) must equal error.request_id (%q)", entry["request_id"], body.Error.RequestID)
}
if entry["http_status"] != float64(w.Code) {
t.Errorf("audit.http_status (%v) must equal response status (%d)", entry["http_status"], w.Code)
}
})
}
}
// TestAuditNotEmittedForHealth verifies /health stays out of the audit log
// (health checks happen frequently and would drown real traffic).
func TestAuditNotEmittedForHealth(t *testing.T) {
p, cleanup := proxyForErrors(t)
defer cleanup()
var buf bytes.Buffer
withAuditLogger(p, &buf)
req := httptest.NewRequest("GET", "/health", nil)
w := httptest.NewRecorder()
p.ServeHTTP(w, req)
if buf.Len() != 0 {
t.Errorf("expected /health to produce no audit log entries; got:\n%s", buf.String())
}
}