(t *testing.T)
| 827 | } |
| 828 | |
| 829 | func TestMethodNotAllowedAndNotFound(t *testing.T) { |
| 830 | e := New() |
| 831 | |
| 832 | // Routes |
| 833 | ri, err := e.AddRoute(Route{Method: http.MethodGet, Path: "/*", Handler: handlerFunc}) |
| 834 | assert.NoError(t, err) |
| 835 | assert.Equal(t, "GET:/*", ri.Name) |
| 836 | |
| 837 | ri, err = e.AddRoute(Route{Method: http.MethodPost, Path: "/users/:id", Handler: handlerFunc}) |
| 838 | assert.NoError(t, err) |
| 839 | assert.Equal(t, "POST:/users/:id", ri.Name) |
| 840 | |
| 841 | var testCases = []struct { |
| 842 | name string |
| 843 | whenMethod string |
| 844 | whenURL string |
| 845 | expectRoute string |
| 846 | expectParam map[string]string |
| 847 | expectError error |
| 848 | expectAllowHeader string |
| 849 | }{ |
| 850 | { |
| 851 | name: "exact match for route+method", |
| 852 | whenMethod: http.MethodPost, |
| 853 | whenURL: "/users/1", |
| 854 | expectRoute: "/users/:id", |
| 855 | expectParam: map[string]string{"id": "1"}, |
| 856 | }, |
| 857 | { |
| 858 | name: "matches node but not method. sends 405 from best match node", |
| 859 | whenMethod: http.MethodPut, |
| 860 | whenURL: "/users/1", |
| 861 | expectRoute: "/users/:id", |
| 862 | expectError: ErrMethodNotAllowed, |
| 863 | expectAllowHeader: "OPTIONS, POST", |
| 864 | }, |
| 865 | { |
| 866 | name: "best match is any route up in tree", |
| 867 | whenMethod: http.MethodGet, |
| 868 | whenURL: "/users/1", |
| 869 | expectRoute: "/*", |
| 870 | expectParam: map[string]string{"*": "users/1"}, |
| 871 | }, |
| 872 | } |
| 873 | for _, tc := range testCases { |
| 874 | t.Run(tc.name, func(t *testing.T) { |
| 875 | |
| 876 | method := http.MethodGet |
| 877 | if tc.whenMethod != "" { |
| 878 | method = tc.whenMethod |
| 879 | } |
| 880 | req := httptest.NewRequest(method, tc.whenURL, nil) |
| 881 | rec := httptest.NewRecorder() |
| 882 | c := e.NewContext(req, rec) |
| 883 | handler := e.router.Route(c) |
| 884 | |
| 885 | err := handler(c) |
| 886 | if tc.expectError != nil { |
nothing calls this directly
no test coverage detected
searching dependent graphs…