forked from stripe/stripe-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
serializer.go
64 lines (51 loc) · 1.34 KB
/
serializer.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
// Serialize, deserialize and store HTTP responses and requests.
package playback
import (
"io/ioutil"
"net/http"
"net/url"
)
type serializer interface {
serializeReq(httpRequest) (interface{}, error)
serializeResp(httpResponse) (interface{}, error)
EncodeCassette(Cassette) ([]byte, error)
DecodeCassette([]byte) (Cassette, error)
}
type httpRequest struct {
Method string
Body []byte
Headers http.Header
URL url.URL
}
type httpResponse struct {
Headers http.Header
Body []byte
StatusCode int
}
func newHTTPResponse(resp *http.Response) (wrappedResponse httpResponse, err error) {
wrappedResponse = httpResponse{}
wrappedResponse.Headers = resp.Header
wrappedResponse.StatusCode = resp.StatusCode
var bodyBytes []byte
bodyBytes, err = ioutil.ReadAll(resp.Body)
defer resp.Body.Close()
if err != nil {
return wrappedResponse, err
}
wrappedResponse.Body = bodyBytes
return wrappedResponse, nil
}
func newHTTPRequest(req *http.Request) (wrappedRequest httpRequest, err error) {
wrappedRequest = httpRequest{}
wrappedRequest.Method = req.Method
wrappedRequest.Headers = req.Header
wrappedRequest.URL = *req.URL
var bodyBytes []byte
bodyBytes, err = ioutil.ReadAll(req.Body)
defer req.Body.Close()
if err != nil {
return wrappedRequest, err
}
wrappedRequest.Body = bodyBytes
return wrappedRequest, nil
}