Skip to content

Commit fd1cc42

Browse files
committed
add Body.Close() to bridgeServer.makeRequest + some test names cleanup
1 parent 4625449 commit fd1cc42

7 files changed

Lines changed: 8 additions & 52 deletions

File tree

internal/integrationtest/apidump_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ func TestAPIDump(t *testing.T) {
7676

7777
resp := bridgeServer.makeRequest(t, http.MethodPost, tc.path, fix.Request())
7878
require.Equal(t, http.StatusOK, resp.StatusCode)
79-
defer resp.Body.Close()
8079
_, _ = io.ReadAll(resp.Body)
8180

8281
// Verify dump files were created.
@@ -196,8 +195,7 @@ func TestAPIDumpPassthrough(t *testing.T) {
196195
withCustomProvider(tc.providerFunc(upstream.URL, dumpDir)),
197196
)
198197

199-
resp := bridgeServer.makeRequest(t, http.MethodGet, tc.requestPath, nil)
200-
defer resp.Body.Close()
198+
bridgeServer.makeRequest(t, http.MethodGet, tc.requestPath, nil)
201199

202200
// Find dump files in the passthrough directory.
203201
passthroughDir := filepath.Join(dumpDir, tc.name, "passthrough")

internal/integrationtest/bridge_test.go

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ func TestAnthropicMessages(t *testing.T) {
8080
require.NoError(t, err)
8181
resp := bridgeServer.makeRequest(t, http.MethodPost, pathAnthropicMessages, reqBody)
8282
require.Equal(t, http.StatusOK, resp.StatusCode)
83-
defer resp.Body.Close()
8483

8584
// Response-specific checks.
8685
if tc.streaming {
@@ -146,7 +145,6 @@ func TestAWSBedrockIntegration(t *testing.T) {
146145
)
147146

148147
resp := bridgeServer.makeRequest(t, http.MethodPost, pathAnthropicMessages, fixtures.Request(t, fixtures.AntSingleBuiltinTool))
149-
defer resp.Body.Close()
150148

151149
require.Equal(t, http.StatusInternalServerError, resp.StatusCode)
152150
body, err := io.ReadAll(resp.Body)
@@ -185,7 +183,6 @@ func TestAWSBedrockIntegration(t *testing.T) {
185183
reqBody, err := sjson.SetBytes(fix.Request(), "stream", streaming)
186184
require.NoError(t, err)
187185
resp := bridgeServer.makeRequest(t, http.MethodPost, pathAnthropicMessages, reqBody)
188-
defer resp.Body.Close()
189186

190187
// For streaming responses, consume the body to allow the stream to complete.
191188
if streaming {
@@ -259,7 +256,6 @@ func TestOpenAIChatCompletions(t *testing.T) {
259256
require.NoError(t, err)
260257
resp := bridgeServer.makeRequest(t, http.MethodPost, pathOpenAIChatCompletions, reqBody)
261258
require.Equal(t, http.StatusOK, resp.StatusCode)
262-
defer resp.Body.Close()
263259

264260
// Response-specific checks.
265261
if tc.streaming {
@@ -350,7 +346,6 @@ func TestOpenAIChatCompletions(t *testing.T) {
350346
// Consume the full response body to ensure the interception completes
351347
_, err = io.ReadAll(resp.Body)
352348
require.NoError(t, err)
353-
resp.Body.Close()
354349

355350
// Verify the MCP tool was actually invoked
356351
invocations := mockMCP.getCallsByTool(mockToolName)
@@ -515,7 +510,6 @@ func TestSimple(t *testing.T) {
515510
require.NoError(t, err)
516511
resp := bridgeServer.makeRequest(t, http.MethodPost, tc.path, reqBody, http.Header{"User-Agent": {tc.userAgent}})
517512
require.Equal(t, http.StatusOK, resp.StatusCode)
518-
defer resp.Body.Close()
519513

520514
// Then: I expect the upstream request to have the correct path.
521515
received := upstream.receivedRequests()
@@ -629,7 +623,6 @@ func TestSessionIDTracking(t *testing.T) {
629623

630624
resp := bridgeServer.makeRequest(t, http.MethodPost, pathAnthropicMessages, reqBody, tc.header)
631625
require.Equal(t, http.StatusOK, resp.StatusCode)
632-
defer resp.Body.Close()
633626

634627
// Drain the body to let the stream complete.
635628
_, err := io.ReadAll(resp.Body)
@@ -710,7 +703,6 @@ func TestFallthrough(t *testing.T) {
710703
bridgeServer := newBridgeTestServer(t, t.Context(), upstream.URL+tc.basePath)
711704

712705
resp := bridgeServer.makeRequest(t, http.MethodGet, tc.requestPath, nil)
713-
defer resp.Body.Close()
714706

715707
require.Equal(t, http.StatusOK, resp.StatusCode)
716708

@@ -1050,7 +1042,6 @@ func TestErrorHandling(t *testing.T) {
10501042
require.NoError(t, err)
10511043

10521044
resp := bridgeServer.makeRequest(t, http.MethodPost, tc.path, reqBody)
1053-
t.Cleanup(func() { _ = resp.Body.Close() })
10541045

10551046
tc.responseHandlerFn(resp)
10561047
bridgeServer.Recorder.VerifyAllInterceptionsEnded(t)
@@ -1118,7 +1109,6 @@ func TestErrorHandling(t *testing.T) {
11181109
bridgeServer := newBridgeTestServer(t, ctx, upstream.URL)
11191110

11201111
resp := bridgeServer.makeRequest(t, http.MethodPost, tc.path, fix.Request())
1121-
t.Cleanup(func() { _ = resp.Body.Close() })
11221112

11231113
tc.responseHandlerFn(resp)
11241114
bridgeServer.Recorder.VerifyAllInterceptionsEnded(t)
@@ -1179,7 +1169,6 @@ func TestStableRequestEncoding(t *testing.T) {
11791169
for range count {
11801170
resp := bridgeServer.makeRequest(t, http.MethodPost, tc.path, fix.Request())
11811171
require.Equal(t, http.StatusOK, resp.StatusCode)
1182-
_ = resp.Body.Close()
11831172
}
11841173

11851174
// All upstream request bodies should be identical.
@@ -1294,7 +1283,6 @@ func TestAnthropicToolChoiceParallelDisabled(t *testing.T) {
12941283

12951284
resp := bridgeServer.makeRequest(t, http.MethodPost, pathAnthropicMessages, reqBody)
12961285
require.Equal(t, http.StatusOK, resp.StatusCode)
1297-
_ = resp.Body.Close()
12981286

12991287
// Verify tool_choice in the upstream request.
13001288
received := upstream.receivedRequests()
@@ -1352,7 +1340,6 @@ func TestThinkingAdaptiveIsPreserved(t *testing.T) {
13521340
resp := bridgeServer.makeRequest(t, http.MethodPost, pathAnthropicMessages, reqBody)
13531341
require.Equal(t, http.StatusOK, resp.StatusCode)
13541342
_, _ = io.ReadAll(resp.Body)
1355-
_ = resp.Body.Close()
13561343

13571344
// Verify the thinking field was preserved in the upstream request.
13581345
received := upstream.receivedRequests()
@@ -1414,7 +1401,6 @@ func TestEnvironmentDoNotLeak(t *testing.T) {
14141401

14151402
resp := bridgeServer.makeRequest(t, http.MethodPost, tc.path, fix.Request())
14161403
require.Equal(t, http.StatusOK, resp.StatusCode)
1417-
defer resp.Body.Close()
14181404

14191405
// Verify that environment values did not leak.
14201406
received := upstream.receivedRequests()
@@ -1536,9 +1522,8 @@ func TestActorHeaders(t *testing.T) {
15361522
reqBody, err := sjson.SetBytes(fixtures.Request(t, tc.fixture), "stream", tc.streaming)
15371523
require.NoError(t, err)
15381524

1539-
resp := bridgeServer.makeRequest(t, http.MethodPost, tc.path, reqBody)
1525+
bridgeServer.makeRequest(t, http.MethodPost, tc.path, reqBody)
15401526
require.NotEmpty(t, receivedHeaders)
1541-
defer resp.Body.Close()
15421527

15431528
// Verify that the actor headers were only received if intended.
15441529
found := make(map[string][]string)

internal/integrationtest/circuit_breaker_test.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,6 @@ func TestCircuitBreaker_FullRecoveryCycle(t *testing.T) {
139139
resp := bridgeServer.makeRequest(t, http.MethodPost, tc.path, []byte(tc.requestBody), tc.headers)
140140
_, err := io.ReadAll(resp.Body)
141141
require.NoError(t, err)
142-
resp.Body.Close()
143142
return resp
144143
}
145144

@@ -293,7 +292,6 @@ func TestCircuitBreaker_HalfOpenFailure(t *testing.T) {
293292
resp := bridgeServer.makeRequest(t, http.MethodPost, tc.path, []byte(tc.requestBody), tc.headers)
294293
_, err := io.ReadAll(resp.Body)
295294
require.NoError(t, err)
296-
resp.Body.Close()
297295
return resp
298296
}
299297

@@ -440,7 +438,6 @@ func TestCircuitBreaker_HalfOpenMaxRequests(t *testing.T) {
440438
resp := bridgeServer.makeRequest(t, http.MethodPost, tc.path, []byte(tc.requestBody), tc.headers)
441439
_, err := io.ReadAll(resp.Body)
442440
require.NoError(t, err)
443-
resp.Body.Close()
444441
return resp
445442
}
446443

@@ -564,7 +561,6 @@ func TestCircuitBreaker_PerModelIsolation(t *testing.T) {
564561
})
565562
_, err := io.ReadAll(resp.Body)
566563
require.NoError(t, err)
567-
resp.Body.Close()
568564
return resp
569565
}
570566

internal/integrationtest/metrics_test.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,6 @@ func TestMetrics_Interception(t *testing.T) {
126126
)
127127

128128
resp := bridgeServer.makeRequest(t, http.MethodPost, tc.path, fix.Request())
129-
defer resp.Body.Close()
130129
_, _ = io.ReadAll(resp.Body)
131130

132131
count := promtest.ToFloat64(m.InterceptionCount.WithLabelValues(
@@ -207,7 +206,6 @@ func TestMetrics_PassthroughCount(t *testing.T) {
207206
)
208207

209208
resp := bridgeServer.makeRequest(t, http.MethodGet, "/openai/v1/models", nil)
210-
defer resp.Body.Close()
211209
require.Equal(t, http.StatusOK, resp.StatusCode)
212210

213211
count := promtest.ToFloat64(m.PassthroughCount.WithLabelValues(
@@ -231,7 +229,6 @@ func TestMetrics_PromptCount(t *testing.T) {
231229

232230
resp := bridgeServer.makeRequest(t, http.MethodPost, pathOpenAIChatCompletions, fix.Request())
233231
require.Equal(t, http.StatusOK, resp.StatusCode)
234-
defer resp.Body.Close()
235232
_, _ = io.ReadAll(resp.Body)
236233

237234
prompts := promtest.ToFloat64(m.PromptCount.WithLabelValues(
@@ -255,7 +252,6 @@ func TestMetrics_NonInjectedToolUseCount(t *testing.T) {
255252

256253
resp := bridgeServer.makeRequest(t, http.MethodPost, pathOpenAIChatCompletions, fix.Request())
257254
require.Equal(t, http.StatusOK, resp.StatusCode)
258-
defer resp.Body.Close()
259255
_, _ = io.ReadAll(resp.Body)
260256

261257
count := promtest.ToFloat64(m.NonInjectedToolUseCount.WithLabelValues(
@@ -285,7 +281,6 @@ func TestMetrics_InjectedToolUseCount(t *testing.T) {
285281

286282
resp := bridgeServer.makeRequest(t, http.MethodPost, pathAnthropicMessages, fix.Request())
287283
require.Equal(t, http.StatusOK, resp.StatusCode)
288-
defer resp.Body.Close()
289284
_, _ = io.ReadAll(resp.Body)
290285

291286
// Wait until full roundtrip has completed.

internal/integrationtest/responses_test.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,6 @@ func TestResponsesOutputMatchesUpstream(t *testing.T) {
337337
bridgeServer := newBridgeTestServer(t, ctx, upstream.URL)
338338

339339
resp := bridgeServer.makeRequest(t, http.MethodPost, pathOpenAIResponses, fix.Request(), http.Header{"User-Agent": {tc.userAgent}})
340-
defer resp.Body.Close()
341340
require.Equal(t, http.StatusOK, resp.StatusCode)
342341
got, err := io.ReadAll(resp.Body)
343342

@@ -425,7 +424,6 @@ func TestResponsesBackgroundModeForbidden(t *testing.T) {
425424
// Create a request with background mode enabled
426425
reqBytes := responsesRequestBytes(t, tc.streaming, keyVal{"background", true})
427426
resp := bridgeServer.makeRequest(t, http.MethodPost, pathOpenAIResponses, reqBytes)
428-
defer resp.Body.Close()
429427

430428
require.Equal(t, "application/json", resp.Header.Get("Content-Type"))
431429
require.Equal(t, http.StatusNotImplemented, resp.StatusCode)
@@ -531,7 +529,6 @@ func TestResponsesParallelToolsOverwritten(t *testing.T) {
531529
bridgeServer := newBridgeTestServer(t, ctx, upstream.URL)
532530

533531
resp := bridgeServer.makeRequest(t, http.MethodPost, pathOpenAIResponses, []byte(tc.request))
534-
defer resp.Body.Close()
535532
_, err := io.ReadAll(resp.Body)
536533
require.NoError(t, err)
537534
})
@@ -588,7 +585,6 @@ func TestClientAndConnectionError(t *testing.T) {
588585

589586
reqBytes := responsesRequestBytes(t, tc.streaming)
590587
resp := bridgeServer.makeRequest(t, http.MethodPost, pathOpenAIResponses, reqBytes)
591-
defer resp.Body.Close()
592588

593589
require.Equal(t, "application/json", resp.Header.Get("Content-Type"))
594590
require.Equal(t, http.StatusInternalServerError, resp.StatusCode)
@@ -666,7 +662,6 @@ func TestUpstreamError(t *testing.T) {
666662

667663
reqBytes := responsesRequestBytes(t, tc.streaming)
668664
resp := bridgeServer.makeRequest(t, http.MethodPost, pathOpenAIResponses, reqBytes)
669-
defer resp.Body.Close()
670665

671666
require.Equal(t, tc.statusCode, resp.StatusCode)
672667
require.Equal(t, tc.contentType, resp.Header.Get("Content-Type"))
@@ -843,7 +838,6 @@ func TestResponsesInjectedTool(t *testing.T) {
843838
bridgeServer := newBridgeTestServer(t, ctx, upstream.URL, withMCP(mockMCP))
844839

845840
resp := bridgeServer.makeRequest(t, http.MethodPost, pathOpenAIResponses, fix.Request())
846-
defer resp.Body.Close()
847841
require.Equal(t, http.StatusOK, resp.StatusCode)
848842

849843
body, err := io.ReadAll(resp.Body)

internal/integrationtest/setupbridge.go

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@ type bridgeConfig struct {
4848
mcpProxy mcp.ServerProxier
4949
userID string
5050
metadata recorder.Metadata
51-
logger slog.Logger
52-
loggerSet bool
51+
logger slog.Logger
5352
}
5453

5554
// bridgeTestServer wraps an httptest.Server running a RequestBridge.
@@ -76,6 +75,7 @@ func (s *bridgeTestServer) makeRequest(t *testing.T, method string, path string,
7675
}
7776
resp, err := http.DefaultClient.Do(req)
7877
require.NoError(t, err)
78+
t.Cleanup(func() { _ = resp.Body.Close() })
7979
return resp
8080
}
8181

@@ -126,7 +126,7 @@ func withActor(id string, md recorder.Metadata) bridgeOption {
126126
// a RequestBridge with sensible defaults:
127127
// - All standard providers (unless withProvider / withCustomProvider)
128128
// - NoopMCPManager (unless withMCP)
129-
// - slogtest debug logger (unless withLogger)
129+
// - slogtest debug logger
130130
// - defaultTracer (unless withTracer)
131131
// - defaultActorID (unless withActor)
132132
func newBridgeTestServer(
@@ -146,9 +146,7 @@ func newBridgeTestServer(
146146
if cfg.tracer == nil {
147147
cfg.tracer = defaultTracer
148148
}
149-
if !cfg.loggerSet {
150-
cfg.logger = newLogger(t)
151-
}
149+
cfg.logger = newLogger(t)
152150
if cfg.mcpProxy == nil {
153151
cfg.mcpProxy = newNoopMCPManager()
154152
}
@@ -168,8 +166,7 @@ func newBridgeTestServer(
168166
}
169167

170168
mockRec := &testutil.MockRecorder{}
171-
var rec aibridge.Recorder = mockRec
172-
rec = aibridge.NewRecorder(cfg.logger, cfg.tracer, func() (aibridge.Recorder, error) {
169+
rec := aibridge.NewRecorder(cfg.logger, cfg.tracer, func() (aibridge.Recorder, error) {
173170
return mockRec, nil
174171
})
175172

@@ -237,9 +234,6 @@ func setupInjectedToolTest(
237234

238235
resp := bridgeServer.makeRequest(t, http.MethodPost, path, reqBody)
239236
require.Equal(t, http.StatusOK, resp.StatusCode)
240-
t.Cleanup(func() {
241-
_ = resp.Body.Close()
242-
})
243237

244238
// We must ALWAYS have 2 calls to the bridge for injected tool tests.
245239
require.Eventually(t, func() bool {

internal/integrationtest/trace_test.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,6 @@ func TestTraceAnthropic(t *testing.T) {
120120
require.NoError(t, err)
121121
resp := bridgeServer.makeRequest(t, http.MethodPost, pathAnthropicMessages, reqBody)
122122
require.Equal(t, http.StatusOK, resp.StatusCode)
123-
defer resp.Body.Close()
124123
bridgeServer.Close()
125124

126125
require.Equal(t, 1, len(bridgeServer.Recorder.RecordedInterceptions()))
@@ -235,7 +234,6 @@ func TestTraceAnthropicErr(t *testing.T) {
235234
} else {
236235
require.Equal(t, tc.expectCode, resp.StatusCode)
237236
}
238-
defer resp.Body.Close()
239237
bridgeServer.Close()
240238

241239
require.Equal(t, 1, len(bridgeServer.Recorder.RecordedInterceptions()))
@@ -350,11 +348,10 @@ func TestInjectedToolsTrace(t *testing.T) {
350348
validatorFn = openaiChatToolResultValidator(t)
351349
}
352350

353-
recorderClient, mockMCP, resp := setupInjectedToolTest(
351+
recorderClient, mockMCP, _ := setupInjectedToolTest(
354352
t, tc.fixture, tc.streaming, tracer,
355353
tc.path, validatorFn, tc.opts...,
356354
)
357-
defer resp.Body.Close()
358355

359356
require.Len(t, recorderClient.RecordedInterceptions(), 1)
360357
intcID := recorderClient.RecordedInterceptions()[0].ID
@@ -474,7 +471,6 @@ func TestTraceOpenAI(t *testing.T) {
474471
require.NoError(t, err)
475472
resp := bridgeServer.makeRequest(t, http.MethodPost, tc.path, reqBody)
476473
require.Equal(t, http.StatusOK, resp.StatusCode)
477-
defer resp.Body.Close()
478474
bridgeServer.Close()
479475

480476
require.Equal(t, 1, len(bridgeServer.Recorder.RecordedInterceptions()))
@@ -631,7 +627,6 @@ func TestTraceOpenAIErr(t *testing.T) {
631627
resp := bridgeServer.makeRequest(t, http.MethodPost, tc.path, reqBody)
632628

633629
require.Equal(t, tc.expectCode, resp.StatusCode)
634-
defer resp.Body.Close()
635630
bridgeServer.Close()
636631

637632
require.Equal(t, 1, len(bridgeServer.Recorder.RecordedInterceptions()))
@@ -670,7 +665,6 @@ func TestTracePassthrough(t *testing.T) {
670665
)
671666

672667
resp := bridgeServer.makeRequest(t, http.MethodGet, "/openai/v1/models", nil)
673-
defer resp.Body.Close()
674668
require.Equal(t, http.StatusOK, resp.StatusCode)
675669
bridgeServer.Close()
676670

0 commit comments

Comments
 (0)