mirror of
				https://github.com/nsnail/Ocelot.git
				synced 2025-11-04 15:30:49 +08:00 
			
		
		
		
	tests passing
This commit is contained in:
		@@ -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())
 | 
			
		||||
            {
 | 
			
		||||
 
 | 
			
		||||
@@ -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())
 | 
			
		||||
            {
 | 
			
		||||
 
 | 
			
		||||
@@ -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);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -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<DownstreamMethodTransformerMiddleware>();
 | 
			
		||||
 | 
			
		||||
            // We check whether the request is ratelimit, and if there is no continue processing
 | 
			
		||||
            builder.UseRateLimiting();
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -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);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
@@ -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<DownstreamRequestInitialiserMiddleware>()
 | 
			
		||||
                .UseMiddleware<DownstreamMethodTransformerMiddleware>();
 | 
			
		||||
            return builder.UseMiddleware<DownstreamRequestInitialiserMiddleware>();
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -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
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user