tests passing

This commit is contained in:
TomPallister 2020-02-09 15:37:27 +00:00
parent 4bd2243f5e
commit 6bd903bc87
7 changed files with 41 additions and 40 deletions

View File

@ -1,6 +1,7 @@
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Ocelot.DependencyInjection; using Ocelot.DependencyInjection;
using Ocelot.Middleware; using Ocelot.Middleware;
using Ocelot.Provider.Kubernetes; 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. // 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()) if (env.IsDevelopment())
{ {

View File

@ -7,6 +7,7 @@ using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options; 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. // 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()) if (env.IsDevelopment())
{ {

View File

@ -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<DownstreamMethodTransformerMiddleware>())
{
_next = next;
}
public async Task Invoke(DownstreamContext context)
{
if (context.DownstreamReRoute.DownstreamHttpMethod != null)
{
context.DownstreamRequest.Method = context.DownstreamReRoute.DownstreamHttpMethod;
}
await _next.Invoke(context);
}
}
}

View File

@ -2,7 +2,6 @@
using Ocelot.Authorisation.Middleware; using Ocelot.Authorisation.Middleware;
using Ocelot.Cache.Middleware; using Ocelot.Cache.Middleware;
using Ocelot.Claims.Middleware; using Ocelot.Claims.Middleware;
using Ocelot.DownstreamMethodTransformer.Middleware;
using Ocelot.DownstreamRouteFinder.Middleware; using Ocelot.DownstreamRouteFinder.Middleware;
using Ocelot.DownstreamUrlCreator.Middleware; using Ocelot.DownstreamUrlCreator.Middleware;
using Ocelot.Errors.Middleware; using Ocelot.Errors.Middleware;
@ -69,9 +68,6 @@ namespace Ocelot.Middleware.Pipeline
// Initialises downstream request // Initialises downstream request
builder.UseDownstreamRequestInitialiser(); builder.UseDownstreamRequestInitialiser();
//change Http Method
builder.UseMiddleware<DownstreamMethodTransformerMiddleware>();
// We check whether the request is ratelimit, and if there is no continue processing // We check whether the request is ratelimit, and if there is no continue processing
builder.UseRateLimiting(); builder.UseRateLimiting();

View File

@ -34,6 +34,11 @@ namespace Ocelot.Request.Middleware
context.DownstreamRequest = _creator.Create(downstreamRequest.Data); context.DownstreamRequest = _creator.Create(downstreamRequest.Data);
if (!string.IsNullOrEmpty(context.DownstreamReRoute?.DownstreamHttpMethod))
{
context.DownstreamRequest.Method = context.DownstreamReRoute.DownstreamHttpMethod;
}
await _next.Invoke(context); await _next.Invoke(context);
} }
} }

View File

@ -1,5 +1,3 @@
using Microsoft.AspNetCore.Builder;
using Ocelot.DownstreamUrlCreator.Middleware;
using Ocelot.Middleware.Pipeline; using Ocelot.Middleware.Pipeline;
namespace Ocelot.Request.Middleware namespace Ocelot.Request.Middleware
@ -8,8 +6,7 @@ namespace Ocelot.Request.Middleware
{ {
public static IOcelotPipelineBuilder UseDownstreamRequestInitialiser(this IOcelotPipelineBuilder builder) public static IOcelotPipelineBuilder UseDownstreamRequestInitialiser(this IOcelotPipelineBuilder builder)
{ {
return builder.UseMiddleware<DownstreamRequestInitialiserMiddleware>() return builder.UseMiddleware<DownstreamRequestInitialiserMiddleware>();
.UseMiddleware<DownstreamMethodTransformerMiddleware>();
} }
} }
} }

View File

@ -1,6 +1,4 @@
using Ocelot.Middleware; namespace Ocelot.UnitTests.Request
namespace Ocelot.UnitTests.Request
{ {
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Moq; using Moq;
@ -9,6 +7,8 @@ namespace Ocelot.UnitTests.Request
using Ocelot.Request.Creator; using Ocelot.Request.Creator;
using Ocelot.Request.Mapper; using Ocelot.Request.Mapper;
using Ocelot.Request.Middleware; using Ocelot.Request.Middleware;
using Ocelot.Configuration.Builder;
using Ocelot.Middleware;
using Ocelot.Responses; using Ocelot.Responses;
using Shouldly; using Shouldly;
using System.Net.Http; using System.Net.Http;
@ -65,6 +65,24 @@ namespace Ocelot.UnitTests.Request
.Then(_ => ThenTheContexRequestIsMappedToADownstreamRequest()) .Then(_ => ThenTheContexRequestIsMappedToADownstreamRequest())
.And(_ => ThenTheDownstreamRequestIsStored()) .And(_ => ThenTheDownstreamRequestIsStored())
.And(_ => ThenTheNextMiddlewareIsInvoked()) .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(); .BDDfy();
} }
@ -80,6 +98,16 @@ namespace Ocelot.UnitTests.Request
.BDDfy(); .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() private void GivenTheHttpContextContainsARequest()
{ {
_httpContext _httpContext