forked from scroll-tech/scroll
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrpc_test.go
78 lines (61 loc) · 1.65 KB
/
rpc_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
package utils
import (
"compress/flate"
"context"
"testing"
"github.com/scroll-tech/go-ethereum/rpc"
"github.com/stretchr/testify/assert"
)
type testService struct{}
type echoArgs struct {
S string
}
type echoResult struct {
Name string
ID int
Args *echoArgs
}
func (s *testService) NoArgsRets() {}
func (s *testService) Echo(str string, i int, args *echoArgs) echoResult {
return echoResult{str, i, args}
}
func TestStartHTTPEndpoint(t *testing.T) {
endpoint := "localhost:18080"
handler, _, err := StartHTTPEndpoint(endpoint, []rpc.API{
{
Public: true,
Namespace: "test",
Service: new(testService),
},
})
assert.NoError(t, err)
defer handler.Shutdown(context.Background())
client, err := rpc.Dial("http://" + endpoint)
assert.NoError(t, err)
assert.NoError(t, client.Call(nil, "test_noArgsRets"))
result := echoResult{}
assert.NoError(t, client.Call(&result, "test_echo", "test", 0, &echoArgs{S: "test"}))
assert.Equal(t, 0, result.ID)
assert.Equal(t, "test", result.Name)
defer client.Close()
}
func TestStartWSEndpoint(t *testing.T) {
endpoint := "localhost:18081"
handler, _, err := StartWSEndpoint(endpoint, []rpc.API{
{
Public: true,
Namespace: "test",
Service: new(testService),
},
}, flate.NoCompression)
assert.NoError(t, err)
defer handler.Shutdown(context.Background())
client, err := rpc.Dial("ws://" + endpoint)
assert.NoError(t, err)
assert.NoError(t, client.Call(nil, "test_noArgsRets"))
result := echoResult{}
assert.NoError(t, client.Call(&result, "test_echo", "test", 0, &echoArgs{S: "test"}))
assert.Equal(t, 0, result.ID)
assert.Equal(t, "test", result.Name)
defer client.Close()
}