(t *testing.T)
| 45 | } |
| 46 | |
| 47 | func TestMiddleware(t *testing.T) { |
| 48 | router := NewRouter() |
| 49 | router.HandleFunc("/", dummyHandler).Methods("GET") |
| 50 | |
| 51 | mw := &testMiddleware{} |
| 52 | router.useInterface(mw) |
| 53 | |
| 54 | rw := NewRecorder() |
| 55 | req := newRequest("GET", "/") |
| 56 | |
| 57 | t.Run("regular middleware call", func(t *testing.T) { |
| 58 | router.ServeHTTP(rw, req) |
| 59 | if mw.timesCalled != 1 { |
| 60 | t.Fatalf("Expected %d calls, but got only %d", 1, mw.timesCalled) |
| 61 | } |
| 62 | }) |
| 63 | |
| 64 | t.Run("not called for 404", func(t *testing.T) { |
| 65 | req = newRequest("GET", "/not/found") |
| 66 | router.ServeHTTP(rw, req) |
| 67 | if mw.timesCalled != 1 { |
| 68 | t.Fatalf("Expected %d calls, but got only %d", 1, mw.timesCalled) |
| 69 | } |
| 70 | }) |
| 71 | |
| 72 | t.Run("not called for method mismatch", func(t *testing.T) { |
| 73 | req = newRequest("POST", "/") |
| 74 | router.ServeHTTP(rw, req) |
| 75 | if mw.timesCalled != 1 { |
| 76 | t.Fatalf("Expected %d calls, but got only %d", 1, mw.timesCalled) |
| 77 | } |
| 78 | }) |
| 79 | |
| 80 | t.Run("regular call using function middleware", func(t *testing.T) { |
| 81 | router.Use(mw.Middleware) |
| 82 | req = newRequest("GET", "/") |
| 83 | router.ServeHTTP(rw, req) |
| 84 | if mw.timesCalled != 3 { |
| 85 | t.Fatalf("Expected %d calls, but got only %d", 3, mw.timesCalled) |
| 86 | } |
| 87 | }) |
| 88 | } |
| 89 | |
| 90 | func TestMiddlewareSubrouter(t *testing.T) { |
| 91 | router := NewRouter() |
nothing calls this directly
no test coverage detected