forked from privapps/github-copilot-svcs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_test.go
More file actions
488 lines (427 loc) · 12.3 KB
/
Copy pathapi_test.go
File metadata and controls
488 lines (427 loc) · 12.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
package integration_test
import (
"encoding/json"
"fmt"
"io"
"net"
"net/http"
"os"
"strings"
"testing"
"time"
"github.com/privapps/github-copilot-svcs/internal"
)
var (
testServer *internal.Server
baseURL string
cleanup func()
)
// TestMain sets up and tears down the test server for all integration tests
func TestMain(m *testing.M) {
// Set up test server
var err error
testServer, baseURL, cleanup, err = setupTestServer()
if err != nil {
fmt.Printf("Failed to setup test server: %v\n", err)
os.Exit(1)
}
// Wait for server to be ready
if !waitForServer(baseURL, 15*time.Second) {
cleanup()
fmt.Println("Server failed to start within timeout")
os.Exit(1)
}
fmt.Printf("Test server ready at %s\n", baseURL)
// Run tests
code := m.Run()
// Cleanup
cleanup()
os.Exit(code)
}
func TestHealthEndpoint(t *testing.T) {
tests := []struct {
name string
endpoint string
expectedStatus int
expectedFields []string
}{
{
name: "basic health check",
endpoint: "/health",
expectedStatus: http.StatusOK,
expectedFields: []string{"status", "timestamp", "version"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
resp, err := http.Get(baseURL + tt.endpoint)
if err != nil {
t.Fatalf("Failed to make request: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != tt.expectedStatus {
t.Errorf("Expected status %d, got %d", tt.expectedStatus, resp.StatusCode)
}
// Check response body is valid JSON with expected fields
var result map[string]interface{}
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
t.Errorf("Failed to decode JSON response: %v", err)
return
}
for _, field := range tt.expectedFields {
if _, exists := result[field]; !exists {
t.Errorf("Expected field '%s' not found in response", field)
}
}
// Verify status is "healthy"
if status, ok := result["status"].(string); !ok || status != "healthy" {
t.Errorf("Expected status 'healthy', got %v", result["status"])
}
})
}
}
func TestModelsEndpoint(t *testing.T) {
tests := []struct {
name string
method string
endpoint string
expectedStatus int
checkJSON bool
expectedFields []string
}{
{
name: "get models list",
method: "GET",
endpoint: "/v1/models",
expectedStatus: http.StatusOK,
checkJSON: true,
expectedFields: []string{"object", "data"},
},
{
name: "models endpoint with POST method",
method: "POST",
endpoint: "/v1/models",
expectedStatus: http.StatusOK, // Models endpoint accepts POST
checkJSON: true,
expectedFields: []string{"object", "data"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
req, err := http.NewRequest(tt.method, baseURL+tt.endpoint, http.NoBody)
if err != nil {
t.Fatalf("Failed to create request: %v", err)
}
client := &http.Client{Timeout: 10 * time.Second}
resp, err := client.Do(req)
if err != nil {
t.Fatalf("Failed to make request: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != tt.expectedStatus {
body, _ := io.ReadAll(resp.Body)
t.Errorf("Expected status %d, got %d. Response: %s", tt.expectedStatus, resp.StatusCode, string(body))
}
if tt.checkJSON {
var result map[string]interface{}
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
t.Errorf("Failed to decode JSON response: %v", err)
return
}
for _, field := range tt.expectedFields {
if _, exists := result[field]; !exists {
t.Errorf("Expected field '%s' not found in response", field)
}
}
// Check that data is an array
if data, ok := result["data"].([]interface{}); !ok {
t.Errorf("Expected 'data' to be an array, got %T", result["data"])
} else if len(data) == 0 {
t.Log("Note: Models list is empty - this may be expected in test environment")
}
}
})
}
}
func TestChatCompletionsEndpoint(t *testing.T) {
tests := []struct {
name string
method string
endpoint string
body string
expectedStatus int
contentType string
}{
{
name: "chat completions with empty body",
method: "POST",
endpoint: "/v1/chat/completions",
body: "",
expectedStatus: http.StatusBadRequest,
contentType: "application/json",
},
{
name: "chat completions with invalid JSON",
method: "POST",
endpoint: "/v1/chat/completions",
body: `{"invalid": json}`,
expectedStatus: http.StatusBadRequest,
contentType: "application/json",
},
{
name: "chat completions with wrong method",
method: "GET",
endpoint: "/v1/chat/completions",
body: "",
expectedStatus: http.StatusMethodNotAllowed,
contentType: "application/json",
},
{
name: "chat completions with basic valid request",
method: "POST",
endpoint: "/v1/chat/completions",
body: `{"model":"gpt-4","messages":[{"role":"user","content":"test"}]}`,
expectedStatus: http.StatusUnauthorized, // Should be 401 if auth is missing
contentType: "application/json",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var body io.Reader
if tt.body != "" {
body = strings.NewReader(tt.body)
}
req, err := http.NewRequest(tt.method, baseURL+tt.endpoint, body)
if err != nil {
t.Fatalf("Failed to create request: %v", err)
}
if tt.contentType != "" {
req.Header.Set("Content-Type", tt.contentType)
}
client := &http.Client{Timeout: 10 * time.Second}
resp, err := client.Do(req)
if err != nil {
t.Fatalf("Failed to make request: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != tt.expectedStatus {
respBody, _ := io.ReadAll(resp.Body)
t.Errorf("Expected status %d, got %d. Response: %s", tt.expectedStatus, resp.StatusCode, string(respBody))
}
})
}
}
func TestCORSHeaders(t *testing.T) {
tests := []struct {
name string
endpoint string
origin string
method string
expectedStatus int
checkCORS bool
}{
{
name: "CORS preflight request",
endpoint: "/v1/models",
origin: "http://localhost:3000",
method: "OPTIONS",
expectedStatus: http.StatusOK,
checkCORS: true,
},
{
name: "CORS actual request",
endpoint: "/health",
origin: "http://localhost:3000",
method: "GET",
expectedStatus: http.StatusOK,
checkCORS: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
req, err := http.NewRequest(tt.method, baseURL+tt.endpoint, http.NoBody)
if err != nil {
t.Fatalf("Failed to create request: %v", err)
}
if tt.origin != "" {
req.Header.Set("Origin", tt.origin)
}
if tt.method == "OPTIONS" {
req.Header.Set("Access-Control-Request-Method", "GET")
req.Header.Set("Access-Control-Request-Headers", "Content-Type")
}
client := &http.Client{Timeout: 5 * time.Second}
resp, err := client.Do(req)
if err != nil {
t.Fatalf("Failed to make request: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != tt.expectedStatus {
t.Errorf("Expected status %d, got %d", tt.expectedStatus, resp.StatusCode)
}
if tt.checkCORS {
// Check for CORS headers
allowOrigin := resp.Header.Get("Access-Control-Allow-Origin")
if allowOrigin == "" {
t.Error("Expected Access-Control-Allow-Origin header")
}
if tt.method == "OPTIONS" {
allowMethods := resp.Header.Get("Access-Control-Allow-Methods")
if allowMethods == "" {
t.Error("Expected Access-Control-Allow-Methods header for preflight")
}
}
}
})
}
}
func TestErrorConditions(t *testing.T) {
tests := []struct {
name string
endpoint string
expectedStatus int
}{
{
name: "nonexistent endpoint",
endpoint: "/nonexistent",
expectedStatus: http.StatusNotFound,
},
{
name: "invalid path",
endpoint: "/v1/invalid",
expectedStatus: http.StatusNotFound,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
resp, err := http.Get(baseURL + tt.endpoint)
if err != nil {
t.Fatalf("Failed to make request: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != tt.expectedStatus {
t.Errorf("Expected status %d, got %d", tt.expectedStatus, resp.StatusCode)
}
})
}
}
func TestSecurityHeaders(t *testing.T) {
resp, err := http.Get(baseURL + "/health")
if err != nil {
t.Fatalf("Failed to make request: %v", err)
}
defer resp.Body.Close()
expectedHeaders := map[string]string{
"X-Content-Type-Options": "nosniff",
"X-Frame-Options": "DENY",
"X-XSS-Protection": "1; mode=block",
}
for header, expectedValue := range expectedHeaders {
actualValue := resp.Header.Get(header)
if actualValue != expectedValue {
t.Errorf("Expected header %s to be '%s', got '%s'", header, expectedValue, actualValue)
}
}
}
func TestServerShutdown(t *testing.T) {
// This test verifies that the server can be gracefully shut down
// We'll create a separate server instance for this test
server, serverURL, shutdownFunc, err := setupTestServer()
if err != nil {
t.Fatalf("Failed to setup test server: %v", err)
}
// Wait for server to be ready
if !waitForServer(serverURL, 5*time.Second) {
shutdownFunc()
t.Fatal("Server failed to start within timeout")
}
// Make a request to ensure server is working
resp, err := http.Get(serverURL + "/health")
if err != nil {
shutdownFunc()
t.Fatalf("Failed to make request: %v", err)
}
resp.Body.Close()
if resp.StatusCode != http.StatusOK {
t.Errorf("Expected status %d, got %d", http.StatusOK, resp.StatusCode)
}
// Shutdown the server
shutdownFunc()
// Verify server is no longer responding
time.Sleep(200 * time.Millisecond) // Give time for shutdown
resp2, err := http.Get(serverURL + "/health")
if resp2 != nil {
defer resp2.Body.Close()
}
if err == nil {
t.Error("Expected server to be shut down, but it's still responding")
}
_ = server // Use server variable to avoid unused warning
}
// setupTestServer creates a test server instance and returns cleanup function
func setupTestServer() (server *internal.Server, baseURL string, cleanup func(), err error) {
// Find an available port
listener, err := net.Listen("tcp", ":0")
if err != nil {
return nil, "", nil, fmt.Errorf("failed to find available port: %w", err)
}
port := listener.Addr().(*net.TCPAddr).Port
listener.Close()
// Create test configuration with proper defaults
cfg := &internal.Config{
Port: port,
}
// Set default headers to prevent validation errors
internal.SetDefaultHeaders(cfg)
internal.SetDefaultCORS(cfg)
internal.SetDefaultTimeouts(cfg)
// Create HTTP client for the server
httpClient := &http.Client{
Timeout: 30 * time.Second,
}
// Create server instance
server = internal.NewServer(cfg, httpClient)
baseURL = fmt.Sprintf("http://localhost:%d", port)
// Start server in background goroutine
serverErrCh := make(chan error, 1)
go func() {
// For testing, we'll just call Start() which blocks
if err := server.Start(); err != nil && err != http.ErrServerClosed {
serverErrCh <- err
}
}()
cleanup = func() {
if server != nil {
if err := server.Stop(); err != nil {
fmt.Printf("Error stopping server: %v\n", err)
}
}
// Give server time to shutdown gracefully
time.Sleep(200 * time.Millisecond)
}
// Check for immediate startup errors
select {
case err := <-serverErrCh:
cleanup()
return nil, "", nil, fmt.Errorf("server failed to start: %w", err)
case <-time.After(1 * time.Second):
// Server seems to be starting OK
}
return server, baseURL, cleanup, nil
}
// waitForServer waits for the server to be ready to accept connections
func waitForServer(baseURL string, timeout time.Duration) bool {
client := &http.Client{Timeout: 1 * time.Second}
deadline := time.Now().Add(timeout)
for time.Now().Before(deadline) {
resp, err := client.Get(baseURL + "/health")
if err == nil {
resp.Body.Close()
if resp.StatusCode == http.StatusOK {
return true
}
}
time.Sleep(200 * time.Millisecond)
}
return false
}