/
RequestRecordingServer.go
189 lines (170 loc) · 4.71 KB
/
RequestRecordingServer.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package main
import (
"fmt"
"io/ioutil"
"net"
"net/http"
"net/http/httptest"
"strconv"
"time"
)
//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
}
//CreateRequestRecordingServer ...
func CreateRequestRecordingServer(port int) *RequestRecordingServer {
return &RequestRecordingServer{
requests: []RecordedRequest{},
port: port,
use: []UseWithPredicates{},
}
}
//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.requests = append(instance.requests, recordedRequest)
if instance.use != nil {
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)
} else {
w.WriteHeader(http.StatusOK)
}
})
instance.server = httptest.NewUnstartedServer(handler)
instance.server.Listener, _ = net.Listen("tcp", ":"+strconv.Itoa(instance.port))
instance.server.Start()
}
//Stop ...
func (instance *RequestRecordingServer) Stop() {
instance.server.Close()
time.Sleep(1 * time.Microsecond)
}
//Clear ...
func (instance *RequestRecordingServer) Clear() {
instance.requests = []RecordedRequest{}
instance.use = []UseWithPredicates{}
}
//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) {
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
if !result {
Log.Println(fmt.Sprintf("path does not equal %s it equals %s", path, r.request.URL.Path))
}
return result
})
}
//RequestWithMethod ...
func RequestWithMethod(method string) HTTPRequestPredicate {
return HTTPRequestPredicate(func(r RecordedRequest) bool {
result := r.request.Method == method
if !result {
Log.Println("request method does not equal " + method)
}
return result
})
}
//RequestWithHeader ...
func RequestWithHeader(key string, value string) HTTPRequestPredicate {
return HTTPRequestPredicate(func(r RecordedRequest) bool {
result := r.request.Header.Get(key) == value
if !result {
Log.Println(fmt.Sprintf("request method does not contain header with key %s and value %s actual %s", key, value, r.request.Header.Get(key)))
}
return result
})
}
//RequestWithBody ...
func RequestWithBody(value string) HTTPRequestPredicate {
return HTTPRequestPredicate(func(r RecordedRequest) bool {
result := string(r.body) == value
if !result {
Log.Println(fmt.Sprintf("request body does not equal %s it equals %s", value, r.body))
}
return result
})
}
//RequestWithQuerystring ...
func RequestWithQuerystring(value string) HTTPRequestPredicate {
return HTTPRequestPredicate(func(r RecordedRequest) bool {
result := r.request.URL.RawQuery == value
if !result {
Log.Println("request query does not equal " + value + " | it equals " + r.request.URL.RawQuery)
}
return result
})
}