(t *testing.T)
| 529 | } |
| 530 | |
| 531 | func TestServer_CORSPreflightOrigins(t *testing.T) { |
| 532 | cases := []struct { |
| 533 | name string |
| 534 | allowedOrigins []string |
| 535 | originHeader string |
| 536 | expectedStatusCode int |
| 537 | expectCORSHeaders bool |
| 538 | }{ |
| 539 | { |
| 540 | name: "preflight with wildcard origins", |
| 541 | allowedOrigins: []string{"*"}, |
| 542 | originHeader: "https://example.com", |
| 543 | expectedStatusCode: http.StatusOK, |
| 544 | expectCORSHeaders: true, |
| 545 | }, |
| 546 | { |
| 547 | name: "preflight with specific valid origin", |
| 548 | allowedOrigins: []string{"https://localhost:3000"}, |
| 549 | originHeader: "https://localhost:3000", |
| 550 | expectedStatusCode: http.StatusOK, |
| 551 | expectCORSHeaders: true, |
| 552 | }, |
| 553 | { |
| 554 | name: "preflight with invalid origin", |
| 555 | allowedOrigins: []string{"https://localhost:3000"}, |
| 556 | originHeader: "https://malicious.com", |
| 557 | expectedStatusCode: http.StatusOK, // Request succeeds but no CORS headers |
| 558 | expectCORSHeaders: false, |
| 559 | }, |
| 560 | } |
| 561 | |
| 562 | for _, tc := range cases { |
| 563 | t.Run(tc.name, func(t *testing.T) { |
| 564 | t.Parallel() |
| 565 | ctx := logctx.WithLogger(context.Background(), slog.New(slog.NewTextHandler(os.Stdout, nil))) |
| 566 | s, err := httpapi.NewServer(ctx, httpapi.ServerConfig{ |
| 567 | AgentType: msgfmt.AgentTypeClaude, |
| 568 | AgentIO: nil, |
| 569 | Port: 0, |
| 570 | ChatBasePath: "/chat", |
| 571 | AllowedHosts: []string{"*"}, // Set wildcard to isolate CORS testing |
| 572 | AllowedOrigins: tc.allowedOrigins, |
| 573 | }) |
| 574 | require.NoError(t, err) |
| 575 | tsServer := httptest.NewServer(s.Handler()) |
| 576 | t.Cleanup(tsServer.Close) |
| 577 | |
| 578 | req, err := http.NewRequest("OPTIONS", tsServer.URL+"/status", nil) |
| 579 | require.NoError(t, err) |
| 580 | |
| 581 | if tc.originHeader != "" { |
| 582 | req.Header.Set("Origin", tc.originHeader) |
| 583 | } |
| 584 | req.Header.Set("Access-Control-Request-Method", "GET") |
| 585 | req.Header.Set("Access-Control-Request-Headers", "Content-Type") |
| 586 | |
| 587 | client := &http.Client{} |
| 588 | resp, err := client.Do(req) |
nothing calls this directly
no test coverage detected