fix(aibridge): write a separate API dump per upstream call (#32600)
Bridged API dumps used one path per interception, so key failover
attempts overwrote each other. Each upstream call now gets its own file:
`<ts>-<interceptionID>-<seq>.{req,resp}.txt`.
Fixes AIGOV-333
## Things to know
- The sequence number guarantees unique names even when two calls land
in the same millisecond.
- SDK retries on BYOK and non-pool paths likely get separate files too
(unverified).
- Agentic loop iterations are out of scope: they only come from
deprecated injected MCP tools. `responses/blocking.go` rebuilds the
middleware per iteration, so numbering restarts there and only the
timestamp separates iterations.
- No docs changes; the docs don't describe the file name pattern.
<details>
<summary>Design notes</summary>
- The bridge middleware owns the sequence counter. All failover attempts
of one call go through the same middleware.
- Both new tests fail without the fix.
</details>
---
_Generated by Coder Agents on behalf of @roothorp._
This commit is contained in:
1 parent
329c60fffc
commit
c8fe5ccfdd
4 files changed
+229
-82
No files matched your search
@@ -11,6 +11,7 @@ import (
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/tidwall/pretty"
|
||||
@@ -36,19 +37,21 @@ type MiddlewareNext = func(*http.Request) (*http.Response, error)
|
||||
// Middleware is an HTTP middleware function compatible with SDK WithMiddleware options.
|
||||
type Middleware = func(*http.Request, MiddlewareNext) (*http.Response, error)
|
||||
|
||||
// NewBridgeMiddleware returns a middleware function that dumps requests and responses to files.
|
||||
// If baseDir is empty, returns nil (no middleware).
|
||||
// NewBridgeMiddleware returns a middleware that dumps each upstream request and
|
||||
// response passing through it to its own numbered files, so key failover
|
||||
// attempts do not overwrite each other. If baseDir is empty, it returns nil.
|
||||
func NewBridgeMiddleware(baseDir string, provider string, model string, interceptionID uuid.UUID, logger slog.Logger, clk quartz.Clock) Middleware {
|
||||
if baseDir == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
d := &Dumper{
|
||||
dumpPath: interceptDumpPath(baseDir, provider, model, interceptionID, clk),
|
||||
logger: logger,
|
||||
}
|
||||
|
||||
var seq atomic.Int64
|
||||
return func(req *http.Request, next MiddlewareNext) (*http.Response, error) {
|
||||
d := &Dumper{
|
||||
dumpPath: interceptDumpPath(baseDir, provider, model, interceptionID, seq.Add(1), clk),
|
||||
logger: logger,
|
||||
}
|
||||
|
||||
if err := d.DumpRequest(req); err != nil {
|
||||
logger.Named("apidump").Warn(req.Context(), "failed to dump request", slog.Error(err))
|
||||
}
|
||||
@@ -227,10 +230,11 @@ func (*Dumper) writeRedactedHeaders(w io.Writer, headers http.Header, sensitive
|
||||
return nil
|
||||
}
|
||||
|
||||
// interceptDumpPath returns the base file path (without req/resp suffix) for an interception dump.
|
||||
func interceptDumpPath(baseDir string, provider string, model string, interceptionID uuid.UUID, clk quartz.Clock) string {
|
||||
// interceptDumpPath returns the base file path (without req/resp suffix) for
|
||||
// one upstream call of an interception.
|
||||
func interceptDumpPath(baseDir string, provider string, model string, interceptionID uuid.UUID, seq int64, clk quartz.Clock) string {
|
||||
safeModel := strings.ReplaceAll(model, "/", "-")
|
||||
return filepath.Join(baseDir, provider, safeModel, fmt.Sprintf("%d-%s", clk.Now().UTC().UnixMilli(), interceptionID))
|
||||
return filepath.Join(baseDir, provider, safeModel, fmt.Sprintf("%d-%s-%03d", clk.Now().UTC().UnixMilli(), interceptionID, seq))
|
||||
}
|
||||
|
||||
// passthroughDumpPath returns the base file path (without req/resp suffix) for a passthrough dump.
|
||||
|
||||
@@ -251,6 +251,68 @@ func TestBridgedMiddleware_ModelWithSlash(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
// TestBridgedMiddleware_SeparateFilesPerCall verifies that upstream calls in
|
||||
// one interception, such as key failover attempts, do not overwrite each
|
||||
// other's dumps, even at the same clock instant.
|
||||
func TestBridgedMiddleware_SeparateFilesPerCall(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tmpDir := t.TempDir()
|
||||
logger := slogtest.Make(t, &slogtest.Options{IgnoreErrors: false}).Leveled(slog.LevelDebug)
|
||||
clk := quartz.NewMock(t)
|
||||
interceptionID := uuid.New()
|
||||
middleware := NewBridgeMiddleware(tmpDir, "openai", "gpt-4", interceptionID, logger, clk)
|
||||
require.NotNil(t, middleware)
|
||||
|
||||
calls := []struct {
|
||||
reqBody string
|
||||
status int
|
||||
respBody string
|
||||
}{
|
||||
{reqBody: `{"attempt":1}`, status: http.StatusTooManyRequests, respBody: `{"error":"rate limited"}`},
|
||||
{reqBody: `{"attempt":2}`, status: http.StatusOK, respBody: `{"ok":true}`},
|
||||
}
|
||||
for _, c := range calls {
|
||||
req, err := http.NewRequestWithContext(t.Context(), http.MethodPost, "https://api.openai.com/v1/chat/completions", strings.NewReader(c.reqBody))
|
||||
require.NoError(t, err)
|
||||
|
||||
resp, err := middleware(req, func(_ *http.Request) (*http.Response, error) {
|
||||
return &http.Response{
|
||||
StatusCode: c.status,
|
||||
Status: http.StatusText(c.status),
|
||||
Proto: "HTTP/1.1",
|
||||
Header: http.Header{},
|
||||
Body: io.NopCloser(strings.NewReader(c.respBody)),
|
||||
}, nil
|
||||
})
|
||||
require.NoError(t, err)
|
||||
_, err = io.ReadAll(resp.Body)
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, resp.Body.Close())
|
||||
}
|
||||
|
||||
modelDir := filepath.Join(tmpDir, "openai", "gpt-4")
|
||||
reqFiles, err := filepath.Glob(filepath.Join(modelDir, "*"+SuffixRequest))
|
||||
require.NoError(t, err)
|
||||
respFiles, err := filepath.Glob(filepath.Join(modelDir, "*"+SuffixResponse))
|
||||
require.NoError(t, err)
|
||||
require.Len(t, reqFiles, len(calls))
|
||||
require.Len(t, respFiles, len(calls))
|
||||
|
||||
// Glob returns sorted names, and the zero-padded sequence number keeps
|
||||
// them in call order.
|
||||
for i, c := range calls {
|
||||
require.Contains(t, reqFiles[i], interceptionID.String())
|
||||
reqContent, err := os.ReadFile(reqFiles[i])
|
||||
require.NoError(t, err)
|
||||
require.Contains(t, string(reqContent), string(prettyPrintJSON([]byte(c.reqBody))))
|
||||
|
||||
respContent, err := os.ReadFile(respFiles[i])
|
||||
require.NoError(t, err)
|
||||
require.Contains(t, string(respContent), c.respBody)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrettyPrintJSON(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
|
||||
@@ -47,7 +47,7 @@ func TestWriteRedactedHeaders(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
d := &Dumper{
|
||||
dumpPath: interceptDumpPath("/tmp", "test", "test", uuid.New(), quartz.NewMock(t)),
|
||||
dumpPath: interceptDumpPath("/tmp", "test", "test", uuid.New(), 1, quartz.NewMock(t)),
|
||||
logger: slog.Make(),
|
||||
}
|
||||
|
||||
|
||||
@@ -13,6 +13,7 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
"github.com/tidwall/sjson"
|
||||
|
||||
"github.com/coder/coder/v2/aibridge"
|
||||
"github.com/coder/coder/v2/aibridge/aibridgetest"
|
||||
@@ -20,7 +21,9 @@ import (
|
||||
"github.com/coder/coder/v2/aibridge/fixtures"
|
||||
"github.com/coder/coder/v2/aibridge/intercept/apidump"
|
||||
"github.com/coder/coder/v2/aibridge/internal/testutil"
|
||||
"github.com/coder/coder/v2/aibridge/keypool"
|
||||
"github.com/coder/coder/v2/aibridge/provider"
|
||||
"github.com/coder/quartz"
|
||||
)
|
||||
|
||||
const osSep = string(filepath.Separator)
|
||||
@@ -28,45 +31,59 @@ const osSep = string(filepath.Separator)
|
||||
func TestAPIDump(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
anthropicFunc := func(addr, dumpDir string, pool *keypool.Pool) aibridge.Provider {
|
||||
cfg := anthropicCfgWithAPIDump(addr, apiKey, dumpDir)
|
||||
if pool != nil {
|
||||
cfg.KeyPool = pool
|
||||
}
|
||||
return aibridgetest.NewAnthropicProvider(t, cfg, nil)
|
||||
}
|
||||
openAIFunc := func(addr, dumpDir string, pool *keypool.Pool) aibridge.Provider {
|
||||
cfg := openaiCfgWithAPIDump(addr, apiKey, dumpDir)
|
||||
if pool != nil {
|
||||
cfg.KeyPool = pool
|
||||
}
|
||||
return provider.NewOpenAI(cfg)
|
||||
}
|
||||
|
||||
cases := []struct {
|
||||
name string
|
||||
fixture []byte
|
||||
providerFunc func(addr, dumpDir string) aibridge.Provider
|
||||
providerFunc func(addr, dumpDir string, pool *keypool.Pool) aibridge.Provider
|
||||
path string
|
||||
headers http.Header
|
||||
expectProviderDir string
|
||||
streaming bool
|
||||
// failover rate limits the first of two pool keys, so the
|
||||
// interception makes two upstream calls that must each keep their
|
||||
// own dumps.
|
||||
failover bool
|
||||
}{
|
||||
{
|
||||
name: "anthropic",
|
||||
fixture: fixtures.AntSimple,
|
||||
providerFunc: func(addr, dumpDir string) aibridge.Provider {
|
||||
return aibridgetest.NewAnthropicProvider(t, anthropicCfgWithAPIDump(addr, apiKey, dumpDir), nil)
|
||||
},
|
||||
name: "anthropic",
|
||||
fixture: fixtures.AntSimple,
|
||||
providerFunc: anthropicFunc,
|
||||
path: pathAnthropicMessages,
|
||||
expectProviderDir: config.ProviderAnthropic,
|
||||
},
|
||||
{
|
||||
name: "openai_chat_completions",
|
||||
fixture: fixtures.OaiChatSimple,
|
||||
providerFunc: func(addr, dumpDir string) aibridge.Provider {
|
||||
return provider.NewOpenAI(openaiCfgWithAPIDump(addr, apiKey, dumpDir))
|
||||
},
|
||||
name: "openai_chat_completions",
|
||||
fixture: fixtures.OaiChatSimple,
|
||||
providerFunc: openAIFunc,
|
||||
path: pathOpenAIChatCompletions,
|
||||
expectProviderDir: config.ProviderOpenAI,
|
||||
},
|
||||
{
|
||||
name: "openai_responses",
|
||||
fixture: fixtures.OaiResponsesBlockingSimple,
|
||||
providerFunc: func(addr, dumpDir string) aibridge.Provider {
|
||||
return provider.NewOpenAI(openaiCfgWithAPIDump(addr, apiKey, dumpDir))
|
||||
},
|
||||
name: "openai_responses",
|
||||
fixture: fixtures.OaiResponsesBlockingSimple,
|
||||
providerFunc: openAIFunc,
|
||||
path: pathOpenAIResponses,
|
||||
expectProviderDir: config.ProviderOpenAI,
|
||||
},
|
||||
{
|
||||
name: "copilot_chat_completions",
|
||||
fixture: fixtures.OaiChatSimple,
|
||||
providerFunc: func(addr, dumpDir string) aibridge.Provider {
|
||||
providerFunc: func(addr, dumpDir string, _ *keypool.Pool) aibridge.Provider {
|
||||
return provider.NewCopilot(config.Copilot{BaseURL: addr, APIDumpDir: dumpDir})
|
||||
},
|
||||
path: pathCopilotChatCompletions,
|
||||
@@ -76,7 +93,7 @@ func TestAPIDump(t *testing.T) {
|
||||
{
|
||||
name: "copilot_responses",
|
||||
fixture: fixtures.OaiResponsesBlockingSimple,
|
||||
providerFunc: func(addr, dumpDir string) aibridge.Provider {
|
||||
providerFunc: func(addr, dumpDir string, _ *keypool.Pool) aibridge.Provider {
|
||||
return provider.NewCopilot(config.Copilot{BaseURL: addr, APIDumpDir: dumpDir})
|
||||
},
|
||||
path: pathCopilotResponses,
|
||||
@@ -86,7 +103,7 @@ func TestAPIDump(t *testing.T) {
|
||||
{
|
||||
name: "copilot_custom_name_chat_completions",
|
||||
fixture: fixtures.OaiChatSimple,
|
||||
providerFunc: func(addr, dumpDir string) aibridge.Provider {
|
||||
providerFunc: func(addr, dumpDir string, _ *keypool.Pool) aibridge.Provider {
|
||||
return provider.NewCopilot(config.Copilot{
|
||||
Name: "copilot-business",
|
||||
BaseURL: addr,
|
||||
@@ -100,7 +117,7 @@ func TestAPIDump(t *testing.T) {
|
||||
{
|
||||
name: "copilot_custom_name_responses",
|
||||
fixture: fixtures.OaiChatSimple,
|
||||
providerFunc: func(addr, dumpDir string) aibridge.Provider {
|
||||
providerFunc: func(addr, dumpDir string, _ *keypool.Pool) aibridge.Provider {
|
||||
return provider.NewCopilot(config.Copilot{
|
||||
Name: "copilot-enterprise",
|
||||
BaseURL: addr,
|
||||
@@ -111,6 +128,57 @@ func TestAPIDump(t *testing.T) {
|
||||
headers: http.Header{"Authorization": {"Bearer test-copilot-token"}},
|
||||
expectProviderDir: "copilot-enterprise",
|
||||
},
|
||||
{
|
||||
name: "anthropic_failover_blocking",
|
||||
fixture: fixtures.AntSimple,
|
||||
providerFunc: anthropicFunc,
|
||||
path: pathAnthropicMessages,
|
||||
expectProviderDir: config.ProviderAnthropic,
|
||||
failover: true,
|
||||
},
|
||||
{
|
||||
name: "anthropic_failover_streaming",
|
||||
fixture: fixtures.AntSimple,
|
||||
providerFunc: anthropicFunc,
|
||||
path: pathAnthropicMessages,
|
||||
expectProviderDir: config.ProviderAnthropic,
|
||||
streaming: true,
|
||||
failover: true,
|
||||
},
|
||||
{
|
||||
name: "openai_chat_completions_failover_blocking",
|
||||
fixture: fixtures.OaiChatSimple,
|
||||
providerFunc: openAIFunc,
|
||||
path: pathOpenAIChatCompletions,
|
||||
expectProviderDir: config.ProviderOpenAI,
|
||||
failover: true,
|
||||
},
|
||||
{
|
||||
name: "openai_chat_completions_failover_streaming",
|
||||
fixture: fixtures.OaiChatSimple,
|
||||
providerFunc: openAIFunc,
|
||||
path: pathOpenAIChatCompletions,
|
||||
expectProviderDir: config.ProviderOpenAI,
|
||||
streaming: true,
|
||||
failover: true,
|
||||
},
|
||||
{
|
||||
name: "openai_responses_failover_blocking",
|
||||
fixture: fixtures.OaiResponsesBlockingSimple,
|
||||
providerFunc: openAIFunc,
|
||||
path: pathOpenAIResponses,
|
||||
expectProviderDir: config.ProviderOpenAI,
|
||||
failover: true,
|
||||
},
|
||||
{
|
||||
name: "openai_responses_failover_streaming",
|
||||
fixture: fixtures.OaiResponsesStreamingSimple,
|
||||
providerFunc: openAIFunc,
|
||||
path: pathOpenAIResponses,
|
||||
expectProviderDir: config.ProviderOpenAI,
|
||||
streaming: true,
|
||||
failover: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
@@ -120,86 +188,99 @@ func TestAPIDump(t *testing.T) {
|
||||
ctx, cancel := context.WithTimeout(t.Context(), testutil.WaitLong)
|
||||
t.Cleanup(cancel)
|
||||
|
||||
// Setup mock upstream server.
|
||||
// Setup mock upstream server. With failover, the first key is
|
||||
// rate limited and the second succeeds.
|
||||
fix := fixtures.Parse(t, tc.fixture)
|
||||
srv := testutil.NewMockUpstream(ctx, t, testutil.NewFixtureResponse(fix))
|
||||
upstreamResponses := []testutil.UpstreamResponse{testutil.NewFixtureResponse(fix)}
|
||||
wantStatuses := []int{http.StatusOK}
|
||||
var pool *keypool.Pool
|
||||
if tc.failover {
|
||||
upstreamResponses = append([]testutil.UpstreamResponse{testutil.NewErrorResponse(http.StatusTooManyRequests, "60")}, upstreamResponses...)
|
||||
wantStatuses = append([]int{http.StatusTooManyRequests}, wantStatuses...)
|
||||
var err error
|
||||
pool, err = keypool.New(tc.expectProviderDir, []string{"k0", "k1"}, quartz.NewMock(t), nil)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
srv := testutil.NewMockUpstream(ctx, t, upstreamResponses...)
|
||||
|
||||
// Create temp dir for API dumps.
|
||||
dumpDir := t.TempDir()
|
||||
|
||||
bridgeServer := newBridgeTestServer(ctx, t, srv.URL,
|
||||
withCustomProvider(tc.providerFunc(srv.URL, dumpDir)),
|
||||
withCustomProvider(tc.providerFunc(srv.URL, dumpDir, pool)),
|
||||
)
|
||||
|
||||
resp, err := bridgeServer.makeRequest(t, http.MethodPost, tc.path, fix.Request(), tc.headers)
|
||||
requestBody := fix.Request()
|
||||
if tc.streaming {
|
||||
var err error
|
||||
requestBody, err = sjson.SetBytes(requestBody, "stream", true)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
resp, err := bridgeServer.makeRequest(t, http.MethodPost, tc.path, requestBody, tc.headers)
|
||||
require.NoError(t, err)
|
||||
defer resp.Body.Close()
|
||||
require.Equal(t, http.StatusOK, resp.StatusCode)
|
||||
_, err = io.ReadAll(resp.Body)
|
||||
require.NoError(t, err)
|
||||
received := srv.ReceivedRequests()
|
||||
require.Len(t, received, len(wantStatuses))
|
||||
|
||||
// Verify dump files were created.
|
||||
interceptions := bridgeServer.Recorder.RecordedInterceptions()
|
||||
require.Len(t, interceptions, 1)
|
||||
interceptionID := interceptions[0].ID
|
||||
|
||||
// Find dump files for this interception by walking the dump directory.
|
||||
var reqDumpFile, respDumpFile string
|
||||
// Find dump files for this interception by walking the dump
|
||||
// directory. Files are named
|
||||
// {timestamp}-{interceptionID}-{seq}.{req|resp}.txt, and Walk
|
||||
// visits them in lexical order, which is call order.
|
||||
var reqDumpFiles, respDumpFiles []string
|
||||
err = filepath.Walk(dumpDir, func(path string, info os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
if err != nil || info.IsDir() || !strings.Contains(path, interceptionID) {
|
||||
return err
|
||||
}
|
||||
if info.IsDir() {
|
||||
return nil
|
||||
}
|
||||
// Files are named: {timestamp}-{interceptionID}.{req|resp}.txt
|
||||
if strings.Contains(path, interceptionID) {
|
||||
if strings.HasSuffix(path, apidump.SuffixRequest) {
|
||||
reqDumpFile = path
|
||||
} else if strings.HasSuffix(path, apidump.SuffixResponse) {
|
||||
respDumpFile = path
|
||||
}
|
||||
switch {
|
||||
case strings.HasSuffix(path, apidump.SuffixRequest):
|
||||
reqDumpFiles = append(reqDumpFiles, path)
|
||||
case strings.HasSuffix(path, apidump.SuffixResponse):
|
||||
respDumpFiles = append(respDumpFiles, path)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
require.NoError(t, err)
|
||||
require.NotEmpty(t, reqDumpFile, "request dump file should exist")
|
||||
require.NotEmpty(t, respDumpFile, "response dump file should exist")
|
||||
require.Len(t, reqDumpFiles, len(wantStatuses), "one request dump per upstream call")
|
||||
require.Len(t, respDumpFiles, len(wantStatuses), "one response dump per upstream call")
|
||||
|
||||
// Verify dump files are in the correct provider subdirectory.
|
||||
require.Contains(t, reqDumpFile, filepath.Join(dumpDir, tc.expectProviderDir)+osSep,
|
||||
"request dump should be in the %s provider directory", tc.expectProviderDir)
|
||||
require.Contains(t, respDumpFile, filepath.Join(dumpDir, tc.expectProviderDir)+osSep,
|
||||
"response dump should be in the %s provider directory", tc.expectProviderDir)
|
||||
for i, wantStatus := range wantStatuses {
|
||||
// Verify dump files are in the correct provider subdirectory.
|
||||
require.Contains(t, reqDumpFiles[i], filepath.Join(dumpDir, tc.expectProviderDir)+osSep,
|
||||
"request dump should be in the %s provider directory", tc.expectProviderDir)
|
||||
require.Contains(t, respDumpFiles[i], filepath.Join(dumpDir, tc.expectProviderDir)+osSep,
|
||||
"response dump should be in the %s provider directory", tc.expectProviderDir)
|
||||
|
||||
// Verify request dump contains expected HTTP request format.
|
||||
reqDumpData, err := os.ReadFile(reqDumpFile)
|
||||
require.NoError(t, err)
|
||||
// The request dump holds the body sent upstream on this call.
|
||||
// Compare semantically because key order may differ.
|
||||
reqDumpData, err := os.ReadFile(reqDumpFiles[i])
|
||||
require.NoError(t, err)
|
||||
dumpReq, err := http.ReadRequest(bufio.NewReader(bytes.NewReader(reqDumpData)))
|
||||
require.NoError(t, err)
|
||||
dumpBody, err := io.ReadAll(dumpReq.Body)
|
||||
require.NoError(t, err)
|
||||
require.JSONEq(t, string(received[i].Body), string(dumpBody), "request dump %d", i)
|
||||
|
||||
// Parse the dumped HTTP request.
|
||||
dumpReq, err := http.ReadRequest(bufio.NewReader(bytes.NewReader(reqDumpData)))
|
||||
require.NoError(t, err)
|
||||
dumpBody, err := io.ReadAll(dumpReq.Body)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Compare requests semantically (key order may differ).
|
||||
require.JSONEq(t, string(dumpBody), string(fix.Request()), "request body JSON should match semantically")
|
||||
|
||||
// Verify response dump contains expected HTTP response format.
|
||||
respDumpData, err := os.ReadFile(respDumpFile)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Parse the dumped HTTP response.
|
||||
dumpResp, err := http.ReadResponse(bufio.NewReader(bytes.NewReader(respDumpData)), nil)
|
||||
require.NoError(t, err)
|
||||
defer dumpResp.Body.Close()
|
||||
require.Equal(t, http.StatusOK, dumpResp.StatusCode)
|
||||
dumpRespBody, err := io.ReadAll(dumpResp.Body)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Compare responses semantically (key order may differ).
|
||||
expectedRespBody := fix.NonStreaming()
|
||||
require.JSONEq(t, string(expectedRespBody), string(dumpRespBody), "response body JSON should match semantically")
|
||||
respDumpData, err := os.ReadFile(respDumpFiles[i])
|
||||
require.NoError(t, err)
|
||||
dumpResp, err := http.ReadResponse(bufio.NewReader(bytes.NewReader(respDumpData)), nil)
|
||||
require.NoError(t, err)
|
||||
dumpRespBody, err := io.ReadAll(dumpResp.Body)
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, dumpResp.Body.Close())
|
||||
require.Equal(t, wantStatus, dumpResp.StatusCode, "response dump %d", i)
|
||||
if wantStatus == http.StatusOK && !tc.streaming {
|
||||
require.JSONEq(t, string(fix.NonStreaming()), string(dumpRespBody), "response body JSON should match semantically")
|
||||
}
|
||||
}
|
||||
|
||||
bridgeServer.Recorder.VerifyAllInterceptionsEnded(t)
|
||||
})
|
||||
|
||||
Reference in new issue
Block a user