-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathscripts_test.go
267 lines (230 loc) · 9.12 KB
/
scripts_test.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
package whatsonchain
import (
"bytes"
"context"
"fmt"
"io/ioutil"
"net/http"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
// mockHTTPScript for mocking requests
type mockHTTPScript struct{}
// Do is a mock http request
func (m *mockHTTPScript) Do(req *http.Request) (*http.Response, error) {
resp := new(http.Response)
resp.StatusCode = http.StatusBadRequest
// No req found
if req == nil {
return resp, fmt.Errorf("missing request")
}
// Valid
if strings.Contains(req.URL.String(), "script/995ea8d0f752f41cdd99bb9d54cb004709e04c7dc4088bcbbbb9ea5c390a43c3/history") {
resp.StatusCode = http.StatusOK
resp.Body = ioutil.NopCloser(bytes.NewBuffer([]byte(`[{"tx_hash":"52dfceb815ad129a0fd946e3d665f44fa61f068135b9f38b05d3c697e11bad48","height":620539},{"tx_hash":"4ec3b63d764558303eda720e8e51f69bbcfe81376075657313fb587306f8a9b0","height":620539}]`)))
}
// Invalid
if strings.Contains(req.URL.String(), "script/invalidTx/history") {
resp.Body = ioutil.NopCloser(bytes.NewBuffer([]byte(``)))
return resp, fmt.Errorf("bad request")
}
// Not found
if strings.Contains(req.URL.String(), "script/notFound/history") {
resp.StatusCode = http.StatusNotFound
resp.Body = ioutil.NopCloser(bytes.NewBuffer([]byte(``)))
return resp, nil
}
// Valid (has utxo)
if strings.Contains(req.URL.String(), "script/92cf18576a49ddad3e18f4af23b85d8d8218e03ce3b7533aced3fdd286f7e6cb/unspent") {
resp.StatusCode = http.StatusOK
resp.Body = ioutil.NopCloser(bytes.NewBuffer([]byte(`[{"height": 640558,"tx_pos": 1,"tx_hash": "5c6ac3a685be0791aa6e6eedb03d48cbf76046ea499e0a9cefbdc0fb3969ad13","value": 533335}]`)))
}
// Valid (empty)
if strings.Contains(req.URL.String(), "script/995ea8d0f752f41cdd99bb9d54cb004709e04c7dc4088bcbbbb9ea5c390a43c3/unspent") {
resp.StatusCode = http.StatusOK
resp.Body = ioutil.NopCloser(bytes.NewBuffer([]byte(`[]`)))
}
// Invalid
if strings.Contains(req.URL.String(), "script/invalidTx/unspent") {
resp.Body = ioutil.NopCloser(bytes.NewBuffer([]byte(``)))
return resp, fmt.Errorf("bad request")
}
// Not found
if strings.Contains(req.URL.String(), "script/notFound/unspent") {
resp.StatusCode = http.StatusNotFound
resp.Body = ioutil.NopCloser(bytes.NewBuffer([]byte(``)))
return resp, nil
}
// Valid (unspent)
if strings.Contains(req.URL.String(), "/scripts/unspent") {
resp.StatusCode = http.StatusOK
resp.Body = ioutil.NopCloser(bytes.NewBuffer([]byte(`[{"script":"f814a7c3a40164aacc440871e8b7b14eb6a45f0ca7dcbeaea709edc83274c5e7","unspent":[{"height":620539,"tx_pos":0,"tx_hash":"4ec3b63d764558303eda720e8e51f69bbcfe81376075657313fb587306f8a9b0","value":450000}],"error":""},{"script":"995ea8d0f752f41cdd99bb9d54cb004709e04c7dc4088bcbbbb9ea5c390a43c3","unspent":[],"error":""}]`)))
}
// Default is valid
return resp, nil
}
// mockHTTPScriptErrors for mocking requests
type mockHTTPScriptErrors struct{}
// Do is a mock http request
func (m *mockHTTPScriptErrors) Do(req *http.Request) (*http.Response, error) {
resp := new(http.Response)
resp.StatusCode = http.StatusBadRequest
// No req found
if req == nil {
return resp, fmt.Errorf("missing request")
}
// Invalid (info) return an error
if strings.Contains(req.URL.String(), "/scripts/unspent") {
resp.StatusCode = http.StatusInternalServerError
resp.Body = ioutil.NopCloser(bytes.NewBuffer([]byte(``)))
return resp, fmt.Errorf("missing request")
}
return nil, fmt.Errorf("no valid response found")
}
// mockHTTPScriptNotFound for mocking requests
type mockHTTPScriptNotFound struct{}
// Do is a mock http request
func (m *mockHTTPScriptNotFound) Do(req *http.Request) (*http.Response, error) {
resp := new(http.Response)
resp.StatusCode = http.StatusNotFound
// No req found
if req == nil {
return resp, fmt.Errorf("missing request")
}
// Invalid (info) return an error
if strings.Contains(req.URL.String(), "/scripts/unspent") {
resp.Body = ioutil.NopCloser(bytes.NewBuffer([]byte(``)))
return resp, nil
}
return resp, nil
}
// TestClient_GetScriptHistory tests the GetScriptHistory()
func TestClient_GetScriptHistory(t *testing.T) {
t.Parallel()
// New mock client
client := newMockClient(&mockHTTPScript{})
ctx := context.Background()
// Create the list of tests
var tests = []struct {
input string
height int64
hash string
expectedError bool
statusCode int
}{
{"995ea8d0f752f41cdd99bb9d54cb004709e04c7dc4088bcbbbb9ea5c390a43c3", 620539, "52dfceb815ad129a0fd946e3d665f44fa61f068135b9f38b05d3c697e11bad48", false, http.StatusOK},
{"invalidTx", 0, "", true, http.StatusBadRequest},
{"notFound", 0, "", true, http.StatusNotFound},
}
// Test all
for _, test := range tests {
if output, err := client.GetScriptHistory(ctx, test.input); err == nil && test.expectedError {
t.Errorf("%s Failed: expected to throw an error, no error [%s] inputted", t.Name(), test.input)
} else if err != nil && !test.expectedError {
t.Errorf("%s Failed: [%s] inputted, received: [%v] error [%s]", t.Name(), test.input, output, err.Error())
} else if output != nil && output[0].Height != test.height && !test.expectedError {
t.Errorf("%s Failed: [%s] inputted and [%d] height expected, received: [%d]", t.Name(), test.input, test.height, output[0].Height)
} else if output != nil && output[0].TxHash != test.hash && !test.expectedError {
t.Errorf("%s Failed: [%s] inputted and [%s] hash expected, received: [%s]", t.Name(), test.input, test.hash, output[0].TxHash)
} else if client.LastRequest().StatusCode != test.statusCode {
t.Errorf("%s Expected status code to be %d, got %d, [%s] inputted", t.Name(), test.statusCode, client.LastRequest().StatusCode, test.input)
}
}
}
// TestClient_GetScriptUnspentTransactions tests the GetScriptUnspentTransactions()
func TestClient_GetScriptUnspentTransactions(t *testing.T) {
t.Parallel()
// New mock client
client := newMockClient(&mockHTTPScript{})
ctx := context.Background()
// Create the list of tests
var tests = []struct {
input string
height int64
hash string
expectedError bool
statusCode int
}{
{"92cf18576a49ddad3e18f4af23b85d8d8218e03ce3b7533aced3fdd286f7e6cb", 640558, "5c6ac3a685be0791aa6e6eedb03d48cbf76046ea499e0a9cefbdc0fb3969ad13", false, http.StatusOK},
{"995ea8d0f752f41cdd99bb9d54cb004709e04c7dc4088bcbbbb9ea5c390a43c3", 0, "", false, http.StatusOK},
{"invalidTx", 0, "", true, http.StatusBadRequest},
{"notFound", 0, "", true, http.StatusNotFound},
}
// Test all
for _, test := range tests {
if output, err := client.GetScriptUnspentTransactions(ctx, test.input); err == nil && test.expectedError {
t.Errorf("%s Failed: expected to throw an error, no error [%s] inputted", t.Name(), test.input)
} else if err != nil && !test.expectedError {
t.Errorf("%s Failed: [%s] inputted, received: [%v] error [%s]", t.Name(), test.input, output, err.Error())
} else if len(output) > 0 && output[0].Height != test.height && !test.expectedError {
t.Errorf("%s Failed: [%s] inputted and [%d] height expected, received: [%d]", t.Name(), test.input, test.height, output[0].Height)
} else if len(output) > 0 && output[0].TxHash != test.hash && !test.expectedError {
t.Errorf("%s Failed: [%s] inputted and [%s] hash expected, received: [%s]", t.Name(), test.input, test.hash, output[0].TxHash)
} else if client.LastRequest().StatusCode != test.statusCode {
t.Errorf("%s Expected status code to be %d, got %d, [%s] inputted", t.Name(), test.statusCode, client.LastRequest().StatusCode, test.input)
}
}
}
// TestClient_BulkScriptUnspentTransactions tests the BulkScriptUnspentTransactions()
func TestClient_BulkScriptUnspentTransactions(t *testing.T) {
t.Parallel()
t.Run("valid response", func(t *testing.T) {
client := newMockClient(&mockHTTPScript{})
ctx := context.Background()
balances, err := client.BulkScriptUnspentTransactions(ctx, &ScriptsList{Scripts: []string{
"f814a7c3a40164aacc440871e8b7b14eb6a45f0ca7dcbeaea709edc83274c5e7",
"995ea8d0f752f41cdd99bb9d54cb004709e04c7dc4088bcbbbb9ea5c390a43c3",
}})
assert.NoError(t, err)
assert.NotNil(t, balances)
assert.Equal(t, 2, len(balances))
})
t.Run("max scripts (error)", func(t *testing.T) {
client := newMockClient(&mockHTTPScript{})
ctx := context.Background()
balances, err := client.BulkScriptUnspentTransactions(ctx, &ScriptsList{Scripts: []string{
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"10",
"11",
"12",
"13",
"14",
"15",
"16",
"17",
"18",
"19",
"20",
"21",
}})
assert.Error(t, err)
assert.Nil(t, balances)
})
t.Run("bad response (error)", func(t *testing.T) {
client := newMockClient(&mockHTTPScriptErrors{})
ctx := context.Background()
balances, err := client.BulkScriptUnspentTransactions(ctx, &ScriptsList{Scripts: []string{
"f814a7c3a40164aacc440871e8b7b14eb6a45f0ca7dcbeaea709edc83274c5e7",
}})
assert.Error(t, err)
assert.Nil(t, balances)
})
t.Run("not found (error)", func(t *testing.T) {
client := newMockClient(&mockHTTPScriptNotFound{})
ctx := context.Background()
balances, err := client.BulkScriptUnspentTransactions(ctx, &ScriptsList{Scripts: []string{
"notFound",
}})
assert.Error(t, err)
assert.Nil(t, balances)
})
}