Ocelot/test/Ocelot.UnitTests/Middleware/OcelotPiplineBuilderTests.cs
geffzhang 903b380a5b update .net core 3.0 RTM (#1025)
* feat: update to asp.net core 3.0 preview 9

* fix :  AspDotNetLogger unittest

* feat:  update generic host  and  useMvc

1、Using 'UseMvc' to configure MVC is not supported while using Endpoint Routing https://github.com/aspnet/AspNetCore/issues/9542
2、 use IHost and IHostBuilder

* feat : update .net core 3.0 rc1

* eureka extension

* fixed logger formatter error

* fixed synchronous operations are disallowed of ReadToEnd method

* fix log tests

* Flush method of FakeStream should do nothing

* Update ContentTests.cs

* Fixed ws tests

* feat: delelte comment code

* feat: update .net core 3.0 RTM

* Update OcelotBuilderTests.cs

* Update .travis.yml

mono 6.0.0 and dotnet 3.0.100

* Update Ocelot.IntegrationTests.csproj

update Microsoft.Data.SQLite 3.0.0

* Update .travis.yml

* feat: remove FrameworkReference

1、 remove FrameworkReference
2、 update package

* add appveyor configuration to use version of VS2019 with dotnet core 3 sdk support

* update obsoleted SetCollectionValidator method

* Swap out OpenCover for Coverlet

* Bump Cake to 0.35.0

* Downgrade coveralls.net to 0.7.0
Fix disposing of PollConsul instance

* Remove environment specific path separator

* Do not return ReportGenerator on Mac/Linux

* Remove direct dependency on IInternalConfiguration

* Fix ordering of variable assignment

* Fix broken tests

* Fix acceptance tests for Consul
2019-10-28 07:24:30 +00:00

155 lines
4.7 KiB
C#

using System;
using System.Threading.Tasks;
namespace Ocelot.UnitTests.Middleware
{
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Moq;
using Ocelot.DependencyInjection;
using Ocelot.Logging;
using Ocelot.Middleware;
using Ocelot.Middleware.Pipeline;
using Shouldly;
using System.Collections.Generic;
using System.Reflection;
using TestStack.BDDfy;
using Xunit;
public class OcelotPiplineBuilderTests
{
private readonly IServiceCollection _services;
private readonly IConfiguration _configRoot;
private DownstreamContext _downstreamContext;
private int _counter;
public OcelotPiplineBuilderTests()
{
_configRoot = new ConfigurationRoot(new List<IConfigurationProvider>());
_services = new ServiceCollection();
_services.AddSingleton<IWebHostEnvironment>(GetHostingEnvironment());
_services.AddSingleton<IConfiguration>(_configRoot);
_services.AddOcelot();
}
private IWebHostEnvironment GetHostingEnvironment()
{
var environment = new Mock<IWebHostEnvironment>();
environment
.Setup(e => e.ApplicationName)
.Returns(typeof(OcelotPiplineBuilderTests).GetTypeInfo().Assembly.GetName().Name);
return environment.Object;
}
[Fact]
public void should_build_generic()
{
this.When(x => WhenIUseAGeneric())
.Then(x => ThenTheGenericIsInThePipeline())
.BDDfy();
}
[Fact]
public void should_build_func()
{
this.When(x => WhenIUseAFunc())
.Then(x => ThenTheFuncIsInThePipeline())
.BDDfy();
}
private void WhenIUseAGeneric()
{
var provider = _services.BuildServiceProvider();
IOcelotPipelineBuilder builder = new OcelotPipelineBuilder(provider);
builder = builder.UseMiddleware<Ocelot.Errors.Middleware.ExceptionHandlerMiddleware>();
var del = builder.Build();
_downstreamContext = new DownstreamContext(new DefaultHttpContext());
del.Invoke(_downstreamContext);
}
private void ThenTheGenericIsInThePipeline()
{
_downstreamContext.HttpContext.Response.StatusCode.ShouldBe(500);
}
private void WhenIUseAFunc()
{
_counter = 0;
var provider = _services.BuildServiceProvider();
IOcelotPipelineBuilder builder = new OcelotPipelineBuilder(provider);
builder = builder.Use(async (ctx, next) =>
{
_counter++;
await next.Invoke();
});
var del = builder.Build();
_downstreamContext = new DownstreamContext(new DefaultHttpContext());
del.Invoke(_downstreamContext);
}
private void ThenTheFuncIsInThePipeline()
{
_counter.ShouldBe(1);
_downstreamContext.HttpContext.Response.StatusCode.ShouldBe(404);
}
[Fact]
public void Middleware_Multi_Parameters_Invoke()
{
var provider = _services.BuildServiceProvider();
IOcelotPipelineBuilder builder = new OcelotPipelineBuilder(provider);
builder = builder.UseMiddleware<MultiParametersInvokeMiddleware>();
var del = builder.Build();
_downstreamContext = new DownstreamContext(new DefaultHttpContext());
del.Invoke(_downstreamContext);
}
private class MultiParametersInvokeMiddleware : OcelotMiddleware
{
private readonly OcelotRequestDelegate _next;
public MultiParametersInvokeMiddleware(OcelotRequestDelegate next)
: base(new FakeLogger())
{
_next = next;
}
public Task Invoke(DownstreamContext context, IServiceProvider serviceProvider)
{
return Task.CompletedTask;
}
}
}
internal class FakeLogger : IOcelotLogger
{
public void LogCritical(string message, Exception exception)
{
}
public void LogDebug(string message)
{
}
public void LogError(string message, Exception exception)
{
}
public void LogInformation(string message)
{
}
public void LogTrace(string message)
{
}
public void LogWarning(string message)
{
}
}
}