MCPcopy Create free account
hub / github.com/BryanMwangi/pine / TestGroup_MiddlewareOrder

Function TestGroup_MiddlewareOrder

group_test.go:87–125  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

85}
86
87func TestGroup_MiddlewareOrder(t *testing.T) {
88 app := New()
89
90 var order []string
91
92 globalMw := func(next Handler) Handler {
93 return func(c *Ctx) error {
94 order = append(order, "global")
95 return next(c)
96 }
97 }
98 groupMw := func(next Handler) Handler {
99 return func(c *Ctx) error {
100 order = append(order, "group")
101 return next(c)
102 }
103 }
104
105 app.Use(globalMw)
106 g := app.Group("/v1", groupMw)
107 g.Get("/ping", func(c *Ctx) error {
108 order = append(order, "handler")
109 return c.SendString("pong")
110 })
111
112 req := httptest.NewRequest(http.MethodGet, "/v1/ping", nil)
113 rr := httptest.NewRecorder()
114 app.ServeHTTP(rr, req)
115
116 want := []string{"global", "group", "handler"}
117 if len(order) != len(want) {
118 t.Fatalf("expected order %v, got %v", want, order)
119 }
120 for i, s := range want {
121 if order[i] != s {
122 t.Errorf("position %d: expected %q, got %q", i, s, order[i])
123 }
124 }
125}
126
127func TestGroup_GlobalUseAfterGroup(t *testing.T) {
128 app := New()

Callers

nothing calls this directly

Calls 6

UseMethod · 0.80
SendStringMethod · 0.80
ServeHTTPMethod · 0.80
NewFunction · 0.70
GroupMethod · 0.45
GetMethod · 0.45

Tested by

no test coverage detected