示例#1
0
func (v *pactValidator) validateInteraction(i *consumer.Interaction) (bool, error) {
	req, err := i.ToHTTPRequest(v.u.String())
	if err != nil {
		return false, err
	}
	resp, err := v.c.Do(req)
	if resp != nil && resp.Body != nil {
		defer resp.Body.Close()
	}

	if err != nil {
		return false, err
	}

	providerResponse, err := provider.CreateResponseFromHTTPResponse(resp)
	if err != nil {
		return false, err
	}

	if diffs, err := comparers.MatchResponse(i.Response, providerResponse); err != nil {
		return false, err
	} else if len(diffs) > 0 {
		diff.FormatDiff(diffs, v.l, fmt.Sprintf("The response for state '%s' did not match, the differences are below:", i.State))
		return false, nil
	}
	return true, nil
}
func Test_MatchResponse_Scenarios(t *testing.T) {
	for _, test := range matchResponseTestData {
		providerResponse, err := provider.CreateResponseFromHTTPResponse(test.act)
		if err != nil {
			t.Error(err)
			t.FailNow()
		}
		diff, err := MatchResponse(test.exp, providerResponse)
		if err != nil {
			t.Error(err)
		}
		if len(diff) != test.diffCount {
			t.Errorf("expected diff count to be %d, but actual count id %d", test.diffCount, len(diff))
		} else if len(diff) > 0 && !strings.Contains(diff.Error(), test.diffPath) {
			t.Errorf("expected diff at %s, but got %s", test.diffPath, diff.Error())
		}
	}
}