TestStableRequestEncoding validates that a given intercepted request and a given set of injected tools should result identical payloads. Should the payload vary, it may subvert any caching mechanisms the provider may have.
(t *testing.T)
| 1399 | // |
| 1400 | // Should the payload vary, it may subvert any caching mechanisms the provider may have. |
| 1401 | func TestStableRequestEncoding(t *testing.T) { |
| 1402 | t.Parallel() |
| 1403 | |
| 1404 | cases := []struct { |
| 1405 | name string |
| 1406 | fixture []byte |
| 1407 | path string |
| 1408 | }{ |
| 1409 | { |
| 1410 | name: config.ProviderAnthropic, |
| 1411 | fixture: fixtures.AntSimple, |
| 1412 | path: pathAnthropicMessages, |
| 1413 | }, |
| 1414 | { |
| 1415 | name: config.ProviderOpenAI, |
| 1416 | fixture: fixtures.OaiChatSimple, |
| 1417 | path: pathOpenAIChatCompletions, |
| 1418 | }, |
| 1419 | } |
| 1420 | |
| 1421 | for _, tc := range cases { |
| 1422 | t.Run(tc.name, func(t *testing.T) { |
| 1423 | t.Parallel() |
| 1424 | |
| 1425 | ctx, cancel := context.WithTimeout(t.Context(), testutil.WaitLong) |
| 1426 | t.Cleanup(cancel) |
| 1427 | |
| 1428 | // Setup MCP tools. |
| 1429 | mockMCP := setupMCPForTest(t, defaultTracer) |
| 1430 | |
| 1431 | fix := fixtures.Parse(t, tc.fixture) |
| 1432 | |
| 1433 | // Create a mock upstream that serves the same blocking response for each request. |
| 1434 | count := 10 |
| 1435 | responses := make([]upstreamResponse, count) |
| 1436 | for i := range count { |
| 1437 | responses[i] = newFixtureResponse(fix) |
| 1438 | } |
| 1439 | upstream := newMockUpstream(ctx, t, responses...) |
| 1440 | |
| 1441 | bridgeServer := newBridgeTestServer(ctx, t, upstream.URL, |
| 1442 | withMCP(mockMCP), |
| 1443 | ) |
| 1444 | |
| 1445 | // Make multiple requests and verify they all have identical payloads. |
| 1446 | for range count { |
| 1447 | resp, err := bridgeServer.makeRequest(t, http.MethodPost, tc.path, fix.Request()) |
| 1448 | require.NoError(t, err) |
| 1449 | defer resp.Body.Close() |
| 1450 | require.Equal(t, http.StatusOK, resp.StatusCode) |
| 1451 | } |
| 1452 | |
| 1453 | // All upstream request bodies should be identical. |
| 1454 | received := upstream.receivedRequests() |
| 1455 | require.Len(t, received, count) |
| 1456 | reference := string(received[0].Body) |
| 1457 | for _, r := range received[1:] { |
| 1458 | assert.JSONEq(t, reference, string(r.Body)) |
nothing calls this directly
no test coverage detected