Esempio n. 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 testResponseCase(t *testing.T, fileName string) {
	data, err := ioutil.ReadFile("./" + fileName)
	if err != nil {
		t.Error(err)
		t.FailNow()
	}
	tc := &ResponseTestCase{}
	if err := json.Unmarshal(data, tc); err != nil {
		t.Error(err)
		t.FailNow()
	}

	diffs, err := comparers.MatchResponse(tc.Expected, tc.Actual)
	match := (len(diffs) == 0)
	if err != nil {
		t.Error(err)
	} else if match != tc.Match {
		t.Error(tc.Comment)
	}
}