mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-04-22 06:22:50 +08:00
Added the possibility to manage the placeholders from outside ocelot (#724)
This commit is contained in:
parent
9bbb6364f2
commit
35253025c7
@ -35,6 +35,8 @@
|
|||||||
FileValidationFailedError,
|
FileValidationFailedError,
|
||||||
UnableToFindDelegatingHandlerProviderError,
|
UnableToFindDelegatingHandlerProviderError,
|
||||||
CouldNotFindPlaceholderError,
|
CouldNotFindPlaceholderError,
|
||||||
CouldNotFindAggregatorError
|
CouldNotFindAggregatorError,
|
||||||
|
CannotAddPlaceholderError,
|
||||||
|
CannotRemovePlaceholderError
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
12
src/Ocelot/Infrastructure/CannotAddPlaceholderError.cs
Normal file
12
src/Ocelot/Infrastructure/CannotAddPlaceholderError.cs
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
using Ocelot.Errors;
|
||||||
|
|
||||||
|
namespace Ocelot.Infrastructure
|
||||||
|
{
|
||||||
|
public class CannotAddPlaceholderError : Error
|
||||||
|
{
|
||||||
|
public CannotAddPlaceholderError(string message)
|
||||||
|
: base(message, OcelotErrorCode.CannotAddPlaceholderError)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
12
src/Ocelot/Infrastructure/CannotRemovePlaceholderError.cs
Normal file
12
src/Ocelot/Infrastructure/CannotRemovePlaceholderError.cs
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
using Ocelot.Errors;
|
||||||
|
|
||||||
|
namespace Ocelot.Infrastructure
|
||||||
|
{
|
||||||
|
public class CannotRemovePlaceholderError : Error
|
||||||
|
{
|
||||||
|
public CannotRemovePlaceholderError(string message)
|
||||||
|
: base(message, OcelotErrorCode.CannotRemovePlaceholderError)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,3 +1,4 @@
|
|||||||
|
using System;
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
using Ocelot.Request.Middleware;
|
using Ocelot.Request.Middleware;
|
||||||
using Ocelot.Responses;
|
using Ocelot.Responses;
|
||||||
@ -8,5 +9,7 @@ namespace Ocelot.Infrastructure
|
|||||||
{
|
{
|
||||||
Response<string> Get(string key);
|
Response<string> Get(string key);
|
||||||
Response<string> Get(string key, DownstreamRequest request);
|
Response<string> Get(string key, DownstreamRequest request);
|
||||||
|
Response Add(string key, Func<Response<string>> func);
|
||||||
|
Response Remove(string key);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -59,6 +59,28 @@ namespace Ocelot.Infrastructure
|
|||||||
return new ErrorResponse<string>(new CouldNotFindPlaceholderError(key));
|
return new ErrorResponse<string>(new CouldNotFindPlaceholderError(key));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Response Add(string key, Func<Response<string>> 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<Response<string>> GetRemoteIpAddress()
|
private Func<Response<string>> GetRemoteIpAddress()
|
||||||
{
|
{
|
||||||
return () =>
|
return () =>
|
||||||
|
@ -91,5 +91,37 @@ namespace Ocelot.UnitTests.Infrastructure
|
|||||||
var result = _placeholders.Get("{TraceId}");
|
var result = _placeholders.Get("{TraceId}");
|
||||||
result.Data.ShouldBe(traceId);
|
result.Data.ShouldBe(traceId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void should_return_ok_when_added()
|
||||||
|
{
|
||||||
|
var result = _placeholders.Add("{Test}", () => new OkResponse<string>("test"));
|
||||||
|
result.IsError.ShouldBeFalse();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void should_return_ok_when_removed()
|
||||||
|
{
|
||||||
|
var result = _placeholders.Add("{Test}", () => new OkResponse<string>("test"));
|
||||||
|
result = _placeholders.Remove("{Test}");
|
||||||
|
result.IsError.ShouldBeFalse();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void should_return_error_when_added()
|
||||||
|
{
|
||||||
|
var result = _placeholders.Add("{Test}", () => new OkResponse<string>("test"));
|
||||||
|
result = _placeholders.Add("{Test}", () => new OkResponse<string>("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");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -125,7 +125,7 @@ namespace Ocelot.UnitTests.Responder
|
|||||||
// If this test fails then it's because the number of error codes has changed.
|
// 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
|
// You should make the appropriate changes to the test cases here to ensure
|
||||||
// they cover all the error codes, and then modify this assertion.
|
// 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)
|
private void ShouldMapErrorToStatusCode(OcelotErrorCode errorCode, HttpStatusCode expectedHttpStatusCode)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user