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

Function TestErrMatchNotFound

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

Source from the content-addressed store, hash-verified

2117}
2118
2119func TestErrMatchNotFound(t *testing.T) {
2120 emptyHandler := func(w http.ResponseWriter, r *http.Request) {}
2121
2122 r := NewRouter()
2123 r.HandleFunc("/", emptyHandler)
2124 s := r.PathPrefix("/sub/").Subrouter()
2125 s.HandleFunc("/", emptyHandler)
2126
2127 // Regular 404 not found
2128 req, _ := http.NewRequest("GET", "/sub/whatever", nil)
2129 match := new(RouteMatch)
2130 matched := r.Match(req, match)
2131
2132 if matched {
2133 t.Errorf("Subrouter should not have matched that, got %v", match.Route)
2134 }
2135 // Even without a custom handler, MatchErr is set to ErrNotFound
2136 if match.MatchErr != ErrNotFound {
2137 t.Errorf("Expected ErrNotFound MatchErr, but was %v", match.MatchErr)
2138 }
2139
2140 // Now lets add a 404 handler to subrouter
2141 s.NotFoundHandler = http.NotFoundHandler()
2142 req, _ = http.NewRequest("GET", "/sub/whatever", nil)
2143
2144 // Test the subrouter first
2145 match = new(RouteMatch)
2146 matched = s.Match(req, match)
2147 // Now we should get a match
2148 if !matched {
2149 t.Errorf("Subrouter should have matched %s", req.RequestURI)
2150 }
2151 // But MatchErr should be set to ErrNotFound anyway
2152 if match.MatchErr != ErrNotFound {
2153 t.Errorf("Expected ErrNotFound MatchErr, but was %v", match.MatchErr)
2154 }
2155
2156 // Now test the parent (MatchErr should propagate)
2157 match = new(RouteMatch)
2158 matched = r.Match(req, match)
2159
2160 // Now we should get a match
2161 if !matched {
2162 t.Errorf("Router should have matched %s via subrouter", req.RequestURI)
2163 }
2164 // But MatchErr should be set to ErrNotFound anyway
2165 if match.MatchErr != ErrNotFound {
2166 t.Errorf("Expected ErrNotFound MatchErr, but was %v", match.MatchErr)
2167 }
2168}
2169
2170// methodsSubrouterTest models the data necessary for testing handler
2171// matching for subrouters created after HTTP methods matcher registration.

Callers

nothing calls this directly

Calls 6

HandleFuncMethod · 0.95
PathPrefixMethod · 0.95
MatchMethod · 0.95
NewRouterFunction · 0.85
SubrouterMethod · 0.80
MatchMethod · 0.65

Tested by

no test coverage detected