MCPcopy Create free account
hub / github.com/coder/coder / TestAnthropic_CreateInterceptor

Function TestAnthropic_CreateInterceptor

aibridge/provider/anthropic_internal_test.go:119–228  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

117}
118
119func TestAnthropic_CreateInterceptor(t *testing.T) {
120 t.Parallel()
121
122 provider := NewAnthropic(config.Anthropic{Key: "test-key"}, nil)
123
124 t.Run("Messages_NonStreamingRequest_BlockingInterceptor", func(t *testing.T) {
125 t.Parallel()
126
127 body := `{"model": "claude-opus-4-5", "max_tokens": 1024, "messages": [{"role": "user", "content": "hello"}], "stream": false}`
128 req := httptest.NewRequest(http.MethodPost, routeMessages, bytes.NewBufferString(body))
129 w := httptest.NewRecorder()
130
131 interceptor, err := provider.CreateInterceptor(w, req, testTracer)
132
133 require.NoError(t, err)
134 require.NotNil(t, interceptor)
135 assert.False(t, interceptor.Streaming())
136 })
137
138 t.Run("Messages_StreamingRequest_StreamingInterceptor", func(t *testing.T) {
139 t.Parallel()
140
141 body := `{"model": "claude-opus-4-5", "max_tokens": 1024, "messages": [{"role": "user", "content": "hello"}], "stream": true}`
142 req := httptest.NewRequest(http.MethodPost, routeMessages, bytes.NewBufferString(body))
143 w := httptest.NewRecorder()
144
145 interceptor, err := provider.CreateInterceptor(w, req, testTracer)
146
147 require.NoError(t, err)
148 require.NotNil(t, interceptor)
149 assert.True(t, interceptor.Streaming())
150 })
151
152 t.Run("Messages_InvalidRequestBody", func(t *testing.T) {
153 t.Parallel()
154
155 body := `invalid json`
156 req := httptest.NewRequest(http.MethodPost, routeMessages, bytes.NewBufferString(body))
157 w := httptest.NewRecorder()
158
159 interceptor, err := provider.CreateInterceptor(w, req, testTracer)
160
161 require.Error(t, err)
162 require.Nil(t, interceptor)
163 assert.Contains(t, err.Error(), "unmarshal request body")
164 })
165
166 t.Run("Messages_ClientHeaders", func(t *testing.T) {
167 t.Parallel()
168
169 var receivedHeaders http.Header
170
171 // Mock upstream that captures headers.
172 mockUpstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
173 receivedHeaders = r.Header.Clone()
174 w.Header().Set("Content-Type", "application/json")
175 w.WriteHeader(http.StatusOK)
176 _, _ = w.Write([]byte(`{"id":"msg-123","type":"message","role":"assistant","content":[{"type":"text","text":"Hello!"}],"model":"claude-opus-4-5","stop_reason":"end_turn","usage":{"input_tokens":10,"output_tokens":5}}`))

Callers

nothing calls this directly

Calls 15

CreateInterceptorMethod · 0.95
NewAnthropicFunction · 0.70
RunMethod · 0.65
StreamingMethod · 0.65
SetMethod · 0.65
WriteMethod · 0.65
CleanupMethod · 0.65
SetupMethod · 0.65
ProcessRequestMethod · 0.65
GetMethod · 0.65
ErrorMethod · 0.45
ContainsMethod · 0.45

Tested by

no test coverage detected