forked from stellar/go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_expectation.go
60 lines (51 loc) · 1.54 KB
/
client_expectation.go
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
package httptest
import (
"net/http"
"github.com/jarcoal/httpmock"
"github.com/stellar/go/support/errors"
)
// Return specifies the response for a ClientExpectation, which is then
// committed to the connected mock client.
func (ce *ClientExpectation) Return(r httpmock.Responder) *ClientExpectation {
ce.Client.MockTransport.RegisterResponder(
ce.Method,
ce.URL,
r,
)
return ce
}
// ReturnError causes this expectation to resolve to an error.
func (ce *ClientExpectation) ReturnError(msg string) *ClientExpectation {
return ce.Return(func(*http.Request) (*http.Response, error) {
return nil, errors.New(msg)
})
}
// ReturnString causes this expectation to resolve to a string-based body with
// the provided status code.
func (ce *ClientExpectation) ReturnString(
status int,
body string,
) *ClientExpectation {
return ce.Return(
httpmock.NewStringResponder(status, body),
)
}
// ReturnText causes this expectation to resolve to a json-based body with the
// provided status code. Panics when the provided body cannot be encoded to
// JSON.
func (ce *ClientExpectation) ReturnJSON(
status int,
body interface{},
) *ClientExpectation {
r, err := httpmock.NewJsonResponder(status, body)
if err != nil {
panic(err)
}
return ce.Return(r)
}
// ReturnNotFound is a simple helper that causes this expectation to resolve to
// a 404 error. If a customized body is needed, use something like
// `ReturnString`` instead.
func (ce *ClientExpectation) ReturnNotFound() *ClientExpectation {
return ce.ReturnString(http.StatusNotFound, "not found")
}