Skip to content

Commit 0abaee3

Browse files
committed
chore: lint fixes part 7
1 parent 523ed94 commit 0abaee3

File tree

9 files changed

+201
-107
lines changed

9 files changed

+201
-107
lines changed

intercept/apidump/apidump_test.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func TestBridgedMiddleware_RedactsSensitiveRequestHeaders(t *testing.T) {
5252
req.Header.Set("User-Agent", "test-client")
5353

5454
// Call middleware with a mock next function
55-
_, err = middleware(req, func(r *http.Request) (*http.Response, error) {
55+
resp, err := middleware(req, func(r *http.Request) (*http.Response, error) {
5656
return &http.Response{
5757
StatusCode: http.StatusOK,
5858
Status: "200 OK",
@@ -62,6 +62,7 @@ func TestBridgedMiddleware_RedactsSensitiveRequestHeaders(t *testing.T) {
6262
}, nil
6363
})
6464
require.NoError(t, err)
65+
defer resp.Body.Close()
6566

6667
// Read the request dump file
6768
modelDir := filepath.Join(tmpDir, "openai", "gpt-4")
@@ -170,7 +171,7 @@ func TestBridgedMiddleware_PreservesRequestBody(t *testing.T) {
170171
require.NoError(t, err)
171172

172173
var capturedBody []byte
173-
_, err = middleware(req, func(r *http.Request) (*http.Response, error) {
174+
resp2, err := middleware(req, func(r *http.Request) (*http.Response, error) {
174175
// Read the body in the next handler to verify it's still available
175176
capturedBody, _ = io.ReadAll(r.Body)
176177
return &http.Response{
@@ -182,6 +183,7 @@ func TestBridgedMiddleware_PreservesRequestBody(t *testing.T) {
182183
}, nil
183184
})
184185
require.NoError(t, err)
186+
defer resp2.Body.Close()
185187

186188
// Verify the body was preserved for the next handler
187189
require.Equal(t, originalBody, string(capturedBody))
@@ -202,7 +204,7 @@ func TestBridgedMiddleware_ModelWithSlash(t *testing.T) {
202204
req, err := http.NewRequest(http.MethodPost, "https://api.google.com/v1/chat", bytes.NewReader([]byte(`{}`)))
203205
require.NoError(t, err)
204206

205-
_, err = middleware(req, func(r *http.Request) (*http.Response, error) {
207+
resp3, err := middleware(req, func(r *http.Request) (*http.Response, error) {
206208
return &http.Response{
207209
StatusCode: http.StatusOK,
208210
Status: "200 OK",
@@ -212,6 +214,7 @@ func TestBridgedMiddleware_ModelWithSlash(t *testing.T) {
212214
}, nil
213215
})
214216
require.NoError(t, err)
217+
defer resp3.Body.Close()
215218

216219
// Verify files are created with sanitized model name
217220
modelDir := filepath.Join(tmpDir, "google", "gemini-1.5-pro")
@@ -290,7 +293,7 @@ func TestBridgedMiddleware_AllSensitiveRequestHeaders(t *testing.T) {
290293
req.Header.Set("Proxy-Authorization", "Basic proxy-creds")
291294
req.Header.Set("X-Amz-Security-Token", "aws-security-token")
292295

293-
_, err = middleware(req, func(r *http.Request) (*http.Response, error) {
296+
resp4, err := middleware(req, func(r *http.Request) (*http.Response, error) {
294297
return &http.Response{
295298
StatusCode: http.StatusOK,
296299
Status: "200 OK",
@@ -300,6 +303,7 @@ func TestBridgedMiddleware_AllSensitiveRequestHeaders(t *testing.T) {
300303
}, nil
301304
})
302305
require.NoError(t, err)
306+
defer resp4.Body.Close()
303307

304308
modelDir := filepath.Join(tmpDir, "openai", "gpt-4")
305309
reqDumpPath := findDumpFile(t, modelDir, SuffixRequest)
@@ -358,7 +362,7 @@ func TestPassthroughMiddleware(t *testing.T) {
358362
req, err := http.NewRequest(http.MethodGet, "https://api.openai.com/v1/models", nil)
359363
require.NoError(t, err)
360364

361-
resp, err := rt.RoundTrip(req)
365+
resp, err := rt.RoundTrip(req) //nolint:bodyclose // resp is nil on error
362366
require.ErrorIs(t, err, innerErr)
363367
require.Nil(t, resp)
364368
})

intercept/apidump/streaming_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ func TestMiddleware_PreservesResponseBody(t *testing.T) {
120120
}, nil
121121
})
122122
require.NoError(t, err)
123+
defer resp.Body.Close()
123124

124125
// Verify the response body is still readable after middleware
125126
capturedBody, err := io.ReadAll(resp.Body)

internal/integrationtest/apidump_test.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,11 @@ func TestAPIDump(t *testing.T) {
128128
withCustomProvider(tc.providerFunc(srv.URL, dumpDir)),
129129
)
130130

131-
resp := bridgeServer.makeRequest(t, http.MethodPost, tc.path, fix.Request(), tc.headers)
131+
resp, err := bridgeServer.makeRequest(t, http.MethodPost, tc.path, fix.Request(), tc.headers)
132+
require.NoError(t, err)
133+
defer resp.Body.Close()
132134
require.Equal(t, http.StatusOK, resp.StatusCode)
133-
_, err := io.ReadAll(resp.Body)
135+
_, err = io.ReadAll(resp.Body)
134136
require.NoError(t, err)
135137

136138
// Verify dump files were created.
@@ -187,6 +189,7 @@ func TestAPIDump(t *testing.T) {
187189
// Parse the dumped HTTP response.
188190
dumpResp, err := http.ReadResponse(bufio.NewReader(bytes.NewReader(respDumpData)), nil)
189191
require.NoError(t, err)
192+
defer dumpResp.Body.Close()
190193
require.Equal(t, http.StatusOK, dumpResp.StatusCode)
191194
dumpRespBody, err := io.ReadAll(dumpResp.Body)
192195
require.NoError(t, err)
@@ -256,12 +259,14 @@ func TestAPIDumpPassthrough(t *testing.T) {
256259
withCustomProvider(tc.providerFunc(upstream.URL, dumpDir)),
257260
)
258261

259-
bridgeServer.makeRequest(t, http.MethodGet, tc.requestPath, nil)
262+
resp, err := bridgeServer.makeRequest(t, http.MethodGet, tc.requestPath, nil)
263+
require.NoError(t, err)
264+
defer resp.Body.Close()
260265

261266
// Find dump files in the passthrough directory.
262267
passthroughDir := filepath.Join(dumpDir, tc.name, "passthrough")
263268
var reqDumpFile, respDumpFile string
264-
err := filepath.Walk(passthroughDir, func(path string, info os.FileInfo, err error) error {
269+
err = filepath.Walk(passthroughDir, func(path string, info os.FileInfo, err error) error {
265270
if err != nil {
266271
return err
267272
}
@@ -299,6 +304,7 @@ func TestAPIDumpPassthrough(t *testing.T) {
299304
require.NoError(t, err)
300305
dumpResp, err := http.ReadResponse(bufio.NewReader(bytes.NewReader(respDumpData)), nil)
301306
require.NoError(t, err)
307+
defer dumpResp.Body.Close()
302308
require.Equal(t, http.StatusOK, dumpResp.StatusCode)
303309
dumpRespBody, err := io.ReadAll(dumpResp.Body)
304310
require.NoError(t, err)

internal/integrationtest/bridge_test.go

Lines changed: 57 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,9 @@ func TestAnthropicMessages(t *testing.T) {
8989
// Make API call to aibridge for Anthropic /v1/messages
9090
reqBody, err := sjson.SetBytes(fix.Request(), "stream", tc.streaming)
9191
require.NoError(t, err)
92-
resp := bridgeServer.makeRequest(t, http.MethodPost, pathAnthropicMessages, reqBody)
92+
resp, err := bridgeServer.makeRequest(t, http.MethodPost, pathAnthropicMessages, reqBody)
93+
require.NoError(t, err)
94+
defer resp.Body.Close()
9395
require.Equal(t, http.StatusOK, resp.StatusCode)
9496

9597
// Response-specific checks.
@@ -220,7 +222,9 @@ func TestAnthropicMessagesModelThoughts(t *testing.T) {
220222

221223
reqBody, err := sjson.SetBytes(fix.Request(), "stream", tc.streaming)
222224
require.NoError(t, err)
223-
resp := bridgeServer.makeRequest(t, http.MethodPost, pathAnthropicMessages, reqBody)
225+
resp, err := bridgeServer.makeRequest(t, http.MethodPost, pathAnthropicMessages, reqBody)
226+
require.NoError(t, err)
227+
defer resp.Body.Close()
224228
require.Equal(t, http.StatusOK, resp.StatusCode)
225229

226230
if tc.streaming {
@@ -258,7 +262,9 @@ func TestAWSBedrockIntegration(t *testing.T) {
258262
withCustomProvider(provider.NewAnthropic(anthropicCfg("http://unused", apiKey), bedrockCfg)),
259263
)
260264

261-
resp := bridgeServer.makeRequest(t, http.MethodPost, pathAnthropicMessages, fixtures.Request(t, fixtures.AntSingleBuiltinTool))
265+
resp, err := bridgeServer.makeRequest(t, http.MethodPost, pathAnthropicMessages, fixtures.Request(t, fixtures.AntSingleBuiltinTool))
266+
require.NoError(t, err)
267+
defer resp.Body.Close()
262268

263269
require.Equal(t, http.StatusInternalServerError, resp.StatusCode)
264270
body, err := io.ReadAll(resp.Body)
@@ -296,7 +302,9 @@ func TestAWSBedrockIntegration(t *testing.T) {
296302
// We override the AWS Bedrock client to route requests through our mock server.
297303
reqBody, err := sjson.SetBytes(fix.Request(), "stream", streaming)
298304
require.NoError(t, err)
299-
resp := bridgeServer.makeRequest(t, http.MethodPost, pathAnthropicMessages, reqBody)
305+
resp, err := bridgeServer.makeRequest(t, http.MethodPost, pathAnthropicMessages, reqBody)
306+
require.NoError(t, err)
307+
defer resp.Body.Close()
300308

301309
// For streaming responses, consume the body to allow the stream to complete.
302310
if streaming {
@@ -419,9 +427,11 @@ func TestAWSBedrockIntegration(t *testing.T) {
419427
require.NoError(t, err)
420428

421429
// Send with Anthropic-Beta header containing flags that should be filtered.
422-
resp := bridgeServer.makeRequest(t, http.MethodPost, pathAnthropicMessages, reqBody, http.Header{
430+
resp, err := bridgeServer.makeRequest(t, http.MethodPost, pathAnthropicMessages, reqBody, http.Header{
423431
"Anthropic-Beta": {"interleaved-thinking-2025-05-14,effort-2025-11-24,context-management-2025-06-27,prompt-caching-scope-2026-01-05"},
424432
})
433+
require.NoError(t, err)
434+
defer resp.Body.Close()
425435
require.Equal(t, http.StatusOK, resp.StatusCode)
426436
_, err = io.ReadAll(resp.Body)
427437
require.NoError(t, err)
@@ -502,7 +512,9 @@ func TestOpenAIChatCompletions(t *testing.T) {
502512
// Make API call to aibridge for OpenAI /v1/chat/completions
503513
reqBody, err := sjson.SetBytes(fix.Request(), "stream", tc.streaming)
504514
require.NoError(t, err)
505-
resp := bridgeServer.makeRequest(t, http.MethodPost, pathOpenAIChatCompletions, reqBody)
515+
resp, err := bridgeServer.makeRequest(t, http.MethodPost, pathOpenAIChatCompletions, reqBody)
516+
require.NoError(t, err)
517+
defer resp.Body.Close()
506518
require.Equal(t, http.StatusOK, resp.StatusCode)
507519

508520
// Response-specific checks.
@@ -583,7 +595,9 @@ func TestOpenAIChatCompletions(t *testing.T) {
583595
// Add the stream param to the request.
584596
reqBody, err := sjson.SetBytes(fix.Request(), "stream", true)
585597
require.NoError(t, err)
586-
resp := bridgeServer.makeRequest(t, http.MethodPost, pathOpenAIChatCompletions, reqBody)
598+
resp, err := bridgeServer.makeRequest(t, http.MethodPost, pathOpenAIChatCompletions, reqBody)
599+
require.NoError(t, err)
600+
defer resp.Body.Close()
587601
require.Equal(t, http.StatusOK, resp.StatusCode)
588602

589603
// Verify SSE headers are sent correctly
@@ -767,7 +781,9 @@ func TestSimple(t *testing.T) {
767781
// When: calling the "API server" with the fixture's request body.
768782
reqBody, err := sjson.SetBytes(fix.Request(), "stream", streaming)
769783
require.NoError(t, err)
770-
resp := bridgeServer.makeRequest(t, http.MethodPost, tc.path, reqBody, http.Header{"User-Agent": {tc.userAgent}})
784+
resp, err := bridgeServer.makeRequest(t, http.MethodPost, tc.path, reqBody, http.Header{"User-Agent": {tc.userAgent}})
785+
require.NoError(t, err)
786+
defer resp.Body.Close()
771787
require.Equal(t, http.StatusOK, resp.StatusCode)
772788

773789
// Then: I expect the upstream request to have the correct path.
@@ -875,11 +891,13 @@ func TestSessionIDTracking(t *testing.T) {
875891
require.NoError(t, err)
876892
}
877893

878-
resp := bridgeServer.makeRequest(t, http.MethodPost, pathAnthropicMessages, reqBody, tc.header)
894+
resp, err := bridgeServer.makeRequest(t, http.MethodPost, pathAnthropicMessages, reqBody, tc.header)
895+
require.NoError(t, err)
896+
defer resp.Body.Close()
879897
require.Equal(t, http.StatusOK, resp.StatusCode)
880898

881899
// Drain the body to let the stream complete.
882-
_, err := io.ReadAll(resp.Body)
900+
_, err = io.ReadAll(resp.Body)
883901
require.NoError(t, err)
884902

885903
interceptions := bridgeServer.Recorder.RecordedInterceptions()
@@ -951,7 +969,9 @@ func TestFallthrough(t *testing.T) {
951969
upstream := newMockUpstream(t.Context(), t, newFixtureResponse(fix))
952970
bridgeServer := newBridgeTestServer(t.Context(), t, upstream.URL+tc.basePath)
953971

954-
resp := bridgeServer.makeRequest(t, http.MethodGet, tc.requestPath, nil)
972+
resp, err := bridgeServer.makeRequest(t, http.MethodGet, tc.requestPath, nil)
973+
require.NoError(t, err)
974+
defer resp.Body.Close()
955975

956976
require.Equal(t, http.StatusOK, resp.StatusCode)
957977

@@ -984,6 +1004,7 @@ func TestAnthropicInjectedTools(t *testing.T) {
9841004

9851005
// Build the requirements & make the assertions which are common to all providers.
9861006
bridgeServer, mockMCP, resp := setupInjectedToolTest(t, fixtures.AntSingleInjectedTool, streaming, defaultTracer, pathAnthropicMessages, anthropicToolResultValidator(t))
1007+
defer resp.Body.Close()
9871008

9881009
// Ensure expected tool was invoked with expected input.
9891010
toolUsages := bridgeServer.Recorder.RecordedToolUsages()
@@ -1067,6 +1088,7 @@ func TestOpenAIInjectedTools(t *testing.T) {
10671088

10681089
// Build the requirements & make the assertions which are common to all providers.
10691090
bridgeServer, mockMCP, resp := setupInjectedToolTest(t, fixtures.OaiChatSingleInjectedTool, streaming, defaultTracer, pathOpenAIChatCompletions, openaiChatToolResultValidator(t))
1091+
defer resp.Body.Close()
10701092

10711093
// Ensure expected tool was invoked with expected input.
10721094
toolUsages := bridgeServer.Recorder.RecordedToolUsages()
@@ -1290,7 +1312,9 @@ func TestErrorHandling(t *testing.T) {
12901312
reqBody, err := sjson.SetBytes(fix.Request(), "stream", streaming)
12911313
require.NoError(t, err)
12921314

1293-
resp := bridgeServer.makeRequest(t, http.MethodPost, tc.path, reqBody)
1315+
resp, err := bridgeServer.makeRequest(t, http.MethodPost, tc.path, reqBody)
1316+
require.NoError(t, err)
1317+
defer resp.Body.Close()
12941318

12951319
tc.responseHandlerFn(resp)
12961320
bridgeServer.Recorder.VerifyAllInterceptionsEnded(t)
@@ -1357,7 +1381,9 @@ func TestErrorHandling(t *testing.T) {
13571381

13581382
bridgeServer := newBridgeTestServer(ctx, t, upstream.URL)
13591383

1360-
resp := bridgeServer.makeRequest(t, http.MethodPost, tc.path, fix.Request())
1384+
resp, err := bridgeServer.makeRequest(t, http.MethodPost, tc.path, fix.Request())
1385+
require.NoError(t, err)
1386+
defer resp.Body.Close()
13611387

13621388
tc.responseHandlerFn(resp)
13631389
bridgeServer.Recorder.VerifyAllInterceptionsEnded(t)
@@ -1416,7 +1442,9 @@ func TestStableRequestEncoding(t *testing.T) {
14161442

14171443
// Make multiple requests and verify they all have identical payloads.
14181444
for range count {
1419-
resp := bridgeServer.makeRequest(t, http.MethodPost, tc.path, fix.Request())
1445+
resp, err := bridgeServer.makeRequest(t, http.MethodPost, tc.path, fix.Request())
1446+
require.NoError(t, err)
1447+
defer resp.Body.Close()
14201448
require.Equal(t, http.StatusOK, resp.StatusCode)
14211449
}
14221450

@@ -1679,7 +1707,9 @@ func TestAnthropicToolChoiceParallelDisabled(t *testing.T) {
16791707
reqBody, err := sjson.SetBytes(fix.Request(), "tool_choice", tc.toolChoice)
16801708
require.NoError(t, err)
16811709

1682-
resp := bridgeServer.makeRequest(t, http.MethodPost, pathAnthropicMessages, reqBody)
1710+
resp, err := bridgeServer.makeRequest(t, http.MethodPost, pathAnthropicMessages, reqBody)
1711+
require.NoError(t, err)
1712+
defer resp.Body.Close()
16831713
require.Equal(t, http.StatusOK, resp.StatusCode)
16841714

16851715
// Verify tool_choice in the upstream request.
@@ -1842,7 +1872,9 @@ func TestChatCompletionsParallelToolCallsDisabled(t *testing.T) {
18421872
reqBody, err = sjson.SetBytes(reqBody, "stream", streaming)
18431873
require.NoError(t, err)
18441874

1845-
resp := bridgeServer.makeRequest(t, http.MethodPost, pathOpenAIChatCompletions, reqBody)
1875+
resp, err := bridgeServer.makeRequest(t, http.MethodPost, pathOpenAIChatCompletions, reqBody)
1876+
require.NoError(t, err)
1877+
defer resp.Body.Close()
18461878
_, err = io.ReadAll(resp.Body)
18471879
require.NoError(t, err)
18481880

@@ -1886,7 +1918,9 @@ func TestThinkingAdaptiveIsPreserved(t *testing.T) {
18861918
reqBody, err = sjson.SetBytes(reqBody, "stream", streaming)
18871919
require.NoError(t, err)
18881920

1889-
resp := bridgeServer.makeRequest(t, http.MethodPost, pathAnthropicMessages, reqBody)
1921+
resp, err := bridgeServer.makeRequest(t, http.MethodPost, pathAnthropicMessages, reqBody)
1922+
require.NoError(t, err)
1923+
defer resp.Body.Close()
18901924
require.Equal(t, http.StatusOK, resp.StatusCode)
18911925
_, err = io.ReadAll(resp.Body)
18921926
require.NoError(t, err)
@@ -1949,7 +1983,9 @@ func TestEnvironmentDoNotLeak(t *testing.T) {
19491983

19501984
bridgeServer := newBridgeTestServer(ctx, t, upstream.URL)
19511985

1952-
resp := bridgeServer.makeRequest(t, http.MethodPost, tc.path, fix.Request())
1986+
resp, err := bridgeServer.makeRequest(t, http.MethodPost, tc.path, fix.Request())
1987+
require.NoError(t, err)
1988+
defer resp.Body.Close()
19531989
require.Equal(t, http.StatusOK, resp.StatusCode)
19541990

19551991
// Verify that environment values did not leak.
@@ -2063,7 +2099,9 @@ func TestActorHeaders(t *testing.T) {
20632099
reqBody, err := sjson.SetBytes(fix.Request(), "stream", tc.streaming)
20642100
require.NoError(t, err)
20652101

2066-
resp := bridgeServer.makeRequest(t, http.MethodPost, tc.path, reqBody)
2102+
resp, err := bridgeServer.makeRequest(t, http.MethodPost, tc.path, reqBody)
2103+
require.NoError(t, err)
2104+
defer resp.Body.Close()
20672105
// Drain the body so streaming responses complete without
20682106
// a "connection reset" error in the mock upstream.
20692107
_, err = io.ReadAll(resp.Body)

0 commit comments

Comments
 (0)