MCPcopy Create free account
hub / github.com/goadesign/goa / TestRequestPatternInMiddleware

Function TestRequestPatternInMiddleware

http/mux_test.go:200–251  ·  view source on GitHub ↗

TestRequestPatternInMiddleware verifies that r.Pattern is set by the built-in middleware before user-registered middlewares run. This is critical for observability middleware (e.g., otelhttp) that reads r.Pattern to set the http.route span attribute at span-start time.

(t *testing.T)

Source from the content-addressed store, hash-verified

198// critical for observability middleware (e.g., otelhttp) that reads
199// r.Pattern to set the http.route span attribute at span-start time.
200func TestRequestPatternInMiddleware(t *testing.T) {
201 cases := []struct {
202 Name string
203 Method string
204 Pattern string
205 URL string
206 Expected string
207 }{
208 {
209 Name: "simple",
210 Method: "GET",
211 Pattern: "/users",
212 URL: "/users",
213 Expected: "GET /users",
214 },
215 {
216 Name: "with segment",
217 Method: "POST",
218 Pattern: "/users/{id}",
219 URL: "/users/123",
220 Expected: "POST /users/{id}",
221 },
222 {
223 Name: "with wildcard",
224 Method: "GET",
225 Pattern: "/files/{*path}",
226 URL: "/files/a/b/c",
227 Expected: "GET /files/{*path}",
228 },
229 }
230
231 for _, c := range cases {
232 t.Run(c.Name, func(t *testing.T) {
233 var middlewareCalled bool
234 mux := NewMuxer()
235 // Register a user middleware that reads r.Pattern —
236 // simulating otelhttp.NewMiddleware.
237 mux.Use(func(next http.Handler) http.Handler {
238 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
239 assert.Equal(t, c.Expected, r.Pattern)
240 middlewareCalled = true
241 next.ServeHTTP(w, r)
242 })
243 })
244 mux.Handle(c.Method, c.Pattern, func(_ http.ResponseWriter, _ *http.Request) {})
245 req, _ := http.NewRequest(c.Method, c.URL, nil)
246 w := httptest.NewRecorder()
247 mux.ServeHTTP(w, req)
248 assert.True(t, middlewareCalled)
249 })
250 }
251}
252
253func TestResolvePattern(t *testing.T) {
254 cases := []struct {

Callers

nothing calls this directly

Calls 5

ServeHTTPMethod · 0.95
HandleMethod · 0.95
NewMuxerFunction · 0.85
UseMethod · 0.65
RunMethod · 0.45

Tested by

no test coverage detected