TestBuildProviderRouter covers the host-and-routing derivation from the classified provider reload.
(t *testing.T)
| 88 | // TestBuildProviderRouter covers the host-and-routing derivation from |
| 89 | // the classified provider reload. |
| 90 | func TestBuildProviderRouter(t *testing.T) { |
| 91 | t.Parallel() |
| 92 | |
| 93 | t.Run("IncludesEnabledOnly", func(t *testing.T) { |
| 94 | t.Parallel() |
| 95 | |
| 96 | reload := ProviderReload{Providers: []ReloadedProvider{ |
| 97 | enabledProvider("openai", "api.openai.com"), |
| 98 | enabledProvider("anthropic", "api.anthropic.com"), |
| 99 | enabledProvider("custom", "custom-llm.example.com"), |
| 100 | // Host is populated on the non-enabled rows so the Status |
| 101 | // guard, not the empty-host guard, is what excludes them. |
| 102 | {ProviderOutcome: aibridged.ProviderOutcome{Name: "off", Type: "openai", Status: aibridged.ProviderStatusDisabled}, Host: "disabled.example.com"}, |
| 103 | {ProviderOutcome: aibridged.ProviderOutcome{Name: "bad", Type: "openai", Status: aibridged.ProviderStatusError, Err: xerrors.New("nope")}, Host: "errored.example.com"}, |
| 104 | }} |
| 105 | |
| 106 | router, err := buildProviderRouter(reload, []string{"443"}) |
| 107 | require.NoError(t, err) |
| 108 | |
| 109 | assert.Equal(t, "openai", router.providerFromHost("api.openai.com")) |
| 110 | assert.Equal(t, "anthropic", router.providerFromHost("api.anthropic.com")) |
| 111 | assert.Equal(t, "custom", router.providerFromHost("custom-llm.example.com")) |
| 112 | assert.Empty(t, router.providerFromHost("unknown.com")) |
| 113 | assert.Empty(t, router.providerFromHost("disabled.example.com"), |
| 114 | "disabled provider must not be routable even with a populated Host") |
| 115 | assert.Empty(t, router.providerFromHost("errored.example.com"), |
| 116 | "errored provider must not be routable even with a populated Host") |
| 117 | |
| 118 | assert.Contains(t, router.mitmHosts, "api.openai.com:443") |
| 119 | assert.Contains(t, router.mitmHosts, "api.anthropic.com:443") |
| 120 | assert.Len(t, router.mitmHosts, 3) |
| 121 | }) |
| 122 | |
| 123 | t.Run("CaseInsensitive", func(t *testing.T) { |
| 124 | t.Parallel() |
| 125 | |
| 126 | reload := ProviderReload{Providers: []ReloadedProvider{ |
| 127 | {ProviderOutcome: aibridged.ProviderOutcome{Name: "provider", Type: "openai", Status: aibridged.ProviderStatusEnabled}, Host: "API.Example.COM"}, |
| 128 | }} |
| 129 | |
| 130 | router, err := buildProviderRouter(reload, []string{"443"}) |
| 131 | require.NoError(t, err) |
| 132 | |
| 133 | assert.Equal(t, "provider", router.providerFromHost("API.Example.COM")) |
| 134 | assert.Equal(t, "provider", router.providerFromHost("api.example.com")) |
| 135 | }) |
| 136 | |
| 137 | t.Run("DefensiveDeduplicatesSameHost", func(t *testing.T) { |
| 138 | t.Parallel() |
| 139 | |
| 140 | // Refresh function should mark the duplicate as ProviderStatusError; |
| 141 | // buildProviderRouter is defensive and tolerates an enabled duplicate |
| 142 | // by giving the first entry the host (first wins). |
| 143 | reload := ProviderReload{Providers: []ReloadedProvider{ |
| 144 | enabledProvider("first", "api.example.com"), |
| 145 | enabledProvider("second", "api.example.com"), |
| 146 | }} |
| 147 |
nothing calls this directly
no test coverage detected