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

Function TestMiddlewareNotFound

middleware_test.go:199–247  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

197}
198
199func TestMiddlewareNotFound(t *testing.T) {
200 mwStr := []byte("Middleware\n")
201 handlerStr := []byte("Logic\n")
202
203 router := NewRouter()
204 router.HandleFunc("/", func(w http.ResponseWriter, e *http.Request) {
205 _, err := w.Write(handlerStr)
206 if err != nil {
207 t.Fatalf("Failed writing HTTP response: %v", err)
208 }
209 })
210 router.Use(func(h http.Handler) http.Handler {
211 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
212 _, err := w.Write(mwStr)
213 if err != nil {
214 t.Fatalf("Failed writing HTTP response: %v", err)
215 }
216 h.ServeHTTP(w, r)
217 })
218 })
219
220 // Test not found call with default handler
221 t.Run("not called", func(t *testing.T) {
222 rw := NewRecorder()
223 req := newRequest("GET", "/notfound")
224
225 router.ServeHTTP(rw, req)
226 if bytes.Contains(rw.Body.Bytes(), mwStr) {
227 t.Fatal("Middleware was called for a 404")
228 }
229 })
230
231 t.Run("not called with custom not found handler", func(t *testing.T) {
232 rw := NewRecorder()
233 req := newRequest("GET", "/notfound")
234
235 router.NotFoundHandler = http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
236 _, err := rw.Write([]byte("Custom 404 handler"))
237 if err != nil {
238 t.Fatalf("Failed writing HTTP response: %v", err)
239 }
240 })
241 router.ServeHTTP(rw, req)
242
243 if bytes.Contains(rw.Body.Bytes(), mwStr) {
244 t.Fatal("Middleware was called for a custom 404")
245 }
246 })
247}
248
249func TestMiddlewareMethodMismatch(t *testing.T) {
250 mwStr := []byte("Middleware\n")

Callers

nothing calls this directly

Calls 8

HandleFuncMethod · 0.95
UseMethod · 0.95
ServeHTTPMethod · 0.95
NewRouterFunction · 0.85
NewRecorderFunction · 0.85
newRequestFunction · 0.85
HandlerFuncMethod · 0.80
WriteMethod · 0.45

Tested by

no test coverage detected