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

Function TestNoMatchMethodErrorHandler

mux_test.go:2033–2070  ·  mux_test.go::TestNoMatchMethodErrorHandler
(t *testing.T)

Source from the content-addressed store, hash-verified

2031}
2032
2033func TestNoMatchMethodErrorHandler(t *testing.T) {
2034 func1 := func(w http.ResponseWriter, r *http.Request) {}
2035
2036 r := NewRouter()
2037 r.HandleFunc("/", func1).Methods("GET", "POST")
2038
2039 req, _ := http.NewRequest("PUT", "http://localhost/", nil)
2040 match := new(RouteMatch)
2041 matched := r.Match(req, match)
2042
2043 if matched {
2044 t.Error("Should not have matched route for methods")
2045 }
2046
2047 if match.MatchErr != ErrMethodMismatch {
2048 t.Error("Should get ErrMethodMismatch error")
2049 }
2050
2051 resp := NewRecorder()
2052 r.ServeHTTP(resp, req)
2053 if resp.Code != http.StatusMethodNotAllowed {
2054 t.Errorf("Expecting code %v", 405)
2055 }
2056
2057 // Add matching route
2058 r.HandleFunc("/", func1).Methods("PUT")
2059
2060 match = new(RouteMatch)
2061 matched = r.Match(req, match)
2062
2063 if !matched {
2064 t.Error("Should have matched route for methods")
2065 }
2066
2067 if match.MatchErr != nil {
2068 t.Error("Should not have any matching error. Found:", match.MatchErr)
2069 }
2070}
2071
2072func TestMultipleDefinitionOfSamePathWithDifferentMethods(t *testing.T) {
2073 emptyHandler := func(w http.ResponseWriter, r *http.Request) {}

Callers

nothing calls this directly

Calls 6

HandleFuncMethod · 0.95
MatchMethod · 0.95
ServeHTTPMethod · 0.95
NewRouterFunction · 0.85
NewRecorderFunction · 0.85
MethodsMethod · 0.45

Tested by

no test coverage detected