-
Notifications
You must be signed in to change notification settings - Fork 145
/
ConnectTests.cs
341 lines (299 loc) · 13.3 KB
/
ConnectTests.cs
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
using System;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Neo4jClient.Cypher;
using Neo4jClient.Execution;
using NSubstitute;
using NUnit.Framework;
namespace Neo4jClient.Test.GraphClientTests
{
[TestFixture]
public class ConnectTests
{
[Test]
[ExpectedException(typeof(ApplicationException), ExpectedMessage = "Received an unexpected HTTP status when executing the request.\r\n\r\nThe response status was: 500 InternalServerError")]
public void ShouldThrowConnectionExceptionFor500Response()
{
using (var testHarness = new RestTestHarness
{
{
MockRequest.Get(""),
MockResponse.Http(500)
}
})
{
testHarness.CreateAndConnectGraphClient();
}
}
[Test]
public void ShouldRetrieveApiEndpoints()
{
using (var testHarness = new RestTestHarness())
{
var graphClient = (GraphClient)testHarness.CreateAndConnectGraphClient();
Assert.AreEqual("/node", graphClient.RootApiResponse.Node);
Assert.AreEqual("/index/node", graphClient.RootApiResponse.NodeIndex);
Assert.AreEqual("/index/relationship", graphClient.RootApiResponse.RelationshipIndex);
Assert.AreEqual("http://foo/db/data/node/123", graphClient.RootApiResponse.ReferenceNode);
Assert.AreEqual("/ext", graphClient.RootApiResponse.ExtensionsInfo);
}
}
[Test]
[ExpectedException(ExpectedMessage = "The graph client is not connected to the server. Call the Connect method first.")]
public void RootNode_ShouldThrowInvalidOperationException_WhenNotConnectedYet()
{
var graphClient = new GraphClient(new Uri("http://foo/db/data"), null);
// ReSharper disable ReturnValueOfPureMethodIsNotUsed
graphClient.RootNode.ToString();
// ReSharper restore ReturnValueOfPureMethodIsNotUsed
}
[Test]
public void RootNode_ShouldReturnReferenceNode()
{
using (var testHarness = new RestTestHarness())
{
var graphClient = testHarness.CreateAndConnectGraphClient();
Assert.IsNotNull(graphClient.RootNode);
Assert.AreEqual(123, graphClient.RootNode.Id);
}
}
[Test]
public void RootNode_ShouldReturnNullReferenceNode_WhenNoReferenceNodeDefined()
{
using (var testHarness = new RestTestHarness
{
{
MockRequest.Get(""),
MockResponse.Json(HttpStatusCode.OK, @"{
'batch' : 'http://foo/db/data/batch',
'node' : 'http://foo/db/data/node',
'node_index' : 'http://foo/db/data/index/node',
'relationship_index' : 'http://foo/db/data/index/relationship',
'extensions_info' : 'http://foo/db/data/ext',
'extensions' : {
}
}")
}
})
{
var graphClient = testHarness.CreateAndConnectGraphClient();
Assert.IsNull(graphClient.RootNode);
}
}
[Test]
public void ShouldParse15M02Version()
{
using (var testHarness = new RestTestHarness())
{
var graphClient = (GraphClient)testHarness.CreateAndConnectGraphClient();
Assert.AreEqual("1.5.0.2", graphClient.RootApiResponse.Version.ToString());
}
}
[Test]
public void ShouldReturnCypher19CapabilitiesForPre20Version()
{
using (var testHarness = new RestTestHarness())
{
var graphClient = testHarness.CreateAndConnectGraphClient();
Assert.AreEqual(CypherCapabilities.Cypher19, graphClient.CypherCapabilities);
}
}
[Test]
public void ShouldSetCypher22CapabilitiesForPost22Version()
{
using (var testHarness = new RestTestHarness())
{
var graphClient = testHarness.CreateAndConnectGraphClient(RestTestHarness.Neo4jVersion.Neo22);
Assert.AreEqual(CypherCapabilities.Cypher22, graphClient.CypherCapabilities);
}
}
[Test]
public void ShouldReturnCypher19CapabilitiesForVersion20()
{
using (var testHarness = new RestTestHarness
{
{
MockRequest.Get(""),
MockResponse.NeoRoot20()
}
})
{
var graphClient = testHarness.CreateAndConnectGraphClient();
Assert.AreEqual(CypherCapabilities.Cypher20, graphClient.CypherCapabilities);
}
}
[Test]
public void UserInfoPreservedInRootUri()
{
var graphClient = new GraphClient(new Uri("http://username:password@foo/db/data"));
Assert.That(graphClient.RootUri.OriginalString, Is.EqualTo("http://username:password@foo/db/data"));
}
[Test]
public void CredentialsPreservedAllTheWayThroughToHttpStack()
{
var httpClient = Substitute.For<IHttpClient>();
httpClient
.SendAsync(Arg.Any<HttpRequestMessage>())
.Returns(callInfo => { throw new NotImplementedException(); });
var graphClient = new GraphClient(new Uri("http://username:password@foo/db/data"), httpClient);
try
{
graphClient.Connect();
}
// ReSharper disable EmptyGeneralCatchClause
catch (NotImplementedException)
{
// This will fail because we're not giving it the right
// HTTP response, but we only care about the request for now
}
// ReSharper restore EmptyGeneralCatchClause
var httpCall = httpClient.ReceivedCalls().Last();
var httpRequest = (HttpRequestMessage) httpCall.GetArguments()[0];
StringAssert.AreEqualIgnoringCase("Basic", httpRequest.Headers.Authorization.Scheme);
StringAssert.AreEqualIgnoringCase("dXNlcm5hbWU6cGFzc3dvcmQ=", httpRequest.Headers.Authorization.Parameter);
}
[Test]
public void PassesCorrectStreamHeader_WhenUseStreamIsTrue()
{
var httpClient = Substitute.For<IHttpClient>();
httpClient
.SendAsync(Arg.Any<HttpRequestMessage>())
.Returns(callInfo => { throw new NotImplementedException(); });
var graphClient = new GraphClient(new Uri("http://username:password@foo/db/data"), httpClient);
try
{
graphClient.Connect();
}
// ReSharper disable EmptyGeneralCatchClause
catch (NotImplementedException)
{
// This will fail because we're not giving it the right
// HTTP response, but we only care about the request for now
}
// ReSharper restore EmptyGeneralCatchClause
var httpCall = httpClient.ReceivedCalls().Last();
var httpRequest = (HttpRequestMessage)httpCall.GetArguments()[0];
Assert.IsTrue(httpRequest.Headers.Contains("X-Stream"));
Assert.Contains("true", httpRequest.Headers.GetValues("X-Stream").ToList());
}
[Test]
public void PassesCorrectStreamHeader_WhenUseStreamIsFalse()
{
var httpClient = Substitute.For<IHttpClient>();
httpClient
.SendAsync(Arg.Any<HttpRequestMessage>())
.Returns(callInfo => { throw new NotImplementedException(); });
var graphClient = new GraphClient(new Uri("http://username:password@foo/db/data"), httpClient);
graphClient.ExecutionConfiguration.UseJsonStreaming = false;
try
{
graphClient.Connect();
}
// ReSharper disable EmptyGeneralCatchClause
catch (NotImplementedException)
{
// This will fail because we're not giving it the right
// HTTP response, but we only care about the request for now
}
// ReSharper restore EmptyGeneralCatchClause
var httpCall = httpClient.ReceivedCalls().Last();
var httpRequest = (HttpRequestMessage)httpCall.GetArguments()[0];
Assert.IsFalse(httpRequest.Headers.Contains("X-Stream"));
}
[Test]
public void ShouldParseRootApiResponseFromAuthenticatedConnection()
{
using (var testHarness = new RestTestHarness()
{
{ MockRequest.Get(""), MockResponse.NeoRoot() }
})
{
var httpClient = testHarness.GenerateHttpClient("http://foo/db/data");
var graphClient = new GraphClient(new Uri("http://username:password@foo/db/data"), httpClient);
graphClient.Connect();
Assert.AreEqual("/node", graphClient.RootApiResponse.Node);
}
}
[Test]
public void ShouldSendCustomUserAgent()
{
// Arrange
var httpClient = Substitute.For<IHttpClient>();
var graphClient = new GraphClient(new Uri("http://localhost"), httpClient);
var expectedUserAgent = graphClient.ExecutionConfiguration.UserAgent;
httpClient
.SendAsync(Arg.Do<HttpRequestMessage>(message =>
{
// Assert
Assert.IsTrue(message.Headers.Contains("User-Agent"), "Contains User-Agent header");
var userAgent = message.Headers.GetValues("User-Agent").Single();
Assert.AreEqual(expectedUserAgent, userAgent, "User-Agent header value is correct");
}))
.Returns(ci => {
var response = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent(@"{
'cypher' : 'http://foo/db/data/cypher',
'batch' : 'http://foo/db/data/batch',
'node' : 'http://foo/db/data/node',
'node_index' : 'http://foo/db/data/index/node',
'relationship_index' : 'http://foo/db/data/index/relationship',
'reference_node' : 'http://foo/db/data/node/123',
'neo4j_version' : '1.5.M02',
'extensions_info' : 'http://foo/db/data/ext',
'extensions' : {
'GremlinPlugin' : {
'execute_script' : 'http://foo/db/data/ext/GremlinPlugin/graphdb/execute_script'
}
}
}")
};
var task = new Task<HttpResponseMessage>(() => response);
task.Start();
return task;
});
// Act
graphClient.Connect();
}
[Test]
public void ShouldFormatUserAgentCorrectly()
{
var graphClient = new GraphClient(new Uri("http://localhost"));
var userAgent = graphClient.ExecutionConfiguration.UserAgent;
Assert.IsTrue(Regex.IsMatch(userAgent, @"Neo4jClient/\d+\.\d+\.\d+\.\d+"), "User agent should be in format Neo4jClient/1.2.3.4");
}
[Test]
public void ShouldFormatAuthorisationHeaderCorrectly()
{
const string username = "user";
const string password = "password";
var expectedHeader = Convert.ToBase64String(Encoding.ASCII.GetBytes(string.Format("{0}:{1}", username, password)));
var graphClient = new GraphClient(new Uri("http://localhost"), username, password);
var httpClient = (HttpClientWrapper) graphClient.ExecutionConfiguration.HttpClient;
Assert.AreEqual(expectedHeader, httpClient.AuthenticationHeaderValue.Parameter);
}
[Test]
public void ShouldFireOnCompletedEvenWhenException()
{
var httpClient = Substitute.For<IHttpClient>();
httpClient
.SendAsync(Arg.Any<HttpRequestMessage>())
.Returns(callInfo => { throw new NotImplementedException(); });
var graphClient = new GraphClient(new Uri("http://foo/db/data"), httpClient);
OperationCompletedEventArgs operationCompletedArgs = null;
graphClient.OperationCompleted+= (s, e) =>
{
operationCompletedArgs = e;
};
// act
Assert.Throws<NotImplementedException>(() => graphClient.Connect());
Assert.NotNull(operationCompletedArgs);
Assert.That(operationCompletedArgs.HasException);
Assert.AreEqual(typeof(NotImplementedException), operationCompletedArgs.Exception.GetType());
}
}
}