MCPcopy Create free account
hub / github.com/go-chi/chi / TestTreeRegexpRecursive

Function TestTreeRegexpRecursive

tree_test.go:338–387  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

336}
337
338func TestTreeRegexpRecursive(t *testing.T) {
339 hStub1 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
340 hStub2 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
341
342 tr := &node{}
343 tr.InsertRoute(mGET, "/one/{firstId:[a-z0-9-]+}/{secondId:[a-z0-9-]+}/first", hStub1)
344 tr.InsertRoute(mGET, "/one/{firstId:[a-z0-9-_]+}/{secondId:[a-z0-9-_]+}/second", hStub2)
345
346 // log.Println("~~~~~~~~~")
347 // log.Println("~~~~~~~~~")
348 // debugPrintTree(0, 0, tr, 0)
349 // log.Println("~~~~~~~~~")
350 // log.Println("~~~~~~~~~")
351
352 tests := []struct {
353 r string // input request path
354 h http.Handler // output matched handler
355 k []string // output param keys
356 v []string // output param values
357 }{
358 {r: "/one/hello/world/first", h: hStub1, k: []string{"firstId", "secondId"}, v: []string{"hello", "world"}},
359 {r: "/one/hi_there/ok/second", h: hStub2, k: []string{"firstId", "secondId"}, v: []string{"hi_there", "ok"}},
360 {r: "/one///first", h: nil, k: []string{}, v: []string{}},
361 {r: "/one/hi/123/second", h: hStub2, k: []string{"firstId", "secondId"}, v: []string{"hi", "123"}},
362 }
363
364 for i, tt := range tests {
365 rctx := NewRouteContext()
366
367 _, handlers, _ := tr.FindRoute(rctx, mGET, tt.r)
368
369 var handler http.Handler
370 if methodHandler, ok := handlers[mGET]; ok {
371 handler = methodHandler.handler
372 }
373
374 paramKeys := rctx.routeParams.Keys
375 paramValues := rctx.routeParams.Values
376
377 if fmt.Sprintf("%v", tt.h) != fmt.Sprintf("%v", handler) {
378 t.Errorf("input [%d]: find '%s' expecting handler:%v , got:%v", i, tt.r, tt.h, handler)
379 }
380 if !stringSliceEqual(tt.k, paramKeys) {
381 t.Errorf("input [%d]: find '%s' expecting paramKeys:(%d)%v , got:(%d)%v", i, tt.r, len(tt.k), tt.k, len(paramKeys), paramKeys)
382 }
383 if !stringSliceEqual(tt.v, paramValues) {
384 t.Errorf("input [%d]: find '%s' expecting paramValues:(%d)%v , got:(%d)%v", i, tt.r, len(tt.v), tt.v, len(paramValues), paramValues)
385 }
386 }
387}
388
389func TestTreeRegexMatchWholeParam(t *testing.T) {
390 hStub1 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})

Callers

nothing calls this directly

Calls 5

InsertRouteMethod · 0.95
FindRouteMethod · 0.95
NewRouteContextFunction · 0.85
stringSliceEqualFunction · 0.85
HandlerFuncMethod · 0.80

Tested by

no test coverage detected