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

Function TestMiddlewareMethodMismatch

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

Source from the content-addressed store, hash-verified

247}
248
249func TestMiddlewareMethodMismatch(t *testing.T) {
250 mwStr := []byte("Middleware\n")
251 handlerStr := []byte("Logic\n")
252
253 router := NewRouter()
254 router.HandleFunc("/", func(w http.ResponseWriter, e *http.Request) {
255 _, err := w.Write(handlerStr)
256 if err != nil {
257 t.Fatalf("Failed writing HTTP response: %v", err)
258 }
259 }).Methods("GET")
260
261 router.Use(func(h http.Handler) http.Handler {
262 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
263 _, err := w.Write(mwStr)
264 if err != nil {
265 t.Fatalf("Failed writing HTTP response: %v", err)
266 }
267 h.ServeHTTP(w, r)
268 })
269 })
270
271 t.Run("not called", func(t *testing.T) {
272 rw := NewRecorder()
273 req := newRequest("POST", "/")
274
275 router.ServeHTTP(rw, req)
276 if bytes.Contains(rw.Body.Bytes(), mwStr) {
277 t.Fatal("Middleware was called for a method mismatch")
278 }
279 })
280
281 t.Run("not called with custom method not allowed handler", func(t *testing.T) {
282 rw := NewRecorder()
283 req := newRequest("POST", "/")
284
285 router.MethodNotAllowedHandler = http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
286 _, err := rw.Write([]byte("Method not allowed"))
287 if err != nil {
288 t.Fatalf("Failed writing HTTP response: %v", err)
289 }
290 })
291 router.ServeHTTP(rw, req)
292
293 if bytes.Contains(rw.Body.Bytes(), mwStr) {
294 t.Fatal("Middleware was called for a method mismatch")
295 }
296 })
297}
298
299func TestMiddlewareNotFoundSubrouter(t *testing.T) {
300 mwStr := []byte("Middleware\n")

Callers

nothing calls this directly

Calls 9

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

Tested by

no test coverage detected