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

Function TestTreeRegexp

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

Source from the content-addressed store, hash-verified

268}
269
270func TestTreeRegexp(t *testing.T) {
271 hStub1 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
272 hStub2 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
273 hStub3 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
274 hStub4 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
275 hStub5 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
276 hStub6 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
277 hStub7 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
278
279 tr := &node{}
280 tr.InsertRoute(mGET, "/articles/{rid:^[0-9]{5,6}}", hStub7)
281 tr.InsertRoute(mGET, "/articles/{zid:^0[0-9]+}", hStub3)
282 tr.InsertRoute(mGET, "/articles/{name:^@[a-z]+}/posts", hStub4)
283 tr.InsertRoute(mGET, "/articles/{op:^[0-9]+}/run", hStub5)
284 tr.InsertRoute(mGET, "/articles/{id:^[0-9]+}", hStub1)
285 tr.InsertRoute(mGET, "/articles/{id:^[1-9]+}-{aux}", hStub6)
286 tr.InsertRoute(mGET, "/articles/{slug}", hStub2)
287
288 // log.Println("~~~~~~~~~")
289 // log.Println("~~~~~~~~~")
290 // debugPrintTree(0, 0, tr, 0)
291 // log.Println("~~~~~~~~~")
292 // log.Println("~~~~~~~~~")
293
294 tests := []struct {
295 r string // input request path
296 h http.Handler // output matched handler
297 k []string // output param keys
298 v []string // output param values
299 }{
300 {r: "/articles", h: nil, k: []string{}, v: []string{}},
301 {r: "/articles/12345", h: hStub7, k: []string{"rid"}, v: []string{"12345"}},
302 {r: "/articles/123", h: hStub1, k: []string{"id"}, v: []string{"123"}},
303 {r: "/articles/how-to-build-a-router", h: hStub2, k: []string{"slug"}, v: []string{"how-to-build-a-router"}},
304 {r: "/articles/0456", h: hStub3, k: []string{"zid"}, v: []string{"0456"}},
305 {r: "/articles/@pk/posts", h: hStub4, k: []string{"name"}, v: []string{"@pk"}},
306 {r: "/articles/1/run", h: hStub5, k: []string{"op"}, v: []string{"1"}},
307 {r: "/articles/1122", h: hStub1, k: []string{"id"}, v: []string{"1122"}},
308 {r: "/articles/1122-yes", h: hStub6, k: []string{"id", "aux"}, v: []string{"1122", "yes"}},
309 }
310
311 for i, tt := range tests {
312 rctx := NewRouteContext()
313
314 _, handlers, _ := tr.FindRoute(rctx, mGET, tt.r)
315
316 var handler http.Handler
317 if methodHandler, ok := handlers[mGET]; ok {
318 handler = methodHandler.handler
319 }
320
321 paramKeys := rctx.routeParams.Keys
322 paramValues := rctx.routeParams.Values
323
324 if fmt.Sprintf("%v", tt.h) != fmt.Sprintf("%v", handler) {
325 t.Errorf("input [%d]: find '%s' expecting handler:%v , got:%v", i, tt.r, tt.h, handler)
326 }
327 if !stringSliceEqual(tt.k, paramKeys) {

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