mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-06-19 06:48:16 +08:00
Delegating Handlers from container (#266)
* #259 quickly hacked poc for this together, needs tidying up maybe * #266 +semver: breaking removed adding delegating handler funcs directly...i feel from container is enough
This commit is contained in:
@ -26,8 +26,9 @@ namespace Ocelot.AcceptanceTests
|
||||
_steps = new Steps();
|
||||
}
|
||||
|
||||
|
||||
[Fact]
|
||||
public void should_call_handlers()
|
||||
public void should_call_di_handlers()
|
||||
{
|
||||
var configuration = new FileConfiguration
|
||||
{
|
||||
@ -42,7 +43,7 @@ namespace Ocelot.AcceptanceTests
|
||||
new FileHostAndPort
|
||||
{
|
||||
Host = "localhost",
|
||||
Port = 61879,
|
||||
Port = 7187,
|
||||
}
|
||||
},
|
||||
UpstreamPathTemplate = "/",
|
||||
@ -51,27 +52,98 @@ namespace Ocelot.AcceptanceTests
|
||||
}
|
||||
};
|
||||
|
||||
var handlerOne = new FakeHandler();
|
||||
var handlerTwo = new FakeHandler();
|
||||
|
||||
this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:61879", "/", 200, "Hello from Laura"))
|
||||
this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:7187", "/", 200, "Hello from Laura"))
|
||||
.And(x => _steps.GivenThereIsAConfiguration(configuration))
|
||||
.And(x => _steps.GivenOcelotIsRunningWithHandlers(handlerOne, handlerTwo))
|
||||
.And(x => _steps.GivenOcelotIsRunningWithHandlersRegisteredInDi<FakeHandler, FakeHandlerTwo>())
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
|
||||
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
|
||||
.And(x => _steps.ThenTheResponseBodyShouldBe("Hello from Laura"))
|
||||
.And(x => ThenTheHandlersAreCalledCorrectly(handlerOne, handlerTwo))
|
||||
.And(x => ThenTheHandlersAreCalledCorrectly())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void ThenTheHandlersAreCalledCorrectly(FakeHandler one, FakeHandler two)
|
||||
|
||||
[Fact]
|
||||
public void should_call_di_handlers_with_dependency()
|
||||
{
|
||||
one.TimeCalled.ShouldBeLessThan(two.TimeCalled);
|
||||
var configuration = new FileConfiguration
|
||||
{
|
||||
ReRoutes = new List<FileReRoute>
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamPathTemplate = "/",
|
||||
DownstreamScheme = "http",
|
||||
DownstreamHostAndPorts = new List<FileHostAndPort>
|
||||
{
|
||||
new FileHostAndPort
|
||||
{
|
||||
Host = "localhost",
|
||||
Port = 7188,
|
||||
}
|
||||
},
|
||||
UpstreamPathTemplate = "/",
|
||||
UpstreamHttpMethod = new List<string> { "Get" },
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
var dependency = new FakeDependency();
|
||||
|
||||
this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:7188", "/", 200, "Hello from Laura"))
|
||||
.And(x => _steps.GivenThereIsAConfiguration(configuration))
|
||||
.And(x => _steps.GivenOcelotIsRunningWithHandlersRegisteredInDi<FakeHandlerWithDependency>(dependency))
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
|
||||
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
|
||||
.And(x => _steps.ThenTheResponseBodyShouldBe("Hello from Laura"))
|
||||
.And(x => ThenTheDependencyIsCalled(dependency))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void ThenTheDependencyIsCalled(FakeDependency dependency)
|
||||
{
|
||||
dependency.Called.ShouldBeTrue();
|
||||
}
|
||||
|
||||
private void ThenTheHandlersAreCalledCorrectly()
|
||||
{
|
||||
FakeHandler.TimeCalled.ShouldBeLessThan(FakeHandlerTwo.TimeCalled);
|
||||
}
|
||||
|
||||
public class FakeDependency
|
||||
{
|
||||
public bool Called;
|
||||
}
|
||||
|
||||
class FakeHandlerWithDependency : DelegatingHandler
|
||||
{
|
||||
private FakeDependency _dependency;
|
||||
|
||||
public FakeHandlerWithDependency(FakeDependency dependency)
|
||||
{
|
||||
_dependency = dependency;
|
||||
}
|
||||
|
||||
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
|
||||
{
|
||||
_dependency.Called = true;
|
||||
return await base.SendAsync(request, cancellationToken);
|
||||
}
|
||||
}
|
||||
|
||||
class FakeHandler : DelegatingHandler
|
||||
{
|
||||
public DateTime TimeCalled { get; private set; }
|
||||
public static DateTime TimeCalled { get; private set; }
|
||||
|
||||
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
|
||||
{
|
||||
TimeCalled = DateTime.Now;
|
||||
return await base.SendAsync(request, cancellationToken);
|
||||
}
|
||||
}
|
||||
class FakeHandlerTwo : DelegatingHandler
|
||||
{
|
||||
public static DateTime TimeCalled { get; private set; }
|
||||
|
||||
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
|
||||
{
|
||||
|
@ -24,6 +24,7 @@ using ConfigurationBuilder = Microsoft.Extensions.Configuration.ConfigurationBui
|
||||
using Ocelot.AcceptanceTests.Caching;
|
||||
using System.IO.Compression;
|
||||
using System.Text;
|
||||
using static Ocelot.AcceptanceTests.HttpDelegatingHandlersTests;
|
||||
|
||||
namespace Ocelot.AcceptanceTests
|
||||
{
|
||||
@ -174,10 +175,9 @@ namespace Ocelot.AcceptanceTests
|
||||
_ocelotClient = _ocelotServer.CreateClient();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// This is annoying cos it should be in the constructor but we need to set up the file before calling startup so its a step.
|
||||
/// </summary>
|
||||
public void GivenOcelotIsRunningWithHandlers(DelegatingHandler handlerOne, DelegatingHandler handlerTwo)
|
||||
public void GivenOcelotIsRunningWithHandlersRegisteredInDi<TOne, TWo>()
|
||||
where TOne : DelegatingHandler
|
||||
where TWo : DelegatingHandler
|
||||
{
|
||||
_webHostBuilder = new WebHostBuilder();
|
||||
|
||||
@ -195,8 +195,40 @@ namespace Ocelot.AcceptanceTests
|
||||
{
|
||||
s.AddSingleton(_webHostBuilder);
|
||||
s.AddOcelot()
|
||||
.AddDelegatingHandler(() => handlerOne)
|
||||
.AddDelegatingHandler(() => handlerTwo);
|
||||
.AddDelegatingHandler<TOne>()
|
||||
.AddDelegatingHandler<TWo>();
|
||||
})
|
||||
.Configure(a =>
|
||||
{
|
||||
a.UseOcelot().Wait();
|
||||
});
|
||||
|
||||
_ocelotServer = new TestServer(_webHostBuilder);
|
||||
|
||||
_ocelotClient = _ocelotServer.CreateClient();
|
||||
}
|
||||
|
||||
public void GivenOcelotIsRunningWithHandlersRegisteredInDi<TOne>(FakeDependency dependency)
|
||||
where TOne : DelegatingHandler
|
||||
{
|
||||
_webHostBuilder = new WebHostBuilder();
|
||||
|
||||
_webHostBuilder
|
||||
.ConfigureAppConfiguration((hostingContext, config) =>
|
||||
{
|
||||
config.SetBasePath(hostingContext.HostingEnvironment.ContentRootPath);
|
||||
var env = hostingContext.HostingEnvironment;
|
||||
config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
|
||||
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);
|
||||
config.AddJsonFile("configuration.json");
|
||||
config.AddEnvironmentVariables();
|
||||
})
|
||||
.ConfigureServices(s =>
|
||||
{
|
||||
s.AddSingleton(_webHostBuilder);
|
||||
s.AddSingleton<FakeDependency>(dependency);
|
||||
s.AddOcelot()
|
||||
.AddDelegatingHandler<TOne>();
|
||||
})
|
||||
.Configure(a =>
|
||||
{
|
||||
|
722
test/Ocelot.AcceptanceTests/Steps.cs.orig
Normal file
722
test/Ocelot.AcceptanceTests/Steps.cs.orig
Normal file
@ -0,0 +1,722 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using CacheManager.Core;
|
||||
using IdentityServer4.AccessTokenValidation;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.TestHost;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using Ocelot.Configuration.File;
|
||||
using Ocelot.DependencyInjection;
|
||||
using Ocelot.Middleware;
|
||||
using Shouldly;
|
||||
using ConfigurationBuilder = Microsoft.Extensions.Configuration.ConfigurationBuilder;
|
||||
using Ocelot.AcceptanceTests.Caching;
|
||||
<<<<<<< HEAD
|
||||
using static Ocelot.AcceptanceTests.HttpDelegatingHandlersTests;
|
||||
||||||| merged common ancestors
|
||||
=======
|
||||
using System.IO.Compression;
|
||||
using System.Text;
|
||||
>>>>>>> develop
|
||||
|
||||
namespace Ocelot.AcceptanceTests
|
||||
{
|
||||
public class Steps : IDisposable
|
||||
{
|
||||
private TestServer _ocelotServer;
|
||||
private HttpClient _ocelotClient;
|
||||
private HttpResponseMessage _response;
|
||||
private HttpContent _postContent;
|
||||
private BearerToken _token;
|
||||
public HttpClient OcelotClient => _ocelotClient;
|
||||
public string RequestIdKey = "OcRequestId";
|
||||
private readonly Random _random;
|
||||
private IWebHostBuilder _webHostBuilder;
|
||||
|
||||
public Steps()
|
||||
{
|
||||
_random = new Random();
|
||||
}
|
||||
|
||||
public void GivenThereIsAConfiguration(FileConfiguration fileConfiguration)
|
||||
{
|
||||
var configurationPath = TestConfiguration.ConfigurationPath;
|
||||
|
||||
var jsonConfiguration = JsonConvert.SerializeObject(fileConfiguration);
|
||||
|
||||
if (File.Exists(configurationPath))
|
||||
{
|
||||
File.Delete(configurationPath);
|
||||
}
|
||||
|
||||
File.WriteAllText(configurationPath, jsonConfiguration);
|
||||
}
|
||||
|
||||
public void GivenThereIsAConfiguration(FileConfiguration fileConfiguration, string configurationPath)
|
||||
{
|
||||
var jsonConfiguration = JsonConvert.SerializeObject(fileConfiguration);
|
||||
|
||||
if (File.Exists(configurationPath))
|
||||
{
|
||||
File.Delete(configurationPath);
|
||||
}
|
||||
|
||||
File.WriteAllText(configurationPath, jsonConfiguration);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// This is annoying cos it should be in the constructor but we need to set up the file before calling startup so its a step.
|
||||
/// </summary>
|
||||
public void GivenOcelotIsRunning()
|
||||
{
|
||||
_webHostBuilder = new WebHostBuilder();
|
||||
|
||||
_webHostBuilder
|
||||
.ConfigureAppConfiguration((hostingContext, config) =>
|
||||
{
|
||||
config.SetBasePath(hostingContext.HostingEnvironment.ContentRootPath);
|
||||
var env = hostingContext.HostingEnvironment;
|
||||
config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
|
||||
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);
|
||||
config.AddJsonFile("configuration.json");
|
||||
config.AddEnvironmentVariables();
|
||||
})
|
||||
.ConfigureServices(s =>
|
||||
{
|
||||
s.AddOcelot();
|
||||
})
|
||||
.Configure(app =>
|
||||
{
|
||||
app.UseOcelot().Wait();
|
||||
});
|
||||
|
||||
_ocelotServer = new TestServer(_webHostBuilder);
|
||||
|
||||
_ocelotClient = _ocelotServer.CreateClient();
|
||||
}
|
||||
|
||||
internal void GivenOcelotIsRunningUsingButterfly(string butterflyUrl)
|
||||
{
|
||||
_webHostBuilder = new WebHostBuilder();
|
||||
|
||||
_webHostBuilder
|
||||
.ConfigureAppConfiguration((hostingContext, config) =>
|
||||
{
|
||||
config.SetBasePath(hostingContext.HostingEnvironment.ContentRootPath);
|
||||
var env = hostingContext.HostingEnvironment;
|
||||
config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
|
||||
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);
|
||||
config.AddJsonFile("configuration.json");
|
||||
config.AddEnvironmentVariables();
|
||||
})
|
||||
.ConfigureServices(s =>
|
||||
{
|
||||
s.AddOcelot()
|
||||
.AddOpenTracing(option =>
|
||||
{
|
||||
//this is the url that the butterfly collector server is running on...
|
||||
option.CollectorUrl = butterflyUrl;
|
||||
option.Service = "Ocelot";
|
||||
});
|
||||
})
|
||||
.Configure(app =>
|
||||
{
|
||||
app.Use(async (context, next) =>
|
||||
{
|
||||
await next.Invoke();
|
||||
});
|
||||
app.UseOcelot().Wait();
|
||||
});
|
||||
|
||||
_ocelotServer = new TestServer(_webHostBuilder);
|
||||
|
||||
_ocelotClient = _ocelotServer.CreateClient();
|
||||
}
|
||||
|
||||
/*
|
||||
public void GivenIHaveAddedXForwardedForHeader(string value)
|
||||
{
|
||||
_ocelotClient.DefaultRequestHeaders.TryAddWithoutValidation("X-Forwarded-For", value);
|
||||
}*/
|
||||
|
||||
public void GivenOcelotIsRunningWithMiddleareBeforePipeline<T>(Func<object, Task> callback)
|
||||
{
|
||||
_webHostBuilder = new WebHostBuilder();
|
||||
|
||||
_webHostBuilder
|
||||
.ConfigureAppConfiguration((hostingContext, config) =>
|
||||
{
|
||||
config.SetBasePath(hostingContext.HostingEnvironment.ContentRootPath);
|
||||
var env = hostingContext.HostingEnvironment;
|
||||
config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
|
||||
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);
|
||||
config.AddJsonFile("configuration.json");
|
||||
config.AddEnvironmentVariables();
|
||||
})
|
||||
.ConfigureServices(s =>
|
||||
{
|
||||
s.AddOcelot();
|
||||
})
|
||||
.Configure(app =>
|
||||
{
|
||||
app.UseMiddleware<T>(callback);
|
||||
app.UseOcelot().Wait();
|
||||
});
|
||||
|
||||
_ocelotServer = new TestServer(_webHostBuilder);
|
||||
|
||||
_ocelotClient = _ocelotServer.CreateClient();
|
||||
}
|
||||
|
||||
public void GivenOcelotIsRunningWithHandlersRegisteredInDi<TOne, TWo>()
|
||||
where TOne : DelegatingHandler
|
||||
where TWo : DelegatingHandler
|
||||
{
|
||||
_webHostBuilder = new WebHostBuilder();
|
||||
|
||||
_webHostBuilder
|
||||
.ConfigureAppConfiguration((hostingContext, config) =>
|
||||
{
|
||||
config.SetBasePath(hostingContext.HostingEnvironment.ContentRootPath);
|
||||
var env = hostingContext.HostingEnvironment;
|
||||
config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
|
||||
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);
|
||||
config.AddJsonFile("configuration.json");
|
||||
config.AddEnvironmentVariables();
|
||||
})
|
||||
.ConfigureServices(s =>
|
||||
{
|
||||
s.AddSingleton(_webHostBuilder);
|
||||
s.AddOcelot()
|
||||
.AddDelegatingHandler<TOne>()
|
||||
.AddDelegatingHandler<TWo>();
|
||||
})
|
||||
.Configure(a =>
|
||||
{
|
||||
a.UseOcelot().Wait();
|
||||
});
|
||||
|
||||
_ocelotServer = new TestServer(_webHostBuilder);
|
||||
|
||||
_ocelotClient = _ocelotServer.CreateClient();
|
||||
}
|
||||
|
||||
public void GivenOcelotIsRunningWithHandlersRegisteredInDi<TOne>(FakeDependency dependency)
|
||||
where TOne : DelegatingHandler
|
||||
{
|
||||
_webHostBuilder = new WebHostBuilder();
|
||||
|
||||
_webHostBuilder
|
||||
.ConfigureAppConfiguration((hostingContext, config) =>
|
||||
{
|
||||
config.SetBasePath(hostingContext.HostingEnvironment.ContentRootPath);
|
||||
var env = hostingContext.HostingEnvironment;
|
||||
config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
|
||||
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);
|
||||
config.AddJsonFile("configuration.json");
|
||||
config.AddEnvironmentVariables();
|
||||
})
|
||||
.ConfigureServices(s =>
|
||||
{
|
||||
s.AddSingleton(_webHostBuilder);
|
||||
s.AddSingleton<FakeDependency>(dependency);
|
||||
s.AddOcelot()
|
||||
.AddDelegatingHandler<TOne>();
|
||||
})
|
||||
.Configure(a =>
|
||||
{
|
||||
a.UseOcelot().Wait();
|
||||
});
|
||||
|
||||
_ocelotServer = new TestServer(_webHostBuilder);
|
||||
|
||||
_ocelotClient = _ocelotServer.CreateClient();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// This is annoying cos it should be in the constructor but we need to set up the file before calling startup so its a step.
|
||||
/// </summary>
|
||||
public void GivenOcelotIsRunning(Action<IdentityServerAuthenticationOptions> options, string authenticationProviderKey)
|
||||
{
|
||||
_webHostBuilder = new WebHostBuilder();
|
||||
|
||||
_webHostBuilder
|
||||
.ConfigureAppConfiguration((hostingContext, config) =>
|
||||
{
|
||||
config.SetBasePath(hostingContext.HostingEnvironment.ContentRootPath);
|
||||
var env = hostingContext.HostingEnvironment;
|
||||
config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
|
||||
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);
|
||||
config.AddJsonFile("configuration.json");
|
||||
config.AddEnvironmentVariables();
|
||||
})
|
||||
.ConfigureServices(s =>
|
||||
{
|
||||
s.AddOcelot();
|
||||
s.AddAuthentication()
|
||||
.AddIdentityServerAuthentication(authenticationProviderKey, options);
|
||||
})
|
||||
.Configure(app =>
|
||||
{
|
||||
app.UseOcelot().Wait();
|
||||
});
|
||||
|
||||
_ocelotServer = new TestServer(_webHostBuilder);
|
||||
|
||||
_ocelotClient = _ocelotServer.CreateClient();
|
||||
}
|
||||
|
||||
public void ThenTheResponseHeaderIs(string key, string value)
|
||||
{
|
||||
var header = _response.Headers.GetValues(key);
|
||||
header.First().ShouldBe(value);
|
||||
}
|
||||
|
||||
public void GivenOcelotIsRunningUsingJsonSerializedCache()
|
||||
{
|
||||
_webHostBuilder = new WebHostBuilder();
|
||||
|
||||
_webHostBuilder
|
||||
.ConfigureAppConfiguration((hostingContext, config) =>
|
||||
{
|
||||
config.SetBasePath(hostingContext.HostingEnvironment.ContentRootPath);
|
||||
var env = hostingContext.HostingEnvironment;
|
||||
config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
|
||||
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);
|
||||
config.AddJsonFile("configuration.json");
|
||||
config.AddEnvironmentVariables();
|
||||
})
|
||||
.ConfigureServices(s =>
|
||||
{
|
||||
s.AddOcelot()
|
||||
.AddCacheManager((x) =>
|
||||
{
|
||||
x.WithMicrosoftLogging(log =>
|
||||
{
|
||||
log.AddConsole(LogLevel.Debug);
|
||||
})
|
||||
.WithJsonSerializer()
|
||||
.WithHandle(typeof(InMemoryJsonHandle<>));
|
||||
});
|
||||
})
|
||||
.Configure(app =>
|
||||
{
|
||||
app.UseOcelot().Wait();
|
||||
});
|
||||
|
||||
_ocelotServer = new TestServer(_webHostBuilder);
|
||||
|
||||
_ocelotClient = _ocelotServer.CreateClient();
|
||||
}
|
||||
|
||||
public void GivenOcelotIsRunningUsingConsulToStoreConfig()
|
||||
{
|
||||
_webHostBuilder = new WebHostBuilder();
|
||||
|
||||
_webHostBuilder
|
||||
.ConfigureAppConfiguration((hostingContext, config) =>
|
||||
{
|
||||
config.SetBasePath(hostingContext.HostingEnvironment.ContentRootPath);
|
||||
var env = hostingContext.HostingEnvironment;
|
||||
config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
|
||||
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);
|
||||
config.AddJsonFile("configuration.json");
|
||||
config.AddEnvironmentVariables();
|
||||
})
|
||||
.ConfigureServices(s =>
|
||||
{
|
||||
s.AddOcelot().AddStoreOcelotConfigurationInConsul();
|
||||
})
|
||||
.Configure(app =>
|
||||
{
|
||||
app.UseOcelot().Wait();
|
||||
});
|
||||
|
||||
_ocelotServer = new TestServer(_webHostBuilder);
|
||||
|
||||
_ocelotClient = _ocelotServer.CreateClient();
|
||||
}
|
||||
|
||||
public void GivenOcelotIsRunningUsingConsulToStoreConfigAndJsonSerializedCache()
|
||||
{
|
||||
_webHostBuilder = new WebHostBuilder();
|
||||
|
||||
_webHostBuilder
|
||||
.ConfigureAppConfiguration((hostingContext, config) =>
|
||||
{
|
||||
config.SetBasePath(hostingContext.HostingEnvironment.ContentRootPath);
|
||||
var env = hostingContext.HostingEnvironment;
|
||||
config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
|
||||
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);
|
||||
config.AddJsonFile("configuration.json");
|
||||
config.AddEnvironmentVariables();
|
||||
})
|
||||
.ConfigureServices(s =>
|
||||
{
|
||||
s.AddOcelot()
|
||||
.AddCacheManager((x) =>
|
||||
{
|
||||
x.WithMicrosoftLogging(log =>
|
||||
{
|
||||
log.AddConsole(LogLevel.Debug);
|
||||
})
|
||||
.WithJsonSerializer()
|
||||
.WithHandle(typeof(InMemoryJsonHandle<>));
|
||||
})
|
||||
.AddStoreOcelotConfigurationInConsul();
|
||||
})
|
||||
.Configure(app =>
|
||||
{
|
||||
app.UseOcelot().Wait();
|
||||
});
|
||||
|
||||
_ocelotServer = new TestServer(_webHostBuilder);
|
||||
|
||||
_ocelotClient = _ocelotServer.CreateClient();
|
||||
}
|
||||
|
||||
internal void ThenTheResponseShouldBe(FileConfiguration expecteds)
|
||||
{
|
||||
var response = JsonConvert.DeserializeObject<FileConfiguration>(_response.Content.ReadAsStringAsync().Result);
|
||||
|
||||
response.GlobalConfiguration.RequestIdKey.ShouldBe(expecteds.GlobalConfiguration.RequestIdKey);
|
||||
response.GlobalConfiguration.ServiceDiscoveryProvider.Host.ShouldBe(expecteds.GlobalConfiguration.ServiceDiscoveryProvider.Host);
|
||||
response.GlobalConfiguration.ServiceDiscoveryProvider.Port.ShouldBe(expecteds.GlobalConfiguration.ServiceDiscoveryProvider.Port);
|
||||
|
||||
for (var i = 0; i < response.ReRoutes.Count; i++)
|
||||
{
|
||||
for (var j = 0; j < response.ReRoutes[i].DownstreamHostAndPorts.Count; j++)
|
||||
{
|
||||
var result = response.ReRoutes[i].DownstreamHostAndPorts[j];
|
||||
var expected = expecteds.ReRoutes[i].DownstreamHostAndPorts[j];
|
||||
result.Host.ShouldBe(expected.Host);
|
||||
result.Port.ShouldBe(expected.Port);
|
||||
}
|
||||
|
||||
response.ReRoutes[i].DownstreamPathTemplate.ShouldBe(expecteds.ReRoutes[i].DownstreamPathTemplate);
|
||||
response.ReRoutes[i].DownstreamScheme.ShouldBe(expecteds.ReRoutes[i].DownstreamScheme);
|
||||
response.ReRoutes[i].UpstreamPathTemplate.ShouldBe(expecteds.ReRoutes[i].UpstreamPathTemplate);
|
||||
response.ReRoutes[i].UpstreamHttpMethod.ShouldBe(expecteds.ReRoutes[i].UpstreamHttpMethod);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// This is annoying cos it should be in the constructor but we need to set up the file before calling startup so its a step.
|
||||
/// </summary>
|
||||
public void GivenOcelotIsRunning(OcelotPipelineConfiguration ocelotPipelineConfig)
|
||||
{
|
||||
var builder = new ConfigurationBuilder()
|
||||
.SetBasePath(Directory.GetCurrentDirectory())
|
||||
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
|
||||
.AddJsonFile("configuration.json")
|
||||
.AddEnvironmentVariables();
|
||||
|
||||
var configuration = builder.Build();
|
||||
_webHostBuilder = new WebHostBuilder();
|
||||
_webHostBuilder.ConfigureServices(s =>
|
||||
{
|
||||
s.AddSingleton(_webHostBuilder);
|
||||
});
|
||||
|
||||
_ocelotServer = new TestServer(_webHostBuilder
|
||||
.UseConfiguration(configuration)
|
||||
.ConfigureServices(s =>
|
||||
{
|
||||
Action<ConfigurationBuilderCachePart> settings = (x) =>
|
||||
{
|
||||
x.WithMicrosoftLogging(log =>
|
||||
{
|
||||
log.AddConsole(LogLevel.Debug);
|
||||
})
|
||||
.WithDictionaryHandle();
|
||||
};
|
||||
|
||||
s.AddOcelot(configuration);
|
||||
})
|
||||
.ConfigureLogging(l =>
|
||||
{
|
||||
l.AddConsole();
|
||||
l.AddDebug();
|
||||
})
|
||||
.Configure(a =>
|
||||
{
|
||||
a.UseOcelot(ocelotPipelineConfig).Wait();
|
||||
}));
|
||||
|
||||
_ocelotClient = _ocelotServer.CreateClient();
|
||||
}
|
||||
|
||||
public void GivenIHaveAddedATokenToMyRequest()
|
||||
{
|
||||
_ocelotClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _token.AccessToken);
|
||||
}
|
||||
|
||||
public void GivenIHaveAToken(string url)
|
||||
{
|
||||
var tokenUrl = $"{url}/connect/token";
|
||||
var formData = new List<KeyValuePair<string, string>>
|
||||
{
|
||||
new KeyValuePair<string, string>("client_id", "client"),
|
||||
new KeyValuePair<string, string>("client_secret", "secret"),
|
||||
new KeyValuePair<string, string>("scope", "api"),
|
||||
new KeyValuePair<string, string>("username", "test"),
|
||||
new KeyValuePair<string, string>("password", "test"),
|
||||
new KeyValuePair<string, string>("grant_type", "password")
|
||||
};
|
||||
var content = new FormUrlEncodedContent(formData);
|
||||
|
||||
using (var httpClient = new HttpClient())
|
||||
{
|
||||
var response = httpClient.PostAsync(tokenUrl, content).Result;
|
||||
var responseContent = response.Content.ReadAsStringAsync().Result;
|
||||
response.EnsureSuccessStatusCode();
|
||||
_token = JsonConvert.DeserializeObject<BearerToken>(responseContent);
|
||||
}
|
||||
}
|
||||
|
||||
public void GivenIHaveATokenForApiReadOnlyScope(string url)
|
||||
{
|
||||
var tokenUrl = $"{url}/connect/token";
|
||||
var formData = new List<KeyValuePair<string, string>>
|
||||
{
|
||||
new KeyValuePair<string, string>("client_id", "client"),
|
||||
new KeyValuePair<string, string>("client_secret", "secret"),
|
||||
new KeyValuePair<string, string>("scope", "api.readOnly"),
|
||||
new KeyValuePair<string, string>("username", "test"),
|
||||
new KeyValuePair<string, string>("password", "test"),
|
||||
new KeyValuePair<string, string>("grant_type", "password")
|
||||
};
|
||||
var content = new FormUrlEncodedContent(formData);
|
||||
|
||||
using (var httpClient = new HttpClient())
|
||||
{
|
||||
var response = httpClient.PostAsync(tokenUrl, content).Result;
|
||||
var responseContent = response.Content.ReadAsStringAsync().Result;
|
||||
response.EnsureSuccessStatusCode();
|
||||
_token = JsonConvert.DeserializeObject<BearerToken>(responseContent);
|
||||
}
|
||||
}
|
||||
|
||||
public void GivenIHaveATokenForApi2(string url)
|
||||
{
|
||||
var tokenUrl = $"{url}/connect/token";
|
||||
var formData = new List<KeyValuePair<string, string>>
|
||||
{
|
||||
new KeyValuePair<string, string>("client_id", "client"),
|
||||
new KeyValuePair<string, string>("client_secret", "secret"),
|
||||
new KeyValuePair<string, string>("scope", "api2"),
|
||||
new KeyValuePair<string, string>("username", "test"),
|
||||
new KeyValuePair<string, string>("password", "test"),
|
||||
new KeyValuePair<string, string>("grant_type", "password")
|
||||
};
|
||||
var content = new FormUrlEncodedContent(formData);
|
||||
|
||||
using (var httpClient = new HttpClient())
|
||||
{
|
||||
var response = httpClient.PostAsync(tokenUrl, content).Result;
|
||||
var responseContent = response.Content.ReadAsStringAsync().Result;
|
||||
response.EnsureSuccessStatusCode();
|
||||
_token = JsonConvert.DeserializeObject<BearerToken>(responseContent);
|
||||
}
|
||||
}
|
||||
|
||||
public void GivenIHaveAnOcelotToken(string adminPath)
|
||||
{
|
||||
var tokenUrl = $"{adminPath}/connect/token";
|
||||
var formData = new List<KeyValuePair<string, string>>
|
||||
{
|
||||
new KeyValuePair<string, string>("client_id", "admin"),
|
||||
new KeyValuePair<string, string>("client_secret", "secret"),
|
||||
new KeyValuePair<string, string>("scope", "admin"),
|
||||
new KeyValuePair<string, string>("username", "admin"),
|
||||
new KeyValuePair<string, string>("password", "admin"),
|
||||
new KeyValuePair<string, string>("grant_type", "password")
|
||||
};
|
||||
var content = new FormUrlEncodedContent(formData);
|
||||
|
||||
var response = _ocelotClient.PostAsync(tokenUrl, content).Result;
|
||||
var responseContent = response.Content.ReadAsStringAsync().Result;
|
||||
response.EnsureSuccessStatusCode();
|
||||
_token = JsonConvert.DeserializeObject<BearerToken>(responseContent);
|
||||
}
|
||||
|
||||
public void VerifyIdentiryServerStarted(string url)
|
||||
{
|
||||
using (var httpClient = new HttpClient())
|
||||
{
|
||||
var response = httpClient.GetAsync($"{url}/.well-known/openid-configuration").Result;
|
||||
response.EnsureSuccessStatusCode();
|
||||
}
|
||||
}
|
||||
|
||||
public void WhenIGetUrlOnTheApiGateway(string url)
|
||||
{
|
||||
_response = _ocelotClient.GetAsync(url).Result;
|
||||
}
|
||||
|
||||
public void GivenIAddAHeader(string key, string value)
|
||||
{
|
||||
_ocelotClient.DefaultRequestHeaders.Add(key, value);
|
||||
}
|
||||
|
||||
public void WhenIGetUrlOnTheApiGatewayMultipleTimes(string url, int times)
|
||||
{
|
||||
var tasks = new Task[times];
|
||||
|
||||
for (int i = 0; i < times; i++)
|
||||
{
|
||||
var urlCopy = url;
|
||||
tasks[i] = GetForServiceDiscoveryTest(urlCopy);
|
||||
Thread.Sleep(_random.Next(40, 60));
|
||||
}
|
||||
|
||||
Task.WaitAll(tasks);
|
||||
}
|
||||
|
||||
private async Task GetForServiceDiscoveryTest(string url)
|
||||
{
|
||||
var response = await _ocelotClient.GetAsync(url);
|
||||
var content = await response.Content.ReadAsStringAsync();
|
||||
int count = int.Parse(content);
|
||||
count.ShouldBeGreaterThan(0);
|
||||
}
|
||||
|
||||
public void WhenIGetUrlOnTheApiGatewayMultipleTimesForRateLimit(string url, int times)
|
||||
{
|
||||
for (int i = 0; i < times; i++)
|
||||
{
|
||||
var clientId = "ocelotclient1";
|
||||
var request = new HttpRequestMessage(new HttpMethod("GET"), url);
|
||||
request.Headers.Add("ClientId", clientId);
|
||||
_response = _ocelotClient.SendAsync(request).Result;
|
||||
}
|
||||
}
|
||||
|
||||
public void WhenIGetUrlOnTheApiGateway(string url, string requestId)
|
||||
{
|
||||
_ocelotClient.DefaultRequestHeaders.TryAddWithoutValidation(RequestIdKey, requestId);
|
||||
|
||||
_response = _ocelotClient.GetAsync(url).Result;
|
||||
}
|
||||
|
||||
public void WhenIPostUrlOnTheApiGateway(string url)
|
||||
{
|
||||
_response = _ocelotClient.PostAsync(url, _postContent).Result;
|
||||
}
|
||||
|
||||
public void GivenThePostHasContent(string postcontent)
|
||||
{
|
||||
_postContent = new StringContent(postcontent);
|
||||
}
|
||||
|
||||
public void GivenThePostHasGzipContent(object input)
|
||||
{
|
||||
string json = JsonConvert.SerializeObject(input);
|
||||
byte[] jsonBytes = Encoding.UTF8.GetBytes(json);
|
||||
MemoryStream ms = new MemoryStream();
|
||||
using (GZipStream gzip = new GZipStream(ms, CompressionMode.Compress, true))
|
||||
{
|
||||
gzip.Write(jsonBytes, 0, jsonBytes.Length);
|
||||
}
|
||||
ms.Position = 0;
|
||||
StreamContent content = new StreamContent(ms);
|
||||
content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
|
||||
content.Headers.ContentEncoding.Add("gzip");
|
||||
_postContent = content;
|
||||
}
|
||||
|
||||
public void ThenTheResponseBodyShouldBe(string expectedBody)
|
||||
{
|
||||
_response.Content.ReadAsStringAsync().Result.ShouldBe(expectedBody);
|
||||
}
|
||||
|
||||
public void ThenTheStatusCodeShouldBe(HttpStatusCode expectedHttpStatusCode)
|
||||
{
|
||||
_response.StatusCode.ShouldBe(expectedHttpStatusCode);
|
||||
}
|
||||
|
||||
public void ThenTheStatusCodeShouldBe(int expectedHttpStatusCode)
|
||||
{
|
||||
var responseStatusCode = (int)_response.StatusCode;
|
||||
responseStatusCode.ShouldBe(expectedHttpStatusCode);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_ocelotClient?.Dispose();
|
||||
_ocelotServer?.Dispose();
|
||||
}
|
||||
|
||||
public void ThenTheRequestIdIsReturned()
|
||||
{
|
||||
_response.Headers.GetValues(RequestIdKey).First().ShouldNotBeNullOrEmpty();
|
||||
}
|
||||
|
||||
public void ThenTheRequestIdIsReturned(string expected)
|
||||
{
|
||||
_response.Headers.GetValues(RequestIdKey).First().ShouldBe(expected);
|
||||
}
|
||||
|
||||
public void ThenTheContentLengthIs(int expected)
|
||||
{
|
||||
_response.Content.Headers.ContentLength.ShouldBe(expected);
|
||||
}
|
||||
|
||||
public void WhenIMakeLotsOfDifferentRequestsToTheApiGateway()
|
||||
{
|
||||
int numberOfRequests = 100;
|
||||
var aggregateUrl = "/";
|
||||
var aggregateExpected = "{\"Laura\":{Hello from Laura},\"Tom\":{Hello from Tom}}";
|
||||
var tomUrl = "/tom";
|
||||
var tomExpected = "{Hello from Tom}";
|
||||
var lauraUrl = "/laura";
|
||||
var lauraExpected = "{Hello from Laura}";
|
||||
var random = new Random();
|
||||
|
||||
var aggregateTasks = new Task[numberOfRequests];
|
||||
|
||||
for (int i = 0; i < numberOfRequests; i++)
|
||||
{
|
||||
aggregateTasks[i] = Fire(aggregateUrl, aggregateExpected, random);
|
||||
}
|
||||
|
||||
var tomTasks = new Task[numberOfRequests];
|
||||
|
||||
for (int i = 0; i < numberOfRequests; i++)
|
||||
{
|
||||
tomTasks[i] = Fire(tomUrl, tomExpected, random);
|
||||
}
|
||||
|
||||
var lauraTasks = new Task[numberOfRequests];
|
||||
|
||||
for (int i = 0; i < numberOfRequests; i++)
|
||||
{
|
||||
lauraTasks[i] = Fire(lauraUrl, lauraExpected, random);
|
||||
}
|
||||
|
||||
Task.WaitAll(lauraTasks);
|
||||
Task.WaitAll(tomTasks);
|
||||
Task.WaitAll(aggregateTasks);
|
||||
}
|
||||
|
||||
private async Task Fire(string url, string expectedBody, Random random)
|
||||
{
|
||||
var request = new HttpRequestMessage(new HttpMethod("GET"), url);
|
||||
await Task.Delay(random.Next(0, 2));
|
||||
var response = await _ocelotClient.SendAsync(request);
|
||||
var content = await response.Content.ReadAsStringAsync();
|
||||
content.ShouldBe(expectedBody);
|
||||
}
|
||||
}
|
||||
}
|
@ -41,15 +41,13 @@ namespace Ocelot.UnitTests.DependencyInjection
|
||||
private Exception _ex;
|
||||
|
||||
[Fact]
|
||||
public void should_add_delegating_handlers()
|
||||
public void should_add_delegating_handlers_with_di()
|
||||
{
|
||||
var fakeOne = new FakeDelegatingHandler(0);
|
||||
var fakeTwo = new FakeDelegatingHandler(1);
|
||||
|
||||
this.Given(x => WhenISetUpOcelotServices())
|
||||
.When(x => AddDelegate(fakeOne))
|
||||
.And(x => AddDelegate(fakeTwo))
|
||||
.Then(x => ThenTheProviderIsRegisteredAndReturnsHandlers())
|
||||
.When(x => AddDelegate<FakeDelegatingHandler>())
|
||||
.And(x => AddDelegate<FakeDelegatingHandlerTwo>())
|
||||
.Then(x => ThenTheProviderIsRegisteredAndReturnsHandlers<FakeDelegatingHandler, FakeDelegatingHandlerTwo>())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
@ -164,15 +162,12 @@ namespace Ocelot.UnitTests.DependencyInjection
|
||||
path.Path.ShouldBe("/administration");
|
||||
}
|
||||
|
||||
private void ThenTheProviderIsRegisteredAndReturnsHandlers()
|
||||
private void ThenTheProviderIsRegisteredAndReturnsHandlers<TOne, TWo>()
|
||||
{
|
||||
_serviceProvider = _services.BuildServiceProvider();
|
||||
var provider = _serviceProvider.GetService<IDelegatingHandlerHandlerProvider>();
|
||||
var handlers = provider.Get();
|
||||
var handler = (FakeDelegatingHandler)handlers[0].Invoke();
|
||||
handler.Order.ShouldBe(0);
|
||||
handler = (FakeDelegatingHandler)handlers[1].Invoke();
|
||||
handler.Order.ShouldBe(1);
|
||||
var handlers = _serviceProvider.GetServices<DelegatingHandler>().ToList();
|
||||
handlers[0].ShouldBeOfType<TOne>();
|
||||
handlers[1].ShouldBeOfType<TWo>();
|
||||
}
|
||||
|
||||
private void OnlyOneVersionOfEachCacheIsRegistered()
|
||||
@ -213,9 +208,9 @@ namespace Ocelot.UnitTests.DependencyInjection
|
||||
}
|
||||
}
|
||||
|
||||
private void AddDelegate(DelegatingHandler handler)
|
||||
private void AddDelegate<T>() where T : DelegatingHandler
|
||||
{
|
||||
_ocelotBuilder.AddDelegatingHandler(() => handler);
|
||||
_ocelotBuilder.AddDelegatingHandler<T>();
|
||||
}
|
||||
|
||||
private void ThenAnOcelotBuilderIsReturned()
|
||||
|
@ -1,137 +0,0 @@
|
||||
using System;
|
||||
using System.Net.Http;
|
||||
using Moq;
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.Configuration.Builder;
|
||||
using Ocelot.Errors;
|
||||
using Ocelot.Requester;
|
||||
using Ocelot.Responses;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests.Requester
|
||||
{
|
||||
public class DelegatingHandlerHandlerHouseTests
|
||||
{
|
||||
private readonly DelegatingHandlerHandlerHouse _house;
|
||||
private Mock<IDelegatingHandlerHandlerProviderFactory> _factory;
|
||||
private readonly Mock<IDelegatingHandlerHandlerProvider> _provider;
|
||||
private DownstreamReRoute _request;
|
||||
private Response<IDelegatingHandlerHandlerProvider> _result;
|
||||
|
||||
public DelegatingHandlerHandlerHouseTests()
|
||||
{
|
||||
_provider = new Mock<IDelegatingHandlerHandlerProvider>();
|
||||
_factory = new Mock<IDelegatingHandlerHandlerProviderFactory>();
|
||||
_house = new DelegatingHandlerHandlerHouse(_factory.Object);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_create_and_store_provider()
|
||||
{
|
||||
var reRoute = new DownstreamReRouteBuilder().WithIsQos(true)
|
||||
.WithHttpHandlerOptions(new HttpHandlerOptions(true, true, false)).WithReRouteKey("key").Build();
|
||||
|
||||
this.Given(x => GivenTheRequest(reRoute))
|
||||
.And(x => GivenTheProviderReturns())
|
||||
.When(x => WhenIGet())
|
||||
.Then(x => ThenTheFactoryIsCalled(1))
|
||||
.And(x => ThenTheProviderIsNotNull())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_get_provider()
|
||||
{
|
||||
var reRoute = new DownstreamReRouteBuilder().WithIsQos(true)
|
||||
.WithHttpHandlerOptions(new HttpHandlerOptions(true, true, false)).WithReRouteKey("key").Build();
|
||||
|
||||
this.Given(x => GivenTheRequest(reRoute))
|
||||
.And(x => GivenTheProviderReturns())
|
||||
.And(x => WhenIGet())
|
||||
.And(x => GivenTheFactoryIsCleared())
|
||||
.When(x => WhenIGet())
|
||||
.Then(x => ThenTheFactoryIsCalled(0))
|
||||
.And(x => ThenTheProviderIsNotNull())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_error()
|
||||
{
|
||||
var reRoute = new DownstreamReRouteBuilder().WithIsQos(true)
|
||||
.WithHttpHandlerOptions(new HttpHandlerOptions(true, true, false)).WithReRouteKey("key").Build();
|
||||
|
||||
this.Given(x => GivenTheRequest(reRoute))
|
||||
.And(x => GivenTheProviderThrows())
|
||||
.When(x => WhenIGet())
|
||||
.And(x => ThenAnErrorIsReturned())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_error_if_factory_errors()
|
||||
{
|
||||
var reRoute = new DownstreamReRouteBuilder().WithIsQos(true)
|
||||
.WithHttpHandlerOptions(new HttpHandlerOptions(true, true, false)).WithReRouteKey("key").Build();
|
||||
|
||||
this.Given(x => GivenTheRequest(reRoute))
|
||||
.And(x => GivenTheProviderReturnsError())
|
||||
.When(x => WhenIGet())
|
||||
.Then(x => ThenAnUnknownErrorIsReturned())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void ThenAnUnknownErrorIsReturned()
|
||||
{
|
||||
_result.IsError.ShouldBeTrue();
|
||||
}
|
||||
|
||||
private void ThenAnErrorIsReturned()
|
||||
{
|
||||
_result.IsError.ShouldBeTrue();
|
||||
_result.Errors[0].ShouldBeOfType<UnableToFindDelegatingHandlerProviderError>();
|
||||
}
|
||||
|
||||
private void GivenTheProviderThrows()
|
||||
{
|
||||
_factory.Setup(x => x.Get(It.IsAny<DownstreamReRoute>())).Throws<Exception>();
|
||||
}
|
||||
|
||||
private void GivenTheFactoryIsCleared()
|
||||
{
|
||||
_factory = new Mock<IDelegatingHandlerHandlerProviderFactory>();
|
||||
}
|
||||
|
||||
private void ThenTheProviderIsNotNull()
|
||||
{
|
||||
_result.Data.ShouldBe(_provider.Object);
|
||||
}
|
||||
|
||||
private void WhenIGet()
|
||||
{
|
||||
_result = _house.Get(_request);
|
||||
}
|
||||
|
||||
private void GivenTheRequest(DownstreamReRoute request)
|
||||
{
|
||||
_request = request;
|
||||
}
|
||||
|
||||
private void GivenTheProviderReturns()
|
||||
{
|
||||
_factory.Setup(x => x.Get(It.IsAny<DownstreamReRoute>())).Returns(new OkResponse<IDelegatingHandlerHandlerProvider>(_provider.Object));
|
||||
}
|
||||
|
||||
private void GivenTheProviderReturnsError()
|
||||
{
|
||||
_factory.Setup(x => x.Get(It.IsAny<DownstreamReRoute>())).Returns(new ErrorResponse<IDelegatingHandlerHandlerProvider>(It.IsAny<Error>()));
|
||||
}
|
||||
|
||||
private void ThenTheFactoryIsCalled(int times)
|
||||
{
|
||||
_factory.Verify(x => x.Get(_request), Times.Exactly(times));
|
||||
}
|
||||
}
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Net.Http;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Moq;
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.Configuration.Builder;
|
||||
@ -17,50 +18,35 @@ namespace Ocelot.UnitTests.Requester
|
||||
{
|
||||
public class DelegatingHandlerHandlerProviderFactoryTests
|
||||
{
|
||||
private readonly DelegatingHandlerHandlerProviderFactory _factory;
|
||||
private DelegatingHandlerHandlerFactory _factory;
|
||||
private Mock<IOcelotLoggerFactory> _loggerFactory;
|
||||
private DownstreamReRoute _request;
|
||||
private Response<IDelegatingHandlerHandlerProvider> _provider;
|
||||
private readonly Mock<IDelegatingHandlerHandlerProvider> _allRoutesProvider;
|
||||
private Response<List<Func<DelegatingHandler>>> _provider;
|
||||
private readonly Mock<IQosProviderHouse> _qosProviderHouse;
|
||||
private readonly Mock<ITracingHandlerFactory> _tracingFactory;
|
||||
private IServiceProvider _serviceProvider;
|
||||
|
||||
public DelegatingHandlerHandlerProviderFactoryTests()
|
||||
{
|
||||
_tracingFactory = new Mock<ITracingHandlerFactory>();
|
||||
_qosProviderHouse = new Mock<IQosProviderHouse>();
|
||||
_allRoutesProvider = new Mock<IDelegatingHandlerHandlerProvider>();
|
||||
_loggerFactory = new Mock<IOcelotLoggerFactory>();
|
||||
_factory = new DelegatingHandlerHandlerProviderFactory(_loggerFactory.Object, _allRoutesProvider.Object, _tracingFactory.Object, _qosProviderHouse.Object);
|
||||
}
|
||||
|
||||
private void GivenTheQosProviderHouseReturns(Response<IQoSProvider> qosProvider)
|
||||
{
|
||||
_qosProviderHouse
|
||||
.Setup(x => x.Get(It.IsAny<DownstreamReRoute>()))
|
||||
.Returns(qosProvider);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_all_from_all_routes_provider_and_qos()
|
||||
{
|
||||
var handlers = new List<Func<DelegatingHandler>>
|
||||
{
|
||||
() => new FakeDelegatingHandler(0),
|
||||
() => new FakeDelegatingHandler(1)
|
||||
};
|
||||
|
||||
var reRoute = new DownstreamReRouteBuilder().WithIsQos(true)
|
||||
.WithHttpHandlerOptions(new HttpHandlerOptions(true, true, false)).WithReRouteKey("").Build();
|
||||
|
||||
this.Given(x => GivenTheFollowingRequest(reRoute))
|
||||
.And(x => GivenTheQosProviderHouseReturns(new OkResponse<IQoSProvider>(It.IsAny<PollyQoSProvider>())))
|
||||
.And(x => GivenTheAllRoutesProviderReturns(handlers))
|
||||
.And(x => GivenTheServiceProviderReturns<FakeDelegatingHandler, FakeDelegatingHandlerTwo>())
|
||||
.When(x => WhenIGet())
|
||||
.Then(x => ThenThereIsDelegatesInProvider(3))
|
||||
.And(x => ThenTheDelegatesAreAddedCorrectly())
|
||||
.And(x => ThenItIsPolly(2))
|
||||
.BDDfy();
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@ -70,7 +56,7 @@ namespace Ocelot.UnitTests.Requester
|
||||
.WithHttpHandlerOptions(new HttpHandlerOptions(true, true, false)).WithReRouteKey("").Build();
|
||||
|
||||
this.Given(x => GivenTheFollowingRequest(reRoute))
|
||||
.And(x => GivenTheAllRoutesProviderReturns())
|
||||
.And(x => GivenTheServiceProviderReturnsNothing())
|
||||
.When(x => WhenIGet())
|
||||
.Then(x => ThenNoDelegatesAreInTheProvider())
|
||||
.BDDfy();
|
||||
@ -84,7 +70,7 @@ namespace Ocelot.UnitTests.Requester
|
||||
|
||||
this.Given(x => GivenTheFollowingRequest(reRoute))
|
||||
.And(x => GivenTheQosProviderHouseReturns(new OkResponse<IQoSProvider>(It.IsAny<PollyQoSProvider>())))
|
||||
.And(x => GivenTheAllRoutesProviderReturns())
|
||||
.And(x => GivenTheServiceProviderReturnsNothing())
|
||||
.When(x => WhenIGet())
|
||||
.Then(x => ThenThereIsDelegatesInProvider(1))
|
||||
.And(x => ThenItIsPolly(0))
|
||||
@ -99,12 +85,28 @@ namespace Ocelot.UnitTests.Requester
|
||||
|
||||
this.Given(x => GivenTheFollowingRequest(reRoute))
|
||||
.And(x => GivenTheQosProviderHouseReturns(new ErrorResponse<IQoSProvider>(It.IsAny<Error>())))
|
||||
.And(x => GivenTheAllRoutesProviderReturns())
|
||||
.And(x => GivenTheServiceProviderReturnsNothing())
|
||||
.When(x => WhenIGet())
|
||||
.Then(x => ThenAnErrorIsReturned())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenTheServiceProviderReturns<TOne, TTwo>()
|
||||
where TOne : DelegatingHandler
|
||||
where TTwo : DelegatingHandler
|
||||
{
|
||||
IServiceCollection services = new ServiceCollection();
|
||||
services.AddSingleton<DelegatingHandler, TOne>();
|
||||
services.AddSingleton<DelegatingHandler, TTwo>();
|
||||
_serviceProvider = services.BuildServiceProvider();
|
||||
}
|
||||
|
||||
private void GivenTheServiceProviderReturnsNothing()
|
||||
{
|
||||
IServiceCollection services = new ServiceCollection();
|
||||
_serviceProvider = services.BuildServiceProvider();
|
||||
}
|
||||
|
||||
private void ThenAnErrorIsReturned()
|
||||
{
|
||||
_provider.IsError.ShouldBeTrue();
|
||||
@ -112,29 +114,27 @@ namespace Ocelot.UnitTests.Requester
|
||||
|
||||
private void ThenTheDelegatesAreAddedCorrectly()
|
||||
{
|
||||
var delegates = _provider.Data.Get();
|
||||
var delegates = _provider.Data;
|
||||
|
||||
var del = delegates[0].Invoke();
|
||||
var handler = (FakeDelegatingHandler) del;
|
||||
handler.Order.ShouldBe(0);
|
||||
handler.Order.ShouldBe(1);
|
||||
|
||||
del = delegates[1].Invoke();
|
||||
handler = (FakeDelegatingHandler)del;
|
||||
handler.Order.ShouldBe(1);
|
||||
var handlerTwo = (FakeDelegatingHandlerTwo) del;
|
||||
handlerTwo.Order.ShouldBe(2);
|
||||
}
|
||||
|
||||
private void GivenTheAllRoutesProviderReturns()
|
||||
private void GivenTheQosProviderHouseReturns(Response<IQoSProvider> qosProvider)
|
||||
{
|
||||
_allRoutesProvider.Setup(x => x.Get()).Returns(new List<Func<DelegatingHandler>>());
|
||||
}
|
||||
|
||||
private void GivenTheAllRoutesProviderReturns(List<Func<DelegatingHandler>> handlers)
|
||||
{
|
||||
_allRoutesProvider.Setup(x => x.Get()).Returns(handlers);
|
||||
_qosProviderHouse
|
||||
.Setup(x => x.Get(It.IsAny<DownstreamReRoute>()))
|
||||
.Returns(qosProvider);
|
||||
}
|
||||
|
||||
private void ThenItIsPolly(int i)
|
||||
{
|
||||
var delegates = _provider.Data.Get();
|
||||
var delegates = _provider.Data;
|
||||
var del = delegates[i].Invoke();
|
||||
del.ShouldBeOfType<PollyCircuitBreakingDelegatingHandler>();
|
||||
}
|
||||
@ -142,7 +142,7 @@ namespace Ocelot.UnitTests.Requester
|
||||
private void ThenThereIsDelegatesInProvider(int count)
|
||||
{
|
||||
_provider.ShouldNotBeNull();
|
||||
_provider.Data.Get().Count.ShouldBe(count);
|
||||
_provider.Data.Count.ShouldBe(count);
|
||||
}
|
||||
|
||||
private void GivenTheFollowingRequest(DownstreamReRoute request)
|
||||
@ -152,13 +152,14 @@ namespace Ocelot.UnitTests.Requester
|
||||
|
||||
private void WhenIGet()
|
||||
{
|
||||
_factory = new DelegatingHandlerHandlerFactory(_loggerFactory.Object, _tracingFactory.Object, _qosProviderHouse.Object, _serviceProvider);
|
||||
_provider = _factory.Get(_request);
|
||||
}
|
||||
|
||||
private void ThenNoDelegatesAreInTheProvider()
|
||||
{
|
||||
_provider.ShouldNotBeNull();
|
||||
_provider.Data.Get().Count.ShouldBe(0);
|
||||
_provider.Data.Count.ShouldBe(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,62 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Net.Http;
|
||||
using Ocelot.Requester;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests.Requester
|
||||
{
|
||||
public class DelegatingHandlerHandlerProviderTests
|
||||
{
|
||||
private readonly DelegatingHandlerHandlerProvider _provider;
|
||||
private List<Func<DelegatingHandler>> _handlers;
|
||||
|
||||
public DelegatingHandlerHandlerProviderTests()
|
||||
{
|
||||
_provider = new DelegatingHandlerHandlerProvider();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_empty_list()
|
||||
{
|
||||
this.When(x => WhenIGet())
|
||||
.Then(x => ThenAnEmptyListIsReturned())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_get_delegating_handlers_in_order_first_in_first_out()
|
||||
{
|
||||
this.Given(x => GivenTheHandlers())
|
||||
.When(x => WhenIGet())
|
||||
.Then(x => ThenTheHandlersAreReturnedInOrder())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void ThenAnEmptyListIsReturned()
|
||||
{
|
||||
_handlers.Count.ShouldBe(0);
|
||||
}
|
||||
|
||||
private void ThenTheHandlersAreReturnedInOrder()
|
||||
{
|
||||
var handler = (FakeDelegatingHandler)_handlers[0].Invoke();
|
||||
handler.Order.ShouldBe(0);
|
||||
handler = (FakeDelegatingHandler)_handlers[1].Invoke();
|
||||
handler.Order.ShouldBe(1);
|
||||
}
|
||||
|
||||
private void WhenIGet()
|
||||
{
|
||||
_handlers = _provider.Get();
|
||||
}
|
||||
|
||||
private void GivenTheHandlers()
|
||||
{
|
||||
_provider.Add(() => new FakeDelegatingHandler(0));
|
||||
_provider.Add(() => new FakeDelegatingHandler(1));
|
||||
}
|
||||
}
|
||||
}
|
@ -9,6 +9,7 @@ namespace Ocelot.UnitTests.Requester
|
||||
{
|
||||
public FakeDelegatingHandler()
|
||||
{
|
||||
Order = 1;
|
||||
}
|
||||
|
||||
public FakeDelegatingHandler(int order)
|
||||
@ -26,4 +27,21 @@ namespace Ocelot.UnitTests.Requester
|
||||
return Task.FromResult(new HttpResponseMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public class FakeDelegatingHandlerTwo : DelegatingHandler
|
||||
{
|
||||
public FakeDelegatingHandlerTwo()
|
||||
{
|
||||
Order = 2;
|
||||
}
|
||||
|
||||
public int Order {get;private set;}
|
||||
public DateTime TimeCalled {get;private set;}
|
||||
|
||||
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
|
||||
{
|
||||
TimeCalled = DateTime.Now;
|
||||
return new HttpResponseMessage();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -15,25 +15,22 @@ namespace Ocelot.UnitTests.Requester
|
||||
public class HttpClientBuilderTests
|
||||
{
|
||||
private readonly HttpClientBuilder _builder;
|
||||
private readonly Mock<IDelegatingHandlerHandlerHouse> _house;
|
||||
private readonly Mock<IDelegatingHandlerHandlerProvider> _provider;
|
||||
private readonly Mock<IDelegatingHandlerHandlerFactory> _factory;
|
||||
private IHttpClient _httpClient;
|
||||
private HttpResponseMessage _response;
|
||||
private DownstreamReRoute _request;
|
||||
|
||||
public HttpClientBuilderTests()
|
||||
{
|
||||
_provider = new Mock<IDelegatingHandlerHandlerProvider>();
|
||||
_house = new Mock<IDelegatingHandlerHandlerHouse>();
|
||||
_builder = new HttpClientBuilder(_house.Object);
|
||||
_factory = new Mock<IDelegatingHandlerHandlerFactory>();
|
||||
_builder = new HttpClientBuilder(_factory.Object);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_build_http_client()
|
||||
{
|
||||
this.Given(x => GivenTheProviderReturns())
|
||||
this.Given(x => GivenTheFactoryReturns())
|
||||
.And(x => GivenARequest())
|
||||
.And(x => GivenTheHouseReturns())
|
||||
.When(x => WhenIBuild())
|
||||
.Then(x => ThenTheHttpClientShouldNotBeNull())
|
||||
.BDDfy();
|
||||
@ -51,9 +48,8 @@ namespace Ocelot.UnitTests.Requester
|
||||
() => fakeTwo
|
||||
};
|
||||
|
||||
this.Given(x => GivenTheProviderReturns(handlers))
|
||||
this.Given(x => GivenTheFactoryReturns(handlers))
|
||||
.And(x => GivenARequest())
|
||||
.And(x => GivenTheHouseReturns())
|
||||
.And(x => WhenIBuild())
|
||||
.When(x => WhenICallTheClient())
|
||||
.Then(x => ThenTheFakeAreHandledInOrder(fakeOne, fakeTwo))
|
||||
@ -69,13 +65,6 @@ namespace Ocelot.UnitTests.Requester
|
||||
_request = reRoute;
|
||||
}
|
||||
|
||||
private void GivenTheHouseReturns()
|
||||
{
|
||||
_house
|
||||
.Setup(x => x.Get(It.IsAny<DownstreamReRoute>()))
|
||||
.Returns(new OkResponse<IDelegatingHandlerHandlerProvider>(_provider.Object));
|
||||
}
|
||||
|
||||
private void ThenSomethingIsReturned()
|
||||
{
|
||||
_response.ShouldNotBeNull();
|
||||
@ -91,18 +80,20 @@ namespace Ocelot.UnitTests.Requester
|
||||
fakeOne.TimeCalled.ShouldBeGreaterThan(fakeTwo.TimeCalled);
|
||||
}
|
||||
|
||||
private void GivenTheProviderReturns()
|
||||
private void GivenTheFactoryReturns()
|
||||
{
|
||||
_provider
|
||||
.Setup(x => x.Get())
|
||||
.Returns(new List<Func<DelegatingHandler>>(){ () => new FakeDelegatingHandler()});
|
||||
var handlers = new List<Func<DelegatingHandler>>(){ () => new FakeDelegatingHandler()};
|
||||
|
||||
_factory
|
||||
.Setup(x => x.Get(It.IsAny<DownstreamReRoute>()))
|
||||
.Returns(new OkResponse<List<Func<DelegatingHandler>>>(handlers));
|
||||
}
|
||||
|
||||
private void GivenTheProviderReturns(List<Func<DelegatingHandler>> handlers)
|
||||
private void GivenTheFactoryReturns(List<Func<DelegatingHandler>> handlers)
|
||||
{
|
||||
_provider
|
||||
.Setup(x => x.Get())
|
||||
.Returns(handlers);
|
||||
_factory
|
||||
.Setup(x => x.Get(It.IsAny<DownstreamReRoute>()))
|
||||
.Returns(new OkResponse<List<Func<DelegatingHandler>>>(handlers));
|
||||
}
|
||||
|
||||
private void WhenIBuild()
|
||||
|
@ -18,8 +18,7 @@ namespace Ocelot.UnitTests.Requester
|
||||
public class HttpClientHttpRequesterTest
|
||||
{
|
||||
private readonly Mock<IHttpClientCache> _cacheHandlers;
|
||||
private Mock<IDelegatingHandlerHandlerHouse> _house;
|
||||
private Mock<IDelegatingHandlerHandlerProvider> _provider;
|
||||
private Mock<IDelegatingHandlerHandlerFactory> _house;
|
||||
private Response<HttpResponseMessage> _response;
|
||||
private readonly HttpClientHttpRequester _httpClientRequester;
|
||||
private DownstreamContext _request;
|
||||
@ -28,10 +27,8 @@ namespace Ocelot.UnitTests.Requester
|
||||
|
||||
public HttpClientHttpRequesterTest()
|
||||
{
|
||||
_provider = new Mock<IDelegatingHandlerHandlerProvider>();
|
||||
_provider.Setup(x => x.Get()).Returns(new List<Func<DelegatingHandler>>());
|
||||
_house = new Mock<IDelegatingHandlerHandlerHouse>();
|
||||
_house.Setup(x => x.Get(It.IsAny<DownstreamReRoute>())).Returns(new OkResponse<IDelegatingHandlerHandlerProvider>(_provider.Object));
|
||||
_house = new Mock<IDelegatingHandlerHandlerFactory>();
|
||||
_house.Setup(x => x.Get(It.IsAny<DownstreamReRoute>())).Returns(new OkResponse<List<Func<DelegatingHandler>>>(new List<Func<DelegatingHandler>>()));
|
||||
_logger = new Mock<IOcelotLogger>();
|
||||
_loggerFactory = new Mock<IOcelotLoggerFactory>();
|
||||
_loggerFactory
|
||||
|
Reference in New Issue
Block a user