Skip to content

Commit 745c899

Browse files
committed
Fix wrapper request body struct.
1 parent 6201e5b commit 745c899

File tree

2 files changed

+13
-14
lines changed

2 files changed

+13
-14
lines changed

server/server.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,7 @@ var strategyHandler func(tradehook string, payload []byte)
1212

1313
// strategyWrapper retrieve tradehook information from response body and send it to customer strategy
1414
func strategyWrapper(w http.ResponseWriter, r *http.Request) {
15-
var requestBody struct {
16-
Tradehook string `json:"tradehook"`
17-
Payload []byte `json:"payload"`
18-
}
15+
var requestBody map[string]interface{}
1916

2017
body, err := ioutil.ReadAll(r.Body)
2118
if err != nil {
@@ -26,8 +23,13 @@ func strategyWrapper(w http.ResponseWriter, r *http.Request) {
2623
if err != nil {
2724
log.Fatal(err)
2825
}
26+
rawData := requestBody["data"]
27+
data, err := json.Marshal(&rawData)
28+
if err != nil {
29+
log.Fatal(err)
30+
}
2931

30-
strategyHandler(requestBody.Tradehook, requestBody.Payload)
32+
strategyHandler(fmt.Sprintf("%v", requestBody["event"]), data)
3133

3234
w.WriteHeader(200)
3335
_, err = w.Write([]byte(http.StatusText(http.StatusOK)))

server/server_test.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,15 @@ func TestRunServerWithStrategy(t *testing.T) {
2222

2323
// Create handler
2424
strategy := func(tradehook string, payload []byte) {
25-
var payloadData struct {
26-
Assets []string `json:"assets"`
27-
Bars interface{} `json:"bars"`
28-
}
25+
var payloadData map[string]interface{}
2926

3027
err := json.Unmarshal(payload, &payloadData)
3128
if err != nil {
3229
assert.NoError(t, err)
3330
}
3431

3532
assert.Equal(t, "bars", tradehook, invalidErrorMsg)
36-
assert.NotEqualf(t, 0, len(payloadData.Assets), invalidErrorMsg)
33+
assert.NotEqualf(t, 0, len(payloadData), invalidErrorMsg)
3734
}
3835

3936
// Start server
@@ -53,11 +50,11 @@ func TestRunServerWithStrategy(t *testing.T) {
5350

5451
// Post request
5552
data, err := json.Marshal(struct {
56-
Tradehook string
57-
Payload []byte
53+
Event string `json:"event"`
54+
Data map[string]string `json:"data"`
5855
}{
59-
Tradehook: "bars",
60-
Payload: []byte("{\n \"assets\": [\"AAPL:US\", \"BTCUSD:BMEX\", \"...\"],\n \"bars\": {\n \"YYYY-MM-01\": {\n \"AAPL\": {\n \"o\": \"113.79\",\n \"h\": \"117.26\",\n \"l\": \"113.62\",\n \"c\": \"115.56\",\n \"v\": 136210200,\n \"t\": 782692,\n \"w\": \"116.11665173913042\"\n },\n \"BTCUSD:BMEX\": {\n \"o\": \"...\",\n \"h\": \"...\",\n \"l\": \"...\",\n \"c\": \"...\",\n \"v\": \"...\",\n \"t\": \"...\",\n \"w\": \"...\"\n }\n }\n }\n}"),
56+
Event: "bars",
57+
Data: map[string]string{"foo": "boo", "boo": "foo"},
6158
})
6259

6360
res, err = http.Post(fmt.Sprintf("%s/", server.URL), "", ioutil.NopCloser(bytes.NewBuffer(data)))

0 commit comments

Comments
 (0)