Skip to content

Commit

Permalink
Merge pull request #67 from WheeskyJack/WheeskyJack-patch-1a
Browse files Browse the repository at this point in the history
Update node_test.go with TestStaticNodeMatchMiddleware test case
  • Loading branch information
vardius authored Aug 27, 2024
2 parents cddd74a + bf09e9f commit 5976a56
Showing 1 changed file with 102 additions and 0 deletions.
102 changes: 102 additions & 0 deletions mux/node_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package mux

import (
"net/http"
"reflect"
"testing"

"github.com/vardius/gorouter/v4/context"
"github.com/vardius/gorouter/v4/middleware"
)

func TestNewNode(t *testing.T) {
Expand Down Expand Up @@ -302,3 +304,103 @@ func TestRegexpdNodeMatchRoute(t *testing.T) {
})
}
}

func buildMockMiddlewareFunc(body string) middleware.Middleware {
fn := func(h middleware.Handler) middleware.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if _, err := w.Write([]byte(body)); err != nil {
panic(err)
}
h.(http.Handler).ServeHTTP(w, r)
})
}

return middleware.WrapperFunc(fn)
}

type middlewareTest struct {
name string
node Node
path string
expectedResult middleware.Collection
}

func TestStaticNodeMatchMiddleware(t *testing.T) {
paramSize := 3
middleware1 := buildMockMiddlewareFunc("1")
middleware2 := buildMockMiddlewareFunc("2")
middleware3 := buildMockMiddlewareFunc("3")
allMiddleware := middleware.NewCollection(middleware1, middleware2, middleware3)

node1 := NewNode("test", uint8(paramSize))
node1.PrependMiddleware(allMiddleware)

node2 := NewNode("test", uint8(paramSize))
node2.PrependMiddleware(allMiddleware)
node2.SkipSubPath()

node3 := NewNode("test", uint8(paramSize))
node3Middleware := middleware.NewCollection(middleware1, middleware2)
node3.PrependMiddleware(node3Middleware)
subpathNode := NewNode("subpath", node3.MaxParamsSize())
subpathMiddleware := middleware.NewCollection(middleware3)
subpathNode.PrependMiddleware(subpathMiddleware)
node3.WithChildren(node3.Tree().withNode(subpathNode).sort())
node3.WithChildren(node3.Tree().Compile())

tests := []middlewareTest{
{
name: "StaticNode Exact match",
node: node1,
path: "test",
expectedResult: allMiddleware,
},
{
name: "StaticNode Subpath match with skipSubPath",
node: node2,
path: "test/subpath",
expectedResult: allMiddleware,
},
{
name: "StaticNode Subpath match without skipSubPath",
node: node3,
path: "test/subpath",
expectedResult: allMiddleware,
},
{
name: "StaticNode Match with only prefix",
node: node3,
path: "testxyz",
expectedResult: node3Middleware,
},
{
name: "StaticNode No match",
node: node1,
path: "nomatch",
expectedResult: nil,
},
}

runMiddlewareTests(tests, t)
}

func runMiddlewareTests(tests []middlewareTest, t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := tt.node.MatchMiddleware(tt.path)
if len(got) != len(tt.expectedResult) {
t.Errorf("%s: middleware length mismatch: got= %v, want %v", tt.name, got, tt.expectedResult)
}
for k, v := range tt.expectedResult {
// reflect.DeepEqual do not work for function values.
// hence compare the pointers of functions as a substitute.
// function pointers are unique to each function, even if the functions have the same code.
expectedPointer := reflect.ValueOf(v).Pointer()
gotPointer := reflect.ValueOf(got[k]).Pointer()
if expectedPointer != gotPointer {
t.Errorf("%s: middleware mismatch: got= %v, want %v", tt.name, v, got[k])
}
}
})
}
}

0 comments on commit 5976a56

Please sign in to comment.