-
Notifications
You must be signed in to change notification settings - Fork 12
/
dialer_test.go
173 lines (151 loc) · 5.34 KB
/
dialer_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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package enigma_test
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/http/cookiejar"
"net/url"
"testing"
"time"
"github.com/qlik-oss/enigma-go/v4"
"github.com/stretchr/testify/assert"
)
// This example shows how to connect to a locally running Qlik Associative Engine, print the version number and disconnect again.
func Example() {
ctx := context.Background()
global, err := enigma.Dialer{}.Dial(ctx, "ws://localhost:9076", nil)
if err != nil {
panic(err)
}
engineVersion, err := global.EngineVersion(ctx)
fmt.Println(engineVersion.ComponentVersion)
global.DisconnectFromServer()
}
func TestFullRpcScenario(t *testing.T) {
ctx := context.Background()
global, _ := enigma.Dialer{MockMode: true}.Dial(context.Background(), "", nil)
testSocket := global.GetMockSocket()
testSocket.AddReceivedMessage(`{"jsonrpc":"2.0","method":"OnConnected","params":{"qSessionState":"SESSION_CREATED"}}`)
testSocket.ExpectCall(
`{"jsonrpc":"2.0","delta":false,"method":"OpenDoc","handle":-1,"id":1,"params":["doc","","","",false]}`,
`{"jsonrpc":"2.0","id":1,"result":{"qReturn":{"qType":"Doc","qHandle":1,"qGenericId":"doc.qvf"}},"change":[1]}`)
testSocket.ExpectCall(
`{"jsonrpc":"2.0","delta":false,"method":"GetObject","handle":1,"id":2,"params":["hyperhyper"]}`,
`{"jsonrpc":"2.0","id":2,"result":{"qReturn":{"qType":"GenericObject","qHandle":4,"qGenericType":"sheet","qGenericId":"JzJMza"}}}`)
testSocket.ExpectCall(
`{"jsonrpc":"2.0","delta":false,"method":"GetLayout","handle":4,"id":3,"params":[]}`,
`{
"jsonrpc": "2.0",
"id": 3,
"delta": false,
"result": {
"qLayout": {
"qInfo": {
"qId": "SheetList",
"qType": "SheetList"
},
"qAppObjectList": {
"qItems": [{
"qInfo": {
"qId": "GnAzpy",
"qType": "sheet"
},
"qMeta": {
"title": "Budget Analysis",
"description": "Analyze actual versus budget budget data. Is the company on target to hit its budgeted amounts?"
},
"qData": {
"customLayoutField": "customdata"
}
}]
}
}
}
}`)
testSocket.ExpectCall(
`{"jsonrpc":"2.0","delta":false,"method":"GetLayout","handle":4,"id":4,"params":[]}`,
`{
"jsonrpc": "2.0",
"id": 4,
"delta": false,
"error": {
"code": 123,
"parameter": "param",
"message":"mes"
}
}`)
// Check the OnConnected information
sessionState, err := global.SessionState(ctx)
assert.Equal(t, "SESSION_CREATED", sessionState)
// Continue with opening the doc
doc, _ := global.OpenDoc(ctx, "doc", "", "", "", false)
obj, _ := doc.GetObject(ctx, "hyperhyper")
type CustomLayout struct {
Info enigma.NxInfo `json:"qInfo,omitempty"`
Meta enigma.NxMeta `json:"qMeta,omitempty"`
AppObjectList struct {
Items []struct {
enigma.NxContainerEntry
Data struct {
CustomLayoutField string `json:"customLayoutField,omitempty"`
} `json:"qData,omitempty"`
} `json:"qItems,omitempty"`
} `json:"qAppObjectList,omitempty"`
}
layout := &CustomLayout{}
layoutRaw, err := obj.GetLayoutRaw(ctx)
if err != nil {
fmt.Println(err)
return
}
json.Unmarshal(layoutRaw, layout)
assert.Equal(t, layout.AppObjectList.Items[0].Info.Id, "GnAzpy")
assert.Equal(t, layout.AppObjectList.Items[0].Data.CustomLayoutField, "customdata")
_, err = obj.GetLayoutRaw(ctx)
enigmaError := err.(enigma.Error)
assert.Equal(t, enigmaError.Code(), 123)
assert.Equal(t, enigmaError.Parameter(), "param")
assert.Equal(t, enigmaError.Message(), "mes")
testSocket.Close()
}
func TestCookieJar(t *testing.T) {
dialer := enigma.Dialer{MockMode: true}
jar, err := cookiejar.New(nil)
dialer.Jar = jar
assert.NoError(t, err, "Error creating cookiejar")
// Have some cookies!
header := http.Header{}
exp := fmt.Sprintf("%v", time.Now().Local().Add(time.Hour*time.Duration(48)).UTC())
header.Add("Set-Cookie", "_session=a518840f-893b-4baf-bdf8-10d78ec14bf5; path=/; expires="+exp+"; secure; httponly")
header.Add("Set-Cookie", "_grant=1d3cdfb9-25d0-42b2-8274-d4b11b97a475; path=/interaction/1d3cdfb9-25d0-42b2-8274-d4b11b97a475; expires="+exp+"; secure; httponly")
header.Add("Set-Cookie", "_grant=1d3cdfb9-25d0-42b2-8274-d4b11b97a475; path=/auth/1d3cdfb9-25d0-42b2-8274-d4b11b97a475; expires="+exp+"; secure; httponly")
response := http.Response{Header: header}
cookies := response.Cookies()
// Set the cookies
url, err := url.Parse("https://www.qlik.com")
assert.NoError(t, err, "Error parsing URL")
dialer.Jar.SetCookies(url, cookies)
// Test whether correct cookie is returned for request to /
returnedCookies := dialer.Jar.Cookies(url)
gotResponse := false
for _, cookie := range returnedCookies {
if cookie.Name == "_session" {
assert.Equal(t, "a518840f-893b-4baf-bdf8-10d78ec14bf5", cookie.Value)
gotResponse = true
}
}
assert.True(t, gotResponse, fmt.Sprintf("Expected one cookie for path: <%v>", url.Path))
// Test whether correct cookie is returns for request to interaction URL
url, err = url.Parse("https://www.qlik.com/interaction/1d3cdfb9-25d0-42b2-8274-d4b11b97a475")
returnedCookies = dialer.Jar.Cookies(url)
assert.NoError(t, err, "Error parsing URL")
gotResponse = false
for _, cookie := range returnedCookies {
if cookie.Name == "_grant" {
assert.Equal(t, "1d3cdfb9-25d0-42b2-8274-d4b11b97a475", cookie.Value)
gotResponse = true
}
}
assert.True(t, gotResponse, fmt.Sprintf("Expected one cookie for path: <%v>", url.Path))
}