diff --git a/samples/OcelotKube/ApiGateway/Startup.cs b/samples/OcelotKube/ApiGateway/Startup.cs index d7b2473c..3e37ffa3 100644 --- a/samples/OcelotKube/ApiGateway/Startup.cs +++ b/samples/OcelotKube/ApiGateway/Startup.cs @@ -1,6 +1,7 @@ using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; using Ocelot.DependencyInjection; using Ocelot.Middleware; using Ocelot.Provider.Kubernetes; @@ -18,7 +19,7 @@ namespace ApiGateway } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. - public void Configure(IApplicationBuilder app, IHostingEnvironment env) + public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { diff --git a/samples/OcelotKube/DownstreamService/Startup.cs b/samples/OcelotKube/DownstreamService/Startup.cs index 9a927a37..a8abb5d4 100644 --- a/samples/OcelotKube/DownstreamService/Startup.cs +++ b/samples/OcelotKube/DownstreamService/Startup.cs @@ -7,6 +7,7 @@ using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; @@ -28,7 +29,7 @@ namespace DownstreamService } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. - public void Configure(IApplicationBuilder app, IHostingEnvironment env) + public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { diff --git a/src/Ocelot/DownstreamMethodTransformer/Middleware/DownstreamMethodTransformerMiddleware.cs b/src/Ocelot/DownstreamMethodTransformer/Middleware/DownstreamMethodTransformerMiddleware.cs deleted file mode 100644 index a17e9167..00000000 --- a/src/Ocelot/DownstreamMethodTransformer/Middleware/DownstreamMethodTransformerMiddleware.cs +++ /dev/null @@ -1,27 +0,0 @@ -using Ocelot.Logging; -using Ocelot.Middleware; -using System.Threading.Tasks; - -namespace Ocelot.DownstreamMethodTransformer.Middleware -{ - public class DownstreamMethodTransformerMiddleware : OcelotMiddleware - { - private readonly OcelotRequestDelegate _next; - - public DownstreamMethodTransformerMiddleware(OcelotRequestDelegate next, IOcelotLoggerFactory loggerFactory) - : base(loggerFactory.CreateLogger()) - { - _next = next; - } - - public async Task Invoke(DownstreamContext context) - { - if (context.DownstreamReRoute.DownstreamHttpMethod != null) - { - context.DownstreamRequest.Method = context.DownstreamReRoute.DownstreamHttpMethod; - } - - await _next.Invoke(context); - } - } -} diff --git a/src/Ocelot/Middleware/Pipeline/OcelotPipelineExtensions.cs b/src/Ocelot/Middleware/Pipeline/OcelotPipelineExtensions.cs index 02ee07f4..d52cfb0d 100644 --- a/src/Ocelot/Middleware/Pipeline/OcelotPipelineExtensions.cs +++ b/src/Ocelot/Middleware/Pipeline/OcelotPipelineExtensions.cs @@ -2,7 +2,6 @@ using Ocelot.Authorisation.Middleware; using Ocelot.Cache.Middleware; using Ocelot.Claims.Middleware; -using Ocelot.DownstreamMethodTransformer.Middleware; using Ocelot.DownstreamRouteFinder.Middleware; using Ocelot.DownstreamUrlCreator.Middleware; using Ocelot.Errors.Middleware; @@ -69,9 +68,6 @@ namespace Ocelot.Middleware.Pipeline // Initialises downstream request builder.UseDownstreamRequestInitialiser(); - //change Http Method - builder.UseMiddleware(); - // We check whether the request is ratelimit, and if there is no continue processing builder.UseRateLimiting(); diff --git a/src/Ocelot/Request/Middleware/DownstreamRequestInitialiserMiddleware.cs b/src/Ocelot/Request/Middleware/DownstreamRequestInitialiserMiddleware.cs index 442bb4b9..c9f6f859 100644 --- a/src/Ocelot/Request/Middleware/DownstreamRequestInitialiserMiddleware.cs +++ b/src/Ocelot/Request/Middleware/DownstreamRequestInitialiserMiddleware.cs @@ -34,6 +34,11 @@ namespace Ocelot.Request.Middleware context.DownstreamRequest = _creator.Create(downstreamRequest.Data); + if (!string.IsNullOrEmpty(context.DownstreamReRoute?.DownstreamHttpMethod)) + { + context.DownstreamRequest.Method = context.DownstreamReRoute.DownstreamHttpMethod; + } + await _next.Invoke(context); } } diff --git a/src/Ocelot/Request/Middleware/HttpRequestBuilderMiddlewareExtensions.cs b/src/Ocelot/Request/Middleware/HttpRequestBuilderMiddlewareExtensions.cs index e6eff7e0..a2931cbb 100644 --- a/src/Ocelot/Request/Middleware/HttpRequestBuilderMiddlewareExtensions.cs +++ b/src/Ocelot/Request/Middleware/HttpRequestBuilderMiddlewareExtensions.cs @@ -1,5 +1,3 @@ -using Microsoft.AspNetCore.Builder; -using Ocelot.DownstreamUrlCreator.Middleware; using Ocelot.Middleware.Pipeline; namespace Ocelot.Request.Middleware @@ -8,8 +6,7 @@ namespace Ocelot.Request.Middleware { public static IOcelotPipelineBuilder UseDownstreamRequestInitialiser(this IOcelotPipelineBuilder builder) { - return builder.UseMiddleware() - .UseMiddleware(); + return builder.UseMiddleware(); } } } diff --git a/test/Ocelot.UnitTests/Request/DownstreamRequestInitialiserMiddlewareTests.cs b/test/Ocelot.UnitTests/Request/DownstreamRequestInitialiserMiddlewareTests.cs index 93d963b6..aadc551c 100644 --- a/test/Ocelot.UnitTests/Request/DownstreamRequestInitialiserMiddlewareTests.cs +++ b/test/Ocelot.UnitTests/Request/DownstreamRequestInitialiserMiddlewareTests.cs @@ -1,6 +1,4 @@ -using Ocelot.Middleware; - -namespace Ocelot.UnitTests.Request +namespace Ocelot.UnitTests.Request { using Microsoft.AspNetCore.Http; using Moq; @@ -9,6 +7,8 @@ namespace Ocelot.UnitTests.Request using Ocelot.Request.Creator; using Ocelot.Request.Mapper; using Ocelot.Request.Middleware; + using Ocelot.Configuration.Builder; + using Ocelot.Middleware; using Ocelot.Responses; using Shouldly; using System.Net.Http; @@ -65,6 +65,24 @@ namespace Ocelot.UnitTests.Request .Then(_ => ThenTheContexRequestIsMappedToADownstreamRequest()) .And(_ => ThenTheDownstreamRequestIsStored()) .And(_ => ThenTheNextMiddlewareIsInvoked()) + .And(_ => ThenTheDownstreamRequestMethodIs("GET")) + .BDDfy(); + } + + [Theory] + [InlineData("POST", "POST")] + [InlineData(null, "GET")] + [InlineData("", "GET")] + public void Should_map_downstream_reroute_method_to_downstream_request(string input, string expected) + { + this.Given(_ => GivenTheHttpContextContainsARequest()) + .And(_ => GivenTheDownstreamReRouteMethodIs(input)) + .And(_ => GivenTheMapperWillReturnAMappedRequest()) + .When(_ => WhenTheMiddlewareIsInvoked()) + .Then(_ => ThenTheContexRequestIsMappedToADownstreamRequest()) + .And(_ => ThenTheDownstreamRequestIsStored()) + .And(_ => ThenTheNextMiddlewareIsInvoked()) + .And(_ => ThenTheDownstreamRequestMethodIs(expected)) .BDDfy(); } @@ -80,6 +98,16 @@ namespace Ocelot.UnitTests.Request .BDDfy(); } + private void GivenTheDownstreamReRouteMethodIs(string input) + { + _downstreamContext.DownstreamReRoute = new DownstreamReRouteBuilder().WithDownStreamHttpMethod(input).Build(); + } + + private void ThenTheDownstreamRequestMethodIs(string expected) + { + _downstreamContext.DownstreamRequest.Method.ShouldBe(expected); + } + private void GivenTheHttpContextContainsARequest() { _httpContext