MCPcopy Create free account
hub / github.com/coder/agentapi / TestServer_CORSPreflightWithHosts

Function TestServer_CORSPreflightWithHosts

lib/httpapi/server_test.go:288–371  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

286}
287
288func 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 }

Callers

nothing calls this directly

Calls 5

HandlerMethod · 0.95
WithLoggerFunction · 0.92
NewServerFunction · 0.92
CloseMethod · 0.80
DoMethod · 0.65

Tested by

no test coverage detected