fupie/ws/route/pattern_test.go

57 lines
1.5 KiB
Go

package route_test
import (
"testing"
"github.com/stretchr/testify/assert"
"sectorinf.com/emilis/fupie/ws/route"
)
func TestPattern(t *testing.T) {
tests := map[string]struct {
patternInput string
testPaths map[string]bool
}{
"wildcard in middle of path": {
patternInput: "/hello/*/bye",
testPaths: map[string]bool{
"/hello/hi/bye": true,
"/hello/no/bye": true,
"/hello/something/bye/something_else": false,
"/random/nothing/bye": false,
},
},
"wildcard within word": {
patternInput: "/hello/seeya-*/bye",
testPaths: map[string]bool{
"/hello/seeya-hi/bye": true,
"/hello/seeya-no/bye": true,
"/hello/hi/bye": false,
"/hello/no/bye": false,
"/hello/something/bye/something_else": false,
"/random/nothing/bye": false,
"/hello/seeya-/bye": true,
},
},
"root /": {
patternInput: "/",
testPaths: map[string]bool{
"": true,
"/": true,
"/hello/something/bye/something_else": false,
"/random/nothing/bye": false,
},
},
}
for name, test := range tests {
test := test
t.Run(name, func(tt *testing.T) {
p := route.Pattern(test.patternInput)
for path, expected := range test.testPaths {
assert.Equal(t, expected, p.Match(path), path)
}
})
}
}