MCPcopy
hub / github.com/gorilla/mux / TestCORSMethodMiddleware

Function TestCORSMethodMiddleware

middleware_test.go:415–527  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

413}
414
415func 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 },

Callers

nothing calls this directly

Calls 11

UseMethod · 0.95
ServeHTTPMethod · 0.95
stringHandlerFunction · 0.85
NewRouterFunction · 0.85
CORSMethodMiddlewareFunction · 0.85
NewRecorderFunction · 0.85
newRequestFunction · 0.85
HandleFuncMethod · 0.80
GetMethod · 0.80
MethodsMethod · 0.45
HeaderMethod · 0.45

Tested by

no test coverage detected