mirror of
https://github.com/optim-enterprises-bv/databunker.git
synced 2025-11-03 11:27:47 +00:00
fix stringPatternMatch() and create test
This commit is contained in:
@@ -259,16 +259,16 @@ func stringPatternMatch(pattern string, value string) bool {
|
||||
if strings.Contains(value, pattern) {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
if strings.HasPrefix(pattern, "*") {
|
||||
pattern = pattern[1:]
|
||||
if strings.Contains(value, pattern) {
|
||||
if strings.HasSuffix(value, pattern) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
if strings.HasSuffix(pattern, "*") {
|
||||
} else if strings.HasSuffix(pattern, "*") {
|
||||
pattern = pattern[:len(pattern)-1]
|
||||
if strings.Contains(value, pattern) {
|
||||
if strings.HasPrefix(value, pattern) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,6 +36,24 @@ func Test_AppNames(t *testing.T) {
|
||||
|
||||
}
|
||||
|
||||
func Test_stringPatternMatch(t *testing.T) {
|
||||
goodJsons := []map[string]interface{}{
|
||||
{"pattern":"*","name": "tom", "result": true},
|
||||
{"pattern":"aa","name": "tom", "result": false},
|
||||
{"pattern":"","name":"aa","result": false},
|
||||
{"pattern":"test*","name":"123testabc", "result": false},
|
||||
{"pattern":"test*","name":"testabc", "result": true},
|
||||
{"pattern":"*test*","name":"test1", "result": true},
|
||||
{"pattern":"*test","name":"123testabc", "result": false},
|
||||
{"pattern":"*test","name":"123test", "result": true},
|
||||
}
|
||||
for _, value := range goodJsons {
|
||||
if stringPatternMatch(value["pattern"].(string), value["name"].(string)) != value["result"].(bool) {
|
||||
t.Fatalf("Failed in %s match %s\n", value["pattern"].(string), value["name"].(string))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func Test_getJSONPost(t *testing.T) {
|
||||
goodJsons := []string{
|
||||
`{"login":"abc","name": "tom", "pass": "mylittlepony", "admin": true}`,
|
||||
|
||||
Reference in New Issue
Block a user