Skip to content

Commit

Permalink
Improve logs shown for discarded mocks (#68)
Browse files Browse the repository at this point in the history
* Improve logs shown for discarded mocks

* Changes from cr done
  • Loading branch information
estermmad authored and jmartin82 committed Oct 8, 2018
1 parent 9e99dc1 commit 89fb40e
Showing 1 changed file with 24 additions and 18 deletions.
42 changes: 24 additions & 18 deletions match/tester.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,17 @@ package match
import (
"errors"
"strings"

"fmt"
"github.com/jmartin82/mmock/scenario"

urlmatcher "github.com/azer/url-router"
"github.com/jmartin82/mmock/definition"
"github.com/ryanuber/go-glob"
)

var (
ErrHostNotMatch = errors.New("Host not match")
ErrSchemeNotMatch = errors.New("Scheme not match")
ErrFragmentNotMatch = errors.New("Fragment not match")
ErrMethodNotMatch = errors.New("Method not match")
ErrPathNotMatch = errors.New("Path not match")
ErrQueryStringMatch = errors.New("Query string not match")
ErrHeadersNotMatch = errors.New("Headers not match")
ErrCookiesNotMatch = errors.New("Cookies not match")
ErrBodyNotMatch = errors.New("Body not match")
ErrScenarioNotMatch = errors.New("Scenario state not match")
ErrPathNotMatch = errors.New("Path not match")
)

func NewTester(scenario scenario.Director) *Tester {
Expand Down Expand Up @@ -140,39 +132,39 @@ func (mm Tester) Check(req *definition.Request, mock *definition.Mock, scenarioA
routes := urlmatcher.New(mock.Request.Path)

if !mm.matchOnEqualsOrIfEmptyOrGlob(req.Host, mock.Request.Host) {
return false, ErrHostNotMatch
return false, fmt.Errorf("Host not match. Actual: %s, Expected: %s", req.Host, mock.Request.Host)
}

if !mm.matchOnEqualsOrIfEmpty(req.Scheme, mock.Request.Scheme) {
return false, ErrSchemeNotMatch
return false, fmt.Errorf("Scheme not match. Actual: %s, Expected: %s", req.Scheme, mock.Request.Scheme)
}

if !mm.matchOnEqualsOrIfEmpty(req.Fragment, mock.Request.Fragment) {
return false, ErrFragmentNotMatch
return false, fmt.Errorf("Fragment not match. Actual: %s, Expected: %s", req.Fragment, mock.Request.Fragment)
}

if !glob.Glob(mock.Request.Path, req.Path) && routes.Match(req.Path) == nil {
return false, ErrPathNotMatch
return false, fmt.Errorf("Path not match. Actual: %s, Expected: %s", req.Path, mock.Request.Path)
}

if !mm.mockIncludesMethod(req.Method, &mock.Request) {
return false, ErrMethodNotMatch
return false, fmt.Errorf("Method not match. Actual: %s, Expected: %s", req.Method, mock.Request.Method)
}

if !mm.matchKeyAndValues(req.QueryStringParameters, mock.Request.QueryStringParameters) {
return false, ErrQueryStringMatch
return false, fmt.Errorf("Query string not match. Actual: %s, Expected: %s", mm.ValuesToString(req.QueryStringParameters), mm.ValuesToString(mock.Request.QueryStringParameters))
}

if !mm.matchKeyAndValue(req.Cookies, mock.Request.Cookies) {
return false, ErrCookiesNotMatch
}

if !mm.matchKeyAndValues(req.Headers, mock.Request.Headers) {
return false, ErrHeadersNotMatch
return false, fmt.Errorf("Headers not match. Actual: %s, Expected: %s", req.Headers, mock.Request.Headers)
}

if len(mock.Request.Body) > 0 && !glob.Glob(mock.Request.Body, req.Body) {
return false, ErrBodyNotMatch
return false, fmt.Errorf("Body not match. Actual: %s, Expected: %s", req.Body, mock.Request.Body)
}

if scenarioAware && !mm.matchScenarioState(&mock.Control.Scenario) {
Expand All @@ -181,3 +173,17 @@ func (mm Tester) Check(req *definition.Request, mock *definition.Mock, scenarioA

return true, nil
}


func (mm Tester) ValuesToString(values definition.Values) (string) {
var valuesStr [] string

for name, value := range values {
name = strings.ToLower(name)
for _, h := range value {
valuesStr = append(valuesStr, fmt.Sprintf("%v: %v", name, h))
}
}

return strings.Join(valuesStr, ", ");
}

0 comments on commit 89fb40e

Please sign in to comment.