Skip to content

Commit

Permalink
Merge branch 'master' of github.com:jmartin82/mmock
Browse files Browse the repository at this point in the history
  • Loading branch information
jcdietrich committed Apr 10, 2024
2 parents 22afc79 + 66005a5 commit 06db2bc
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 18 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ coverage.txt
dist
.vscode
.idea
.github/token
2 changes: 1 addition & 1 deletion .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,4 @@ changelog:
- typo
- rollback
env_files:
github_token: .github
github_token: .github/token
29 changes: 12 additions & 17 deletions pkg/match/payload/json_comparator.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package payload
import (
"encoding/json"
"fmt"
"github.com/jmartin82/mmock/v3/internal/config/logger"
"reflect"
"regexp"
"strings"

"github.com/jmartin82/mmock/v3/internal/config/logger"
)

var log = logger.Log
Expand All @@ -22,7 +23,7 @@ func isArray(s string) bool {
func (jc *JSONComparator) doCompareJSONRegexUnmarshaled(patterns, values map[string]interface{}) bool {
var matches bool
matches = jc.match(patterns, values)
if !matches{
if !matches {
log.Debugf("values: %v don't match: %v", values, patterns)
}
return matches
Expand Down Expand Up @@ -63,8 +64,8 @@ func (jc *JSONComparator) doCompareArrayRegexUnmarshaled(patterns, values []map[

for i := 0; i < len(patterns); i++ {
if !jc.match(patterns[i], values[i]) {
log.Debugf("value %v doesn't match %v",
values[i], patterns[i])
log.Debugf("value %v doesn't match %v",
values[i], patterns[i])
return false
}
}
Expand Down Expand Up @@ -93,7 +94,7 @@ func (jc *JSONComparator) match(p, v map[string]interface{}) bool {
patternType = reflect.ValueOf(pattern).Kind()

if valueType == reflect.Map && patternType == reflect.Map {
log.Debugf("recursing into map %v", field)
log.Debugf("recursing into map %v", field)

result = jc.doCompareJSONRegexUnmarshaled(
pattern.(map[string]interface{}),
Expand All @@ -105,13 +106,13 @@ func (jc *JSONComparator) match(p, v map[string]interface{}) bool {
} else if (valueType == reflect.Array || valueType == reflect.Slice) &&
(patternType == reflect.Array || patternType == reflect.Slice) {

log.Debugf("recursing into array %v", field)
log.Debugf("recursing into array %v", field)
valueJsonBytes, err1 := json.Marshal(value)
patternJsonBytes, err2 := json.Marshal(pattern)

if err1 != nil || err2 != nil {
log.Errorf("value %v raised %v and pattern %v raised %v",
value, err1, pattern, err2)
log.Errorf("value %v raised %v and pattern %v raised %v",
value, err1, pattern, err2)
return false
}

Expand All @@ -121,15 +122,9 @@ func (jc *JSONComparator) match(p, v map[string]interface{}) bool {
if !result {
return false
}
} else {
var eql bool
eql = reflect.DeepEqual(pattern, value)

if !eql {
log.Debugf("value %v doesn't DeepEqual %v", value, pattern)
}

return eql
} else if eql := reflect.DeepEqual(pattern, value); !eql {
log.Debugf("value %v doesn't DeepEqual %v", value, pattern)
return false
}
}
matched, err := regexp.MatchString(str, fmt.Sprint(value))
Expand Down
1 change: 1 addition & 0 deletions pkg/match/payload/json_comparator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func TestJSONComparator_Compare(t *testing.T) {
{"Test different arrays", args{"[{\"name\":\"john\",\"age\":30}]", "[{\"age\":30,\"name\":\"bob\"}]"}, false},
{"Test different format", args{"{\"name\":\"bob\",\"age\":30}", "{\"name\" : \"bob\"\n,\"age\" : 30}"}, true},
{"Test different values", args{"{\"name\":\"bobs\",\"age\":30}", "{\"name\":\"bob\",\"age\":30}"}, false},
{"Test different values - Regression JSON check", args{"{\"age\":30,\"name\":\"Name 1\"}", "{\"age\":30,\"name\":\"Name 2\"}"}, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit 06db2bc

Please sign in to comment.