forked from metal3-io/baremetal-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
269 lines (224 loc) · 7.35 KB
/
server.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package testserver
import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptest"
"regexp"
"strings"
"testing"
)
// New returns a MockServer
func New(t *testing.T, name string) *MockServer {
mux := http.NewServeMux()
t.Logf("%s: new server created", name)
return &MockServer{
t: t,
name: name,
mux: mux,
responsesByMethod: make(map[string]map[string]response),
defaultResponses: []defaultResponse{},
}
}
type response struct {
code int
payload string
}
type defaultResponse struct {
response
method string
re *regexp.Regexp
}
type simpleRequest struct {
pattern string
method string
body string
}
// MockServer is a simple http testing server
type MockServer struct {
t *testing.T
mux *http.ServeMux
name string
Requests string
FullRequests []simpleRequest
server *httptest.Server
errorCode int
responsesByMethod map[string]map[string]response
defaultResponses []defaultResponse
}
// Endpoint returns the URL to the server
func (m *MockServer) Endpoint() string {
if m == nil || m.server == nil {
// The consumer of this method expects something valid, but
// won't use it if m is nil.
return "https://ironic.test/v1/"
}
response := m.server.URL + "/v1/"
m.t.Logf("%s: endpoint: %s", m.name, response)
return response
}
func (m *MockServer) logRequest(r *http.Request, response string) {
m.t.Logf("%s: %s %s -> %s", m.name, r.Method, r.URL, response)
m.Requests += r.URL.Path + ";"
bodyRaw, _ := io.ReadAll(r.Body)
m.FullRequests = append(m.FullRequests, simpleRequest{
pattern: r.URL.Path,
method: r.Method,
body: string(bodyRaw),
})
}
// Handler attaches a generic handler function to a request URL pattern
func (m *MockServer) Handler(pattern string, handlerFunc http.HandlerFunc) *MockServer {
m.t.Logf("%s: adding handler for %s", m.name, pattern)
m.mux.HandleFunc(pattern, func(w http.ResponseWriter, r *http.Request) {
handlerFunc(w, r)
})
return m
}
func (m *MockServer) buildHandler(pattern string) func(http.ResponseWriter, *http.Request) {
handler := func(w http.ResponseWriter, r *http.Request) {
if response, ok := m.responsesByMethod[r.URL.Path][r.Method]; ok {
m.sendData(w, r, response.code, response.payload)
return
}
m.defaultHandler(w, r)
}
return handler
}
func (m *MockServer) parsePattern(patternWithMethod string) (pattern string, method string) {
method = http.MethodGet
res := strings.Split(patternWithMethod, ":")
if len(res) > 1 {
method = res[1]
}
pattern = res[0]
return
}
// ResponseWithCode attaches a handler function that returns the given payload
// from requests to the URL pattern along with the specified code
func (m *MockServer) ResponseWithCode(patternWithMethod string, payload string, code int) *MockServer {
pattern, method := m.parsePattern(patternWithMethod)
mh, ok := m.responsesByMethod[pattern]
if !ok {
m.responsesByMethod[pattern] = map[string]response{}
m.mux.HandleFunc(pattern, m.buildHandler(pattern))
}
if _, ok = mh[method]; ok {
panic(fmt.Sprintf("Method handler for [%s] %s was already defined", method, pattern))
}
m.t.Logf("%s: adding response for [%s] %s", m.name, method, pattern)
m.responsesByMethod[pattern][method] = response{
code: code,
payload: payload,
}
return m
}
// ResponseJSON marshals the JSON object as payload returned by the response
// handler
func (m *MockServer) ResponseJSON(pattern string, payload interface{}) *MockServer {
content, err := json.Marshal(payload)
if err != nil {
m.t.Error(err)
}
m.ResponseWithCode(pattern, string(content), http.StatusOK)
return m
}
// ErrorResponse attaches a handler function that returns the given
// error code from requests to the URL pattern
func (m *MockServer) ErrorResponse(pattern string, errorCode int) *MockServer {
m.t.Logf("%s: adding error response handler for %s", m.name, pattern)
m.mux.HandleFunc(pattern, func(w http.ResponseWriter, r *http.Request) {
m.logRequest(r, fmt.Sprintf("%d", errorCode))
http.Error(w, "An error", errorCode)
})
return m
}
// Start runs the server
func (m *MockServer) Start() *MockServer {
m.server = httptest.NewServer(m.mux)
//catch all handler
m.mux.HandleFunc("/", m.defaultHandler)
return m
}
// Stop closes the server down
func (m *MockServer) Stop() {
m.server.Close()
}
// AddDefaultResponseJSON adds a default response for the specified pattern
func (m *MockServer) AddDefaultResponseJSON(patternWithVars string, httpMethod string, code int, payload interface{}) *MockServer {
content, err := json.Marshal(payload)
if err != nil {
m.t.Error(err)
}
return m.AddDefaultResponse(patternWithVars, httpMethod, code, string(content))
}
// GetLastRequestFor returns the last request for the specified pattern/method.
// If method is empty, the response will be applied for any method
func (m *MockServer) GetLastRequestFor(pattern string, method string) (string, bool) {
for i := len(m.FullRequests) - 1; i >= 0; i-- {
r := m.FullRequests[i]
if r.method == "" || r.method == method {
if r.pattern == pattern {
return r.body, true
}
}
}
return "", false
}
// AddDefaultResponse adds a default response for the specified pattern/method.
// It is possible to use variables in the pattern using curly braces, ie `/v1/nodes/{id}/power`
// Pattern variables can be reused in the payload, so that they will be substituted with the actual value when sending the response
// If httpMethod is empty, the response will be applied for any method
func (m *MockServer) AddDefaultResponse(patternWithVars string, httpMethod string, code int, payload string) *MockServer {
pattern := "^" + regexp.MustCompile("{(.[^}]*)}").ReplaceAllString(patternWithVars, "(?P<$1>.[^/]*)") + "$"
m.t.Logf("%s: adding default response for %s (%s) -> {%d, %s}", m.name, patternWithVars, pattern, code, payload)
defaultResponse := defaultResponse{
re: regexp.MustCompile(pattern),
method: httpMethod,
response: response{
code: code,
payload: payload,
},
}
m.defaultResponses = append(m.defaultResponses, defaultResponse)
return m
}
func (m *MockServer) defaultHandler(w http.ResponseWriter, r *http.Request) {
url := r.URL.Path
method := r.Method
for _, response := range m.defaultResponses {
if response.method == "" || response.method == method {
match := response.re.FindStringSubmatch(url)
if match == nil {
continue
}
m.t.Logf("%s: found default response for %s: {%d, %s}", m.name, url, response.code, response.payload)
payload := response.payload
for i, name := range response.re.SubexpNames() {
if i != 0 && name != "" {
payload = strings.ReplaceAll(payload, "{"+name+"}", match[i])
}
}
m.sendData(w, r, response.code, payload)
return
}
}
m.t.Logf("%s: Cannot find any default response for [%s] %s", m.name, method, url)
m.logRequest(r, "")
}
func (m *MockServer) sendData(w http.ResponseWriter, r *http.Request, code int, payload string) {
m.logRequest(r, payload)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(code)
fmt.Fprint(w, payload)
}
// SendJSONResponse marshalls the payload to a JSON object and sends
// the response using the given writer
func (m *MockServer) SendJSONResponse(payload interface{}, code int, w http.ResponseWriter, r *http.Request) {
content, err := json.Marshal(payload)
if err != nil {
m.t.Error(err)
}
m.sendData(w, r, code, string(content))
}