(t *testing.T)
| 413 | } |
| 414 | |
| 415 | func TestCORSMethodMiddleware(t *testing.T) { |
| 416 | testCases := []struct { |
| 417 | name string |
| 418 | registerRoutes func(r *Router) |
| 419 | requestHeader http.Header |
| 420 | requestMethod string |
| 421 | requestPath string |
| 422 | expectedAccessControlAllowMethodsHeader string |
| 423 | expectedResponse string |
| 424 | }{ |
| 425 | { |
| 426 | name: "does not set without OPTIONS matcher", |
| 427 | registerRoutes: func(r *Router) { |
| 428 | r.HandleFunc("/foo", stringHandler("a")).Methods(http.MethodGet, http.MethodPut, http.MethodPatch) |
| 429 | }, |
| 430 | requestMethod: "GET", |
| 431 | requestPath: "/foo", |
| 432 | expectedAccessControlAllowMethodsHeader: "", |
| 433 | expectedResponse: "a", |
| 434 | }, |
| 435 | { |
| 436 | name: "sets on non OPTIONS", |
| 437 | registerRoutes: func(r *Router) { |
| 438 | r.HandleFunc("/foo", stringHandler("a")).Methods(http.MethodGet, http.MethodPut, http.MethodPatch) |
| 439 | r.HandleFunc("/foo", stringHandler("b")).Methods(http.MethodOptions) |
| 440 | }, |
| 441 | requestMethod: "GET", |
| 442 | requestPath: "/foo", |
| 443 | expectedAccessControlAllowMethodsHeader: "GET,PUT,PATCH,OPTIONS", |
| 444 | expectedResponse: "a", |
| 445 | }, |
| 446 | { |
| 447 | name: "sets without preflight headers", |
| 448 | registerRoutes: func(r *Router) { |
| 449 | r.HandleFunc("/foo", stringHandler("a")).Methods(http.MethodGet, http.MethodPut, http.MethodPatch) |
| 450 | r.HandleFunc("/foo", stringHandler("b")).Methods(http.MethodOptions) |
| 451 | }, |
| 452 | requestMethod: "OPTIONS", |
| 453 | requestPath: "/foo", |
| 454 | expectedAccessControlAllowMethodsHeader: "GET,PUT,PATCH,OPTIONS", |
| 455 | expectedResponse: "b", |
| 456 | }, |
| 457 | { |
| 458 | name: "does not set on error", |
| 459 | registerRoutes: func(r *Router) { |
| 460 | r.HandleFunc("/foo", stringHandler("a")) |
| 461 | }, |
| 462 | requestMethod: "OPTIONS", |
| 463 | requestPath: "/foo", |
| 464 | expectedAccessControlAllowMethodsHeader: "", |
| 465 | expectedResponse: "a", |
| 466 | }, |
| 467 | { |
| 468 | name: "sets header on valid preflight", |
| 469 | registerRoutes: func(r *Router) { |
| 470 | r.HandleFunc("/foo", stringHandler("a")).Methods(http.MethodGet, http.MethodPut, http.MethodPatch) |
| 471 | r.HandleFunc("/foo", stringHandler("b")).Methods(http.MethodOptions) |
| 472 | }, |
nothing calls this directly
no test coverage detected