Skip to content

Commit

Permalink
Merge pull request #69 from WheeskyJack/WheeskyJack-patch-5a
Browse files Browse the repository at this point in the history
Update node_test.go with TestRegexpNodeMatchMiddleware test case
  • Loading branch information
vardius authored Sep 5, 2024
2 parents ecc5fc8 + 763d082 commit 572992b
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions mux/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 572992b

Please sign in to comment.