summaryrefslogtreecommitdiff
path: root/vendor/github.com/onsi/gomega/matchers/have_http_body_matcher.go
blob: 6a3dcdc3533479083ab3a5d7d81aadf105e06de6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package matchers

import (
	"fmt"
	"net/http"
	"net/http/httptest"

	"github.com/onsi/gomega/format"
	"github.com/onsi/gomega/internal/gutil"
	"github.com/onsi/gomega/types"
)

type HaveHTTPBodyMatcher struct {
	Expected   interface{}
	cachedBody []byte
}

func (matcher *HaveHTTPBodyMatcher) Match(actual interface{}) (bool, error) {
	body, err := matcher.body(actual)
	if err != nil {
		return false, err
	}

	switch e := matcher.Expected.(type) {
	case string:
		return (&EqualMatcher{Expected: e}).Match(string(body))
	case []byte:
		return (&EqualMatcher{Expected: e}).Match(body)
	case types.GomegaMatcher:
		return e.Match(body)
	default:
		return false, fmt.Errorf("HaveHTTPBody matcher expects string, []byte, or GomegaMatcher. Got:\n%s", format.Object(matcher.Expected, 1))
	}
}

func (matcher *HaveHTTPBodyMatcher) FailureMessage(actual interface{}) (message string) {
	body, err := matcher.body(actual)
	if err != nil {
		return fmt.Sprintf("failed to read body: %s", err)
	}

	switch e := matcher.Expected.(type) {
	case string:
		return (&EqualMatcher{Expected: e}).FailureMessage(string(body))
	case []byte:
		return (&EqualMatcher{Expected: e}).FailureMessage(body)
	case types.GomegaMatcher:
		return e.FailureMessage(body)
	default:
		return fmt.Sprintf("HaveHTTPBody matcher expects string, []byte, or GomegaMatcher. Got:\n%s", format.Object(matcher.Expected, 1))
	}
}

func (matcher *HaveHTTPBodyMatcher) NegatedFailureMessage(actual interface{}) (message string) {
	body, err := matcher.body(actual)
	if err != nil {
		return fmt.Sprintf("failed to read body: %s", err)
	}

	switch e := matcher.Expected.(type) {
	case string:
		return (&EqualMatcher{Expected: e}).NegatedFailureMessage(string(body))
	case []byte:
		return (&EqualMatcher{Expected: e}).NegatedFailureMessage(body)
	case types.GomegaMatcher:
		return e.NegatedFailureMessage(body)
	default:
		return fmt.Sprintf("HaveHTTPBody matcher expects string, []byte, or GomegaMatcher. Got:\n%s", format.Object(matcher.Expected, 1))
	}
}

// body returns the body. It is cached because once we read it in Match()
// the Reader is closed and it is not readable again in FailureMessage()
// or NegatedFailureMessage()
func (matcher *HaveHTTPBodyMatcher) body(actual interface{}) ([]byte, error) {
	if matcher.cachedBody != nil {
		return matcher.cachedBody, nil
	}

	body := func(a *http.Response) ([]byte, error) {
		if a.Body != nil {
			defer a.Body.Close()
			var err error
			matcher.cachedBody, err = gutil.ReadAll(a.Body)
			if err != nil {
				return nil, fmt.Errorf("error reading response body: %w", err)
			}
		}
		return matcher.cachedBody, nil
	}

	switch a := actual.(type) {
	case *http.Response:
		return body(a)
	case *httptest.ResponseRecorder:
		return body(a.Result())
	default:
		return nil, fmt.Errorf("HaveHTTPBody matcher expects *http.Response or *httptest.ResponseRecorder. Got:\n%s", format.Object(actual, 1))
	}

}