(t *testing.T)
| 286 | } |
| 287 | |
| 288 | func TestServer_CORSPreflightWithHosts(t *testing.T) { |
| 289 | cases := []struct { |
| 290 | name string |
| 291 | allowedHosts []string |
| 292 | hostHeader string |
| 293 | originHeader string |
| 294 | expectedStatusCode int |
| 295 | expectCORSHeaders bool |
| 296 | }{ |
| 297 | { |
| 298 | name: "preflight with wildcard hosts", |
| 299 | allowedHosts: []string{"*"}, |
| 300 | hostHeader: "example.com", |
| 301 | originHeader: "https://example.com", |
| 302 | expectedStatusCode: http.StatusOK, |
| 303 | expectCORSHeaders: true, |
| 304 | }, |
| 305 | { |
| 306 | name: "preflight with specific valid host", |
| 307 | allowedHosts: []string{"localhost"}, |
| 308 | hostHeader: "localhost:3000", |
| 309 | originHeader: "https://localhost:3000", |
| 310 | expectedStatusCode: http.StatusOK, |
| 311 | expectCORSHeaders: true, |
| 312 | }, |
| 313 | { |
| 314 | name: "preflight with invalid host", |
| 315 | allowedHosts: []string{"localhost"}, |
| 316 | hostHeader: "malicious.com", |
| 317 | originHeader: "https://malicious.com", |
| 318 | expectedStatusCode: http.StatusBadRequest, |
| 319 | expectCORSHeaders: false, |
| 320 | }, |
| 321 | } |
| 322 | |
| 323 | for _, tc := range cases { |
| 324 | t.Run(tc.name, func(t *testing.T) { |
| 325 | t.Parallel() |
| 326 | ctx := logctx.WithLogger(context.Background(), slog.New(slog.NewTextHandler(os.Stdout, nil))) |
| 327 | s, err := httpapi.NewServer(ctx, httpapi.ServerConfig{ |
| 328 | AgentType: msgfmt.AgentTypeClaude, |
| 329 | AgentIO: nil, |
| 330 | Port: 0, |
| 331 | ChatBasePath: "/chat", |
| 332 | AllowedHosts: tc.allowedHosts, |
| 333 | AllowedOrigins: []string{"*"}, // Set wildcard origins to isolate host testing |
| 334 | }) |
| 335 | require.NoError(t, err) |
| 336 | tsServer := httptest.NewServer(s.Handler()) |
| 337 | t.Cleanup(tsServer.Close) |
| 338 | |
| 339 | // Test CORS preflight request |
| 340 | req, err := http.NewRequest("OPTIONS", tsServer.URL+"/status", nil) |
| 341 | require.NoError(t, err) |
| 342 | |
| 343 | if tc.hostHeader != "" { |
| 344 | req.Host = tc.hostHeader |
| 345 | } |
nothing calls this directly
no test coverage detected