|
1 | 1 | package plugin |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "net/http" |
| 5 | + "net/http/httptest" |
4 | 6 | "testing" |
5 | 7 |
|
| 8 | + "github.com/gin-gonic/gin" |
| 9 | + |
| 10 | + "github.com/DouDOU-start/airgate-core/ent" |
6 | 11 | "github.com/DouDOU-start/airgate-core/internal/auth" |
7 | 12 | "github.com/DouDOU-start/airgate-core/internal/routing" |
8 | 13 | ) |
@@ -41,6 +46,72 @@ func TestParseBody_StreamTrue(t *testing.T) { |
41 | 46 | } |
42 | 47 | } |
43 | 48 |
|
| 49 | +func TestBuildPluginRequestUsesWriterForStreamRequest(t *testing.T) { |
| 50 | + t.Parallel() |
| 51 | + |
| 52 | + recorder := httptest.NewRecorder() |
| 53 | + c, _ := gin.CreateTestContext(recorder) |
| 54 | + c.Request = httptest.NewRequest(http.MethodPost, "/v1/images/generations", nil) |
| 55 | + state := &forwardState{ |
| 56 | + requestPath: "/v1/images/generations", |
| 57 | + stream: true, |
| 58 | + realtime: true, |
| 59 | + keyInfo: &auth.APIKeyInfo{}, |
| 60 | + account: &ent.Account{}, |
| 61 | + } |
| 62 | + |
| 63 | + req := buildPluginRequest(c, state) |
| 64 | + if !req.Stream { |
| 65 | + t.Fatalf("Stream = false, want true") |
| 66 | + } |
| 67 | + if req.Writer == nil { |
| 68 | + t.Fatalf("Writer = nil, want stream writer") |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +func TestBuildPluginRequestOmitsWriterForPlainNonStreamRequest(t *testing.T) { |
| 73 | + t.Parallel() |
| 74 | + |
| 75 | + recorder := httptest.NewRecorder() |
| 76 | + c, _ := gin.CreateTestContext(recorder) |
| 77 | + c.Request = httptest.NewRequest(http.MethodPost, "/v1/chat/completions", nil) |
| 78 | + state := &forwardState{ |
| 79 | + requestPath: "/v1/chat/completions", |
| 80 | + stream: false, |
| 81 | + realtime: false, |
| 82 | + keyInfo: &auth.APIKeyInfo{}, |
| 83 | + account: &ent.Account{}, |
| 84 | + } |
| 85 | + |
| 86 | + req := buildPluginRequest(c, state) |
| 87 | + if req.Writer != nil { |
| 88 | + t.Fatalf("Writer = %T, want nil", req.Writer) |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +func TestBuildPluginRequestOmitsWriterForNonStreamImagesRequest(t *testing.T) { |
| 93 | + t.Parallel() |
| 94 | + |
| 95 | + recorder := httptest.NewRecorder() |
| 96 | + c, _ := gin.CreateTestContext(recorder) |
| 97 | + c.Request = httptest.NewRequest(http.MethodPost, "/v1/images/generations", nil) |
| 98 | + state := &forwardState{ |
| 99 | + requestPath: "/v1/images/generations", |
| 100 | + stream: false, |
| 101 | + realtime: false, |
| 102 | + keyInfo: &auth.APIKeyInfo{}, |
| 103 | + account: &ent.Account{}, |
| 104 | + } |
| 105 | + |
| 106 | + req := buildPluginRequest(c, state) |
| 107 | + if req.Stream { |
| 108 | + t.Fatalf("Stream = true, want false") |
| 109 | + } |
| 110 | + if req.Writer != nil { |
| 111 | + t.Fatalf("Writer = %T, want nil", req.Writer) |
| 112 | + } |
| 113 | +} |
| 114 | + |
44 | 115 | func TestRoutesForAPIKeyUsesBoundGroupOnly(t *testing.T) { |
45 | 116 | t.Parallel() |
46 | 117 |
|
|
0 commit comments