TestServeHTTP_ActorHeaders validates that actor headers are correctly forwarded to upstream AI providers when SendActorHeaders is enabled in the provider configuration. These headers allow upstream providers to identify the user making the request for tracking and auditing purposes.
(t *testing.T)
| 607 | // These headers allow upstream providers to identify the user making the request for |
| 608 | // tracking and auditing purposes. |
| 609 | func TestServeHTTP_ActorHeaders(t *testing.T) { |
| 610 | t.Parallel() |
| 611 | |
| 612 | testUsername := "testuser" |
| 613 | testUserID := uuid.New() |
| 614 | |
| 615 | cases := []struct { |
| 616 | path string |
| 617 | }{ |
| 618 | // Not a complete set of paths; we're not testing the specific APIs - just the provider configs. |
| 619 | { |
| 620 | path: "/openai/v1/chat/completions", |
| 621 | }, |
| 622 | { |
| 623 | path: "/anthropic/v1/messages", |
| 624 | }, |
| 625 | } |
| 626 | |
| 627 | for _, tc := range cases { |
| 628 | t.Run(tc.path, func(t *testing.T) { |
| 629 | t.Parallel() |
| 630 | |
| 631 | // Setup mock upstream AI server that captures headers. |
| 632 | var receivedHeaders http.Header |
| 633 | upstreamSrv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 634 | receivedHeaders = r.Header.Clone() |
| 635 | w.WriteHeader(http.StatusTeapot) |
| 636 | _, _ = w.Write([]byte(`i am a teapot`)) |
| 637 | })) |
| 638 | t.Cleanup(upstreamSrv.Close) |
| 639 | |
| 640 | // Setup with SendActorHeaders enabled. |
| 641 | logger := slogtest.Make(t, &slogtest.Options{IgnoreErrors: true}) |
| 642 | ctrl := gomock.NewController(t) |
| 643 | client := mock.NewMockDRPCClient(ctrl) |
| 644 | |
| 645 | // Create providers with SendActorHeaders=true. |
| 646 | providers := []aibridge.Provider{ |
| 647 | aibridge.NewOpenAIProvider(aibridge.OpenAIConfig{ |
| 648 | BaseURL: upstreamSrv.URL, |
| 649 | SendActorHeaders: true, |
| 650 | }), |
| 651 | aibridge.NewAnthropicProvider(aibridge.AnthropicConfig{ |
| 652 | BaseURL: upstreamSrv.URL, |
| 653 | SendActorHeaders: true, |
| 654 | }, nil), |
| 655 | } |
| 656 | |
| 657 | pool, err := aibridged.NewCachedBridgePool(aibridged.DefaultPoolOptions, providers, logger, nil, testTracer) |
| 658 | require.NoError(t, err) |
| 659 | conn := &mockDRPCConn{} |
| 660 | client.EXPECT().DRPCConn().AnyTimes().Return(conn) |
| 661 | |
| 662 | // Return authorization response with user ID and username. |
| 663 | client.EXPECT().IsAuthorized(gomock.Any(), gomock.Any()).AnyTimes().Return(&proto.IsAuthorizedResponse{ |
| 664 | OwnerId: testUserID.String(), |
| 665 | Username: testUsername, |
| 666 | }, nil) |
nothing calls this directly
no test coverage detected