-
Notifications
You must be signed in to change notification settings - Fork 1
/
methods_check_by_address_test.go
96 lines (77 loc) · 3.08 KB
/
methods_check_by_address_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
package sourcify
import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"github.com/ethereum/go-ethereum/common"
"github.com/stretchr/testify/assert"
)
func TestCheckContractByAddresses(t *testing.T) {
// Create a mock HTTP server
mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == MethodCheckByAddresses.URI {
// Simulate a successful response with sample contract addresses
contractAddresses := []*CheckContractAddress{
{
Address: common.HexToAddress("0x1234567890abcdef"),
Status: "verified",
ChainIDs: []string{"1", "2"},
},
}
err := json.NewEncoder(w).Encode(contractAddresses)
if err != nil {
t.Errorf("failed to encode mock contract addresses: %v", err)
}
} else {
http.NotFound(w, r)
}
}))
defer mockServer.Close()
// Create a client for the mock server
client := NewClient(WithBaseURL(mockServer.URL))
// Define test data
addresses := []string{"0x054B2223509D430269a31De4AE2f335890be5C8F"}
chainIds := []int{56}
// Call the function to check contract addresses
contractAddresses, err := CheckContractByAddresses(client, addresses, chainIds, MethodMatchTypeFull)
// Verify the results
assert.NoError(t, err, "CheckContractByAddresses returned an error")
expectedContractAddresses := []*CheckContractAddress{
{
Address: common.HexToAddress("0x1234567890abcdef"),
Status: "verified",
ChainIDs: []string{"1", "2"},
},
}
assert.Equal(t, expectedContractAddresses, contractAddresses, "CheckContractByAddresses returned unexpected contract addresses")
}
func TestCheckContractByAddresses_Error(t *testing.T) {
// Create a mock HTTP server that returns an error
mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
}))
defer mockServer.Close()
// Create a client for the mock server
client := NewClient(WithBaseURL(mockServer.URL))
// Define test data
addresses := []string{"0x054B2223509D430269a31De4AE2f335890be5C8F"}
chainIds := []int{56}
// Call the function to check contract addresses
contractAddresses, err := CheckContractByAddresses(client, addresses, chainIds, MethodMatchTypeFull)
// Verify the results
assert.Error(t, err, "CheckContractByAddresses should return an error")
assert.Nil(t, contractAddresses, "CheckContractByAddresses should return nil contract addresses")
}
func TestCheckContractByAddresses_InvalidMatchType(t *testing.T) {
// Create a client for testing
client := NewClient(WithBaseURL("https://example.com"))
// Define test data
addresses := []string{"0x054B2223509D430269a31De4AE2f335890be5C8F"}
chainIds := []int{56}
// Call the function with an invalid match type
contractAddresses, err := CheckContractByAddresses(client, addresses, chainIds, "invalid")
// Verify the results
assert.Error(t, err, "CheckContractByAddresses should return an error for invalid match type")
assert.Nil(t, contractAddresses, "CheckContractByAddresses should return nil contract addresses for invalid match type")
}