Example #1
0
// MakeRequestList makes a request for a list of JSON objects, checks the http response code, and
// returns the object list as []map[string]interface{}
func MakeRequestList(method, url string, expectedCode int) ([]map[string]interface{}, *http.Response) {
	// make the request
	respBody, resp := MakeRequest(method, url, "", expectedCode)
	if respBody == "" {
		return nil, resp
	}
	// here we assume that errors return a text/plain body
	if resp.StatusCode >= 400 {
		gomega.Ω(resp.Header.Get("Content-Type")).Should(gomega.HavePrefix("text/plain"))
		return nil, resp
	}
	// parse json into an array of maps
	gomega.Ω(resp.Header.Get("Content-Type")).Should(gomega.HavePrefix("application/json"))
	var res []map[string]interface{}
	err := json.Unmarshal([]byte(respBody), &res)
	gomega.Ω(err).ShouldNot(gomega.HaveOccurred())
	return res, resp
}
Example #2
0
func matcherToGomegaMatcher(matcher interface{}) (types.GomegaMatcher, error) {
	switch x := matcher.(type) {
	case string, int, bool, float64:
		return gomega.Equal(x), nil
	case []interface{}:
		var matchers []types.GomegaMatcher
		for _, valueI := range x {
			if subMatcher, ok := valueI.(types.GomegaMatcher); ok {
				matchers = append(matchers, subMatcher)
			} else {
				matchers = append(matchers, gomega.ContainElement(valueI))
			}
		}
		return gomega.And(matchers...), nil
	}
	matcher = sanitizeExpectedValue(matcher)
	if matcher == nil {
		return nil, fmt.Errorf("Missing Required Attribute")
	}
	matcherMap, ok := matcher.(map[string]interface{})
	if !ok {
		panic(fmt.Sprintf("Unexpected matcher type: %T\n\n", matcher))
	}
	var matchType string
	var value interface{}
	for matchType, value = range matcherMap {
		break
	}
	switch matchType {
	case "have-prefix":
		return gomega.HavePrefix(value.(string)), nil
	case "have-suffix":
		return gomega.HaveSuffix(value.(string)), nil
	case "match-regexp":
		return gomega.MatchRegexp(value.(string)), nil
	case "have-len":
		value = sanitizeExpectedValue(value)
		return gomega.HaveLen(value.(int)), nil
	case "contain-element":
		subMatcher, err := matcherToGomegaMatcher(value)
		if err != nil {
			return nil, err
		}
		return gomega.ContainElement(subMatcher), nil
	case "not":
		subMatcher, err := matcherToGomegaMatcher(value)
		if err != nil {
			return nil, err
		}
		return gomega.Not(subMatcher), nil
	case "consist-of":
		subMatchers, err := sliceToGomega(value)
		if err != nil {
			return nil, err
		}
		var interfaceSlice []interface{}
		for _, d := range subMatchers {
			interfaceSlice = append(interfaceSlice, d)
		}
		return gomega.ConsistOf(interfaceSlice...), nil
	case "and":
		subMatchers, err := sliceToGomega(value)
		if err != nil {
			return nil, err
		}
		return gomega.And(subMatchers...), nil
	case "or":
		subMatchers, err := sliceToGomega(value)
		if err != nil {
			return nil, err
		}
		return gomega.Or(subMatchers...), nil
	case "gt", "ge", "lt", "le":
		// Golang json escapes '>', '<' symbols, so we use 'gt', 'le' instead
		comparator := map[string]string{
			"gt": ">",
			"ge": ">=",
			"lt": "<",
			"le": "<=",
		}[matchType]
		return gomega.BeNumerically(comparator, value), nil

	default:
		return nil, fmt.Errorf("Unknown matcher: %s", matchType)

	}
}
Example #3
0
		in:   `{"ge": 1}`,
		want: gomega.BeNumerically(">=", float64(1)),
	},
	{
		in:   `{"lt": 1}`,
		want: gomega.BeNumerically("<", float64(1)),
	},
	{
		in:   `{"le": 1}`,
		want: gomega.BeNumerically("<=", float64(1)),
	},

	// String
	{
		in:   `{"have-prefix": "foo"}`,
		want: gomega.HavePrefix("foo"),
	},
	{
		in:   `{"have-suffix": "foo"}`,
		want: gomega.HaveSuffix("foo"),
	},
	// Regex support is based on golangs regex engine https://golang.org/pkg/regexp/syntax/
	{
		in:   `{"match-regexp": "foo"}`,
		want: gomega.MatchRegexp("foo"),
	},

	// Collection
	{
		in:   `{"consist-of": ["foo"]}`,
		want: gomega.ConsistOf(gomega.Equal("foo")),