-
Notifications
You must be signed in to change notification settings - Fork 1
/
websocket_test.go
62 lines (46 loc) · 1.36 KB
/
websocket_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
// SPDX-FileCopyrightText: 2020-2024 caixw
//
// SPDX-License-Identifier: MIT
package jsonrpc
import (
"context"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/gorilla/websocket"
"github.com/issue9/assert/v4"
)
var _ Transport = &websocketTransport{}
func TestNewWebsocketTransport(t *testing.T) {
a := assert.New(t, false)
rpcServer := initServer(a)
ctx, cancel := context.WithCancel(context.Background())
upgrader := websocket.Upgrader{CheckOrigin: func(r *http.Request) bool { return true }}
a.NotNil(upgrader)
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
conn, err := upgrader.Upgrade(w, r, nil)
a.NotError(err).NotNil(conn)
t := NewWebsocketTransport(conn)
c := rpcServer.NewConn(t, nil)
c.Serve(ctx)
}))
defer srv.Close()
dialer := &websocket.Dialer{}
conn, _, err := dialer.Dial(strings.Replace(srv.URL, "http", "ws", 1)+"/websocket", nil)
a.NotError(err)
client := rpcServer.NewConn(NewWebsocketTransport(conn), nil)
err = client.Notify("f1", &inType{Age: 18})
a.NotError(err)
err = client.Send("f1", &inType{Age: 18}, func(out *outType) error {
a.Equal(out.Age, 18)
return nil
})
a.NotError(err)
err = client.Send("f1", &inType{Age: 19, Last: "l"}, func(out *outType) error {
a.Equal(out.Age, 19).Equal(out.Name, "l")
return nil
})
a.NotError(err)
cancel()
}