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

Function TestSubrouterCustomMethodNotAllowed

mux_test.go:2796–2842  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

2794}
2795
2796func TestSubrouterCustomMethodNotAllowed(t *testing.T) {
2797 handler := func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) }
2798
2799 router := NewRouter()
2800 router.HandleFunc("/test", handler).Methods(http.MethodGet)
2801 router.MethodNotAllowedHandler = customMethodNotAllowedHandler{msg: "custom router handler"}
2802
2803 subrouter := router.PathPrefix("/sub").Subrouter()
2804 subrouter.HandleFunc("/test", handler).Methods(http.MethodGet)
2805 subrouter.MethodNotAllowedHandler = customMethodNotAllowedHandler{msg: "custom sub router handler"}
2806
2807 testCases := map[string]struct {
2808 path string
2809 expMsg string
2810 }{
2811 "router method not allowed": {
2812 path: "/test",
2813 expMsg: "custom router handler",
2814 },
2815 "subrouter method not allowed": {
2816 path: "/sub/test",
2817 expMsg: "custom sub router handler",
2818 },
2819 }
2820
2821 for name, tc := range testCases {
2822 t.Run(name, func(tt *testing.T) {
2823 w := NewRecorder()
2824 req := newRequest(http.MethodPut, tc.path)
2825
2826 router.ServeHTTP(w, req)
2827
2828 if w.Code != http.StatusMethodNotAllowed {
2829 tt.Errorf("Expected status code 405 (got %d)", w.Code)
2830 }
2831
2832 b, err := io.ReadAll(w.Body)
2833 if err != nil {
2834 tt.Errorf("failed to read body: %v", err)
2835 }
2836
2837 if string(b) != tc.expMsg {
2838 tt.Errorf("expected msg %q, got %q", tc.expMsg, string(b))
2839 }
2840 })
2841 }
2842}
2843
2844func TestSubrouterNotFound(t *testing.T) {
2845 handler := func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) }

Callers

nothing calls this directly

Calls 9

HandleFuncMethod · 0.95
PathPrefixMethod · 0.95
ServeHTTPMethod · 0.95
NewRouterFunction · 0.85
NewRecorderFunction · 0.85
newRequestFunction · 0.85
SubrouterMethod · 0.80
WriteHeaderMethod · 0.45
MethodsMethod · 0.45

Tested by

no test coverage detected