diff --git a/match/tester.go b/match/tester.go index cdcaa58..d81244b 100644 --- a/match/tester.go +++ b/match/tester.go @@ -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 { @@ -140,27 +132,27 @@ 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) { @@ -168,11 +160,11 @@ func (mm Tester) Check(req *definition.Request, mock *definition.Mock, scenarioA } 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) { @@ -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, ", "); +} \ No newline at end of file