Skip to content

Commit

Permalink
Merge pull request #65 from WheeskyJack/WheeskyJack-patch-1
Browse files Browse the repository at this point in the history
Update node_test.go with TestWildcardNodeMatchRoute test case
  • Loading branch information
vardius authored Aug 26, 2024
2 parents d124c05 + b1a0809 commit 8a3ec11
Showing 1 changed file with 95 additions and 9 deletions.
104 changes: 95 additions & 9 deletions mux/node_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package mux

import (
"reflect"
"testing"

"github.com/vardius/gorouter/v4/context"
Expand Down Expand Up @@ -39,7 +40,7 @@ type mockroute struct {
handler interface{}
}

func newRoute(h interface{}) *mockroute {
func newMockRoute(h interface{}) *mockroute {
if h == nil {
panic("Handler can not be nil.")
}
Expand All @@ -54,13 +55,14 @@ func (r *mockroute) Handler() interface{} {
}

func TestStaticNodeMatchRoute(t *testing.T) {
homeRoute := newRoute("testhomeroute")
searchRoute := newRoute("testsearchroute")
paramSize := 3
homeRoute := newMockRoute("testhomeroute")
searchRoute := newMockRoute("testsearchroute")

homeSkipSubpath := staticNode{name: "home", route: homeRoute, maxParamsSize: 3}
homeSkipSubpath := staticNode{name: "home", route: homeRoute, maxParamsSize: uint8(paramSize)}
homeSkipSubpath.SkipSubPath()

home := staticNode{name: "home", route: nil, maxParamsSize: 3}
home := staticNode{name: "home", route: nil, maxParamsSize: uint8(paramSize)}
home.WithRoute(homeRoute)

search := NewNode("search", home.MaxParamsSize())
Expand All @@ -81,21 +83,21 @@ func TestStaticNodeMatchRoute(t *testing.T) {
node: home,
path: "home",
expectedRoute: homeRoute,
expectedParams: make(context.Params, 3),
expectedParams: make(context.Params, paramSize),
},
{
name: "Exact Match with Skip SubPath",
node: homeSkipSubpath,
path: "home/about",
expectedRoute: homeRoute,
expectedParams: make(context.Params, 3),
expectedParams: make(context.Params, paramSize),
},
{
name: "Match with SubPath",
node: home,
path: "home/search",
expectedRoute: searchRoute,
expectedParams: make(context.Params, 3),
expectedParams: make(context.Params, paramSize),
},
{
name: "No Match",
Expand All @@ -119,7 +121,91 @@ func TestStaticNodeMatchRoute(t *testing.T) {
if route != tt.expectedRoute {
t.Errorf("%s: expected route %v, got %v", tt.name, tt.expectedRoute, route)
}
if len(params) != len(tt.expectedParams) {
if !reflect.DeepEqual(params, tt.expectedParams) {
t.Errorf("%s: expected params %v, got %v", tt.name, tt.expectedParams, params)
}
})
}
}

func TestWildcardNodeMatchRoute(t *testing.T) {
paramSize := 3
productRoute := newMockRoute("testproductroute")
itemRoute := newMockRoute("testitemroute")
viewRoute := newMockRoute("testviewroute")
params := make(context.Params, paramSize)

product := staticNode{name: "product", route: nil, maxParamsSize: uint8(paramSize)}
product.WithRoute(productRoute)

// Create a wildcard node for "{item}" under "product"
item := NewNode("{item}", product.MaxParamsSize())
item.WithRoute(itemRoute)

view := NewNode("view", product.MaxParamsSize()+1)
view.WithRoute(viewRoute)

// Build the tree structure
product.WithChildren(product.Tree().withNode(item).sort())
item.WithChildren(item.Tree().withNode(view).sort())
product.WithChildren(product.Tree().Compile())

// Create a static node for "product" with skip subpath functionality
productSkipSubpath := staticNode{name: "product", route: nil, maxParamsSize: uint8(paramSize)}
productSkipSubpath.WithRoute(productRoute)

itemSkipSubpath := NewNode("{item}", productSkipSubpath.MaxParamsSize()) // wildcardNode
itemSkipSubpath.WithRoute(itemRoute)
itemSkipSubpath.SkipSubPath()

// Build the tree structure
productSkipSubpath.WithChildren(productSkipSubpath.Tree().withNode(itemSkipSubpath).sort())
productSkipSubpath.WithChildren(productSkipSubpath.Tree().Compile())

tests := []struct {
name string
node staticNode
path string
expectedRoute Route
expectedParams context.Params
}{
{
name: "Exact Match",
node: product,
path: "product/item1",
expectedRoute: itemRoute,
expectedParams: append(params, context.Param{Key: "item", Value: "item1"}),
},
{
name: "Match with SubPath",
node: product,
path: "product/item1/view",
expectedRoute: viewRoute,
expectedParams: append(params, context.Param{Key: "item", Value: "item1"}),
},
{
name: "Exact Match with Skip SubPath",
node: productSkipSubpath,
path: "product/item1/order",
expectedRoute: itemRoute,
expectedParams: append(params, context.Param{Key: "item", Value: "item1"}),
},
{
name: "No Match with SubPath",
node: product,
path: "product/item1/order",
expectedRoute: nil,
expectedParams: nil,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
route, params := tt.node.MatchRoute(tt.path)
if route != tt.expectedRoute {
t.Errorf("%s: expected route %v, got %v", tt.name, tt.expectedRoute, route)
}
if !reflect.DeepEqual(params, tt.expectedParams) {
t.Errorf("%s: expected params %v, got %v", tt.name, tt.expectedParams, params)
}
})
Expand Down

0 comments on commit 8a3ec11

Please sign in to comment.