MCPcopy Index your code
hub / github.com/coder/coder / TestServeHTTP_DelegatedAPIKey_Integration

Function TestServeHTTP_DelegatedAPIKey_Integration

coderd/aibridged/aibridged_test.go:369–405  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

367// authoritative caller identity by the handler, without any HTTP-layer header
368// extraction.
369func 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
407func TestServeHTTP_StripCoderToken(t *testing.T) {
408 t.Parallel()

Callers

nothing calls this directly

Calls 14

TransportForMethod · 0.95
NewTransportFactoryFunction · 0.92
ContextFunction · 0.92
GetKeyIdMethod · 0.80
newTestServerFunction · 0.70
DRPCConnMethod · 0.65
IsAuthorizedMethod · 0.65
AcquireMethod · 0.65
CloseMethod · 0.65
EXPECTMethod · 0.45
EqualMethod · 0.45
EmptyMethod · 0.45

Tested by

no test coverage detected