-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient_test.go
56 lines (48 loc) · 1.15 KB
/
client_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
package jsonrpc2
import (
"context"
"fmt"
"github.com/semrush/zenrpc"
"github.com/stretchr/testify/assert"
"net/http"
"net/http/httptest"
"os"
"testing"
)
var serverURL string
func TestMain(m *testing.M) {
mux := http.NewServeMux()
server := zenrpc.NewServer(zenrpc.Options{})
server.Register("", TestService{})
mux.Handle("/rpc", server)
srv := httptest.NewServer(mux)
serverURL = srv.URL
os.Exit(m.Run())
}
func Test_client_Call_Sum_Ok(t *testing.T) {
client := NewClient(fmt.Sprintf("%s/rpc", serverURL))
var val int
err := client.Call(context.Background(), "Sum", struct {
A,B int
}{1,2}, &val)
assert.NoError(t, err)
assert.Equal(t, 3, val)
}
func Test_client_Call_Div_Ok(t *testing.T) {
client := NewClient(fmt.Sprintf("%s/rpc", serverURL))
var val int
err := client.Call(context.Background(), "Divide", struct {
A,B int
}{4,2}, &val)
assert.NoError(t, err)
assert.Equal(t, 2, val)
}
func Test_client_Call_Div_Fail(t *testing.T) {
client := NewClient(fmt.Sprintf("%s/rpc", serverURL))
var val int
err := client.Call(context.Background(), "Divide", struct {
A,B int
}{4,0}, &val)
assert.Error(t, err)
assert.Zero(t, val)
}