Skip to content

Commit c49a3ae

Browse files
committed
InMemoryConnection serializes PostData
InMemoryConnection is consistent with NEST 1.x behaviour Added a logging example to demonstrate how to use OnRequestCompleted() to capture requests and responses
1 parent 245418f commit c49a3ae

File tree

2 files changed

+96
-15
lines changed

2 files changed

+96
-15
lines changed

src/Elasticsearch.Net/Connection/InMemoryConnection.cs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.IO;
4+
using System.IO.Compression;
45
using System.Text;
56
using System.Threading.Tasks;
67

@@ -11,8 +12,6 @@ public class InMemoryConnection : HttpConnection
1112
private readonly byte[] _responseBody;
1213
private readonly int _statusCode;
1314

14-
public List<Tuple<string, Uri, PostData<object>>> Requests = new List<Tuple<string, Uri, PostData<object>>>();
15-
1615
public InMemoryConnection()
1716
{
1817
_statusCode = 200;
@@ -34,10 +33,23 @@ protected ElasticsearchResponse<TReturn> ReturnConnectionStatus<TReturn>(Request
3433
where TReturn : class
3534
{
3635
var body = responseBody ?? _responseBody;
37-
var builder = new ResponseBuilder<TReturn>(requestData)
36+
var data = requestData.PostData;
37+
if (data != null)
38+
{
39+
using (var stream = new MemoryStream())
40+
{
41+
if (requestData.HttpCompression)
42+
using (var zipStream = new GZipStream(stream, CompressionMode.Compress))
43+
data.Write(zipStream, requestData.ConnectionSettings);
44+
else
45+
data.Write(stream, requestData.ConnectionSettings);
46+
}
47+
}
48+
49+
var builder = new ResponseBuilder<TReturn>(requestData)
3850
{
3951
StatusCode = statusCode ?? this._statusCode,
40-
Stream = (body != null) ? new MemoryStream(body) : null
52+
Stream = (body != null) ? new MemoryStream(body) : null,
4153
};
4254
var cs = builder.ToResponse();
4355
return cs;

src/Tests/ClientConcepts/LowLevel/Connecting.doc.cs

Lines changed: 80 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Collections.Specialized;
44
using System.Net;
5+
using System.Text;
56
using System.Threading.Tasks;
67
using Elasticsearch.Net;
78
using FluentAssertions;
@@ -173,22 +174,90 @@ public void AvailableOptions()
173174
*/
174175
}
175176

176-
/**
177-
* You can pass a callback of type `Action&lt;IApiCallDetails&gt;` that can eaves drop every time a response (good or bad) is created.
178-
* If you have complex logging needs this is a good place to add that in.
177+
/**
178+
* You can pass a callback of type `Action&lt;IApiCallDetails&gt;` that can eaves drop every time a response (good or bad) is created.
179+
* If you have complex logging needs this is a good place to add that in.
180+
*/
181+
[U]
182+
public void OnRequestCompletedIsCalled()
183+
{
184+
var counter = 0;
185+
var connectionPool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
186+
var settings = new ConnectionSettings(connectionPool, new InMemoryConnection())
187+
.OnRequestCompleted(r => counter++);
188+
var client = new ElasticClient(settings);
189+
client.RootNodeInfo();
190+
counter.Should().Be(1);
191+
client.RootNodeInfoAsync();
192+
counter.Should().Be(2);
193+
}
194+
195+
/**
196+
* An example of using `OnRequestCompleted()` for complex logging. Remember, if you would also like
197+
* to capture the request and/or response bytes, you also need to set `.DisableDirectStreaming()`
198+
* to `true`
179199
*/
180-
[U]public void OnRequestCompletedIsCalled()
200+
[U]public async Task UsingOnRequestCompletedForLogging()
181201
{
182-
var counter = 0;
202+
var list = new List<string>();
183203
var connectionPool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
184204
var settings = new ConnectionSettings(connectionPool, new InMemoryConnection())
185-
.OnRequestCompleted(r => counter++);
205+
.DisableDirectStreaming()
206+
.OnRequestCompleted(response =>
207+
{
208+
// log out the request
209+
if (response.RequestBodyInBytes != null)
210+
{
211+
list.Add(
212+
$"{response.HttpMethod} {response.Uri} \n" +
213+
$"{Encoding.UTF8.GetString(response.RequestBodyInBytes)}");
214+
}
215+
else
216+
{
217+
list.Add($"{response.HttpMethod} {response.Uri}");
218+
}
219+
220+
// log out the response
221+
if (response.ResponseBodyInBytes != null)
222+
{
223+
list.Add($"Status: {response.HttpStatusCode}\n" +
224+
$"{Encoding.UTF8.GetString(response.ResponseBodyInBytes)}\n" +
225+
$"{new string('-', 30)}\n");
226+
}
227+
else
228+
{
229+
list.Add($"Status: {response.HttpStatusCode}\n" +
230+
$"{new string('-', 30)}\n");
231+
}
232+
});
233+
186234
var client = new ElasticClient(settings);
187-
client.RootNodeInfo();
188-
counter.Should().Be(1);
189-
client.RootNodeInfoAsync();
190-
counter.Should().Be(2);
191-
}
235+
236+
var syncResponse = client.Search<object>(s => s
237+
.Scroll("2m")
238+
.Sort(ss => ss
239+
.Ascending(SortSpecialField.DocumentIndexOrder)
240+
)
241+
);
242+
243+
list.Count.Should().Be(2);
244+
245+
var asyncResponse = await client.SearchAsync<object>(s => s
246+
.Scroll("2m")
247+
.Sort(ss => ss
248+
.Ascending(SortSpecialField.DocumentIndexOrder)
249+
)
250+
);
251+
252+
list.Count.Should().Be(4);
253+
list.ShouldAllBeEquivalentTo(new []
254+
{
255+
"POST http://localhost:9200/_search?scroll=2m \n{\"sort\":[{\"_doc\":{\"order\":\"asc\"}}]}",
256+
"Status: 200\n------------------------------\n",
257+
"POST http://localhost:9200/_search?scroll=2m \n{\"sort\":[{\"_doc\":{\"order\":\"asc\"}}]}",
258+
"Status: 200\n------------------------------\n"
259+
});
260+
}
192261

193262
public void ConfiguringSSL()
194263
{

0 commit comments

Comments
 (0)