-
Notifications
You must be signed in to change notification settings - Fork 6
/
responseoption.go
72 lines (59 loc) · 1.31 KB
/
responseoption.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
package aduket
import (
"encoding/json"
"encoding/xml"
"net/http"
"time"
)
type ResponseRuleOption func(*responseRule)
func StatusCode(statusCode int) ResponseRuleOption {
return func(r *responseRule) {
r.statusCode = statusCode
}
}
func JSONBody(body interface{}) ResponseRuleOption {
return func(r *responseRule) {
r.body = jsonToResponseBody(body)
}
}
func XMLBody(body interface{}) ResponseRuleOption {
return func(r *responseRule) {
r.body = xmlToResponseBody(body)
}
}
func StringBody(str string) ResponseRuleOption {
return func(r *responseRule) {
r.body = stringToResponseBody(str)
}
}
func ByteBody(b []byte) ResponseRuleOption {
return func(r *responseRule) {
r.body = b
}
}
func CorruptedBody() ResponseRuleOption {
return func(r *responseRule) {
r.sendCorruptedBody = true
}
}
func Header(header http.Header) ResponseRuleOption {
return func(r *responseRule) {
r.header = header
}
}
func Timeout(duration time.Duration) ResponseRuleOption {
return func(r *responseRule) {
r.timeout = duration
}
}
func jsonToResponseBody(j interface{}) responseBody {
jsonBytes, _ := json.Marshal(j)
return jsonBytes
}
func xmlToResponseBody(x interface{}) responseBody {
xmlBytes, _ := xml.Marshal(x)
return xmlBytes
}
func stringToResponseBody(s string) responseBody {
return []byte(s)
}