forked from fulldump/apitest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
response.go
79 lines (61 loc) · 1.27 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
package apitest
import (
"bytes"
"encoding/json"
"io"
"io/ioutil"
"net/http"
)
type Response struct {
http.Response
apitest *Apitest
client *http.Client
response_body_bytes []byte
request_body_bytes []byte
}
func (r *Response) BodyClose() {
io.Copy(ioutil.Discard, r.Body)
r.Body.Close()
}
func (r *Response) BodyRequestBytes() []byte {
return r.request_body_bytes
}
func (r *Response) BodyRequestString() string {
return string(r.BodyRequestBytes())
}
func (r *Response) BodyBytes() []byte {
if nil == r.response_body_bytes {
body, err := ioutil.ReadAll(r.Body)
r.response_body_bytes = body
if err != nil {
panic(err)
}
r.BodyClose()
}
return r.response_body_bytes
}
func (r *Response) BodyString() string {
return string(r.BodyBytes())
}
func (r *Response) BodyJson() interface{} {
b := bytes.NewBuffer(r.BodyBytes())
d := json.NewDecoder(b)
d.UseNumber()
var body interface{}
if err := d.Decode(&body); err != nil {
panic(err)
}
r.BodyClose()
return body
}
func (r *Response) BodyJsonMap() *map[string]interface{} {
b := bytes.NewBuffer(r.BodyBytes())
d := json.NewDecoder(b)
d.UseNumber()
body := map[string]interface{}{}
if err := d.Decode(&body); err != nil {
panic(err)
}
r.BodyClose()
return &body
}