-
Notifications
You must be signed in to change notification settings - Fork 12
/
session_test.go
92 lines (70 loc) · 2.62 KB
/
session_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
package enigma
import (
"encoding/json"
"github.com/stretchr/testify/assert"
"golang.org/x/net/context"
"net/http"
"reflect"
"testing"
)
func createAndConnectSession() (*session, *MockSocket, *RemoteObject) {
// Setup qix session
session := newSession(&Dialer{
CreateSocket: func(ctx context.Context, url string, header http.Header) (Socket, error) { return NewMockSocket("") },
})
session.connect(context.Background(), "", nil)
// Create rpc object
rpcObject := session.getRemoteObject(&ObjectInterface{Handle: -1})
return session, session.GetMockSocket(), rpcObject
}
func TestRpcInvocation(t *testing.T) {
ctx := context.Background()
_, testSocket, rpcObject := createAndConnectSession()
testSocket.ExpectCall(
`{"jsonrpc":"2.0","delta":false,"method":"DummyQixMethod","handle":-1,"id":1,"params":["a","b"]}`,
`{"handle": -1, "id": 1, "result": "resultstring"}`)
// Invoke rpc Method
resultHolder := ""
rpcerr := rpcObject.RPC(ctx, "DummyQixMethod", &resultHolder, "a", "b")
// Expected response should be received
assert.Equal(t, resultHolder, "resultstring")
assert.Nil(t, rpcerr)
}
func TestRpcInvocationWithChangeListContext(t *testing.T) {
cl := ChangeLists{}
ctx := context.WithValue(context.Background(), ChangeListsKey{}, &cl)
_, testSocket, rpcObject := createAndConnectSession()
testSocket.ExpectCall(
`{"jsonrpc":"2.0","delta":false,"method":"DummyQixMethod","handle":-1,"id":1,"params":["a","b"]}`,
`{"handle": -1, "id": 1, "result": "resultstring","change":[1,7,8,9,10,11,12,13,14,15,16,17],"close":[5,6]}`)
// Invoke rpc Method
resultHolder := ""
rpcerr := rpcObject.RPC(ctx, "DummyQixMethod", &resultHolder, "a", "b")
// Expected response should be received
assert.Equal(t, "resultstring", resultHolder)
assert.Nil(t, rpcerr)
expectedChanges := []int{1, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17}
assert.Equal(t, expectedChanges, cl.Changed)
expectedCloses := []int{5, 6}
assert.Equal(t, expectedCloses, cl.Closed)
}
func TestClose(t *testing.T) {
session, testSocket, _ := createAndConnectSession()
// Close socket and wait for suspend event
testSocket.Close()
<-session.Disconnected()
}
func TestRawArrayConversion(t *testing.T) {
p1 := json.RawMessage("string1")
p2 := json.RawMessage("string2")
params := []interface{}{p1, p2}
result := convertToRawSliceIfNeeded(params)
assert.Equal(t, "[]json.RawMessage", reflect.TypeOf(result).String())
}
func TestRawArrayConversionWithoutRawContent(t *testing.T) {
p1 := "string1"
p2 := "string2"
params := []interface{}{p1, p2}
result := convertToRawSliceIfNeeded(params)
assert.Equal(t, params, result, "Should not convert the slice")
}