diff --git a/src/Ocelot/Errors/OcelotErrorCode.cs b/src/Ocelot/Errors/OcelotErrorCode.cs index 7a2dd030..a971c8cd 100644 --- a/src/Ocelot/Errors/OcelotErrorCode.cs +++ b/src/Ocelot/Errors/OcelotErrorCode.cs @@ -35,6 +35,8 @@ FileValidationFailedError, UnableToFindDelegatingHandlerProviderError, CouldNotFindPlaceholderError, - CouldNotFindAggregatorError + CouldNotFindAggregatorError, + CannotAddPlaceholderError, + CannotRemovePlaceholderError } } diff --git a/src/Ocelot/Infrastructure/CannotAddPlaceholderError.cs b/src/Ocelot/Infrastructure/CannotAddPlaceholderError.cs new file mode 100644 index 00000000..93ca6857 --- /dev/null +++ b/src/Ocelot/Infrastructure/CannotAddPlaceholderError.cs @@ -0,0 +1,12 @@ +using Ocelot.Errors; + +namespace Ocelot.Infrastructure +{ + public class CannotAddPlaceholderError : Error + { + public CannotAddPlaceholderError(string message) + : base(message, OcelotErrorCode.CannotAddPlaceholderError) + { + } + } +} diff --git a/src/Ocelot/Infrastructure/CannotRemovePlaceholderError.cs b/src/Ocelot/Infrastructure/CannotRemovePlaceholderError.cs new file mode 100644 index 00000000..335a8da1 --- /dev/null +++ b/src/Ocelot/Infrastructure/CannotRemovePlaceholderError.cs @@ -0,0 +1,12 @@ +using Ocelot.Errors; + +namespace Ocelot.Infrastructure +{ + public class CannotRemovePlaceholderError : Error + { + public CannotRemovePlaceholderError(string message) + : base(message, OcelotErrorCode.CannotRemovePlaceholderError) + { + } + } +} \ No newline at end of file diff --git a/src/Ocelot/Infrastructure/IPlaceholders.cs b/src/Ocelot/Infrastructure/IPlaceholders.cs index 1d2bbfa5..24b8e2ae 100644 --- a/src/Ocelot/Infrastructure/IPlaceholders.cs +++ b/src/Ocelot/Infrastructure/IPlaceholders.cs @@ -1,3 +1,4 @@ +using System; using System.Net.Http; using Ocelot.Request.Middleware; using Ocelot.Responses; @@ -8,5 +9,7 @@ namespace Ocelot.Infrastructure { Response Get(string key); Response Get(string key, DownstreamRequest request); + Response Add(string key, Func> func); + Response Remove(string key); } -} \ No newline at end of file +} diff --git a/src/Ocelot/Infrastructure/Placeholders.cs b/src/Ocelot/Infrastructure/Placeholders.cs index 3aecec31..325499e5 100644 --- a/src/Ocelot/Infrastructure/Placeholders.cs +++ b/src/Ocelot/Infrastructure/Placeholders.cs @@ -59,6 +59,28 @@ namespace Ocelot.Infrastructure return new ErrorResponse(new CouldNotFindPlaceholderError(key)); } + public Response Add(string key, Func> func) + { + if(_placeholders.ContainsKey(key)) + { + return new ErrorResponse(new CannotAddPlaceholderError($"Unable to add placeholder: {key}, placeholder already exists")); + } + + _placeholders.Add(key, func); + return new OkResponse(); + } + + public Response Remove(string key) + { + if(!_placeholders.ContainsKey(key)) + { + return new ErrorResponse(new CannotRemovePlaceholderError($"Unable to remove placeholder: {key}, placeholder does not exists")); + } + + _placeholders.Remove(key); + return new OkResponse(); + } + private Func> GetRemoteIpAddress() { return () => diff --git a/test/Ocelot.UnitTests/Infrastructure/PlaceholdersTests.cs b/test/Ocelot.UnitTests/Infrastructure/PlaceholdersTests.cs index 191d15fc..d2ac7401 100644 --- a/test/Ocelot.UnitTests/Infrastructure/PlaceholdersTests.cs +++ b/test/Ocelot.UnitTests/Infrastructure/PlaceholdersTests.cs @@ -91,5 +91,37 @@ namespace Ocelot.UnitTests.Infrastructure var result = _placeholders.Get("{TraceId}"); result.Data.ShouldBe(traceId); } + + [Fact] + public void should_return_ok_when_added() + { + var result = _placeholders.Add("{Test}", () => new OkResponse("test")); + result.IsError.ShouldBeFalse(); + } + + [Fact] + public void should_return_ok_when_removed() + { + var result = _placeholders.Add("{Test}", () => new OkResponse("test")); + result = _placeholders.Remove("{Test}"); + result.IsError.ShouldBeFalse(); + } + + [Fact] + public void should_return_error_when_added() + { + var result = _placeholders.Add("{Test}", () => new OkResponse("test")); + result = _placeholders.Add("{Test}", () => new OkResponse("test")); + result.IsError.ShouldBeTrue(); + result.Errors[0].Message.ShouldBe("Unable to add placeholder: {Test}, placeholder already exists"); + } + + [Fact] + public void should_return_error_when_removed() + { + var result = _placeholders.Remove("{Test}"); + result.IsError.ShouldBeTrue(); + result.Errors[0].Message.ShouldBe("Unable to remove placeholder: {Test}, placeholder does not exists"); + } } } diff --git a/test/Ocelot.UnitTests/Responder/ErrorsToHttpStatusCodeMapperTests.cs b/test/Ocelot.UnitTests/Responder/ErrorsToHttpStatusCodeMapperTests.cs index 9460300b..54e39eac 100644 --- a/test/Ocelot.UnitTests/Responder/ErrorsToHttpStatusCodeMapperTests.cs +++ b/test/Ocelot.UnitTests/Responder/ErrorsToHttpStatusCodeMapperTests.cs @@ -125,7 +125,7 @@ namespace Ocelot.UnitTests.Responder // 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(34, "Looks like the number of error codes has changed. Do you need to modify ErrorsToHttpStatusCodeMapper?"); + Enum.GetNames(typeof(OcelotErrorCode)).Length.ShouldBe(36, "Looks like the number of error codes has changed. Do you need to modify ErrorsToHttpStatusCodeMapper?"); } private void ShouldMapErrorToStatusCode(OcelotErrorCode errorCode, HttpStatusCode expectedHttpStatusCode)