2016-02-25 22:59:38 +00:00
|
|
|
package pathmatch
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/fatih/structs"
|
|
|
|
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2019-06-21 04:57:03 +00:00
|
|
|
func TestFindAndLoad(t *testing.T) {
|
2016-02-25 22:59:38 +00:00
|
|
|
|
|
|
|
tests := []struct{
|
2019-06-21 06:28:50 +00:00
|
|
|
Pattern *Pattern
|
2016-02-25 22:59:38 +00:00
|
|
|
StructPtr interface{}
|
|
|
|
Path string
|
|
|
|
Expected map[string]string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
Pattern: MustCompile("/{this}/{that}/{these}/{those}"),
|
|
|
|
StructPtr: new(struct{
|
|
|
|
This string `match:"this"`
|
|
|
|
That string `match:"that"`
|
|
|
|
These string `match:"these"`
|
|
|
|
Those string `match:"those"`
|
|
|
|
}),
|
|
|
|
Path: "/apple/banana/cherry/grape",
|
|
|
|
Expected: map[string]string{
|
|
|
|
"This":"apple",
|
|
|
|
"That":"banana",
|
|
|
|
"These":"cherry",
|
|
|
|
"Those":"grape",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
Pattern: MustCompile("/user/{sessionKey}"),
|
|
|
|
StructPtr: new(struct{
|
|
|
|
SessionKey string `match:"sessionKey"`
|
|
|
|
}),
|
|
|
|
Path: "/user/76M6.mXQfgiGSC_YJ5uXSnWUmELbe8OgOm5n.iZ98Ij",
|
|
|
|
Expected: map[string]string{
|
|
|
|
"SessionKey":"76M6.mXQfgiGSC_YJ5uXSnWUmELbe8OgOm5n.iZ98Ij",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Pattern: MustCompile("/user/{sessionKey}/"),
|
|
|
|
StructPtr: new(struct{
|
|
|
|
SessionKey string `match:"sessionKey"`
|
|
|
|
}),
|
|
|
|
Path: "/user/76M6.mXQfgiGSC_YJ5uXSnWUmELbe8OgOm5n.iZ98Ij/",
|
|
|
|
Expected: map[string]string{
|
|
|
|
"SessionKey":"76M6.mXQfgiGSC_YJ5uXSnWUmELbe8OgOm5n.iZ98Ij",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
Pattern: MustCompile("/user/{sessionKey}/vehicle"),
|
|
|
|
StructPtr: new(struct{
|
|
|
|
SessionKey string `match:"sessionKey"`
|
|
|
|
}),
|
|
|
|
Path: "/user/76M6.mXQfgiGSC_YJ5uXSnWUmELbe8OgOm5n.iZ98Ij/vehicle",
|
|
|
|
Expected: map[string]string{
|
|
|
|
"SessionKey":"76M6.mXQfgiGSC_YJ5uXSnWUmELbe8OgOm5n.iZ98Ij",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Pattern: MustCompile("/user/{sessionKey}/vehicle/"),
|
|
|
|
StructPtr: new(struct{
|
|
|
|
SessionKey string `match:"sessionKey"`
|
|
|
|
}),
|
|
|
|
Path: "/user/76M6.mXQfgiGSC_YJ5uXSnWUmELbe8OgOm5n.iZ98Ij/vehicle/",
|
|
|
|
Expected: map[string]string{
|
|
|
|
"SessionKey":"76M6.mXQfgiGSC_YJ5uXSnWUmELbe8OgOm5n.iZ98Ij",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
Pattern: MustCompile("/user/{sessionKey}/vehicle/DEFAULT"),
|
|
|
|
StructPtr: new(struct{
|
|
|
|
SessionKey string `match:"sessionKey"`
|
|
|
|
}),
|
|
|
|
Path: "/user/76M6.mXQfgiGSC_YJ5uXSnWUmELbe8OgOm5n.iZ98Ij/vehicle/DEFAULT",
|
|
|
|
Expected: map[string]string{
|
|
|
|
"SessionKey":"76M6.mXQfgiGSC_YJ5uXSnWUmELbe8OgOm5n.iZ98Ij",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Pattern: MustCompile("/user/{sessionKey}/vehicle/DEFAULT/"),
|
|
|
|
StructPtr: new(struct{
|
|
|
|
SessionKey string `match:"sessionKey"`
|
|
|
|
}),
|
|
|
|
Path: "/user/76M6.mXQfgiGSC_YJ5uXSnWUmELbe8OgOm5n.iZ98Ij/vehicle/DEFAULT/",
|
|
|
|
Expected: map[string]string{
|
|
|
|
"SessionKey":"76M6.mXQfgiGSC_YJ5uXSnWUmELbe8OgOm5n.iZ98Ij",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
/*
|
|
|
|
|
|
|
|
{
|
|
|
|
Pattern: MustCompile("/user/{sessionKey}/vehicle/{vehicleIdcode}"),
|
|
|
|
Args: []interface{}{new(string), new(string), },
|
|
|
|
Path: "/user/76M6.mXQfgiGSC_YJ5uXSnWUmELbe8OgOm5n.iZ98Ij/vehicle/DEFAULT",
|
|
|
|
ExpectedArgs: []string{"76M6.mXQfgiGSC_YJ5uXSnWUmELbe8OgOm5n.iZ98Ij", "DEFAULT"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Pattern: MustCompile("/user/{sessionKey}/vehicle/{vehicleIdcode}/"),
|
|
|
|
Args: []interface{}{new(string), new(string), },
|
|
|
|
Path: "/user/76M6.mXQfgiGSC_YJ5uXSnWUmELbe8OgOm5n.iZ98Ij/vehicle/DEFAULT/",
|
|
|
|
ExpectedArgs: []string{"76M6.mXQfgiGSC_YJ5uXSnWUmELbe8OgOm5n.iZ98Ij", "DEFAULT"},
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
Pattern: MustCompile("/user/{sessionKey}/vehicle/{vehicleIdcode}"),
|
|
|
|
Args: []interface{}{new(string), new(string), },
|
|
|
|
Path: "/user/76M6.mXQfgiGSC_YJ5uXSnWUmELbe8OgOm5n.iZ98Ij/vehicle/N9Z_tiv7",
|
|
|
|
ExpectedArgs: []string{"76M6.mXQfgiGSC_YJ5uXSnWUmELbe8OgOm5n.iZ98Ij", "N9Z_tiv7"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Pattern: MustCompile("/user/{sessionKey}/vehicle/{vehicleIdcode}/"),
|
|
|
|
Args: []interface{}{new(string), new(string), },
|
|
|
|
Path: "/user/76M6.mXQfgiGSC_YJ5uXSnWUmELbe8OgOm5n.iZ98Ij/vehicle/N9Z_tiv7/",
|
|
|
|
ExpectedArgs: []string{"76M6.mXQfgiGSC_YJ5uXSnWUmELbe8OgOm5n.iZ98Ij", "N9Z_tiv7"},
|
|
|
|
},
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
for testNumber, test := range tests {
|
|
|
|
|
|
|
|
|
|
|
|
for structFieldName, structFieldValue := range structs.Map(test.StructPtr) {
|
|
|
|
structFieldValueString, ok := structFieldValue.(string)
|
|
|
|
if !ok {
|
|
|
|
t.Errorf("For test #%d, expected test.StructPtr.%s to be of type string, but actually was %T.", testNumber, structFieldName, structFieldValue)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if expected, actual := "", structFieldValueString; expected != actual {
|
|
|
|
t.Errorf("For test #%d, expected test.StructPtr.%s to (initially) be %q, but actually was %q.", testNumber, structFieldName, expected, actual)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-21 04:57:03 +00:00
|
|
|
if didMatch, err := test.Pattern.FindAndLoad(test.Path, test.StructPtr); nil != err {
|
2016-02-25 22:59:38 +00:00
|
|
|
t.Errorf("For test #%d, did not expected an error, but actually got one: %v", testNumber, err)
|
|
|
|
continue
|
|
|
|
} else if !didMatch {
|
|
|
|
t.Errorf("For test #%d, expected path to match pattern, but it didn't.", testNumber)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for structFieldName, structFieldValue := range structs.Map(test.StructPtr) {
|
|
|
|
structFieldValueString, ok := structFieldValue.(string)
|
|
|
|
if !ok {
|
|
|
|
t.Errorf("For test #%d, expected test.StructPtr.%s to be of type string, but actually was %T.", testNumber, structFieldName, structFieldValue)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if expected, actual := test.Expected[structFieldName], structFieldValueString; expected != actual {
|
|
|
|
t.Errorf("For test #%d, expected test.StructPtr.%s to (initially) be %q, but actually was %q.", testNumber, structFieldName, expected, actual)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|