MCPcopy
hub / github.com/go-chi/chi / TestTreeRegexpRecursive

Function TestTreeRegexpRecursive

tree_test.go:336–385  ·  tree_test.go::TestTreeRegexpRecursive
(t *testing.T)

Source from the content-addressed store, hash-verified

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