openai_stream_validation_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package handler
  2. import (
  3. "net/http"
  4. "net/http/httptest"
  5. "strings"
  6. "testing"
  7. middleware2 "github.com/Wei-Shaw/sub2api/internal/server/middleware"
  8. "github.com/Wei-Shaw/sub2api/internal/service"
  9. "github.com/gin-gonic/gin"
  10. "github.com/stretchr/testify/require"
  11. "github.com/tidwall/gjson"
  12. )
  13. func TestOpenAICompatibleHandlersRejectInvalidStreamFieldType(t *testing.T) {
  14. gin.SetMode(gin.TestMode)
  15. tests := []struct {
  16. name string
  17. path string
  18. body string
  19. run func(*gin.Context)
  20. }{
  21. {
  22. name: "gateway_responses_string_stream",
  23. path: "/v1/responses",
  24. body: `{"model":"gpt-5","stream":"true","input":"hello"}`,
  25. run: func(c *gin.Context) {
  26. (&GatewayHandler{}).Responses(c)
  27. },
  28. },
  29. {
  30. name: "gateway_responses_number_stream",
  31. path: "/v1/responses",
  32. body: `{"model":"gpt-5","stream":1,"input":"hello"}`,
  33. run: func(c *gin.Context) {
  34. (&GatewayHandler{}).Responses(c)
  35. },
  36. },
  37. {
  38. name: "gateway_chat_completions_string_stream",
  39. path: "/v1/chat/completions",
  40. body: `{"model":"gpt-5","stream":"true","messages":[{"role":"user","content":"hello"}]}`,
  41. run: func(c *gin.Context) {
  42. (&GatewayHandler{}).ChatCompletions(c)
  43. },
  44. },
  45. {
  46. name: "gateway_chat_completions_number_stream",
  47. path: "/v1/chat/completions",
  48. body: `{"model":"gpt-5","stream":1,"messages":[{"role":"user","content":"hello"}]}`,
  49. run: func(c *gin.Context) {
  50. (&GatewayHandler{}).ChatCompletions(c)
  51. },
  52. },
  53. {
  54. name: "openai_chat_completions_string_stream",
  55. path: "/openai/v1/chat/completions",
  56. body: `{"model":"gpt-5","stream":"true","messages":[{"role":"user","content":"hello"}]}`,
  57. run: func(c *gin.Context) {
  58. newOpenAIHandlerForPreviousResponseIDValidation(t, nil).ChatCompletions(c)
  59. },
  60. },
  61. {
  62. name: "openai_chat_completions_number_stream",
  63. path: "/openai/v1/chat/completions",
  64. body: `{"model":"gpt-5","stream":1,"messages":[{"role":"user","content":"hello"}]}`,
  65. run: func(c *gin.Context) {
  66. newOpenAIHandlerForPreviousResponseIDValidation(t, nil).ChatCompletions(c)
  67. },
  68. },
  69. }
  70. for _, tt := range tests {
  71. t.Run(tt.name, func(t *testing.T) {
  72. c, rec := newOpenAICompatibleStreamValidationContext(tt.path, tt.body, false)
  73. tt.run(c)
  74. require.Equal(t, http.StatusBadRequest, rec.Code)
  75. require.Equal(t, invalidStreamFieldTypeMessage, gjson.GetBytes(rec.Body.Bytes(), "error.message").String())
  76. require.Contains(t, rec.Body.String(), "invalid_request_error")
  77. })
  78. }
  79. }
  80. func TestGatewayOpenAICompatibleHandlersAllowBooleanStreamToContinue(t *testing.T) {
  81. gin.SetMode(gin.TestMode)
  82. tests := []struct {
  83. name string
  84. path string
  85. body string
  86. run func(*gin.Context)
  87. }{
  88. {
  89. name: "responses_false",
  90. path: "/v1/responses",
  91. body: `{"model":"gpt-5","stream":false,"input":"hello"}`,
  92. run: func(c *gin.Context) {
  93. (&GatewayHandler{gatewayService: &service.GatewayService{}}).Responses(c)
  94. },
  95. },
  96. {
  97. name: "chat_completions_true",
  98. path: "/v1/chat/completions",
  99. body: `{"model":"gpt-5","stream":true,"messages":[{"role":"user","content":"hello"}]}`,
  100. run: func(c *gin.Context) {
  101. (&GatewayHandler{gatewayService: &service.GatewayService{}}).ChatCompletions(c)
  102. },
  103. },
  104. }
  105. for _, tt := range tests {
  106. t.Run(tt.name, func(t *testing.T) {
  107. c, rec := newOpenAICompatibleStreamValidationContext(tt.path, tt.body, true)
  108. tt.run(c)
  109. require.Equal(t, http.StatusForbidden, rec.Code)
  110. require.Contains(t, rec.Body.String(), "This group is restricted to Claude Code clients")
  111. })
  112. }
  113. }
  114. func newOpenAICompatibleStreamValidationContext(path, body string, claudeCodeOnly bool) (*gin.Context, *httptest.ResponseRecorder) {
  115. rec := httptest.NewRecorder()
  116. c, _ := gin.CreateTestContext(rec)
  117. c.Request = httptest.NewRequest(http.MethodPost, path, strings.NewReader(body))
  118. c.Request.Header.Set("Content-Type", "application/json")
  119. groupID := int64(7)
  120. c.Set(string(middleware2.ContextKeyAPIKey), &service.APIKey{
  121. ID: 11,
  122. GroupID: &groupID,
  123. Group: &service.Group{ID: groupID, ClaudeCodeOnly: claudeCodeOnly},
  124. User: &service.User{ID: 13},
  125. })
  126. c.Set(string(middleware2.ContextKeyUser), middleware2.AuthSubject{UserID: 13, Concurrency: 1})
  127. return c, rec
  128. }