-
Notifications
You must be signed in to change notification settings - Fork 19
/
response.go
167 lines (134 loc) · 3.68 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package wiremock
import (
"net/http"
"time"
)
type Fault string
const (
FaultEmptyResponse Fault = "EMPTY_RESPONSE"
FaultMalformedResponseChunk Fault = "MALFORMED_RESPONSE_CHUNK"
FaultRandomDataThenClose Fault = "RANDOM_DATA_THEN_CLOSE"
FaultConnectionResetByPeer Fault = "CONNECTION_RESET_BY_PEER"
)
type ResponseInterface interface {
ParseResponse() map[string]interface{}
}
type Response struct {
body *string
base64Body []byte
bodyFileName *string
jsonBody interface{}
headers map[string]string
status int64
delayDistribution DelayInterface
chunkedDribbleDelay *chunkedDribbleDelay
fault *Fault
}
func NewResponse() Response {
return Response{
status: http.StatusOK,
}
}
func OK() Response {
return NewResponse().WithStatus(http.StatusOK)
}
// WithLogNormalRandomDelay sets log normal random delay for response
func (r Response) WithLogNormalRandomDelay(median time.Duration, sigma float64) Response {
r.delayDistribution = NewLogNormalRandomDelay(median, sigma)
return r
}
// WithUniformRandomDelay sets uniform random delay for response
func (r Response) WithUniformRandomDelay(lower, upper time.Duration) Response {
r.delayDistribution = NewUniformRandomDelay(lower, upper)
return r
}
// WithFixedDelay sets fixed delay milliseconds for response
func (r Response) WithFixedDelay(delay time.Duration) Response {
r.delayDistribution = NewFixedDelay(delay)
return r
}
// WithDelay sets delay for response
func (r Response) WithDelay(delay DelayInterface) Response {
r.delayDistribution = delay
return r
}
// WithChunkedDribbleDelay sets chunked dribble delay for response
func (r Response) WithChunkedDribbleDelay(numberOfChunks int64, totalDuration time.Duration) Response {
r.chunkedDribbleDelay = &chunkedDribbleDelay{
numberOfChunks: numberOfChunks,
totalDuration: totalDuration.Milliseconds(),
}
return r
}
// WithStatus sets status for response
func (r Response) WithStatus(status int64) Response {
r.status = status
return r
}
// WithHeader sets header for response
func (r Response) WithHeader(key, value string) Response {
if r.headers == nil {
r.headers = make(map[string]string)
}
r.headers[key] = value
return r
}
// WithHeaders sets headers for response
func (r Response) WithHeaders(headers map[string]string) Response {
r.headers = headers
return r
}
func (r Response) WithFault(fault Fault) Response {
r.fault = &fault
return r
}
// WithBody sets body for response
func (r Response) WithBody(body string) Response {
r.body = &body
return r
}
// WithBinaryBody sets binary body for response
func (r Response) WithBinaryBody(body []byte) Response {
r.base64Body = body
return r
}
// WithJSONBody sets json body for response
func (r Response) WithJSONBody(body interface{}) Response {
r.jsonBody = body
return r
}
// WithBodyFile sets body file name for response
func (r Response) WithBodyFile(fileName string) Response {
r.bodyFileName = &fileName
return r
}
func (r Response) ParseResponse() map[string]interface{} {
jsonMap := map[string]interface{}{
"status": r.status,
}
if r.body != nil {
jsonMap["body"] = *r.body
}
if r.base64Body != nil {
jsonMap["base64Body"] = r.base64Body
}
if r.bodyFileName != nil {
jsonMap["bodyFileName"] = *r.bodyFileName
}
if r.jsonBody != nil {
jsonMap["jsonBody"] = r.jsonBody
}
if r.headers != nil {
jsonMap["headers"] = r.headers
}
if r.delayDistribution != nil {
jsonMap["delayDistribution"] = r.delayDistribution.ParseDelay()
}
if r.chunkedDribbleDelay != nil {
jsonMap["chunkedDribbleDelay"] = r.chunkedDribbleDelay
}
if r.fault != nil {
jsonMap["fault"] = *r.fault
}
return jsonMap
}