(t *testing.T)
| 923 | } |
| 924 | |
| 925 | func TestRouterHandleMethodOptions(t *testing.T) { |
| 926 | e := New() |
| 927 | e.contextPathParamAllocSize.Store(1) |
| 928 | r := e.router |
| 929 | |
| 930 | r.Add(Route{Method: http.MethodGet, Path: "/users", Handler: handlerFunc}) |
| 931 | r.Add(Route{Method: http.MethodPost, Path: "/users", Handler: handlerFunc}) |
| 932 | r.Add(Route{Method: http.MethodPut, Path: "/users/:id", Handler: handlerFunc}) |
| 933 | r.Add(Route{Method: http.MethodGet, Path: "/users/:id", Handler: handlerFunc}) |
| 934 | |
| 935 | var testCases = []struct { |
| 936 | name string |
| 937 | whenMethod string |
| 938 | whenURL string |
| 939 | expectAllowHeader string |
| 940 | expectStatus int |
| 941 | }{ |
| 942 | { |
| 943 | name: "allows GET and POST handlers", |
| 944 | whenMethod: http.MethodOptions, |
| 945 | whenURL: "/users", |
| 946 | expectAllowHeader: "OPTIONS, GET, POST", |
| 947 | expectStatus: http.StatusNoContent, |
| 948 | }, |
| 949 | { |
| 950 | name: "allows GET and PUT handlers", |
| 951 | whenMethod: http.MethodOptions, |
| 952 | whenURL: "/users/1", |
| 953 | expectAllowHeader: "OPTIONS, GET, PUT", |
| 954 | expectStatus: http.StatusNoContent, |
| 955 | }, |
| 956 | { |
| 957 | name: "GET does not have allows header", |
| 958 | whenMethod: http.MethodGet, |
| 959 | whenURL: "/users", |
| 960 | expectAllowHeader: "", |
| 961 | expectStatus: http.StatusOK, |
| 962 | }, |
| 963 | { |
| 964 | name: "path with no handlers does not set Allows header", |
| 965 | whenMethod: http.MethodOptions, |
| 966 | whenURL: "/notFound", |
| 967 | expectAllowHeader: "", |
| 968 | expectStatus: http.StatusNotFound, |
| 969 | }, |
| 970 | } |
| 971 | for _, tc := range testCases { |
| 972 | t.Run(tc.name, func(t *testing.T) { |
| 973 | req := httptest.NewRequest(tc.whenMethod, tc.whenURL, nil) |
| 974 | rec := httptest.NewRecorder() |
| 975 | c := e.NewContext(req, rec) |
| 976 | |
| 977 | h := r.Route(c) |
| 978 | err := h(c) |
| 979 | |
| 980 | if tc.expectStatus >= 400 { |
| 981 | assert.Error(t, err) |
| 982 | he := err.(HTTPStatusCoder) |
nothing calls this directly
no test coverage detected
searching dependent graphs…