-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathchain_info_test.go
170 lines (137 loc) · 4.52 KB
/
chain_info_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
package whatsonchain
import (
"bytes"
"context"
"fmt"
"io/ioutil"
"net/http"
"strings"
"testing"
)
// mockHTTPChainValid for mocking requests
type mockHTTPChainValid struct{}
// Do is a mock http request
func (m *mockHTTPChainValid) 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 (chain info)
if strings.Contains(req.URL.String(), "/chain/info") {
resp.StatusCode = http.StatusOK
resp.Body = ioutil.NopCloser(bytes.NewBuffer([]byte(`{"chain":"main","blocks":640504,"headers":640504,"bestblockhash":"00000000000000000187b269ba0ed06be21c0d0d623c68957ad0308b3004f8ee","difficulty":286794300954.8341,"mediantime":1592843022,"verificationprogress":0.9999928741979456,"pruned":false,"chainwork":"0000000000000000000000000000000000000000010e6322afd01e2bb1415909"}`)))
}
// Valid (circulating supply)
if strings.Contains(req.URL.String(), "/circulatingsupply") {
resp.StatusCode = http.StatusOK
resp.Body = ioutil.NopCloser(bytes.NewBuffer([]byte(`18440650`)))
}
// Default is valid
return resp, nil
}
// mockHTTPChainInvalid for mocking requests
type mockHTTPChainInvalid struct{}
// Do is a mock http request
func (m *mockHTTPChainInvalid) 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 (chain info)
if strings.Contains(req.URL.String(), "/chain/info") {
resp.Body = ioutil.NopCloser(bytes.NewBuffer([]byte(``)))
return resp, fmt.Errorf("bad request")
}
// Invalid (circulating supply)
if strings.Contains(req.URL.String(), "/circulatingsupply") {
resp.Body = ioutil.NopCloser(bytes.NewBuffer([]byte(``)))
return resp, fmt.Errorf("bad request")
}
// Default is valid
return resp, nil
}
// mockHTTPChainNotFound for mocking requests
type mockHTTPChainNotFound struct{}
// Do is a mock http request
func (m *mockHTTPChainNotFound) 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 (chain info)
if strings.Contains(req.URL.String(), "/chain/info") {
resp.Body = ioutil.NopCloser(bytes.NewBuffer([]byte(``)))
return resp, nil
}
// Not found (circulating supply)
if strings.Contains(req.URL.String(), "/circulatingsupply") {
resp.Body = ioutil.NopCloser(bytes.NewBuffer([]byte(``)))
return resp, nil
}
// Default is valid
return resp, nil
}
// TestClient_GetChainInfo tests the GetChainInfo()
func TestClient_GetChainInfo(t *testing.T) {
t.Parallel()
// New mock client
client := newMockClient(&mockHTTPChainValid{})
ctx := context.Background()
// Test the valid response
info, err := client.GetChainInfo(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.Blocks != 640504 {
t.Errorf("%s Failed: blocks was [%d] expected [%d]", t.Name(), info.Blocks, 640504)
}
// New invalid mock client
client = newMockClient(&mockHTTPChainInvalid{})
// Test response
_, err = client.GetChainInfo(ctx)
if err == nil {
t.Errorf("%s Failed: error should have occurred", t.Name())
}
// New not found mock client
client = newMockClient(&mockHTTPChainNotFound{})
// Test response
_, err = client.GetChainInfo(ctx)
if err == nil {
t.Errorf("%s Failed: error should have occurred", t.Name())
}
}
// TestClient_GetCirculatingSupply tests the GetCirculatingSupply()
func TestClient_GetCirculatingSupply(t *testing.T) {
t.Parallel()
// New mock client
client := newMockClient(&mockHTTPChainValid{})
ctx := context.Background()
// Test the valid response
supply, err := client.GetCirculatingSupply(ctx)
if err != nil {
t.Errorf("%s Failed: error [%s]", t.Name(), err.Error())
} else if supply != 18440650 {
t.Errorf("%s Failed: supply was [%f] expected [%d]", t.Name(), supply, 18440650)
}
// New invalid mock client
client = newMockClient(&mockHTTPChainInvalid{})
// Test response
_, err = client.GetCirculatingSupply(ctx)
if err == nil {
t.Errorf("%s Failed: error should have occurred", t.Name())
}
// New not found mock client
client = newMockClient(&mockHTTPChainNotFound{})
// Test response
_, err = client.GetCirculatingSupply(ctx)
if err == nil {
t.Errorf("%s Failed: error should have occurred", t.Name())
}
}