(t *testing.T)
| 20 | ) |
| 21 | |
| 22 | func TestTransportFactory_TransportFor(t *testing.T) { |
| 23 | t.Parallel() |
| 24 | |
| 25 | t.Run("ReturnsTransport", func(t *testing.T) { |
| 26 | t.Parallel() |
| 27 | f := aibridged.NewTransportFactory(http.NotFoundHandler()) |
| 28 | rt, err := f.TransportFor("openai", aibridge.SourceAgents) |
| 29 | require.NoError(t, err) |
| 30 | require.NotNil(t, rt) |
| 31 | }) |
| 32 | |
| 33 | t.Run("NilHandlerErrors", func(t *testing.T) { |
| 34 | t.Parallel() |
| 35 | f := aibridged.NewTransportFactory(nil) |
| 36 | _, err := f.TransportFor("openai", aibridge.SourceAgents) |
| 37 | require.Error(t, err) |
| 38 | }) |
| 39 | |
| 40 | t.Run("EmptyProviderErrors", func(t *testing.T) { |
| 41 | t.Parallel() |
| 42 | f := aibridged.NewTransportFactory(http.NotFoundHandler()) |
| 43 | _, err := f.TransportFor("", aibridge.SourceAgents) |
| 44 | require.Error(t, err) |
| 45 | }) |
| 46 | |
| 47 | t.Run("RewritesURLToAibridgeMount", func(t *testing.T) { |
| 48 | t.Parallel() |
| 49 | |
| 50 | // The round-tripper must adapt an upstream-shaped URL.Path |
| 51 | // ("/v1/messages") to the aibridge mount layout |
| 52 | // ("/api/v2/aibridge/<provider>/v1/messages") so callers don't |
| 53 | // have to encode the daemon's routing key into their requests. |
| 54 | got := make(chan string, 1) |
| 55 | handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 56 | got <- r.URL.Path |
| 57 | w.WriteHeader(http.StatusOK) |
| 58 | }) |
| 59 | |
| 60 | rt, err := aibridged.NewTransportFactory(handler).TransportFor("my-anthropic", aibridge.SourceAgents) |
| 61 | require.NoError(t, err) |
| 62 | |
| 63 | ctx := aibridge.WithDelegatedAPIKeyID(testutil.Context(t, testutil.WaitShort), "test-key-id") |
| 64 | req, err := http.NewRequestWithContext(ctx, http.MethodPost, "http://upstream/v1/messages", nil) |
| 65 | require.NoError(t, err) |
| 66 | |
| 67 | // The caller's req.URL.Path is the upstream shape. Capture it so |
| 68 | // we can prove the transport mutates a clone, not the caller's |
| 69 | // request, after RoundTrip returns. |
| 70 | origPath := req.URL.Path |
| 71 | |
| 72 | resp, err := rt.RoundTrip(req) |
| 73 | require.NoError(t, err) |
| 74 | defer resp.Body.Close() |
| 75 | |
| 76 | require.Equal(t, "/api/v2/aibridge/my-anthropic/v1/messages", <-got) |
| 77 | require.Equal(t, origPath, req.URL.Path, |
| 78 | "caller's request URL must not be mutated by RoundTrip") |
| 79 | }) |
nothing calls this directly
no test coverage detected