This repository has been archived by the owner on Oct 6, 2021. It is now read-only.
forked from jarcoal/httpmock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresponse.go
134 lines (118 loc) · 3.94 KB
/
response.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
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package httpmock
import (
"bytes"
"encoding/json"
"encoding/xml"
"io"
"net/http"
"strconv"
"strings"
)
// ResponderFromResponse wraps an *http.Response in a Responder
func ResponderFromResponse(resp *http.Response) Responder {
return func(req *http.Request) (*http.Response, error) {
res := new(http.Response)
*res = *resp
res.Request = req
return res, nil
}
}
// NewErrorResponder creates a Responder that returns an empty request and the
// given error. This can be used to e.g. imitate more deep http errors for the
// client.
func NewErrorResponder(err error) Responder {
return func(req *http.Request) (*http.Response, error) {
return nil, err
}
}
// NewStringResponse creates an *http.Response with a body based on the given string. Also accepts
// an http status code.
func NewStringResponse(status int, body string) *http.Response {
return &http.Response{
Status: strconv.Itoa(status),
StatusCode: status,
Body: NewRespBodyFromString(body),
Header: http.Header{},
}
}
// NewStringResponder creates a Responder from a given body (as a string) and status code.
func NewStringResponder(status int, body string) Responder {
return ResponderFromResponse(NewStringResponse(status, body))
}
// NewBytesResponse creates an *http.Response with a body based on the given bytes. Also accepts
// an http status code.
func NewBytesResponse(status int, body []byte) *http.Response {
return &http.Response{
Status: strconv.Itoa(status),
StatusCode: status,
Body: NewRespBodyFromBytes(body),
Header: http.Header{},
}
}
// NewBytesResponder creates a Responder from a given body (as a byte slice) and status code.
func NewBytesResponder(status int, body []byte) Responder {
return ResponderFromResponse(NewBytesResponse(status, body))
}
// NewJsonResponse creates an *http.Response with a body that is a json encoded representation of
// the given interface{}. Also accepts an http status code.
func NewJsonResponse(status int, body interface{}) (*http.Response, error) {
encoded, err := json.Marshal(body)
if err != nil {
return nil, err
}
response := NewBytesResponse(status, encoded)
response.Header.Set("Content-Type", "application/json")
return response, nil
}
// NewJsonResponder creates a Responder from a given body (as an interface{} that is encoded to
// json) and status code.
func NewJsonResponder(status int, body interface{}) (Responder, error) {
resp, err := NewJsonResponse(status, body)
if err != nil {
return nil, err
}
return ResponderFromResponse(resp), nil
}
// NewXmlResponse creates an *http.Response with a body that is an xml encoded representation
// of the given interface{}. Also accepts an http status code.
func NewXmlResponse(status int, body interface{}) (*http.Response, error) {
encoded, err := xml.Marshal(body)
if err != nil {
return nil, err
}
response := NewBytesResponse(status, encoded)
response.Header.Set("Content-Type", "application/xml")
return response, nil
}
// NewXmlResponder creates a Responder from a given body (as an interface{} that is encoded to xml)
// and status code.
func NewXmlResponder(status int, body interface{}) (Responder, error) {
resp, err := NewXmlResponse(status, body)
if err != nil {
return nil, err
}
return ResponderFromResponse(resp), nil
}
// NewRespBodyFromString creates an io.ReadCloser from a string that is suitable for use as an
// http response body.
func NewRespBodyFromString(body string) io.ReadCloser {
return &dummyReadCloser{strings.NewReader(body)}
}
// NewRespBodyFromBytes creates an io.ReadCloser from a byte slice that is suitable for use as an
// http response body.
func NewRespBodyFromBytes(body []byte) io.ReadCloser {
return &dummyReadCloser{bytes.NewReader(body)}
}
type dummyReadCloser struct {
body io.ReadSeeker
}
func (d *dummyReadCloser) Read(p []byte) (n int, err error) {
n, err = d.body.Read(p)
if err == io.EOF {
d.body.Seek(0, 0)
}
return n, err
}
func (d *dummyReadCloser) Close() error {
return nil
}