End-to-end: a real transport factory wired to a real server. Verifies the delegated key ID survives the in-memory round-trip and is treated as the authoritative caller identity by the handler, without any HTTP-layer header extraction.
(t *testing.T)
| 367 | // authoritative caller identity by the handler, without any HTTP-layer header |
| 368 | // extraction. |
| 369 | func TestServeHTTP_DelegatedAPIKey_Integration(t *testing.T) { |
| 370 | t.Parallel() |
| 371 | |
| 372 | const testKeyID = "abcdef1234" |
| 373 | |
| 374 | srv, client, pool := newTestServer(t) |
| 375 | conn := &mockDRPCConn{} |
| 376 | client.EXPECT().DRPCConn().AnyTimes().Return(conn) |
| 377 | mockH := &mockHandler{} |
| 378 | |
| 379 | client.EXPECT().IsAuthorized(gomock.Any(), gomock.Any()).DoAndReturn( |
| 380 | func(_ context.Context, in *proto.IsAuthorizedRequest) (*proto.IsAuthorizedResponse, error) { |
| 381 | assert.Equal(t, testKeyID, in.GetKeyId()) |
| 382 | assert.Empty(t, in.GetKey()) |
| 383 | return &proto.IsAuthorizedResponse{ |
| 384 | OwnerId: uuid.NewString(), |
| 385 | ApiKeyId: testKeyID, |
| 386 | Username: "u", |
| 387 | }, nil |
| 388 | }) |
| 389 | pool.EXPECT().Acquire(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(mockH, nil) |
| 390 | |
| 391 | factory := aibridged.NewTransportFactory(srv) |
| 392 | rt, err := factory.TransportFor("openai", agplaibridge.SourceAgents) |
| 393 | require.NoError(t, err) |
| 394 | |
| 395 | ctx := agplaibridge.WithDelegatedAPIKeyID(testutil.Context(t, testutil.WaitShort), testKeyID) |
| 396 | req, err := http.NewRequestWithContext(ctx, http.MethodPost, "http://aibridge/openai/v1/chat/completions", nil) |
| 397 | require.NoError(t, err) |
| 398 | |
| 399 | resp, err := rt.RoundTrip(req) |
| 400 | require.NoError(t, err) |
| 401 | defer resp.Body.Close() |
| 402 | |
| 403 | require.Equal(t, http.StatusOK, resp.StatusCode) |
| 404 | require.NotNil(t, mockH.headersReceived, "downstream handler must observe the delegated request") |
| 405 | } |
| 406 | |
| 407 | func TestServeHTTP_StripCoderToken(t *testing.T) { |
| 408 | t.Parallel() |
nothing calls this directly
no test coverage detected