From e205bb9cdfc1f057c1fbb28514a5fbd38c190799 Mon Sep 17 00:00:00 2001 From: Daniel Low Date: Wed, 9 Apr 2014 01:46:36 +0100 Subject: [PATCH] Correctly handle empty response bodies. The thrift representation of an empty array is the same as null, so when the body is empty (such as with a HeadSync), the current implementation of Execute will throw an exception because MemoryStream cannot handle nulls. Instead use an empty byte array. Also add integration test for this case. --- .../ThriftConnection.cs | 4 ++-- .../Connection/Thrift/ThiftBugReportTests.cs | 11 +++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/Connections/Elasticsearch.Net.Connection.Thrift/ThriftConnection.cs b/src/Connections/Elasticsearch.Net.Connection.Thrift/ThriftConnection.cs index 00ba240172e..ffca8619236 100644 --- a/src/Connections/Elasticsearch.Net.Connection.Thrift/ThriftConnection.cs +++ b/src/Connections/Elasticsearch.Net.Connection.Thrift/ThriftConnection.cs @@ -274,13 +274,13 @@ private ElasticsearchResponse Execute(RestRequest restRequest, object de if (result.Status == Status.OK || result.Status == Status.CREATED || result.Status == Status.ACCEPTED) { var response = ElasticsearchResponse.Create( - this._connectionSettings, (int)result.Status, method, path, requestData, new MemoryStream(result.Body)); + this._connectionSettings, (int)result.Status, method, path, requestData, new MemoryStream(result.Body ?? new byte[0])); return response; } else { var response = ElasticsearchResponse.Create( - this._connectionSettings, (int)result.Status, method, path, requestData, new MemoryStream(result.Body)); + this._connectionSettings, (int)result.Status, method, path, requestData, new MemoryStream(result.Body ?? new byte[0])); return response; } } diff --git a/src/Tests/Nest.Tests.Integration/Connection/Thrift/ThiftBugReportTests.cs b/src/Tests/Nest.Tests.Integration/Connection/Thrift/ThiftBugReportTests.cs index bfe544d9240..1e42ff0bfeb 100644 --- a/src/Tests/Nest.Tests.Integration/Connection/Thrift/ThiftBugReportTests.cs +++ b/src/Tests/Nest.Tests.Integration/Connection/Thrift/ThiftBugReportTests.cs @@ -26,5 +26,16 @@ public void IndexExistShouldNotThrowOn404() unknownIndexResult.ConnectionStatus.HttpStatusCode.Should().Be(404); } + + [Test] + public void EmptyResponseShouldNotThrowError() + { + var isValidThriftConnection = this._thriftClient.RootNodeInfo().IsValid; + isValidThriftConnection.Should().BeTrue(); + + var result = this._thriftClient.Connection.HeadSync(ElasticsearchConfiguration.CreateBaseUri(9500)); + result.Success.Should().BeTrue(); + result.OriginalException.Should().BeNull(); + } } }