gateway_test.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. package routes
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "net/http/httptest"
  6. "strings"
  7. "testing"
  8. "github.com/Wei-Shaw/sub2api/internal/config"
  9. "github.com/Wei-Shaw/sub2api/internal/handler"
  10. servermiddleware "github.com/Wei-Shaw/sub2api/internal/server/middleware"
  11. "github.com/Wei-Shaw/sub2api/internal/service"
  12. "github.com/gin-gonic/gin"
  13. "github.com/stretchr/testify/require"
  14. )
  15. func newGatewayRoutesTestRouter(platform ...string) *gin.Engine {
  16. return newGatewayRoutesTestRouterWithConfig(&config.Config{
  17. Gateway: config.GatewayConfig{
  18. MaxBodySize: 1024 * 1024,
  19. TextMaxBodySize: 1024 * 1024,
  20. },
  21. }, platform...)
  22. }
  23. func newGatewayRoutesTestRouterWithConfig(cfg *config.Config, platform ...string) *gin.Engine {
  24. gin.SetMode(gin.TestMode)
  25. router := gin.New()
  26. groupPlatform := service.PlatformOpenAI
  27. if len(platform) > 0 && platform[0] != "" {
  28. groupPlatform = platform[0]
  29. }
  30. RegisterGatewayRoutes(
  31. router,
  32. &handler.Handlers{
  33. Gateway: &handler.GatewayHandler{},
  34. OpenAIGateway: &handler.OpenAIGatewayHandler{},
  35. AsyncImage: handler.NewAsyncImageHandler(nil, nil),
  36. },
  37. servermiddleware.APIKeyAuthMiddleware(func(c *gin.Context) {
  38. groupID := int64(1)
  39. c.Set(string(servermiddleware.ContextKeyAPIKey), &service.APIKey{
  40. GroupID: &groupID,
  41. Group: &service.Group{Platform: groupPlatform},
  42. })
  43. c.Next()
  44. }),
  45. nil,
  46. nil,
  47. nil,
  48. nil,
  49. nil,
  50. cfg,
  51. )
  52. return router
  53. }
  54. func TestGatewayRoutesOpenAIResponsesCompactPathIsRegistered(t *testing.T) {
  55. router := newGatewayRoutesTestRouter()
  56. for _, path := range []string{
  57. "/v1/responses/compact",
  58. "/responses/compact",
  59. "/backend-api/codex/responses",
  60. "/backend-api/codex/responses/compact",
  61. } {
  62. req := httptest.NewRequest(http.MethodPost, path, strings.NewReader(`{"model":"gpt-5"}`))
  63. req.Header.Set("Content-Type", "application/json")
  64. w := httptest.NewRecorder()
  65. router.ServeHTTP(w, req)
  66. require.NotEqual(t, http.StatusNotFound, w.Code, "path=%s should hit OpenAI responses handler", path)
  67. }
  68. }
  69. func TestGatewayRoutesOpenAIAlphaSearchPathsAreRegistered(t *testing.T) {
  70. router := newGatewayRoutesTestRouter()
  71. registered := make(map[string]bool)
  72. for _, route := range router.Routes() {
  73. if route.Method == http.MethodPost {
  74. registered[route.Path] = true
  75. }
  76. }
  77. for _, path := range []string{
  78. "/v1/alpha/search",
  79. "/alpha/search",
  80. "/backend-api/codex/alpha/search",
  81. } {
  82. require.True(t, registered[path], "POST %s should be registered", path)
  83. }
  84. }
  85. func TestGatewayRoutesAlphaSearchRejectsUnsupportedGroup(t *testing.T) {
  86. router := newGatewayRoutesTestRouter(service.PlatformGrok)
  87. req := httptest.NewRequest(http.MethodPost, "/v1/alpha/search", strings.NewReader(`{"model":"gpt-5.6-sol"}`))
  88. req.Header.Set("Content-Type", "application/json")
  89. w := httptest.NewRecorder()
  90. router.ServeHTTP(w, req)
  91. require.Equal(t, http.StatusNotFound, w.Code)
  92. require.Contains(t, w.Body.String(), "only available for OpenAI and Composite groups")
  93. }
  94. func TestGatewayRoutesOpenAIImagesPathsAreRegistered(t *testing.T) {
  95. router := newGatewayRoutesTestRouter()
  96. for _, path := range []string{
  97. "/v1/images/generations",
  98. "/v1/images/edits",
  99. "/images/generations",
  100. "/images/edits",
  101. } {
  102. req := httptest.NewRequest(http.MethodPost, path, strings.NewReader(`{"model":"gpt-image-2","prompt":"draw a cat"}`))
  103. req.Header.Set("Content-Type", "application/json")
  104. w := httptest.NewRecorder()
  105. router.ServeHTTP(w, req)
  106. require.NotEqual(t, http.StatusNotFound, w.Code, "path=%s should hit OpenAI images handler", path)
  107. }
  108. }
  109. func TestGatewayRoutesAsyncImagesPathsAreRegistered(t *testing.T) {
  110. router := newGatewayRoutesTestRouter()
  111. registered := make(map[string]bool)
  112. for _, route := range router.Routes() {
  113. registered[route.Method+" "+route.Path] = true
  114. }
  115. for _, route := range []string{
  116. "POST /v1/images/generations/async",
  117. "POST /v1/images/edits/async",
  118. "GET /v1/images/tasks/:task_id",
  119. "POST /images/generations/async",
  120. "POST /images/edits/async",
  121. "GET /images/tasks/:task_id",
  122. } {
  123. require.True(t, registered[route], "%s should be registered", route)
  124. }
  125. }
  126. func TestGatewayRoutesGrokImagesAndVideosPathsAreRegistered(t *testing.T) {
  127. router := newGatewayRoutesTestRouter(service.PlatformGrok)
  128. for _, path := range []string{
  129. "/v1/images/generations",
  130. "/v1/images/edits",
  131. "/images/generations",
  132. "/images/edits",
  133. "/v1/videos/generations",
  134. "/v1/videos",
  135. "/videos",
  136. "/videos/generations",
  137. "/v1/videos/edits",
  138. "/videos/edits",
  139. "/v1/videos/extensions",
  140. "/videos/extensions",
  141. } {
  142. req := httptest.NewRequest(http.MethodPost, path, strings.NewReader(`{"model":"grok-imagine","prompt":"draw a cat"}`))
  143. req.Header.Set("Content-Type", "application/json")
  144. w := httptest.NewRecorder()
  145. router.ServeHTTP(w, req)
  146. require.NotEqual(t, http.StatusNotFound, w.Code, "path=%s should hit Grok media handler", path)
  147. require.NotContains(t, w.Body.String(), "not supported for this platform")
  148. }
  149. for _, path := range []string{
  150. "/v1/videos/request-123",
  151. "/videos/request-123",
  152. "/v1/videos/generations/request-123",
  153. "/videos/generations/request-123",
  154. "/v1/videos/edits/request-123",
  155. "/videos/edits/request-123",
  156. "/v1/videos/extensions/request-123",
  157. "/videos/extensions/request-123",
  158. "/v1/videos/request-123/content",
  159. "/videos/request-123/content",
  160. "/v1/videos/generations/request-123/content",
  161. "/videos/generations/request-123/content",
  162. "/v1/videos/edits/request-123/content",
  163. "/videos/edits/request-123/content",
  164. "/v1/videos/extensions/request-123/content",
  165. "/videos/extensions/request-123/content",
  166. } {
  167. req := httptest.NewRequest(http.MethodGet, path, nil)
  168. w := httptest.NewRecorder()
  169. router.ServeHTTP(w, req)
  170. require.NotEqual(t, http.StatusNotFound, w.Code, "path=%s should hit Grok video handler", path)
  171. require.NotContains(t, w.Body.String(), "not supported for this platform")
  172. }
  173. }
  174. func TestGatewayRoutesGrokCustomVoiceCRUDPathsAreRegistered(t *testing.T) {
  175. router := newGatewayRoutesTestRouter(service.PlatformGrok)
  176. registered := make(map[string]bool)
  177. for _, route := range router.Routes() {
  178. registered[route.Method+" "+route.Path] = true
  179. }
  180. for _, route := range []string{
  181. "POST /v1/custom-voices",
  182. "GET /v1/custom-voices",
  183. "GET /v1/custom-voices/:voice_id",
  184. "PATCH /v1/custom-voices/:voice_id",
  185. "DELETE /v1/custom-voices/:voice_id",
  186. "GET /v1/custom-voices/:voice_id/audio",
  187. "POST /custom-voices",
  188. "GET /custom-voices",
  189. "GET /custom-voices/:voice_id",
  190. "PATCH /custom-voices/:voice_id",
  191. "DELETE /custom-voices/:voice_id",
  192. "GET /custom-voices/:voice_id/audio",
  193. } {
  194. require.True(t, registered[route], "%s should be registered", route)
  195. }
  196. }
  197. func TestGrokCustomVoiceEndpointUsesRouteTemplateNotRawPath(t *testing.T) {
  198. gin.SetMode(gin.TestMode)
  199. router := gin.New()
  200. var got string
  201. capture := func(c *gin.Context) {
  202. got = grokCustomVoiceEndpoint(c)
  203. c.Status(http.StatusOK)
  204. }
  205. router.GET("/v1/custom-voices/:voice_id/audio", capture)
  206. router.GET("/v1/custom-voices/:voice_id", capture)
  207. for _, tc := range []struct {
  208. path string
  209. want string
  210. }{
  211. {path: "/v1/custom-voices/voice-123", want: "custom-voices/voice-123"},
  212. {path: "/v1/custom-voices/voice-123/audio", want: "custom-voices/voice-123/audio"},
  213. // A voice literally named "audio" matches /:voice_id, not /:voice_id/audio.
  214. // A raw-path suffix check would turn this profile lookup into an audio download.
  215. {path: "/v1/custom-voices/audio", want: "custom-voices/audio"},
  216. {path: "/v1/custom-voices/audio/audio", want: "custom-voices/audio/audio"},
  217. } {
  218. got = ""
  219. req := httptest.NewRequest(http.MethodGet, tc.path, nil)
  220. w := httptest.NewRecorder()
  221. router.ServeHTTP(w, req)
  222. require.Equal(t, http.StatusOK, w.Code, "path=%s", tc.path)
  223. require.Equal(t, tc.want, got, "path=%s", tc.path)
  224. }
  225. }
  226. func TestGatewayRoutesCompositeVideoLookupsUseGrokHandler(t *testing.T) {
  227. router := newGatewayRoutesTestRouter(service.PlatformComposite)
  228. for _, path := range []string{
  229. "/v1/videos/request-123",
  230. "/videos/request-123",
  231. "/v1/videos/request-123/content",
  232. "/videos/request-123/content",
  233. } {
  234. req := httptest.NewRequest(http.MethodGet, path, nil)
  235. w := httptest.NewRecorder()
  236. router.ServeHTTP(w, req)
  237. require.NotEqual(t, http.StatusNotFound, w.Code, "path=%s should hit Grok video lookup handler", path)
  238. require.NotContains(t, w.Body.String(), "not supported for this platform")
  239. }
  240. }
  241. func TestGatewayRoutesCompositeMessagesWithGrokModelUsesOpenAIGateway(t *testing.T) {
  242. router := newGatewayRoutesTestRouter(service.PlatformComposite)
  243. req := httptest.NewRequest(http.MethodPost, "/v1/messages", strings.NewReader(`{"model":"grok-4.3","messages":[{"role":"user","content":"hi"}]}`))
  244. req.Header.Set("Content-Type", "application/json")
  245. w := httptest.NewRecorder()
  246. router.ServeHTTP(w, req)
  247. require.NotEqual(t, http.StatusNotFound, w.Code)
  248. require.NotContains(t, w.Body.String(), "not supported")
  249. require.NotContains(t, w.Body.String(), "OpenAI-compatible endpoint")
  250. require.NotContains(t, w.Body.String(), "composite groups")
  251. }
  252. func TestGatewayRoutesCompositeChatCompletionsWithGrokModelUsesOpenAIGateway(t *testing.T) {
  253. router := newGatewayRoutesTestRouter(service.PlatformComposite)
  254. for _, path := range []string{"/v1/chat/completions", "/chat/completions"} {
  255. req := httptest.NewRequest(http.MethodPost, path, strings.NewReader(`{"model":"grok-4.3","messages":[{"role":"user","content":"hi"}]}`))
  256. req.Header.Set("Content-Type", "application/json")
  257. w := httptest.NewRecorder()
  258. router.ServeHTTP(w, req)
  259. require.NotEqual(t, http.StatusNotFound, w.Code, "path=%s", path)
  260. require.NotContains(t, w.Body.String(), "not supported")
  261. require.NotContains(t, w.Body.String(), "OpenAI-compatible endpoint")
  262. require.NotContains(t, w.Body.String(), "composite groups")
  263. }
  264. }
  265. func TestGatewayRoutesNonGrokVideosAreRejectedAtPlatformGate(t *testing.T) {
  266. router := newGatewayRoutesTestRouter(service.PlatformOpenAI)
  267. for _, tc := range []struct {
  268. method string
  269. path string
  270. body string
  271. }{
  272. {http.MethodPost, "/v1/videos/generations", `{"model":"grok-imagine-video-1.5","prompt":"waves"}`},
  273. {http.MethodPost, "/v1/videos", `{"model":"grok-imagine-video-1.5","prompt":"waves"}`},
  274. {http.MethodPost, "/videos", `{"model":"grok-imagine-video-1.5","prompt":"waves"}`},
  275. {http.MethodPost, "/videos/generations", `{"model":"grok-imagine-video-1.5","prompt":"waves"}`},
  276. {http.MethodPost, "/v1/videos/edits", `{"model":"grok-imagine-video","prompt":"waves","video":{"url":"https://example.com/in.mp4"}}`},
  277. {http.MethodPost, "/videos/edits", `{"model":"grok-imagine-video","prompt":"waves","video":{"url":"https://example.com/in.mp4"}}`},
  278. {http.MethodPost, "/v1/videos/extensions", `{"model":"grok-imagine-video","prompt":"waves","video":{"url":"https://example.com/in.mp4"}}`},
  279. {http.MethodPost, "/videos/extensions", `{"model":"grok-imagine-video","prompt":"waves","video":{"url":"https://example.com/in.mp4"}}`},
  280. {http.MethodGet, "/v1/videos/request-123", ""},
  281. {http.MethodGet, "/videos/request-123", ""},
  282. {http.MethodGet, "/v1/videos/generations/request-123", ""},
  283. {http.MethodGet, "/videos/generations/request-123", ""},
  284. {http.MethodGet, "/v1/videos/edits/request-123", ""},
  285. {http.MethodGet, "/videos/edits/request-123", ""},
  286. {http.MethodGet, "/v1/videos/extensions/request-123", ""},
  287. {http.MethodGet, "/videos/extensions/request-123", ""},
  288. {http.MethodGet, "/v1/videos/request-123/content", ""},
  289. {http.MethodGet, "/videos/request-123/content", ""},
  290. {http.MethodGet, "/v1/videos/generations/request-123/content", ""},
  291. {http.MethodGet, "/videos/generations/request-123/content", ""},
  292. {http.MethodGet, "/v1/videos/edits/request-123/content", ""},
  293. {http.MethodGet, "/videos/edits/request-123/content", ""},
  294. {http.MethodGet, "/v1/videos/extensions/request-123/content", ""},
  295. {http.MethodGet, "/videos/extensions/request-123/content", ""},
  296. } {
  297. req := httptest.NewRequest(tc.method, tc.path, strings.NewReader(tc.body))
  298. req.Header.Set("Content-Type", "application/json")
  299. w := httptest.NewRecorder()
  300. router.ServeHTTP(w, req)
  301. require.Equal(t, http.StatusNotFound, w.Code, "method=%s path=%s", tc.method, tc.path)
  302. require.Contains(t, w.Body.String(), "Videos API is not supported for this platform")
  303. }
  304. }
  305. func TestGatewayRoutesCompositeVideoGenerationAllowed(t *testing.T) {
  306. router := newGatewayRoutesTestRouter(service.PlatformComposite)
  307. req := httptest.NewRequest(http.MethodPost, "/v1/videos/generations", strings.NewReader(`{"model":"grok-imagine-video-1.5","prompt":"waves"}`))
  308. req.Header.Set("Content-Type", "application/json")
  309. w := httptest.NewRecorder()
  310. router.ServeHTTP(w, req)
  311. require.NotEqual(t, http.StatusNotFound, w.Code)
  312. require.NotContains(t, w.Body.String(), "not supported")
  313. }
  314. func TestGatewayRoutesCompositeOpenAIOnlyEndpointsRequireOpenAITarget(t *testing.T) {
  315. router := newGatewayRoutesTestRouter(service.PlatformComposite)
  316. req := httptest.NewRequest(http.MethodPost, "/v1/embeddings", strings.NewReader(`{"model":"gemini-2.5-pro","input":"hello"}`))
  317. req.Header.Set("Content-Type", "application/json")
  318. w := httptest.NewRecorder()
  319. router.ServeHTTP(w, req)
  320. require.Equal(t, http.StatusNotFound, w.Code)
  321. req = httptest.NewRequest(http.MethodPost, "/v1/embeddings", strings.NewReader(`{"model":"text-embedding-3-small","input":"hello"}`))
  322. req.Header.Set("Content-Type", "application/json")
  323. w = httptest.NewRecorder()
  324. router.ServeHTTP(w, req)
  325. require.NotEqual(t, http.StatusNotFound, w.Code)
  326. }
  327. func TestGatewayRoutesGrokAllowsCLICompatibilityEntrypoints(t *testing.T) {
  328. router := newGatewayRoutesTestRouter(service.PlatformGrok)
  329. for _, tc := range []struct {
  330. method string
  331. path string
  332. }{
  333. {http.MethodPost, "/v1/messages"},
  334. {http.MethodPost, "/v1/chat/completions"},
  335. {http.MethodPost, "/chat/completions"},
  336. {http.MethodGet, "/v1/responses"},
  337. {http.MethodGet, "/responses"},
  338. {http.MethodGet, "/backend-api/codex/responses"},
  339. } {
  340. req := httptest.NewRequest(tc.method, tc.path, strings.NewReader(`{"model":"grok"}`))
  341. req.Header.Set("Content-Type", "application/json")
  342. w := httptest.NewRecorder()
  343. router.ServeHTTP(w, req)
  344. require.NotEqual(t, http.StatusNotFound, w.Code, "method=%s path=%s", tc.method, tc.path)
  345. require.NotContains(t, w.Body.String(), "not supported for Grok groups")
  346. }
  347. countTokensRouter := newGatewayRoutesTestRouterWithConfig(&config.Config{
  348. Gateway: config.GatewayConfig{MaxBodySize: 1024 * 1024},
  349. }, service.PlatformGrok)
  350. for _, path := range []string{"/v1/messages/count_tokens", "/messages/count_tokens"} {
  351. req := httptest.NewRequest(http.MethodPost, path, strings.NewReader(`{"model":"grok","messages":[{"role":"user","content":"hi"}]}`))
  352. req.Header.Set("Content-Type", "application/json")
  353. w := httptest.NewRecorder()
  354. countTokensRouter.ServeHTTP(w, req)
  355. require.Equal(t, http.StatusOK, w.Code, "path=%s", path)
  356. var response struct {
  357. InputTokens int `json:"input_tokens"`
  358. }
  359. require.NoError(t, json.Unmarshal(w.Body.Bytes(), &response), "path=%s", path)
  360. require.Positive(t, response.InputTokens, "path=%s", path)
  361. }
  362. for _, path := range []string{
  363. "/v1/responses",
  364. "/responses",
  365. "/backend-api/codex/responses",
  366. } {
  367. req := httptest.NewRequest(http.MethodPost, path, strings.NewReader(`{"model":"grok","input":"hi"}`))
  368. req.Header.Set("Content-Type", "application/json")
  369. w := httptest.NewRecorder()
  370. router.ServeHTTP(w, req)
  371. require.NotEqual(t, http.StatusNotFound, w.Code, "path=%s should still reach Responses handler", path)
  372. }
  373. }
  374. // TestGatewayRoutesResponsesSubpathRejectsNonConformingSubpaths 端到端锁定不变式:
  375. // /responses/*subpath 的子路径会被转发到上游同名端点之后,因此不合规的子路径必须
  376. // 在入口就被拒绝,不得进入调度与转发流程。
  377. func TestGatewayRoutesResponsesSubpathRejectsNonConformingSubpaths(t *testing.T) {
  378. router := newGatewayRoutesTestRouter()
  379. for _, path := range []string{
  380. "/v1/responses/../../x/y",
  381. "/v1/responses/..%2f..%2fx/y",
  382. "/v1/responses/%2e%2e/%2e%2e/x",
  383. "/responses/%2e%2e%2fx",
  384. "/backend-api/codex/responses/..%2f..%2fx",
  385. `/v1/responses/..\..\x`,
  386. "/v1/responses/%3fa=b",
  387. "/v1/responses/x%23frag",
  388. "/v1/responses/compact%2f..",
  389. } {
  390. req := httptest.NewRequest(http.MethodPost, path, strings.NewReader(`{"model":"gpt-5"}`))
  391. req.Header.Set("Content-Type", "application/json")
  392. w := httptest.NewRecorder()
  393. router.ServeHTTP(w, req)
  394. require.Equal(t, http.StatusNotFound, w.Code, "path=%s must be rejected at the edge", path)
  395. require.Contains(t, w.Body.String(), "Unsupported responses subpath", "path=%s", path)
  396. }
  397. }
  398. func TestGatewayRoutesOpenAICountTokensPathIsRegistered(t *testing.T) {
  399. router := newGatewayRoutesTestRouter(service.PlatformOpenAI)
  400. req := httptest.NewRequest(http.MethodPost, "/v1/messages/count_tokens", strings.NewReader(`{"model":"claude-sonnet-4-5","messages":[{"role":"user","content":"hi"}]}`))
  401. req.Header.Set("Content-Type", "application/json")
  402. w := httptest.NewRecorder()
  403. router.ServeHTTP(w, req)
  404. require.NotEqual(t, http.StatusNotFound, w.Code)
  405. }