Skip to content

Commit d005352

Browse files
committed
chore: address comments
1 parent 2d43f46 commit d005352

3 files changed

Lines changed: 22 additions & 13 deletions

File tree

intercept/client_headers.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,18 @@ var nonForwardedHeaders = []string{
2525
}
2626

2727
// authHeaders are headers that carry authentication credentials from the
28-
// client. These are stripped because the SDK re-injects the correct
29-
// provider credentials (API key or per-user token).
28+
// client. The upstream request is built by the SDK, which sets the correct
29+
// provider credentials via option.WithAPIKey. Client auth headers are
30+
// stripped here and the provider credentials are re-injected by
31+
// BuildUpstreamHeaders from the SDK-built request.
3032
var authHeaders = []string{
3133
"Authorization",
3234
"X-Api-Key",
3335
}
3436

35-
// SanitizeClientHeaders returns a copy of the client headers with hop-by-hop,
37+
// PrepareClientHeaders returns a copy of the client headers with hop-by-hop,
3638
// transport, and auth headers removed.
37-
func SanitizeClientHeaders(clientHeaders http.Header) http.Header {
39+
func PrepareClientHeaders(clientHeaders http.Header) http.Header {
3840
sanitized := clientHeaders.Clone()
3941
for _, h := range hopByHopHeaders {
4042
sanitized.Del(h)
@@ -52,7 +54,7 @@ func SanitizeClientHeaders(clientHeaders http.Header) http.Header {
5254
// It starts from the sanitized client headers, then preserves specific
5355
// headers from the SDK-built request that must not be overwritten.
5456
func BuildUpstreamHeaders(sdkHeader http.Header, clientHeaders http.Header, authHeaderName string) http.Header {
55-
headers := SanitizeClientHeaders(clientHeaders)
57+
headers := PrepareClientHeaders(clientHeaders)
5658

5759
// Preserve the auth header set by the SDK from the provider configuration.
5860
if v := sdkHeader.Get(authHeaderName); v != "" {

intercept/client_headers_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ func TestSanitizeClientHeaders(t *testing.T) {
1414
t.Run("nil input returns empty header", func(t *testing.T) {
1515
t.Parallel()
1616

17-
result := SanitizeClientHeaders(nil)
17+
result := PrepareClientHeaders(nil)
1818
require.Empty(t, result)
1919
})
2020

@@ -29,7 +29,7 @@ func TestSanitizeClientHeaders(t *testing.T) {
2929
"X-Custom": {"preserved"},
3030
}
3131

32-
result := SanitizeClientHeaders(input)
32+
result := PrepareClientHeaders(input)
3333

3434
assert.Empty(t, result.Get("Connection"))
3535
assert.Empty(t, result.Get("Keep-Alive"))
@@ -48,7 +48,7 @@ func TestSanitizeClientHeaders(t *testing.T) {
4848
"X-Custom": {"preserved"},
4949
}
5050

51-
result := SanitizeClientHeaders(input)
51+
result := PrepareClientHeaders(input)
5252

5353
assert.Empty(t, result.Get("Host"))
5454
assert.Empty(t, result.Get("Accept-Encoding"))
@@ -65,7 +65,7 @@ func TestSanitizeClientHeaders(t *testing.T) {
6565
"X-Custom": {"preserved"},
6666
}
6767

68-
result := SanitizeClientHeaders(input)
68+
result := PrepareClientHeaders(input)
6969

7070
assert.Empty(t, result.Get("Authorization"))
7171
assert.Empty(t, result.Get("X-Api-Key"))
@@ -79,7 +79,7 @@ func TestSanitizeClientHeaders(t *testing.T) {
7979
"X-Custom": {"value-1", "value-2"},
8080
}
8181

82-
result := SanitizeClientHeaders(input)
82+
result := PrepareClientHeaders(input)
8383

8484
require.Equal(t, []string{"value-1", "value-2"}, result["X-Custom"])
8585
})
@@ -93,7 +93,7 @@ func TestSanitizeClientHeaders(t *testing.T) {
9393
}
9494
originalCopy := input.Clone()
9595

96-
_ = SanitizeClientHeaders(input)
96+
_ = PrepareClientHeaders(input)
9797

9898
require.Equal(t, originalCopy, input)
9999
})

provider/openai_test.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ import (
1818
"golang.org/x/sync/errgroup"
1919
)
2020

21+
const (
22+
chatCompletionResponse = `{"id":"chatcmpl-123","object":"chat.completion","created":1677652288,"model":"gpt-4","choices":[{"index":0,"message":{"role":"assistant","content":"Hello!"},"finish_reason":"stop"}],"usage":{"prompt_tokens":9,"completion_tokens":12,"total_tokens":21}}`
23+
responsesAPIResponse = `{"id":"resp-123","object":"response","created_at":1677652288,"model":"gpt-5","output":[],"usage":{"input_tokens":5,"output_tokens":10,"total_tokens":15}}`
24+
)
25+
2126
type message struct {
2227
Role string
2328
Content string
@@ -166,7 +171,8 @@ func TestOpenAI_CreateInterceptor(t *testing.T) {
166171
receivedHeaders = r.Header.Clone()
167172
w.Header().Set("Content-Type", "application/json")
168173
w.WriteHeader(http.StatusOK)
169-
_, _ = w.Write([]byte(`{"id":"chatcmpl-123","object":"chat.completion","created":1677652288,"model":"gpt-4","choices":[{"index":0,"message":{"role":"assistant","content":"Hello!"},"finish_reason":"stop"}],"usage":{"prompt_tokens":9,"completion_tokens":12,"total_tokens":21}}`))
174+
_, err := w.Write([]byte(chatCompletionResponse))
175+
require.NoError(t, err)
170176
}))
171177
t.Cleanup(mockUpstream.Close)
172178

@@ -207,7 +213,8 @@ func TestOpenAI_CreateInterceptor(t *testing.T) {
207213
receivedHeaders = r.Header.Clone()
208214
w.Header().Set("Content-Type", "application/json")
209215
w.WriteHeader(http.StatusOK)
210-
_, _ = w.Write([]byte(`{"id":"resp-123","object":"response","created_at":1677652288,"model":"gpt-5","output":[],"usage":{"input_tokens":5,"output_tokens":10,"total_tokens":15}}`))
216+
_, err := w.Write([]byte(responsesAPIResponse))
217+
require.NoError(t, err)
211218
}))
212219
t.Cleanup(mockUpstream.Close)
213220

0 commit comments

Comments
 (0)