(t *testing.T)
| 88 | } |
| 89 | |
| 90 | func TestMiddlewareSubrouter(t *testing.T) { |
| 91 | router := NewRouter() |
| 92 | router.HandleFunc("/", dummyHandler).Methods("GET") |
| 93 | |
| 94 | subrouter := router.PathPrefix("/sub").Subrouter() |
| 95 | subrouter.HandleFunc("/x", dummyHandler).Methods("GET") |
| 96 | |
| 97 | mw := &testMiddleware{} |
| 98 | subrouter.useInterface(mw) |
| 99 | |
| 100 | rw := NewRecorder() |
| 101 | req := newRequest("GET", "/") |
| 102 | |
| 103 | t.Run("not called for route outside subrouter", func(t *testing.T) { |
| 104 | router.ServeHTTP(rw, req) |
| 105 | if mw.timesCalled != 0 { |
| 106 | t.Fatalf("Expected %d calls, but got only %d", 0, mw.timesCalled) |
| 107 | } |
| 108 | }) |
| 109 | |
| 110 | t.Run("not called for subrouter root 404", func(t *testing.T) { |
| 111 | req = newRequest("GET", "/sub/") |
| 112 | router.ServeHTTP(rw, req) |
| 113 | if mw.timesCalled != 0 { |
| 114 | t.Fatalf("Expected %d calls, but got only %d", 0, mw.timesCalled) |
| 115 | } |
| 116 | }) |
| 117 | |
| 118 | t.Run("called once for route inside subrouter", func(t *testing.T) { |
| 119 | req = newRequest("GET", "/sub/x") |
| 120 | router.ServeHTTP(rw, req) |
| 121 | if mw.timesCalled != 1 { |
| 122 | t.Fatalf("Expected %d calls, but got only %d", 1, mw.timesCalled) |
| 123 | } |
| 124 | }) |
| 125 | |
| 126 | t.Run("not called for 404 inside subrouter", func(t *testing.T) { |
| 127 | req = newRequest("GET", "/sub/not/found") |
| 128 | router.ServeHTTP(rw, req) |
| 129 | if mw.timesCalled != 1 { |
| 130 | t.Fatalf("Expected %d calls, but got only %d", 1, mw.timesCalled) |
| 131 | } |
| 132 | }) |
| 133 | |
| 134 | t.Run("middleware added to router", func(t *testing.T) { |
| 135 | router.useInterface(mw) |
| 136 | |
| 137 | t.Run("called once for route outside subrouter", func(t *testing.T) { |
| 138 | req = newRequest("GET", "/") |
| 139 | router.ServeHTTP(rw, req) |
| 140 | if mw.timesCalled != 2 { |
| 141 | t.Fatalf("Expected %d calls, but got only %d", 2, mw.timesCalled) |
| 142 | } |
| 143 | }) |
| 144 | |
| 145 | t.Run("called twice for route inside subrouter", func(t *testing.T) { |
| 146 | req = newRequest("GET", "/sub/x") |
| 147 | router.ServeHTTP(rw, req) |
nothing calls this directly
no test coverage detected