forked from schwartzmx/gremgo-neptune
-
Notifications
You must be signed in to change notification settings - Fork 0
/
response_test.go
236 lines (193 loc) · 7.19 KB
/
response_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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package gremgo
import (
"log"
"reflect"
"testing"
)
/*
Dummy responses for mocking
*/
var dummySuccessfulResponse = []byte(`{"result":{"data":[{"id": 2,"label": "person","type": "vertex","properties": [
{"id": 2, "value": "vadas", "label": "name"},
{"id": 3, "value": 27, "label": "age"}]}
], "meta":{}},
"requestId":"1d6d02bd-8e56-421d-9438-3bd6d0079ff1",
"status":{"code":200,"attributes":{},"message":""}}`)
var dummyNeedAuthenticationResponse = []byte(`{"result":{},
"requestId":"1d6d02bd-8e56-421d-9438-3bd6d0079ff1",
"status":{"code":407,"attributes":{},"message":""}}`)
var dummyPartialResponse1 = []byte(`{"result":{"data":[{"id": 2,"label": "person","type": "vertex","properties": [
{"id": 2, "value": "vadas", "label": "name"},
{"id": 3, "value": 27, "label": "age"}]},
], "meta":{}},
"requestId":"1d6d02bd-8e56-421d-9438-3bd6d0079ff1",
"status":{"code":206,"attributes":{},"message":""}}`)
var dummyPartialResponse2 = []byte(`{"result":{"data":[{"id": 4,"label": "person","type": "vertex","properties": [
{"id": 5, "value": "quant", "label": "name"},
{"id": 6, "value": 54, "label": "age"}]},
], "meta":{}},
"requestId":"1d6d02bd-8e56-421d-9438-3bd6d0079ff1",
"status":{"code":200,"attributes":{},"message":""}}`)
var dummySuccessfulResponseMarshalled = Response{
RequestID: "1d6d02bd-8e56-421d-9438-3bd6d0079ff1",
Status: Status{Code: 200},
Result: Result{Data: []byte("testData")},
}
var dummyNeedAuthenticationResponseMarshalled = Response{
RequestID: "1d6d02bd-8e56-421d-9438-3bd6d0079ff1",
Status: Status{Code: 407},
Result: Result{Data: []byte("")},
}
var dummyPartialResponse1Marshalled = Response{
RequestID: "1d6d02bd-8e56-421d-9438-3bd6d0079ff1",
Status: Status{Code: 206}, // Code 206 indicates that the response is not the terminating response in a sequence of responses
Result: Result{Data: []byte("testPartialData1")},
}
var dummyPartialResponse2Marshalled = Response{
RequestID: "1d6d02bd-8e56-421d-9438-3bd6d0079ff1",
Status: Status{Code: 200},
Result: Result{Data: []byte("testPartialData2")},
}
// TestResponseHandling tests the overall response handling mechanism of gremgo
func TestResponseHandling(t *testing.T) {
c := newClient()
c.handleResponse(dummySuccessfulResponse)
var expected []Response
expected = append(expected, dummySuccessfulResponseMarshalled)
r, _ := c.retrieveResponse(dummySuccessfulResponseMarshalled.RequestID)
if reflect.TypeOf(expected).String() != reflect.TypeOf(r).String() {
t.Error("Expected data type does not match actual.")
}
}
func TestResponseAuthHandling(t *testing.T) {
c := newClient()
ws := new(Ws)
ws.auth = &auth{username: "test", password: "test"}
c.conn = ws
c.handleResponse(dummyNeedAuthenticationResponse)
req, err := prepareAuthRequest(dummyNeedAuthenticationResponseMarshalled.RequestID, "test", "test")
if err != nil {
return
}
sampleAuthRequest, err := packageRequest(req)
if err != nil {
log.Println(err)
return
}
c.dispatchRequest(sampleAuthRequest)
authRequest := <-c.requests //Simulate that client send auth challenge to server
if !reflect.DeepEqual(authRequest, sampleAuthRequest) {
t.Error("Expected data type does not match actual.")
}
c.handleResponse(dummySuccessfulResponse) //If authentication is successful the server returns the origin petition
var expectedSuccessful []Response
expectedSuccessful = append(expectedSuccessful, dummySuccessfulResponseMarshalled)
r, _ := c.retrieveResponse(dummySuccessfulResponseMarshalled.RequestID)
if reflect.TypeOf(expectedSuccessful).String() != reflect.TypeOf(r).String() {
t.Error("Expected data type does not match actual.")
}
}
// TestResponseMarshalling tests the ability to marshal a response into a designated response struct for further manipulation
func TestResponseMarshalling(t *testing.T) {
resp, err := marshalResponse(dummySuccessfulResponse)
if err != nil {
t.Error(err)
}
if dummySuccessfulResponseMarshalled.RequestID != resp.RequestID || dummySuccessfulResponseMarshalled.Status.Code != resp.Status.Code {
t.Error("Expected requestId and code does not match actual.")
} else if reflect.TypeOf(resp.Result.Data).String() != "json.RawMessage" {
t.Error("Expected data type does not match actual.")
}
}
// TestResponseSortingSingleResponse tests the ability for sortResponse to save a response received from Gremlin Server
func TestResponseSortingSingleResponse(t *testing.T) {
c := newClient()
c.saveResponse(dummySuccessfulResponseMarshalled, nil)
var expected []interface{}
expected = append(expected, dummySuccessfulResponseMarshalled)
result, _ := c.results.Load(dummySuccessfulResponseMarshalled.RequestID)
if reflect.DeepEqual(result.([]interface{}), expected) != true {
t.Fail()
}
}
// TestResponseSortingMultipleResponse tests the ability for the sortResponse function to categorize and group responses that are sent in a stream
func TestResponseSortingMultipleResponse(t *testing.T) {
c := newClient()
c.saveResponse(dummyPartialResponse1Marshalled, nil)
c.saveResponse(dummyPartialResponse2Marshalled, nil)
var expected []interface{}
expected = append(expected, dummyPartialResponse1Marshalled)
expected = append(expected, dummyPartialResponse2Marshalled)
results, _ := c.results.Load(dummyPartialResponse1Marshalled.RequestID)
if reflect.DeepEqual(results.([]interface{}), expected) != true {
t.Fail()
}
}
// TestResponseRetrieval tests the ability for a requester to retrieve the response for a specified requestId generated when sending the request
func TestResponseRetrieval(t *testing.T) {
c := newClient()
c.saveResponse(dummyPartialResponse1Marshalled, nil)
c.saveResponse(dummyPartialResponse2Marshalled, nil)
resp, _ := c.retrieveResponse(dummyPartialResponse1Marshalled.RequestID)
var expected []Response
expected = append(expected, dummyPartialResponse1Marshalled)
expected = append(expected, dummyPartialResponse2Marshalled)
if reflect.DeepEqual(resp, expected) != true {
t.Fail()
}
}
// TestResponseDeletion tests the ability for a requester to clean up after retrieving a response after delivery to a client
func TestResponseDeletion(t *testing.T) {
c := newClient()
c.saveResponse(dummyPartialResponse1Marshalled, nil)
c.saveResponse(dummyPartialResponse2Marshalled, nil)
c.deleteResponse(dummyPartialResponse1Marshalled.RequestID)
if _, ok := c.results.Load(dummyPartialResponse1Marshalled.RequestID); ok {
t.Fail()
}
}
var codes = []struct {
code int
}{
{200},
{204},
{206},
{401},
{407},
{498},
{499},
{500},
{597},
{598},
{599},
{3434}, // Testing unknown error code
}
// Tests detection of errors and if an error is generated for a specific error code
func TestResponseErrorDetection(t *testing.T) {
for _, co := range codes {
dummyResponse := Response{
RequestID: "",
Status: Status{Code: co.code},
Result: Result{},
}
err := dummyResponse.detectError()
switch {
case co.code == 200:
if err != nil {
t.Log("Successful response returned error.")
}
case co.code == 204:
if err != nil {
t.Log("Successful response returned error.")
}
case co.code == 206:
if err != nil {
t.Log("Successful response returned error.")
}
default:
if err == nil {
t.Log("Unsuccessful response did not return error.")
}
}
}
}