Skip to content

Commit a9a07fa

Browse files
committed
Add tests for DisableDirectStreaming
1 parent 2a6978d commit a9a07fa

File tree

2 files changed

+88
-3
lines changed

2 files changed

+88
-3
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
using Elasticsearch.Net;
2+
using FluentAssertions;
3+
using Nest;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Linq;
7+
using System.Text;
8+
using System.Threading.Tasks;
9+
using Tests.Framework;
10+
11+
namespace Tests.ClientConcepts.LowLevel
12+
{
13+
public class DirectStreaming
14+
{
15+
[U]
16+
public void DisableDirectStreamingOnError()
17+
{
18+
Action<IResponse> assert = r =>
19+
{
20+
r.ApiCall.Should().NotBeNull();
21+
r.ApiCall.RequestBodyInBytes.Should().NotBeNull();
22+
r.ApiCall.ResponseBodyInBytes.Should().NotBeNull();
23+
};
24+
25+
var client = TestClient.GetFixedReturnClient(new { }, 404, s => s.DisableDirectStreaming());
26+
var response = client.Search<object>(s => s);
27+
assert(response);
28+
response = client.SearchAsync<object>(s => s).Result;
29+
assert(response);
30+
}
31+
32+
[U]
33+
public void EnableDirectStreamingOnError()
34+
{
35+
Action<IResponse> assert = r =>
36+
{
37+
r.ApiCall.Should().NotBeNull();
38+
r.ApiCall.RequestBodyInBytes.Should().BeNull();
39+
r.ApiCall.ResponseBodyInBytes.Should().BeNull();
40+
};
41+
42+
var client = TestClient.GetFixedReturnClient(new { }, 404);
43+
var response = client.Search<object>(s => s);
44+
assert(response);
45+
response = client.SearchAsync<object>(s => s).Result;
46+
assert(response);
47+
}
48+
49+
[U]
50+
public void DisableDirectStreamingOnSuccess()
51+
{
52+
Action<IResponse> assert = r =>
53+
{
54+
r.ApiCall.Should().NotBeNull();
55+
r.ApiCall.RequestBodyInBytes.Should().NotBeNull();
56+
r.ApiCall.ResponseBodyInBytes.Should().NotBeNull();
57+
};
58+
59+
var client = TestClient.GetFixedReturnClient(new { }, 200, s => s.DisableDirectStreaming());
60+
var response = client.Search<object>(s => s);
61+
assert(response);
62+
response = client.SearchAsync<object>(s => s).Result;
63+
assert(response);
64+
}
65+
66+
[U]
67+
public void EnableDirectStreamingOnSuccess()
68+
{
69+
Action<IResponse> assert = r =>
70+
{
71+
r.ApiCall.Should().NotBeNull();
72+
r.ApiCall.RequestBodyInBytes.Should().BeNull();
73+
r.ApiCall.ResponseBodyInBytes.Should().BeNull();
74+
};
75+
76+
var client = TestClient.GetFixedReturnClient(new { });
77+
var response = client.Search<object>(s => s);
78+
assert(response);
79+
response = client.SearchAsync<object>(s => s).Result;
80+
assert(response);
81+
}
82+
}
83+
}

src/Tests/Framework/TestClient.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ public static IConnection CreateConnection(ConnectionSettings settings = null, b
7676
? ((IConnection)new HttpConnection())
7777
: new InMemoryConnection();
7878

79-
public static IElasticClient GetFixedReturnClient(object responseJson)
79+
public static IElasticClient GetFixedReturnClient(
80+
object responseJson, int statusCode = 200, Func<ConnectionSettings, ConnectionSettings> modifySettings = null)
8081
{
8182
var serializer = new JsonNetSerializer(new ConnectionSettings());
8283
byte[] fixedResult;
@@ -85,9 +86,10 @@ public static IElasticClient GetFixedReturnClient(object responseJson)
8586
serializer.Serialize(responseJson, ms);
8687
fixedResult = ms.ToArray();
8788
}
88-
var connection = new InMemoryConnection(fixedResult);
89+
var connection = new InMemoryConnection(fixedResult, statusCode);
8990
var connectionPool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
90-
var settings = new ConnectionSettings(connectionPool, connection);
91+
var defaultSettings = new ConnectionSettings(connectionPool, connection);
92+
var settings = (modifySettings != null) ? modifySettings(defaultSettings) : defaultSettings;
9193
return new ElasticClient(settings);
9294
}
9395

0 commit comments

Comments
 (0)