From 763d08233a1e1ef81ac61a34340e56634e3b555e Mon Sep 17 00:00:00 2001 From: WheeskyJack <32392032+WheeskyJack@users.noreply.github.com> Date: Mon, 2 Sep 2024 17:51:38 +0530 Subject: [PATCH] Update node_test.go with TestRegexpNodeMatchMiddleware test case Update node_test.go with TestRegexpNodeMatchMiddleware test case --- mux/node_test.go | 64 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/mux/node_test.go b/mux/node_test.go index 2fb58c2..0a7f12d 100644 --- a/mux/node_test.go +++ b/mux/node_test.go @@ -442,6 +442,70 @@ func TestWildcardNodeMatchMiddleware(t *testing.T) { runMiddlewareTests(tests, t) } +func TestRegexpNodeMatchMiddleware(t *testing.T) { + paramSize := 3 + middleware1 := buildMockMiddlewareFunc("1") + middleware2 := buildMockMiddlewareFunc("2") + middleware3 := buildMockMiddlewareFunc("3") + middleware4 := buildMockMiddlewareFunc("4") + mw1 := middleware.NewCollection(middleware1) + mw2 := middleware.NewCollection(middleware2, middleware3) + mw3 := middleware.NewCollection(middleware4) + + node1 := NewNode("test", uint8(paramSize)) + node1.PrependMiddleware(mw1) + item := NewNode("{item:item1|item2}", node1.MaxParamsSize()) // regexpnode + item.PrependMiddleware(mw2) + view := NewNode("view", node1.MaxParamsSize()+1) + view.PrependMiddleware(mw3) + node1.WithChildren(node1.Tree().withNode(item).sort()) + item.WithChildren(item.Tree().withNode(view).sort()) + node1.WithChildren(node1.Tree().Compile()) + + node2 := NewNode("test", uint8(paramSize)) + node2.PrependMiddleware(mw1) + item2 := NewNode("{item:item1|item2}", node1.MaxParamsSize()) // regexpnode + item2.PrependMiddleware(mw2) + item2.SkipSubPath() + node2.WithChildren(node2.Tree().withNode(item2).sort()) + node2.WithChildren(node2.Tree().Compile()) + + tests := []middlewareTest{ + { + name: "RegexpNode Exact match", + node: node1, + path: "test/item1", + expectedResult: middleware.NewCollection(middleware1, middleware2, middleware3), + }, + { + name: "RegexpNode Subpath match with skipSubPath", + node: node2, + path: "test/item2/random", + expectedResult: middleware.NewCollection(middleware1, middleware2, middleware3), + }, + { + name: "RegexpNode Subpath match without skipSubPath", + node: node1, + path: "test/item1/view", + expectedResult: middleware.NewCollection(middleware1, middleware2, middleware3, middleware4), + }, + { + name: "RegexpNode Subpath No match", + node: node1, + path: "test/item2/nomatch", + expectedResult: middleware.NewCollection(middleware1, middleware2, middleware3), + }, + { + name: "RegexpNode No match", + node: node1, + path: "test/item3/view", + expectedResult: middleware.NewCollection(middleware1), + }, + } + + runMiddlewareTests(tests, t) +} + func runMiddlewareTests(tests []middlewareTest, t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) {