Add back network status test

This commit is contained in:
Patrick O'Grady 2020-09-28 08:01:13 -07:00
parent eee610e20a
commit f37487e905
No known key found for this signature in database
GPG key ID: 8DE11C985C0C8D85

View file

@ -293,6 +293,144 @@ var (
} }
) )
func TestNetworkStatus(t *testing.T) {
tests := map[string]struct {
responses []responseFixture
expectedStatus *types.NetworkStatusResponse
expectedError error
}{
"successful": {
responses: []responseFixture{
{
status: http.StatusOK,
body: loadFixture("get_blockchain_info_response.json"),
url: url,
},
{
status: http.StatusOK,
body: loadFixture("get_block_response.json"),
url: url,
},
{
status: http.StatusOK,
body: loadFixture("get_peer_info_response.json"),
url: url,
},
},
expectedStatus: &types.NetworkStatusResponse{
CurrentBlockIdentifier: blockIdentifier1000,
CurrentBlockTimestamp: block1000.Time * 1000,
GenesisBlockIdentifier: MainnetGenesisBlockIdentifier,
Peers: []*types.Peer{
{
PeerID: "77.93.223.9:8333",
Metadata: forceMarshalMap(t, &PeerInfo{
Addr: "77.93.223.9:8333",
Version: 70015,
SubVer: "/Satoshi:0.14.2/",
StartingHeight: 643579,
RelayTxes: true,
LastSend: 1597606676,
LastRecv: 1597606677,
BanScore: 0,
SyncedHeaders: 644046,
SyncedBlocks: 644046,
}),
},
{
PeerID: "172.105.93.179:8333",
Metadata: forceMarshalMap(t, &PeerInfo{
Addr: "172.105.93.179:8333",
RelayTxes: true,
LastSend: 1597606678,
LastRecv: 1597606676,
Version: 70015,
SubVer: "/Satoshi:0.18.1/",
StartingHeight: 643579,
BanScore: 0,
SyncedHeaders: 644046,
SyncedBlocks: 644046,
}),
},
},
},
},
"blockchain warming up error": {
responses: []responseFixture{
{
status: http.StatusOK,
body: loadFixture("rpc_in_warmup_response.json"),
url: url,
},
},
expectedError: errors.New("rpc in warmup"),
},
"blockchain info error": {
responses: []responseFixture{
{
status: http.StatusInternalServerError,
body: "{}",
url: url,
},
},
expectedError: errors.New("invalid response: 500 Internal Server Error"),
},
"peer info not accessible": {
responses: []responseFixture{
{
status: http.StatusOK,
body: loadFixture("get_blockchain_info_response.json"),
url: url,
},
{
status: http.StatusOK,
body: loadFixture("get_block_response.json"),
url: url,
},
{
status: http.StatusInternalServerError,
body: "{}",
url: url,
},
},
expectedError: errors.New("invalid response: 500 Internal Server Error"),
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
var (
assert = assert.New(t)
)
responses := make(chan responseFixture, len(test.responses))
for _, response := range test.responses {
responses <- response
}
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
response := <-responses
assert.Equal("application/json", r.Header.Get("Content-Type"))
assert.Equal("POST", r.Method)
assert.Equal(response.url, r.URL.RequestURI())
w.WriteHeader(response.status)
fmt.Fprintln(w, response.body)
}))
client := NewClient(ts.URL, MainnetGenesisBlockIdentifier, MainnetCurrency)
status, err := client.NetworkStatus(context.Background())
if test.expectedError != nil {
assert.Contains(err.Error(), test.expectedError.Error())
} else {
assert.NoError(err)
assert.Equal(test.expectedStatus, status)
}
})
}
}
func TestGetPeers(t *testing.T) { func TestGetPeers(t *testing.T) {
tests := map[string]struct { tests := map[string]struct {
responses []responseFixture responses []responseFixture