-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.go
284 lines (241 loc) · 7.34 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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package mock
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/http/httptest"
"net/url"
"reflect"
"strconv"
"strings"
"github.com/thrasher-corp/gocryptotrader/common"
"github.com/thrasher-corp/gocryptotrader/common/crypto"
"github.com/thrasher-corp/gocryptotrader/common/file"
)
// DefaultDirectory defines the main mock directory
const DefaultDirectory = "../../testdata/http_mock/"
const (
contentType = "Content-Type"
applicationURLEncoded = "application/x-www-form-urlencoded"
applicationJSON = "application/json"
textPlain = "text/plain"
)
// VCRMock defines the main mock JSON file and attributes
type VCRMock struct {
Routes map[string]map[string][]HTTPResponse `json:"routes"`
}
// NewVCRServer starts a new VCR server for replaying HTTP requests for testing
// purposes and returns the server connection details
func NewVCRServer(path string) (string, *http.Client, error) {
if path == "" {
return "", nil, errors.New("no path to json mock file found")
}
var mockFile VCRMock
contents, err := ioutil.ReadFile(path)
if err != nil {
pathing := strings.Split(path, "/")
dirPathing := pathing[:len(pathing)-1]
dir := strings.Join(dirPathing, "/")
err = common.CreateDir(dir)
if err != nil {
return "", nil, err
}
data, jErr := json.MarshalIndent(mockFile, "", " ")
if jErr != nil {
return "", nil, jErr
}
err = file.Write(path, data)
if err != nil {
return "", nil, err
}
contents = data
}
if !json.Valid(contents) {
return "",
nil,
fmt.Errorf("contents of file %s are not valid JSON", path)
}
// Get mocking data for the specific service
err = json.Unmarshal(contents, &mockFile)
if err != nil {
return "", nil, err
}
newMux := http.NewServeMux()
// Range over routes and assign responses to explicit paths and http
// methods
if len(mockFile.Routes) != 0 {
for pattern, mockResponses := range mockFile.Routes {
RegisterHandler(pattern, mockResponses, newMux)
}
} else {
newMux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
err := json.NewEncoder(w).Encode("There is no mock data available in file please record a new HTTP response. Please follow README.md in the mock package.")
if err != nil {
panic(err)
}
})
}
tlsServer := httptest.NewTLSServer(newMux)
return tlsServer.URL, tlsServer.Client(), nil
}
// RegisterHandler registers a generalised mock response logic for specific
// routes
func RegisterHandler(pattern string, mock map[string][]HTTPResponse, mux *http.ServeMux) {
mux.HandleFunc(pattern, func(w http.ResponseWriter, r *http.Request) {
httpResponses, ok := mock[r.Method]
if !ok {
log.Fatalf("Mock Test Failure - Method %s not present in mock file",
r.Method)
}
switch r.Method {
case http.MethodGet:
vals, err := url.ParseRequestURI(r.RequestURI)
if err != nil {
log.Fatal("Mock Test Failure - Parse request URI error", err)
}
payload, err := MatchAndGetResponse(httpResponses, vals.Query(), true)
if err != nil {
log.Fatalf("Mock Test Failure - MatchAndGetResponse error %s for %s",
err, r.RequestURI)
}
MessageWriteJSON(w, http.StatusOK, payload)
return
case http.MethodPost, http.MethodPut:
switch r.Header.Get(contentType) {
case applicationURLEncoded:
readBody, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Fatal("Mock Test Failure - ReadAll error", err)
}
vals, err := url.ParseQuery(string(readBody))
if err != nil {
log.Fatal("Mock Test Failure - parse query error", err)
}
payload, err := MatchAndGetResponse(httpResponses, vals, false)
if err != nil {
log.Fatal("Mock Test Failure - MatchAndGetResponse error ", err)
}
MessageWriteJSON(w, http.StatusOK, payload)
return
case "":
payload, err := MatchAndGetResponse(httpResponses, r.URL.Query(), true)
if err != nil {
log.Fatal("Mock Test Failure - MatchAndGetResponse error ", err)
}
MessageWriteJSON(w, http.StatusOK, payload)
return
case applicationJSON:
readBody, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Fatalf("Mock Test Failure - %v", err)
}
reqVals, err := DeriveURLValsFromJSONMap(readBody)
if err != nil {
log.Fatalf("Mock Test Failure - %v", err)
}
payload, err := MatchAndGetResponse(httpResponses, reqVals, false)
if err != nil {
log.Fatal("Mock Test Failure - MatchAndGetResponse error ", err)
}
MessageWriteJSON(w, http.StatusOK, payload)
return
case textPlain:
headerData, ok := r.Header["X-Gemini-Payload"]
if !ok {
log.Fatal("Mock Test Failure - Cannot find header in request")
}
base64data := strings.Join(headerData, "")
jsonThings, err := crypto.Base64Decode(base64data)
if err != nil {
log.Fatal("Mock Test Failure - ", err)
}
reqVals, err := DeriveURLValsFromJSONMap(jsonThings)
if err != nil {
log.Fatalf("Mock Test Failure - %v", err)
}
payload, err := MatchAndGetResponse(httpResponses, reqVals, false)
if err != nil {
log.Fatal("Mock Test Failure - MatchAndGetResponse error ", err)
}
MessageWriteJSON(w, http.StatusOK, payload)
return
default:
log.Fatalf("Mock Test Failure - Unhandled content type %v",
r.Header.Get(contentType))
}
case http.MethodDelete:
payload, err := MatchAndGetResponse(httpResponses, r.URL.Query(), true)
if err != nil {
log.Println(r.URL.Query())
log.Fatal("Mock Test Failure - MatchAndGetResponse error ", err)
}
MessageWriteJSON(w, http.StatusOK, payload)
return
default:
log.Fatal("Mock Test Failure - Unhandled HTTP method:",
r.Header.Get(contentType))
}
})
}
// MessageWriteJSON writes JSON to a connection
func MessageWriteJSON(w http.ResponseWriter, status int, data interface{}) {
w.Header().Set(contentType, applicationJSON)
w.WriteHeader(status)
if data != nil {
err := json.NewEncoder(w).Encode(data)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
log.Fatal("Mock Test Failure - JSON encode error", err)
}
}
}
// MatchAndGetResponse matches incoming request values with mockdata response
// values and returns the payload
func MatchAndGetResponse(mockData []HTTPResponse, requestVals url.Values, isQueryData bool) (json.RawMessage, error) {
for i := range mockData {
var data string
if isQueryData {
data = mockData[i].QueryString
} else {
data = mockData[i].BodyParams
}
var mockVals = url.Values{}
var err error
if json.Valid([]byte(data)) {
something := make(map[string]interface{})
err = json.Unmarshal([]byte(data), &something)
if err != nil {
return nil, err
}
for k, v := range something {
switch val := v.(type) {
case string:
mockVals.Add(k, val)
case bool:
mockVals.Add(k, strconv.FormatBool(val))
case float64:
mockVals.Add(k, strconv.FormatFloat(val, 'f', -1, 64))
case map[string]interface{}, []interface{}, nil:
mockVals.Add(k, fmt.Sprintf("%v", val))
default:
log.Println(reflect.TypeOf(val))
log.Fatal("unhandled type please add as needed")
}
}
} else {
mockVals, err = url.ParseQuery(data)
if err != nil {
return nil, err
}
}
if MatchURLVals(mockVals, requestVals) {
return mockData[i].Data, nil
}
}
return nil, errors.New("no data could be matched")
}