-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRequestRecordingServer.go
More file actions
203 lines (181 loc) · 4.79 KB
/
RequestRecordingServer.go
File metadata and controls
203 lines (181 loc) · 4.79 KB
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package rizo
import (
"fmt"
"io/ioutil"
"net"
"net/http"
"net/http/httptest"
"strconv"
"sync"
"time"
)
func check(err error) {
if err != nil {
panic(err)
}
}
//RecordedRequest ...
type RecordedRequest struct {
Request *http.Request
Body string
}
//HTTPRequestPredicate ...
type HTTPRequestPredicate func(request RecordedRequest) bool
//HTTPResponseFactory ...
type HTTPResponseFactory func(writer http.ResponseWriter)
//UseWithPredicates ...
type UseWithPredicates struct {
ResponseFactory HTTPResponseFactory
RequestPredicates []HTTPRequestPredicate
}
//RequestRecordingServer ...
type RequestRecordingServer struct {
Requests []RecordedRequest
port int
server *httptest.Server
use []UseWithPredicates
lock *sync.Mutex
}
//CreateRequestRecordingServer ...
func CreateRequestRecordingServer(port int) *RequestRecordingServer {
return &RequestRecordingServer{
Requests: []RecordedRequest{},
port: port,
use: []UseWithPredicates{},
lock: &sync.Mutex{},
}
}
//CreateURL ...
func (instance *RequestRecordingServer) CreateURL(path string) string {
return fmt.Sprintf("http://localhost:%d%s", instance.port, path)
}
func (instance *RequestRecordingServer) evaluatePredicates(recordedRequest RecordedRequest, w http.ResponseWriter) {
for _, item := range instance.use {
if item.RequestPredicates != nil {
result := instance.Evaluate(recordedRequest, item.RequestPredicates...)
if result {
item.ResponseFactory(w)
return
}
} else {
item.ResponseFactory(w)
return
}
}
w.WriteHeader(http.StatusOK)
}
//Start ...
func (instance *RequestRecordingServer) Start() {
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(r.Body)
check(err)
recordedRequest := RecordedRequest{
Request: r,
Body: string(body),
}
instance.lock.Lock()
instance.Requests = append(instance.Requests, recordedRequest)
if instance.use != nil {
instance.evaluatePredicates(recordedRequest, w)
} else {
w.WriteHeader(http.StatusOK)
}
instance.lock.Unlock()
})
instance.server = httptest.NewUnstartedServer(handler)
listener, err := net.Listen("tcp", ":"+strconv.Itoa(instance.port))
if err != nil {
panic(err)
}
instance.server.Listener = listener
instance.server.Start()
}
//Stop ...
func (instance *RequestRecordingServer) Stop() {
if instance.server != nil {
instance.server.CloseClientConnections()
instance.server.Close()
time.Sleep(1 * time.Millisecond)
}
}
//Clear ...
func (instance *RequestRecordingServer) Clear() {
instance.lock.Lock()
instance.Requests = []RecordedRequest{}
instance.use = []UseWithPredicates{}
instance.lock.Unlock()
}
//Evaluate ...
func (instance *RequestRecordingServer) Evaluate(request RecordedRequest, predicates ...HTTPRequestPredicate) bool {
results := make([]bool, len(predicates))
for index, predicate := range predicates {
results[index] = predicate(request)
}
thing := true
for _, result := range results {
if !result {
thing = false
break
}
}
return thing
}
//Find ...
func (instance *RequestRecordingServer) Find(predicates ...HTTPRequestPredicate) bool {
for _, request := range instance.Requests {
if instance.Evaluate(request, predicates...) {
return true
}
}
return false
}
//Use ...
func (instance *RequestRecordingServer) Use(factory HTTPResponseFactory) *RequestRecordingServer {
instance.use = append(instance.use, UseWithPredicates{
ResponseFactory: factory,
RequestPredicates: []HTTPRequestPredicate{},
})
return instance
}
//For ...
func (instance *RequestRecordingServer) For(predicates ...HTTPRequestPredicate) {
index := len(instance.use) - 1
for _, item := range predicates {
instance.use[index].RequestPredicates = append(instance.use[index].RequestPredicates, item)
}
}
//RequestWithPath ...
func RequestWithPath(path string) HTTPRequestPredicate {
return HTTPRequestPredicate(func(r RecordedRequest) bool {
result := r.Request.URL.Path == path
return result
})
}
//RequestWithMethod ...
func RequestWithMethod(method string) HTTPRequestPredicate {
return HTTPRequestPredicate(func(r RecordedRequest) bool {
result := r.Request.Method == method
return result
})
}
//RequestWithHeader ...
func RequestWithHeader(key string, value string) HTTPRequestPredicate {
return HTTPRequestPredicate(func(r RecordedRequest) bool {
result := r.Request.Header.Get(key) == value
return result
})
}
//RequestWithBody ...
func RequestWithBody(value string) HTTPRequestPredicate {
return HTTPRequestPredicate(func(r RecordedRequest) bool {
result := string(r.Body) == value
return result
})
}
//RequestWithQuerystring ...
func RequestWithQuerystring(value string) HTTPRequestPredicate {
return HTTPRequestPredicate(func(r RecordedRequest) bool {
result := r.Request.URL.RawQuery == value
return result
})
}