-
Notifications
You must be signed in to change notification settings - Fork 1
/
methods_source_test.go
65 lines (55 loc) · 1.64 KB
/
methods_source_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
package sourcify
import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"github.com/ethereum/go-ethereum/common"
"github.com/stretchr/testify/assert"
)
func TestGetContractSourceCode(t *testing.T) {
// Create a mock HTTP server
mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/files/any/1/0x0000000000000000000000001234567890aBcdEF" {
// Simulate a successful response with sample source code
sourceCodes := &SourceCodes{
Status: "success",
Code: []SourceCode{
{
Name: "Contract.sol",
Path: "/path/to/contract.sol",
Content: "contract MyContract { }",
},
},
}
err := json.NewEncoder(w).Encode(sourceCodes)
if err != nil {
t.Errorf("failed to encode mock source codes: %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
chainID := 1
contractAddress := common.HexToAddress("0x1234567890abcdef")
matchType := MethodMatchTypeAny
// Call the function to get contract source code
sourceCodes, err := GetContractSourceCode(client, chainID, contractAddress, matchType)
// Verify the results
assert.NoError(t, err, "GetContractSourceCode returned an error")
expectedSourceCodes := &SourceCodes{
Status: "success",
Code: []SourceCode{
{
Name: "Contract.sol",
Path: "/path/to/contract.sol",
Content: "contract MyContract { }",
},
},
}
assert.Equal(t, expectedSourceCodes, sourceCodes, "GetContractSourceCode returned unexpected source codes")
}