-
Notifications
You must be signed in to change notification settings - Fork 1
/
methods_chains_test.go
88 lines (76 loc) · 3.06 KB
/
methods_chains_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
package sourcify
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
)
var testChainsResponse = []byte(`[{"name":"Ethereum Mainnet","chain":"ETH","icon":"ethereum","features":[{"name":"EIP155"},{"name":"EIP1559"}],"faucets":[],"nativeCurrency":{"name":"Ether","symbol":"ETH","decimals":18},"infoURL":"https://ethereum.org","shortName":"eth","chainId":1,"networkId":1,"slip44":60,"ens":{"registry":"0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e"},"explorers":[{"name":"etherscan","url":"https://etherscan.io","standard":"EIP3091"}],"supported":true,"monitored":true,"contractFetchAddress":"https://api.etherscan.io/api?module=contract&action=getcontractcreation&contractaddresses=${ADDRESS}&apikey=4K5JKHMHPYK79IQYH8C5E5F5FAVYQS4W5E","rpc":["http://10.10.42.102:8541","https://eth-mainnet.g.alchemy.com/v2/{ALCHEMY_ID}"],"etherscanAPI":"https://api.etherscan.io"}]`)
var testChainsResponseError = []byte(`{"error":"Internal Server Error"}`)
func TestGetChains(t *testing.T) {
// Create a test server with a mocked handler
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write(testChainsResponse)
}))
defer server.Close()
// Create a test client with the test server's URL
client := NewClient(WithBaseURL(server.URL))
// Call the GetChains function
chains, err := GetChains(client)
assert.NoError(t, err)
// Validate the result
expectedChains := []Chain{
{
Name: "Ethereum Mainnet",
Chain: "ETH",
Icon: "ethereum",
Features: []ChainFeature{
{Name: "EIP155"},
{Name: "EIP1559"},
},
Faucets: []interface{}{},
NativeCurrency: ChainNativeCurrency{
Name: "Ether",
Symbol: "ETH",
Decimals: 18,
},
InfoURL: "https://ethereum.org",
ShortName: "eth",
ChainID: 1,
NetworkID: 1,
Slip44: 60,
Ens: ChainEns{Registry: "0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e"},
Explorers: []ChainExplorer{
{
Name: "etherscan",
URL: "https://etherscan.io",
Standard: "EIP3091",
},
},
Supported: true,
Monitored: true,
ContractFetchAddress: "https://api.etherscan.io/api?module=contract&action=getcontractcreation&contractaddresses=${ADDRESS}&apikey=4K5JKHMHPYK79IQYH8C5E5F5FAVYQS4W5E",
RPC: []string{
"http://10.10.42.102:8541",
"https://eth-mainnet.g.alchemy.com/v2/{ALCHEMY_ID}",
},
EtherscanAPI: "https://api.etherscan.io",
},
}
assert.Equal(t, expectedChains, chains)
}
func TestGetChains_ErrorResponse(t *testing.T) {
// Create a test server with a mocked error handler
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
w.Write(testChainsResponseError)
}))
defer server.Close()
// Create a test client with the test server's URL
client := NewClient(WithBaseURL(server.URL))
// Call the GetChains function
_, err := GetChains(client)
assert.Error(t, err)
assert.EqualError(t, err, "unexpected response status: 500 Internal Server Error")
}