This repository has been archived by the owner on Oct 25, 2024. It is now read-only.
forked from schwartzmx/gremtune
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathrequest_test.go
146 lines (122 loc) · 4.23 KB
/
request_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
package gremcos
import (
"encoding/json"
"testing"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
mock_interfaces "github.com/supplyon/gremcos/test/mocks/interfaces"
)
// TestRequestPreparation tests the ability to package a query and a set of bindings into a request struct for further manipulation
func TestRequestPreparation(t *testing.T) {
query := "g.V(x)"
bindings := map[string]interface{}{"x": "10"}
rebindings := map[string]interface{}{}
req, id, err := prepareRequestWithBindings(query, bindings, rebindings)
require.NoError(t, err)
expectedRequest := request{
RequestID: id,
Op: "eval",
Processor: "",
Args: map[string]interface{}{
"gremlin": query,
"bindings": bindings,
"language": "gremlin-groovy",
"rebindings": rebindings,
},
}
assert.Equal(t, req, expectedRequest)
}
// TestRequestPackaging tests the ability for gremcos to format a request using the established Gremlin Server WebSockets protocol for delivery to the server
func TestRequestPackaging(t *testing.T) {
testRequest := request{
RequestID: "1d6d02bd-8e56-421d-9438-3bd6d0079ff1",
Op: "eval",
Processor: "",
Args: map[string]interface{}{
"gremlin": "g.V(x)",
"bindings": map[string]string{"x": "10"},
"language": "gremlin-groovy",
},
}
msg, err := packageRequest(testRequest)
require.NoError(t, err)
j, err := json.Marshal(testRequest)
require.NoError(t, err)
var expected []byte
lenMimeType := byte(len(MimeType))
expected = append(expected, lenMimeType)
expected = append(expected, MimeType...)
expected = append(expected, j...)
assert.Equal(t, msg, expected)
}
// TestRequestDispatch tests the ability for a requester to send a request to the client for writing to Gremlin Server
func TestRequestDispatch(t *testing.T) {
// GIVEN
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
mockedDialer := mock_interfaces.NewMockDialer(mockCtrl)
testRequest := request{
RequestID: "1d6d02bd-8e56-421d-9438-3bd6d0079ff1",
Op: "eval",
Processor: "",
Args: map[string]interface{}{
"gremlin": "g.V(x)",
"bindings": map[string]string{"x": "10"},
"language": "gremlin-groovy",
},
}
c := newClient(mockedDialer)
msg, err := packageRequest(testRequest)
require.NoError(t, err)
// WHEN
c.dispatchRequest(msg)
// c.requests is the channel where all requests are sent for writing
// to Gremlin Server, write workers listen on this channel
req := <-c.requests
// THEN
assert.Equal(t, msg, req)
}
// TestAuthRequestDispatch tests the ability for a requester to send a request to the client for writing to Gremlin Server
func TestAuthRequestDispatch(t *testing.T) {
// GIVEN
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
mockedDialer := mock_interfaces.NewMockDialer(mockCtrl)
id := "1d6d02bd-8e56-421d-9438-3bd6d0079ff1"
testRequest := prepareAuthRequest(id, "test", "root")
c := newClient(mockedDialer)
msg, err := packageRequest(testRequest)
require.NoError(t, err)
// WHEN
c.dispatchRequest(msg)
// c.requests is the channel where all requests are sent for writing
// to Gremlin Server, write workers listen on this channel
req := <-c.requests
// THEN
assert.Equal(t, msg, req)
}
// TestAuthRequestPreparation tests the ability to create successful authentication request
func TestAuthRequestPreparation(t *testing.T) {
id := "1d6d02bd-8e56-421d-9438-3bd6d0079ff1"
testRequest := prepareAuthRequest(id, "test", "root")
assert.Equal(t, testRequest.RequestID, id)
assert.Equal(t, "traversal", testRequest.Processor)
assert.Equal(t, "authentication", testRequest.Op)
assert.Len(t, testRequest.Args, 1)
assert.NotEmpty(t, testRequest.Args["sasl"])
assert.Equal(t, "AHRlc3QAcm9vdA==", testRequest.Args["sasl"])
}
func TestPrepareRequest(t *testing.T) {
query := "g.V()"
testRequest, id, err := prepareRequest(query)
require.NoError(t, err)
assert.NotEmpty(t, id)
assert.Equal(t, "", testRequest.Processor)
assert.Equal(t, "eval", testRequest.Op)
assert.Len(t, testRequest.Args, 2)
assert.NotEmpty(t, testRequest.Args["language"])
assert.Equal(t, "gremlin-groovy", testRequest.Args["language"])
assert.NotEmpty(t, testRequest.Args["gremlin"])
assert.Equal(t, query, testRequest.Args["gremlin"])
}