Beispiel #1
0
func bodyMatches(expected, actual io.Reader) (bool, diff.Differences, error) {
	if expected == nil {
		return true, nil, nil
	}

	var e, a interface{}
	decoder := json.NewDecoder(expected)
	err := decoder.Decode(&e)
	if err != nil {
		return false, nil, err
	}

	if actual != nil {
		decoder = json.NewDecoder(actual)
		err = decoder.Decode(&a)
		if err != nil {
			return false, nil, err
		}
	}

	if result, diffs := diff.DeepDiff(e, a, &diff.DiffConfig{AllowUnexpectedKeys: true, RootPath: "[\"body\"]"}); result {
		return result, nil, nil
	} else {
		return result, diffs, nil
	}
}
Beispiel #2
0
func bodyMatches(expected, actual interface{}, allowUnexpectedKeys bool, expectedBody bool) (bool, diff.Differences, error) {
	if expected == nil && !expectedBody {
		return true, nil, nil
	}
	if result, diffs := diff.DeepDiff(expected, actual, &diff.DiffConfig{AllowUnexpectedKeys: allowUnexpectedKeys, RootPath: "[\"body\"]"}); result {
		return result, nil, nil
	} else {
		return result, diffs, nil
	}
}
func MatchResponse(expected, actual *provider.Response) (diff.Differences, error) {
	diffs := make(diff.Differences, 0)

	if res, sDiff := diff.DeepDiff(expected.Status, actual.Status,
		&diff.DiffConfig{AllowUnexpectedKeys: true, RootPath: "[\"status\"]"}); !res {
		diffs = append(diffs, sDiff...)
	} else if res, hDiff := headerMatches(expected.Headers, actual.Headers); !res {
		diffs = append(diffs, hDiff...)
	} else if res, bDiff, err := bodyMatches(expected.GetBody(), actual.GetBody(), true, expected.BodyHasToBeSerialized()); err != nil {
		return nil, err
	} else if !res {
		diffs = append(diffs, bDiff...)
	}

	return diffs, nil
}
func headerMatches(expected, actual map[string][]string) (bool, diff.Differences) {
	if expected == nil {
		return true, nil
	}

	normalisedExpected := make(map[string][]string, len(expected))
	for key, val := range expected {
		normalisedExpected[strings.ToLower(key)] = val
	}
	var normalisedActual map[string][]string
	if actual != nil {
		normalisedActual = make(map[string][]string)
		for key, val := range actual {
			normalisedActual[strings.ToLower(key)] = val
		}
	}

	return diff.DeepDiff(normalisedExpected, normalisedActual, &diff.DiffConfig{AllowUnexpectedKeys: true, RootPath: "[\"header\"]"})
}
func MatchResponse(expected *provider.Response, actual *http.Response) (diff.Differences, error) {
	diffs := make(diff.Differences, 0)

	b, err := expected.GetData()
	if err != nil {
		return nil, err
	}
	var expBody io.Reader
	if len(b) > 0 {
		expBody = bytes.NewReader(b)
	}
	if res, sDiff := diff.DeepDiff(expected.Status, actual.StatusCode,
		&diff.DiffConfig{AllowUnexpectedKeys: true, RootPath: "[\"status\"]"}); !res {
		diffs = append(diffs, sDiff...)
	} else if res, hDiff := headerMatches(expected.Headers, actual.Header); !res {
		diffs = append(diffs, hDiff...)
	} else if res, bDiff, err := bodyMatches(expBody, actual.Body); err != nil {
		return nil, err
	} else if !res {
		diffs = append(diffs, bDiff...)
	}

	return diffs, nil
}
Beispiel #6
0
func queryMatches(expected, actual url.Values) (bool, diff.Differences) {
	return diff.DeepDiff(expected, actual, &diff.DiffConfig{AllowUnexpectedKeys: false})
}
func headerMatches(expected, actual map[string][]string) (bool, diff.Differences) {
	if expected == nil {
		return true, nil
	}
	return diff.DeepDiff(expected, actual, &diff.DiffConfig{AllowUnexpectedKeys: true, RootPath: "[\"header\"]"})
}