Skip to content

Commit

Permalink
Merge branch 'feature/603-bad-gateway' of https://github.com/jlukawsk…
Browse files Browse the repository at this point in the history
…a/Ocelot into jlukawska-feature/603-bad-gateway
  • Loading branch information
TomPallister committed Apr 11, 2020
2 parents 77d4bb1 + 8ead511 commit 3dfcff8
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/Ocelot/Errors/OcelotErrorCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public enum OcelotErrorCode
CannotAddPlaceholderError = 34,
CannotRemovePlaceholderError = 35,
QuotaExceededError = 36,
RequestCanceled = 37,
RequestCanceled = 37,
ConnectionToDownstreamServiceError = 38,
}
}
13 changes: 13 additions & 0 deletions src/Ocelot/Requester/ConnectionToDownstreamServiceError.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Ocelot.Errors;
using System;

namespace Ocelot.Requester
{
class ConnectionToDownstreamServiceError : Error
{
public ConnectionToDownstreamServiceError(Exception exception)
: base($"Error connecting to downstream service, exception: {exception}", OcelotErrorCode.ConnectionToDownstreamServiceError)
{
}
}
}
7 changes: 7 additions & 0 deletions src/Ocelot/Requester/HttpExeptionToErrorMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ namespace Ocelot.Requester
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Sockets;

public class HttpExeptionToErrorMapper : IExceptionToErrorMapper
{
Expand All @@ -28,6 +30,11 @@ public Error Map(Exception exception)
return new RequestCanceledError(exception.Message);
}

if (type == typeof(HttpRequestException))
{
return new ConnectionToDownstreamServiceError(exception);
}

return new UnableToCompleteRequestError(exception);
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/Ocelot/Responder/ErrorsToHttpStatusCodeMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ public int Map(List<Error> errors)
if (errors.Any(e => e.Code == OcelotErrorCode.UnableToFindDownstreamRouteError))
{
return 404;
}

if (errors.Any(e => e.Code == OcelotErrorCode.ConnectionToDownstreamServiceError))
{
return 502;
}

if (errors.Any(e => e.Code == OcelotErrorCode.UnableToCompleteRequestError))
Expand Down
4 changes: 2 additions & 2 deletions test/Ocelot.AcceptanceTests/HttpTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public void should_return_response_200_when_using_http_two_point_zero()
}

[Fact]
public void should_return_response_500_when_using_http_one_to_talk_to_server_running_http_two()
public void should_return_response_502_when_using_http_one_to_talk_to_server_running_http_two()
{
var port = RandomPortFinder.GetRandomPort();

Expand Down Expand Up @@ -177,7 +177,7 @@ public void should_return_response_500_when_using_http_one_to_talk_to_server_run
.And(x => _steps.GivenThereIsAConfiguration(configuration))
.And(x => _steps.GivenOcelotIsRunning())
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/", httpContent))
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.InternalServerError))
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.BadGateway))
.BDDfy();
}

Expand Down
32 changes: 32 additions & 0 deletions test/Ocelot.AcceptanceTests/ReturnsErrorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,38 @@ public ReturnsErrorTests()
{
_serviceHandler = new ServiceHandler();
_steps = new Steps();
}

[Fact]
public void should_return_bad_gateway_error_if_downstream_service_doesnt_respond()
{
var configuration = new FileConfiguration
{
ReRoutes = new List<FileReRoute>
{
new FileReRoute
{
DownstreamPathTemplate = "/",
UpstreamPathTemplate = "/",
UpstreamHttpMethod = new List<string> { "Get" },
DownstreamHostAndPorts = new List<FileHostAndPort>
{
new FileHostAndPort
{
Host = "localhost",
Port = 53877,
},
},
DownstreamScheme = "http",
},
},
};

this.Given(x => _steps.GivenThereIsAConfiguration(configuration))
.And(x => _steps.GivenOcelotIsRunning())
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.BadGateway))
.BDDfy();
}

[Fact]
Expand Down
2 changes: 1 addition & 1 deletion test/Ocelot.AcceptanceTests/SslTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public void should_not_dangerous_accept_any_server_certificate_validator()
.And(x => _steps.GivenThereIsAConfiguration(configuration))
.And(x => _steps.GivenOcelotIsRunning())
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.InternalServerError))
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.BadGateway))
.BDDfy();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ public void should_return_service_unavailable(OcelotErrorCode errorCode)
public void should_return_internal_server_error(OcelotErrorCode errorCode)
{
ShouldMapErrorToStatusCode(errorCode, HttpStatusCode.InternalServerError);
}

[Theory]
[InlineData(OcelotErrorCode.ConnectionToDownstreamServiceError)]
public void should_return_bad_gateway_error(OcelotErrorCode errorCode)
{
ShouldMapErrorToStatusCode(errorCode, HttpStatusCode.BadGateway);
}

[Theory]
Expand Down Expand Up @@ -125,7 +132,7 @@ public void check_we_have_considered_all_errors_in_these_tests()
// If this test fails then it's because the number of error codes has changed.
// You should make the appropriate changes to the test cases here to ensure
// they cover all the error codes, and then modify this assertion.
Enum.GetNames(typeof(OcelotErrorCode)).Length.ShouldBe(38, "Looks like the number of error codes has changed. Do you need to modify ErrorsToHttpStatusCodeMapper?");
Enum.GetNames(typeof(OcelotErrorCode)).Length.ShouldBe(39, "Looks like the number of error codes has changed. Do you need to modify ErrorsToHttpStatusCodeMapper?");
}

private void ShouldMapErrorToStatusCode(OcelotErrorCode errorCode, HttpStatusCode expectedHttpStatusCode)
Expand Down

0 comments on commit 3dfcff8

Please sign in to comment.