Skip to content

Commit 502975b

Browse files
committed
Wrote more unit tests that check wheter .ServerError is properly set. Works in unit tests not in integration tests..
1 parent a2127de commit 502975b

File tree

7 files changed

+114
-6
lines changed

7 files changed

+114
-6
lines changed

src/Elasticsearch.Net/Connection/InMemoryConnection.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ namespace Elasticsearch.Net.Connection
1111
public class InMemoryConnection : HttpConnection
1212
{
1313
private byte[] _fixedResultBytes = Encoding.UTF8.GetBytes("{ \"USING NEST IN MEMORY CONNECTION\" : null }");
14+
private int _statusCode;
15+
1416
public InMemoryConnection()
1517
: base(new ConnectionConfiguration())
1618
{
@@ -22,10 +24,11 @@ public InMemoryConnection(IConnectionConfigurationValues settings)
2224

2325
}
2426

25-
public InMemoryConnection(IConnectionConfigurationValues settings, string fixedResult)
27+
public InMemoryConnection(IConnectionConfigurationValues settings, string fixedResult, int statusCode = 200)
2628
: this(settings)
2729
{
2830
_fixedResultBytes = Encoding.UTF8.GetBytes(fixedResult);
31+
_statusCode = statusCode;
2932
}
3033

3134
protected override ElasticsearchResponse<Stream> DoSynchronousRequest(HttpWebRequest request, byte[] data = null, IRequestConfiguration requestSpecificConfig = null)
@@ -38,7 +41,7 @@ private ElasticsearchResponse<Stream> ReturnConnectionStatus(HttpWebRequest requ
3841
var method = request.Method;
3942
var path = request.RequestUri.ToString();
4043

41-
var cs = ElasticsearchResponse<Stream>.Create(this.ConnectionSettings, 200, method, path, data);
44+
var cs = ElasticsearchResponse<Stream>.Create(this.ConnectionSettings, _statusCode, method, path, data);
4245
cs.Response = new MemoryStream(_fixedResultBytes);
4346
if (this.ConnectionSettings.ConnectionStatusHandler != null)
4447
this.ConnectionSettings.ConnectionStatusHandler(cs);

src/Elasticsearch.Net/Connection/Transport.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,7 @@ private ElasticsearchServerError ThrowOrGetErrorFromStreamResponse<T>(
660660
var raw = ms.ToArray().Utf8String();
661661
}
662662
ms.Position = 0;
663+
streamResponse.Response.Close();
663664
streamResponse.Response = ms;
664665
}
665666
else

src/Tests/Nest.Tests.Integration/Exceptions/ElasticsearchExceptionTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public void ConnectionException_WithThrowingClient_Async()
4848
.ThrowOnElasticsearchServerExceptions());
4949
Assert.Throws<WebException>(async () => await client.RootNodeInfoAsync());
5050
}
51+
5152
[Test]
5253
public void ServerError_Is_Set_ClientThat_DoesNotThow()
5354
{

src/Tests/Nest.Tests.Unit/BaseJsonTests.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,19 @@ protected void deb(string s)
4141
//Lazy programmers for the win!
4242
throw new Exception(s);
4343
}
44-
protected ElasticClient GetFixedReturnClient(MethodBase methodInfo, string fileName)
44+
protected ElasticClient GetFixedReturnClient(
45+
MethodBase methodInfo,
46+
string fileName = null,
47+
int statusCode = 200,
48+
Func<ConnectionSettings, ConnectionSettings> alterSettings = null
49+
)
4550
{
46-
var settings = new ConnectionSettings(UnitTestDefaults.Uri, UnitTestDefaults.DefaultIndex)
47-
.ExposeRawResponse();
51+
Func<ConnectionSettings, ConnectionSettings> alter = alterSettings ?? (s => s);
52+
var settings = alter(new ConnectionSettings(UnitTestDefaults.Uri, UnitTestDefaults.DefaultIndex)
53+
.ExposeRawResponse());
4854
var file = this.GetFileFromMethod(methodInfo, fileName);
4955
var jsonResponse = File.ReadAllText(file);
50-
var connection = new InMemoryConnection(this._settings, jsonResponse);
56+
var connection = new InMemoryConnection(settings, jsonResponse, statusCode);
5157
var client = new ElasticClient(settings, connection);
5258
return client;
5359
}

