Skip to content

Commit

Permalink
Merge pull request #90 from wigbam/matches-type-fix
Browse files Browse the repository at this point in the history
Fix "matches" handling of non-string types
  • Loading branch information
jekiapp authored May 18, 2022
2 parents 076e5cd + 5947aa0 commit 885ab10
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
15 changes: 11 additions & 4 deletions stub/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,18 @@ func deepEqual(expect, actual interface{}) bool {
}

func regexMatch(expect, actual interface{}) bool {
match, err := regexp.Match(expect.(string), []byte(actual.(string)))
if err != nil {
log.Printf("Error on matching regex %s with %s error:%v\n", expect, actual, err)
var expectedStr, expectedStringOk = expect.(string)
var actualStr, actualStringOk = actual.(string)

if expectedStringOk && actualStringOk {
match, err := regexp.Match(expectedStr, []byte(actualStr))
if err != nil {
log.Printf("Error on matching regex %s with %s error:%v\n", expect, actual, err)
}
return match
}
return match

return deepEqual(expect, actual)
}

func equals(expect, actual map[string]interface{}) bool {
Expand Down
16 changes: 9 additions & 7 deletions stub/stub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ func TestStub(t *testing.T) {
expect: "{\"data\":{\"reply\":\"OK\"},\"error\":\"\"}\n",
},
{
name: "add nested stub nested matches regex",
name: "add nested stub matches regex",
mock: func() *http.Request {
payload := `{
"service":"NestedTesting2",
Expand All @@ -235,11 +235,12 @@ func TestStub(t *testing.T) {
"matches":{
"key": "[a-z]{3}ue",
"greetings": {
"hola": "mundo",
"merhaba": "dunya",
"hola": 1,
"merhaba": true,
"hello": "^he[l]{2,}o$"
},
"cities": ["Istanbul", "Jakarta", ".*"]
"cities": ["Istanbul", "Jakarta", ".*"],
"mixed": [5.5, false, ".*"]
}
},
"output":{
Expand All @@ -262,11 +263,12 @@ func TestStub(t *testing.T) {
"data":{
"key": "value",
"greetings": {
"hola": "mundo",
"merhaba": "dunya",
"hola": 1,
"merhaba": true,
"hello": "helllllo"
},
"cities": ["Istanbul", "Jakarta", "Gotham"]
"cities": ["Istanbul", "Jakarta", "Gotham"],
"mixed": [5.5, false, "Gotham"]
}
}
}`
Expand Down

0 comments on commit 885ab10

Please sign in to comment.