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

Function TestMiddleware

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

Source from the content-addressed store, hash-verified

45}
46
47func 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
90func TestMiddlewareSubrouter(t *testing.T) {
91 router := NewRouter()

Callers

nothing calls this directly

Calls 8

HandleFuncMethod · 0.95
useInterfaceMethod · 0.95
ServeHTTPMethod · 0.95
UseMethod · 0.95
NewRouterFunction · 0.85
NewRecorderFunction · 0.85
newRequestFunction · 0.85
MethodsMethod · 0.45

Tested by

no test coverage detected