Skip to content

Commit cee39ae

Browse files
Mpdreamzgmarz
authored andcommitted
fix #1928 bad auth response misses status code
1 parent 952de7d commit cee39ae

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

Diff for: src/Elasticsearch.Net/Transport/Pipeline/RequestPipeline.cs

+10-1
Original file line numberDiff line numberDiff line change
@@ -465,8 +465,17 @@ public void BadResponse<TReturn>(ref ElasticsearchResponse<TReturn> response, Re
465465

466466
if (response == null)
467467
{
468-
response = new ResponseBuilder<TReturn>(data) { Exception = clientException }.ToResponse();
468+
response = new ResponseBuilder<TReturn>(data)
469+
{
470+
StatusCode = callDetails?.HttpStatusCode,
471+
Exception = clientException
472+
}.ToResponse();
469473
}
474+
if (callDetails?.ResponseBodyInBytes != null && response.ResponseBodyInBytes == null)
475+
response.ResponseBodyInBytes = callDetails.ResponseBodyInBytes;
476+
477+
if (callDetails?.ServerError != null && response.ServerError == null)
478+
response.ServerError = callDetails.ServerError;
470479

471480
response.AuditTrail = this.AuditTrail;
472481
}

Diff for: src/Tests/ClientConcepts/ConnectionPooling/Exceptions/UnrecoverableExceptions.doc.cs

+3
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ [U] public async Task BadAuthenticationHtmlResponseIsIgnored()
8989
(e) =>
9090
{
9191
e.FailureReason.Should().Be(PipelineFailure.BadAuthentication);
92+
e.Response.HttpStatusCode.Should().Be(401);
9293
e.Response.ResponseBodyInBytes.Should().BeNull();
9394
}
9495
);
@@ -112,6 +113,7 @@ [U] public async Task BadAuthenticationHtmlResponseStillExposedWhenUsingDisableD
112113
(e) =>
113114
{
114115
e.FailureReason.Should().Be(PipelineFailure.BadAuthentication);
116+
e.Response.HttpStatusCode.Should().Be(401);
115117
e.Response.ResponseBodyInBytes.Should().NotBeNull();
116118
var responseString = Encoding.UTF8.GetString(e.Response.ResponseBodyInBytes);
117119
responseString.Should().Contain("nginx/");
@@ -142,6 +144,7 @@ [U] public async Task BadAuthOnGetClientCallDoesNotThrowSerializationException()
142144
(e) =>
143145
{
144146
e.FailureReason.Should().Be(PipelineFailure.BadAuthentication);
147+
e.Response.HttpStatusCode.Should().Be(401);
145148
e.Response.ResponseBodyInBytes.Should().NotBeNull();
146149
var responseString = Encoding.UTF8.GetString(e.Response.ResponseBodyInBytes);
147150
responseString.Should().Contain("nginx/");

Diff for: src/Tests/Reproduce/GithubIssue1901.cs

+11-2
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,21 @@ private class Example
3030
<!-- a padding to disable MSIE and Chrome friendly error page -->
3131
<!-- a padding to disable MSIE and Chrome friendly error page -->";
3232

33-
[U]
34-
public async Task BadAuthResponseDoesNotThrowExceptionWhenAttemptingToDeserializeResponse()
33+
[U] public async Task BadAuthResponseDoesNotThrowExceptionWhenAttemptingToDeserializeResponse()
3534
{
3635
var client = TestClient.GetFixedReturnClient(ProxyAuthResponse, 401, contentType: "text/html", exception: new Exception("problem with the request as a result of 401"));
3736
var source = await client.LowLevel.GetSourceAsync<Example>("examples", "example", "1");
3837
source.Success.Should().BeFalse();
3938
}
39+
40+
[U] public async Task BadAuthCarriesStatusCodeAndResponseBodyOverToResponse()
41+
{
42+
var client = TestClient.GetFixedReturnClient(ProxyAuthResponse, 401, contentType: "text/html", exception: new Exception("problem with the request as a result of 401"),
43+
modifySettings: (s) => s.DisableDirectStreaming());
44+
var response = await client.LowLevel.GetAsync<Example>("examples", "example", "1");
45+
response.Success.Should().BeFalse();
46+
response.ResponseBodyInBytes.Should().NotBeNullOrEmpty();
47+
response.HttpStatusCode.Should().Be(401);
48+
}
4049
}
4150
}

0 commit comments

Comments
 (0)