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

Function TestMultipleDefinitionOfSamePathWithDifferentMethods

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

Source from the content-addressed store, hash-verified

2070}
2071
2072func TestMultipleDefinitionOfSamePathWithDifferentMethods(t *testing.T) {
2073 emptyHandler := func(w http.ResponseWriter, r *http.Request) {}
2074
2075 r := NewRouter()
2076 r.HandleFunc("/api", emptyHandler).Methods("POST")
2077 r.HandleFunc("/api", emptyHandler).Queries("time", "{time:[0-9]+}").Methods("GET")
2078
2079 t.Run("Post Method should be matched properly", func(t *testing.T) {
2080 req, _ := http.NewRequest("POST", "http://localhost/api", nil)
2081 match := new(RouteMatch)
2082 matched := r.Match(req, match)
2083 if !matched {
2084 t.Error("Should have matched route for methods")
2085 }
2086 if match.MatchErr != nil {
2087 t.Error("Should not have any matching error. Found:", match.MatchErr)
2088 }
2089 })
2090
2091 t.Run("Get Method with invalid query value should not match", func(t *testing.T) {
2092 req, _ := http.NewRequest("GET", "http://localhost/api?time=-4", nil)
2093 match := new(RouteMatch)
2094 matched := r.Match(req, match)
2095 if matched {
2096 t.Error("Should not have matched route for methods")
2097 }
2098 if match.MatchErr != ErrNotFound {
2099 t.Error("Should have ErrNotFound error. Found:", match.MatchErr)
2100 }
2101 })
2102
2103 t.Run("A mismach method of a valid path should return ErrMethodMismatch", func(t *testing.T) {
2104 r := NewRouter()
2105 r.HandleFunc("/api2", emptyHandler).Methods("POST")
2106 req, _ := http.NewRequest("GET", "http://localhost/api2", nil)
2107 match := new(RouteMatch)
2108 matched := r.Match(req, match)
2109 if matched {
2110 t.Error("Should not have matched route for methods")
2111 }
2112 if match.MatchErr != ErrMethodMismatch {
2113 t.Error("Should have ErrMethodMismatch error. Found:", match.MatchErr)
2114 }
2115 })
2116
2117}
2118
2119func TestErrMatchNotFound(t *testing.T) {
2120 emptyHandler := func(w http.ResponseWriter, r *http.Request) {}

Callers

nothing calls this directly

Calls 5

HandleFuncMethod · 0.95
MatchMethod · 0.95
NewRouterFunction · 0.85
MethodsMethod · 0.45
QueriesMethod · 0.45

Tested by

no test coverage detected