From 7f738563f5a9d3a712af5ccd876fab8eeaa62539 Mon Sep 17 00:00:00 2001 From: Mpdreamz Date: Wed, 23 Mar 2016 14:03:54 +0100 Subject: [PATCH] fix #1928 bad auth response misses status code --- .../Transport/Pipeline/RequestPipeline.cs | 11 ++++++++++- .../Exceptions/UnrecoverableExceptions.doc.cs | 3 +++ src/Tests/Reproduce/GithubIssue1901.cs | 13 +++++++++++-- 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/src/Elasticsearch.Net/Transport/Pipeline/RequestPipeline.cs b/src/Elasticsearch.Net/Transport/Pipeline/RequestPipeline.cs index 5087d506dde..5a03e84cd40 100644 --- a/src/Elasticsearch.Net/Transport/Pipeline/RequestPipeline.cs +++ b/src/Elasticsearch.Net/Transport/Pipeline/RequestPipeline.cs @@ -467,8 +467,17 @@ public void BadResponse(ref ElasticsearchResponse response, Re if (response == null) { - response = new ResponseBuilder(data) { Exception = clientException }.ToResponse(); + response = new ResponseBuilder(data) + { + StatusCode = callDetails?.HttpStatusCode, + Exception = clientException + }.ToResponse(); } + if (callDetails?.ResponseBodyInBytes != null && response.ResponseBodyInBytes == null) + response.ResponseBodyInBytes = callDetails.ResponseBodyInBytes; + + if (callDetails?.ServerError != null && response.ServerError == null) + response.ServerError = callDetails.ServerError; response.AuditTrail = this.AuditTrail; } diff --git a/src/Tests/ClientConcepts/ConnectionPooling/Exceptions/UnrecoverableExceptions.doc.cs b/src/Tests/ClientConcepts/ConnectionPooling/Exceptions/UnrecoverableExceptions.doc.cs index b9aad24fd9f..4cb492e30aa 100644 --- a/src/Tests/ClientConcepts/ConnectionPooling/Exceptions/UnrecoverableExceptions.doc.cs +++ b/src/Tests/ClientConcepts/ConnectionPooling/Exceptions/UnrecoverableExceptions.doc.cs @@ -89,6 +89,7 @@ [U] public async Task BadAuthenticationHtmlResponseIsIgnored() (e) => { e.FailureReason.Should().Be(PipelineFailure.BadAuthentication); + e.Response.HttpStatusCode.Should().Be(401); e.Response.ResponseBodyInBytes.Should().BeNull(); } ); @@ -112,6 +113,7 @@ [U] public async Task BadAuthenticationHtmlResponseStillExposedWhenUsingDisableD (e) => { e.FailureReason.Should().Be(PipelineFailure.BadAuthentication); + e.Response.HttpStatusCode.Should().Be(401); e.Response.ResponseBodyInBytes.Should().NotBeNull(); var responseString = Encoding.UTF8.GetString(e.Response.ResponseBodyInBytes); responseString.Should().Contain("nginx/"); @@ -142,6 +144,7 @@ [U] public async Task BadAuthOnGetClientCallDoesNotThrowSerializationException() (e) => { e.FailureReason.Should().Be(PipelineFailure.BadAuthentication); + e.Response.HttpStatusCode.Should().Be(401); e.Response.ResponseBodyInBytes.Should().NotBeNull(); var responseString = Encoding.UTF8.GetString(e.Response.ResponseBodyInBytes); responseString.Should().Contain("nginx/"); diff --git a/src/Tests/Reproduce/GithubIssue1901.cs b/src/Tests/Reproduce/GithubIssue1901.cs index 65e5d116077..3487dc291e3 100644 --- a/src/Tests/Reproduce/GithubIssue1901.cs +++ b/src/Tests/Reproduce/GithubIssue1901.cs @@ -30,12 +30,21 @@ private class Example "; - [U] - public async Task BadAuthResponseDoesNotThrowExceptionWhenAttemptingToDeserializeResponse() + [U] public async Task BadAuthResponseDoesNotThrowExceptionWhenAttemptingToDeserializeResponse() { var client = TestClient.GetFixedReturnClient(ProxyAuthResponse, 401, contentType: "text/html", exception: new Exception("problem with the request as a result of 401")); var source = await client.LowLevel.GetSourceAsync("examples", "example", "1"); source.Success.Should().BeFalse(); } + + [U] public async Task BadAuthCarriesStatusCodeAndResponseBodyOverToResponse() + { + var client = TestClient.GetFixedReturnClient(ProxyAuthResponse, 401, contentType: "text/html", exception: new Exception("problem with the request as a result of 401"), + modifySettings: (s) => s.DisableDirectStreaming()); + var response = await client.LowLevel.GetAsync("examples", "example", "1"); + response.Success.Should().BeFalse(); + response.ResponseBodyInBytes.Should().NotBeNullOrEmpty(); + response.HttpStatusCode.Should().Be(401); + } } }