src/Tests/Nest.Tests.Unit/Internals/Exceptions/BadQuery.json

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
using System.Reflection;
2+
using Elasticsearch.Net.Connection;
3+
using FluentAssertions;
4+
using Nest.Tests.MockData.Domain;
5+
using Newtonsoft.Json.Converters;
6+
using NUnit.Framework;
7+
using System;
8+
using Elasticsearch.Net;
9+
10+
namespace Nest.Tests.Unit.Internals.Exceptions
11+
{
12+
[TestFixture]
13+
public class BadQueryServerExceptionTests : BaseJsonTests
14+
{
15+
[Test]
16+
public void ReturnedServerExceptionIsAvailableInFull()
17+
{
18+
var client = this.GetFixedReturnClient(MethodBase.GetCurrentMethod(), "BadQuery", 400);
19+
20+
//this always returns as if the query was malfored because we inject the returned
21+
//response in the line above
22+
var result = client.Search<ElasticsearchProject>(q => q.MatchAll());
23+
24+
var error = result.ServerError;
25+
26+
error.Should().NotBeNull();
27+
28+
error.ExceptionType.Should().Be("SearchPhaseExecutionException");
29+
error.Status.Should().Be(400);
30+
error.Error.Should().StartWith("Failed to execute phase [query]");
31+
error.Error.Should().EndWith("}");
32+
}
33+
34+
[Test]
35+
public async void ReturnedServerExceptionIsAvailableInFull_Async()
36+
{
37+
var client = this.GetFixedReturnClient(MethodBase.GetCurrentMethod(), "BadQuery", 400);
38+
39+
//this always returns as if the query was malfored because we inject the returned
40+
//response in the line above
41+
var result = await client.SearchAsync<ElasticsearchProject>(q => q.MatchAll());
42+
43+
var error = result.ServerError;
44+
45+
error.Should().NotBeNull();
46+
47+
error.ExceptionType.Should().Be("SearchPhaseExecutionException");
48+
error.Status.Should().Be(400);
49+
error.Error.Should().StartWith("Failed to execute phase [query]");
50+
error.Error.Should().EndWith("}");
51+
}
52+
53+
[Test]
54+
public void ReturnedServerExceptionIsAvailableInFull_NotExposingRawResponse()
55+
{
56+
var client = this.GetFixedReturnClient(MethodBase.GetCurrentMethod(), "BadQuery", 400, s=> s.ExposeRawResponse(false));
57+
58+
//this always returns as if the query was malfored because we inject the returned
59+
//response in the line above
60+
var result = client.Search<ElasticsearchProject>(q => q.MatchAll());
61+
62+
var error = result.ServerError;
63+
64+
error.Should().NotBeNull();
65+
66+
error.ExceptionType.Should().Be("SearchPhaseExecutionException");
67+
error.Status.Should().Be(400);
68+
error.Error.Should().StartWith("Failed to execute phase [query]");
69+
error.Error.Should().EndWith("}");
70+
}
71+
72+
[Test]
73+
public async void ReturnedServerExceptionIsAvailableInFull_NotExposingRawResponse_Async()
74+
{
75+
var client = this.GetFixedReturnClient(MethodBase.GetCurrentMethod(), "BadQuery", 400, s=> s.ExposeRawResponse(false));
76+
77+
//this always returns as if the query was malfored because we inject the returned
78+
//response in the line above
79+
var result = await client.SearchAsync<ElasticsearchProject>(q => q.MatchAll());
80+
81+
var error = result.ServerError;
82+
83+
error.Should().NotBeNull();
84+
85+
error.ExceptionType.Should().Be("SearchPhaseExecutionException");
86+
error.Status.Should().Be(400);
87+
error.Error.Should().StartWith("Failed to execute phase [query]");
88+
error.Error.Should().EndWith("}");
89+
}
90+
91+
}
92+
}

src/Tests/Nest.Tests.Unit/Nest.Tests.Unit.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@
194194
<Compile Include="Extensions\StringExtensionsTests.cs" />
195195
<Compile Include="Extensions\UriExtensionsTests.cs" />
196196
<Compile Include="BigBadUrlUnitTests.cs" />
197+
<Compile Include="Internals\Exceptions\BadQueryServerExceptionTests.cs" />
197198
<Compile Include="Internals\Inferno\EscapedFormatTests.cs" />
198199
<Compile Include="Internals\Inferno\HostNameWithPathTests.cs" />
199200
<Compile Include="Internals\Inferno\MapTypeNamesTests.cs" />
@@ -607,6 +608,9 @@
607608
<None Include="Core\Update\UpsertUsingScript.json">
608609
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
609610
</None>
611+
<None Include="Internals\Exceptions\BadQuery.json">
612+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
613+
</None>
610614
<None Include="Internals\Serialize\ClassWithCollectionSerializes.json">
611615
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
612616
</None>

0 commit comments

Comments
 (0)