-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
mempool_test.go
178 lines (145 loc) · 5.1 KB
/
mempool_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
package whatsonchain
import (
"bytes"
"context"
"fmt"
"io/ioutil"
"net/http"
"strings"
"testing"
)
// mockHTTPMempoolValid for mocking requests
type mockHTTPMempoolValid struct{}
// Do is a mock http request
func (m *mockHTTPMempoolValid) 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(), "/mempool/info") {
resp.StatusCode = http.StatusOK
resp.Body = ioutil.NopCloser(bytes.NewBuffer([]byte(`{"size": 520,"bytes": 108095,"usage": 549776,"maxmempool": 64000000000,"mempoolminfee": 0}`)))
}
// Valid
if strings.Contains(req.URL.String(), "/mempool/raw") {
resp.StatusCode = http.StatusOK
resp.Body = ioutil.NopCloser(bytes.NewBuffer([]byte(`["86806b3587956552ea0e3f09dfd14f485fc870fa319ab37e98289a5043234644","bd9e6c83f8fdcaa3b66b214a4fbf910976bd16ec926ab983a2367edfa3e2bbd9","9cf4450a20f91419623d9b461d4e47647ce3812f0fd2e2d2904c5f5a24e45bba"]`)))
}
// Default is valid
return resp, nil
}
// mockHTTPMempoolInvalid for mocking requests
type mockHTTPMempoolInvalid struct{}
// Do is a mock http request
func (m *mockHTTPMempoolInvalid) 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
if strings.Contains(req.URL.String(), "/mempool/info") {
resp.Body = ioutil.NopCloser(bytes.NewBuffer([]byte(``)))
return resp, fmt.Errorf("bad request")
}
// Invalid
if strings.Contains(req.URL.String(), "/mempool/raw") {
resp.Body = ioutil.NopCloser(bytes.NewBuffer([]byte(``)))
return resp, fmt.Errorf("bad request")
}
// Default is valid
return resp, nil
}
// mockHTTPMempoolNotFound for mocking requests
type mockHTTPMempoolNotFound struct{}
// Do is a mock http request
func (m *mockHTTPMempoolNotFound) 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")
}
// Not found
if strings.Contains(req.URL.String(), "/mempool/info") {
resp.Body = ioutil.NopCloser(bytes.NewBuffer([]byte(``)))
return resp, nil
}
// Not found
if strings.Contains(req.URL.String(), "/mempool/raw") {
resp.Body = ioutil.NopCloser(bytes.NewBuffer([]byte(``)))
return resp, nil
}
// Default is valid
return resp, nil
}
// TestClient_GetMempoolInfo tests the GetMempoolInfo()
func TestClient_GetMempoolInfo(t *testing.T) {
t.Parallel()
// New mock client
client := newMockClient(&mockHTTPMempoolValid{})
ctx := context.Background()
// Test the valid response
info, err := client.GetMempoolInfo(ctx)
if err != nil {
t.Errorf("%s Failed: error [%s]", t.Name(), err.Error())
} else if info == nil {
t.Errorf("%s Failed: info was nil", t.Name())
} else if info.Size != 520 {
t.Errorf("%s Failed: size was [%d] expected [%d]", t.Name(), info.Size, 520)
} else if info.Bytes != 108095 {
t.Errorf("%s Failed: bytes was [%d] expected [%d]", t.Name(), info.Bytes, 108095)
} else if info.MaxMempool != 64000000000 {
t.Errorf("%s Failed: max mempool was [%d] expected [%d]", t.Name(), info.MaxMempool, 64000000000)
}
// New invalid mock client
client = newMockClient(&mockHTTPMempoolInvalid{})
// Test invalid response
_, err = client.GetMempoolInfo(ctx)
if err == nil {
t.Errorf("%s Failed: error should have occurred", t.Name())
}
// New not found mock client
client = newMockClient(&mockHTTPMempoolNotFound{})
// Test invalid response
_, err = client.GetMempoolInfo(ctx)
if err == nil {
t.Errorf("%s Failed: error should have occurred", t.Name())
}
}
// TestClient_GetMempoolTransactions tests the GetMempoolTransactions()
func TestClient_GetMempoolTransactions(t *testing.T) {
t.Parallel()
// New mock client
client := newMockClient(&mockHTTPMempoolValid{})
ctx := context.Background()
// Test the valid response
info, err := client.GetMempoolTransactions(ctx)
if err != nil {
t.Errorf("%s Failed: error [%s]", t.Name(), err.Error())
} else if info == nil {
t.Errorf("%s Failed: info was nil", t.Name())
} else if info[0] != "86806b3587956552ea0e3f09dfd14f485fc870fa319ab37e98289a5043234644" {
t.Errorf("%s Failed: tx was [%s] expected [%s]", t.Name(), info[0], "86806b3587956552ea0e3f09dfd14f485fc870fa319ab37e98289a5043234644")
} else if info[1] != "bd9e6c83f8fdcaa3b66b214a4fbf910976bd16ec926ab983a2367edfa3e2bbd9" {
t.Errorf("%s Failed: tx was [%s] expected [%s]", t.Name(), info[1], "bd9e6c83f8fdcaa3b66b214a4fbf910976bd16ec926ab983a2367edfa3e2bbd9")
}
// New invalid mock client
client = newMockClient(&mockHTTPMempoolInvalid{})
// Test invalid response
_, err = client.GetMempoolTransactions(ctx)
if err == nil {
t.Errorf("%s Failed: error should have occurred", t.Name())
}
// New not found mock client
client = newMockClient(&mockHTTPMempoolNotFound{})
// Test invalid response
_, err = client.GetMempoolTransactions(ctx)
if err == nil {
t.Errorf("%s Failed: error should have occurred", t.Name())
}
}