(t *testing.T)
| 1821 | } |
| 1822 | |
| 1823 | func TestAIBridgeRouting(t *testing.T) { |
| 1824 | t.Parallel() |
| 1825 | |
| 1826 | dv := coderdtest.DeploymentValues(t) |
| 1827 | dv.AI.BridgeConfig.Enabled = serpent.Bool(true) |
| 1828 | client, closer, api, _ := coderdenttest.NewWithAPI(t, &coderdenttest.Options{ |
| 1829 | Options: &coderdtest.Options{ |
| 1830 | DeploymentValues: dv, |
| 1831 | }, |
| 1832 | LicenseOptions: &coderdenttest.LicenseOptions{ |
| 1833 | Features: license.Features{ |
| 1834 | codersdk.FeatureAIBridge: 1, |
| 1835 | }, |
| 1836 | }, |
| 1837 | }) |
| 1838 | t.Cleanup(func() { |
| 1839 | _ = closer.Close() |
| 1840 | }) |
| 1841 | |
| 1842 | // Register a simple test handler that echoes back the request path. |
| 1843 | testHandler := http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { |
| 1844 | rw.WriteHeader(http.StatusOK) |
| 1845 | _, _ = rw.Write([]byte(r.URL.Path)) |
| 1846 | }) |
| 1847 | api.AGPL.RegisterInMemoryAIBridgedHTTPHandler(testHandler) |
| 1848 | |
| 1849 | cases := []struct { |
| 1850 | name string |
| 1851 | path string |
| 1852 | expectedPath string |
| 1853 | }{ |
| 1854 | { |
| 1855 | name: "StablePrefix", |
| 1856 | path: "/api/v2/aibridge/openai/v1/chat/completions", |
| 1857 | expectedPath: "/openai/v1/chat/completions", |
| 1858 | }, |
| 1859 | } |
| 1860 | |
| 1861 | for _, tc := range cases { |
| 1862 | t.Run(tc.name, func(t *testing.T) { |
| 1863 | t.Parallel() |
| 1864 | |
| 1865 | ctx := testutil.Context(t, testutil.WaitLong) |
| 1866 | req, err := http.NewRequestWithContext(ctx, http.MethodPost, client.URL.String()+tc.path, nil) |
| 1867 | require.NoError(t, err) |
| 1868 | req.Header.Set(codersdk.SessionTokenHeader, client.SessionToken()) |
| 1869 | |
| 1870 | httpClient := &http.Client{} |
| 1871 | resp, err := httpClient.Do(req) |
| 1872 | require.NoError(t, err) |
| 1873 | defer resp.Body.Close() |
| 1874 | require.Equal(t, http.StatusOK, resp.StatusCode) |
| 1875 | |
| 1876 | // Verify that the prefix was stripped correctly and the path was forwarded. |
| 1877 | body, err := io.ReadAll(resp.Body) |
| 1878 | require.NoError(t, err) |
| 1879 | require.Equal(t, tc.expectedPath, string(body)) |
| 1880 | }) |
nothing calls this directly
no test coverage detected