MCPcopy Create free account
hub / github.com/coder/aibridge / TestMiddleware_StreamingResponse

Function TestMiddleware_StreamingResponse

intercept/apidump/streaming_test.go:20–96  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

18)
19
20func TestMiddleware_StreamingResponse(t *testing.T) {
21 t.Parallel()
22
23 tmpDir := t.TempDir()
24 logger := slogtest.Make(t, &slogtest.Options{IgnoreErrors: false}).Leveled(slog.LevelDebug)
25 clk := quartz.NewMock(t)
26 interceptionID := uuid.New()
27
28 middleware := NewBridgeMiddleware(tmpDir, "openai", "gpt-4", interceptionID, logger, clk)
29 require.NotNil(t, middleware)
30
31 req, err := http.NewRequestWithContext(t.Context(), http.MethodPost, "https://api.openai.com/v1/chat/completions", bytes.NewReader([]byte(`{}`)))
32 require.NoError(t, err)
33
34 // Simulate a streaming response with multiple chunks
35 chunks := []string{
36 "data: {\"chunk\": 1}\n\n",
37 "data: {\"chunk\": 2}\n\n",
38 "data: {\"chunk\": 3}\n\n",
39 "data: [DONE]\n\n",
40 }
41
42 // Create a pipe to simulate streaming
43 pr, pw := io.Pipe()
44 go func() {
45 defer pw.Close() //nolint:revive // error handled via pipe read side
46 for _, chunk := range chunks {
47 if _, err := pw.Write([]byte(chunk)); err != nil {
48 return
49 }
50 }
51 }()
52
53 resp, err := middleware(req, func(r *http.Request) (*http.Response, error) {
54 return &http.Response{
55 StatusCode: http.StatusOK,
56 Status: "200 OK",
57 Proto: "HTTP/1.1",
58 Header: http.Header{"Content-Type": []string{"text/event-stream"}},
59 Body: pr,
60 }, nil
61 })
62 require.NoError(t, err)
63
64 // Read response in small chunks to simulate streaming consumption
65 var receivedData bytes.Buffer
66 buf := make([]byte, 16)
67 for {
68 n, err := resp.Body.Read(buf)
69 if n > 0 {
70 _, _ = receivedData.Write(buf[:n]) // bytes.Buffer.Write never fails
71 }
72 if err == io.EOF {
73 break
74 }
75 require.NoError(t, err)
76 }
77 require.NoError(t, resp.Body.Close())

Callers

nothing calls this directly

Calls 5

NewBridgeMiddlewareFunction · 0.85
findDumpFileFunction · 0.85
CloseMethod · 0.45
WriteMethod · 0.45
ReadMethod · 0.45

Tested by

no test coverage detected