mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-06-19 09:38:14 +08:00
hacked together load balancing reroutes in fileconfig (#211)
* hacked together load balancing reroutes in fileconfig * some renaming and refactoring * more renames * hacked away the old config json * test for issue 213 * renamed key * dont share ports * oops * updated docs * mvoed docs around * port being used
This commit is contained in:
468
test/Ocelot.AcceptanceTests/.gitignore
vendored
468
test/Ocelot.AcceptanceTests/.gitignore
vendored
@ -1,234 +1,234 @@
|
||||
## Ignore Visual Studio temporary files, build results, and
|
||||
## files generated by popular Visual Studio add-ons.
|
||||
|
||||
# User-specific files
|
||||
*.suo
|
||||
*.user
|
||||
*.userosscache
|
||||
*.sln.docstates
|
||||
|
||||
# User-specific files (MonoDevelop/Xamarin Studio)
|
||||
*.userprefs
|
||||
|
||||
# Build results
|
||||
[Dd]ebug/
|
||||
[Dd]ebugPublic/
|
||||
[Rr]elease/
|
||||
[Rr]eleases/
|
||||
x64/
|
||||
x86/
|
||||
build/
|
||||
bld/
|
||||
[Bb]in/
|
||||
[Oo]bj/
|
||||
|
||||
# Visual Studio 2015 cache/options directory
|
||||
.vs/
|
||||
# Uncomment if you have tasks that create the project's static files in wwwroot
|
||||
#wwwroot/
|
||||
|
||||
# MSTest test Results
|
||||
[Tt]est[Rr]esult*/
|
||||
[Bb]uild[Ll]og.*
|
||||
|
||||
# NUNIT
|
||||
*.VisualState.xml
|
||||
TestResult.xml
|
||||
|
||||
# Build Results of an ATL Project
|
||||
[Dd]ebugPS/
|
||||
[Rr]eleasePS/
|
||||
dlldata.c
|
||||
|
||||
# DNX
|
||||
project.lock.json
|
||||
artifacts/
|
||||
|
||||
*_i.c
|
||||
*_p.c
|
||||
*_i.h
|
||||
*.ilk
|
||||
*.meta
|
||||
*.obj
|
||||
*.pch
|
||||
*.pdb
|
||||
*.pgc
|
||||
*.pgd
|
||||
*.rsp
|
||||
*.sbr
|
||||
*.tlb
|
||||
*.tli
|
||||
*.tlh
|
||||
*.tmp
|
||||
*.tmp_proj
|
||||
*.log
|
||||
*.vspscc
|
||||
*.vssscc
|
||||
.builds
|
||||
*.pidb
|
||||
*.svclog
|
||||
*.scc
|
||||
|
||||
# Chutzpah Test files
|
||||
_Chutzpah*
|
||||
|
||||
# Visual C++ cache files
|
||||
ipch/
|
||||
*.aps
|
||||
*.ncb
|
||||
*.opendb
|
||||
*.opensdf
|
||||
*.sdf
|
||||
*.cachefile
|
||||
|
||||
# Visual Studio profiler
|
||||
*.psess
|
||||
*.vsp
|
||||
*.vspx
|
||||
*.sap
|
||||
|
||||
# TFS 2012 Local Workspace
|
||||
$tf/
|
||||
|
||||
# Guidance Automation Toolkit
|
||||
*.gpState
|
||||
|
||||
# ReSharper is a .NET coding add-in
|
||||
_ReSharper*/
|
||||
*.[Rr]e[Ss]harper
|
||||
*.DotSettings.user
|
||||
|
||||
# JustCode is a .NET coding add-in
|
||||
.JustCode
|
||||
|
||||
# TeamCity is a build add-in
|
||||
_TeamCity*
|
||||
|
||||
# DotCover is a Code Coverage Tool
|
||||
*.dotCover
|
||||
|
||||
# NCrunch
|
||||
_NCrunch_*
|
||||
.*crunch*.local.xml
|
||||
nCrunchTemp_*
|
||||
|
||||
# MightyMoose
|
||||
*.mm.*
|
||||
AutoTest.Net/
|
||||
|
||||
# Web workbench (sass)
|
||||
.sass-cache/
|
||||
|
||||
# Installshield output folder
|
||||
[Ee]xpress/
|
||||
|
||||
# DocProject is a documentation generator add-in
|
||||
DocProject/buildhelp/
|
||||
DocProject/Help/*.HxT
|
||||
DocProject/Help/*.HxC
|
||||
DocProject/Help/*.hhc
|
||||
DocProject/Help/*.hhk
|
||||
DocProject/Help/*.hhp
|
||||
DocProject/Help/Html2
|
||||
DocProject/Help/html
|
||||
|
||||
# Click-Once directory
|
||||
publish/
|
||||
|
||||
# Publish Web Output
|
||||
*.[Pp]ublish.xml
|
||||
*.azurePubxml
|
||||
# TODO: Comment the next line if you want to checkin your web deploy settings
|
||||
# but database connection strings (with potential passwords) will be unencrypted
|
||||
*.pubxml
|
||||
*.publishproj
|
||||
|
||||
# NuGet Packages
|
||||
*.nupkg
|
||||
# The packages folder can be ignored because of Package Restore
|
||||
**/packages/*
|
||||
# except build/, which is used as an MSBuild target.
|
||||
!**/packages/build/
|
||||
# Uncomment if necessary however generally it will be regenerated when needed
|
||||
#!**/packages/repositories.config
|
||||
|
||||
# Microsoft Azure Build Output
|
||||
csx/
|
||||
*.build.csdef
|
||||
|
||||
# Microsoft Azure Emulator
|
||||
ecf/
|
||||
rcf/
|
||||
|
||||
# Microsoft Azure ApplicationInsights config file
|
||||
ApplicationInsights.config
|
||||
|
||||
# Windows Store app package directory
|
||||
AppPackages/
|
||||
BundleArtifacts/
|
||||
|
||||
# Visual Studio cache files
|
||||
# files ending in .cache can be ignored
|
||||
*.[Cc]ache
|
||||
# but keep track of directories ending in .cache
|
||||
!*.[Cc]ache/
|
||||
|
||||
# Others
|
||||
ClientBin/
|
||||
~$*
|
||||
*~
|
||||
*.dbmdl
|
||||
*.dbproj.schemaview
|
||||
*.pfx
|
||||
*.publishsettings
|
||||
node_modules/
|
||||
orleans.codegen.cs
|
||||
|
||||
# RIA/Silverlight projects
|
||||
Generated_Code/
|
||||
|
||||
# Backup & report files from converting an old project file
|
||||
# to a newer Visual Studio version. Backup files are not needed,
|
||||
# because we have git ;-)
|
||||
_UpgradeReport_Files/
|
||||
Backup*/
|
||||
UpgradeLog*.XML
|
||||
UpgradeLog*.htm
|
||||
|
||||
# SQL Server files
|
||||
*.mdf
|
||||
*.ldf
|
||||
|
||||
# Business Intelligence projects
|
||||
*.rdl.data
|
||||
*.bim.layout
|
||||
*.bim_*.settings
|
||||
|
||||
# Microsoft Fakes
|
||||
FakesAssemblies/
|
||||
|
||||
# GhostDoc plugin setting file
|
||||
*.GhostDoc.xml
|
||||
|
||||
# Node.js Tools for Visual Studio
|
||||
.ntvs_analysis.dat
|
||||
|
||||
# Visual Studio 6 build log
|
||||
*.plg
|
||||
|
||||
# Visual Studio 6 workspace options file
|
||||
*.opt
|
||||
|
||||
# Visual Studio LightSwitch build output
|
||||
**/*.HTMLClient/GeneratedArtifacts
|
||||
**/*.DesktopClient/GeneratedArtifacts
|
||||
**/*.DesktopClient/ModelManifest.xml
|
||||
**/*.Server/GeneratedArtifacts
|
||||
**/*.Server/ModelManifest.xml
|
||||
_Pvt_Extensions
|
||||
|
||||
# Paket dependency manager
|
||||
.paket/paket.exe
|
||||
|
||||
# FAKE - F# Make
|
||||
.fake/
|
||||
## Ignore Visual Studio temporary files, build results, and
|
||||
## files generated by popular Visual Studio add-ons.
|
||||
|
||||
# User-specific files
|
||||
*.suo
|
||||
*.user
|
||||
*.userosscache
|
||||
*.sln.docstates
|
||||
|
||||
# User-specific files (MonoDevelop/Xamarin Studio)
|
||||
*.userprefs
|
||||
|
||||
# Build results
|
||||
[Dd]ebug/
|
||||
[Dd]ebugPublic/
|
||||
[Rr]elease/
|
||||
[Rr]eleases/
|
||||
x64/
|
||||
x86/
|
||||
build/
|
||||
bld/
|
||||
[Bb]in/
|
||||
[Oo]bj/
|
||||
|
||||
# Visual Studio 2015 cache/options directory
|
||||
.vs/
|
||||
# Uncomment if you have tasks that create the project's static files in wwwroot
|
||||
#wwwroot/
|
||||
|
||||
# MSTest test Results
|
||||
[Tt]est[Rr]esult*/
|
||||
[Bb]uild[Ll]og.*
|
||||
|
||||
# NUNIT
|
||||
*.VisualState.xml
|
||||
TestResult.xml
|
||||
|
||||
# Build Results of an ATL Project
|
||||
[Dd]ebugPS/
|
||||
[Rr]eleasePS/
|
||||
dlldata.c
|
||||
|
||||
# DNX
|
||||
project.lock.json
|
||||
artifacts/
|
||||
|
||||
*_i.c
|
||||
*_p.c
|
||||
*_i.h
|
||||
*.ilk
|
||||
*.meta
|
||||
*.obj
|
||||
*.pch
|
||||
*.pdb
|
||||
*.pgc
|
||||
*.pgd
|
||||
*.rsp
|
||||
*.sbr
|
||||
*.tlb
|
||||
*.tli
|
||||
*.tlh
|
||||
*.tmp
|
||||
*.tmp_proj
|
||||
*.log
|
||||
*.vspscc
|
||||
*.vssscc
|
||||
.builds
|
||||
*.pidb
|
||||
*.svclog
|
||||
*.scc
|
||||
|
||||
# Chutzpah Test files
|
||||
_Chutzpah*
|
||||
|
||||
# Visual C++ cache files
|
||||
ipch/
|
||||
*.aps
|
||||
*.ncb
|
||||
*.opendb
|
||||
*.opensdf
|
||||
*.sdf
|
||||
*.cachefile
|
||||
|
||||
# Visual Studio profiler
|
||||
*.psess
|
||||
*.vsp
|
||||
*.vspx
|
||||
*.sap
|
||||
|
||||
# TFS 2012 Local Workspace
|
||||
$tf/
|
||||
|
||||
# Guidance Automation Toolkit
|
||||
*.gpState
|
||||
|
||||
# ReSharper is a .NET coding add-in
|
||||
_ReSharper*/
|
||||
*.[Rr]e[Ss]harper
|
||||
*.DotSettings.user
|
||||
|
||||
# JustCode is a .NET coding add-in
|
||||
.JustCode
|
||||
|
||||
# TeamCity is a build add-in
|
||||
_TeamCity*
|
||||
|
||||
# DotCover is a Code Coverage Tool
|
||||
*.dotCover
|
||||
|
||||
# NCrunch
|
||||
_NCrunch_*
|
||||
.*crunch*.local.xml
|
||||
nCrunchTemp_*
|
||||
|
||||
# MightyMoose
|
||||
*.mm.*
|
||||
AutoTest.Net/
|
||||
|
||||
# Web workbench (sass)
|
||||
.sass-cache/
|
||||
|
||||
# Installshield output folder
|
||||
[Ee]xpress/
|
||||
|
||||
# DocProject is a documentation generator add-in
|
||||
DocProject/buildhelp/
|
||||
DocProject/Help/*.HxT
|
||||
DocProject/Help/*.HxC
|
||||
DocProject/Help/*.hhc
|
||||
DocProject/Help/*.hhk
|
||||
DocProject/Help/*.hhp
|
||||
DocProject/Help/Html2
|
||||
DocProject/Help/html
|
||||
|
||||
# Click-Once directory
|
||||
publish/
|
||||
|
||||
# Publish Web Output
|
||||
*.[Pp]ublish.xml
|
||||
*.azurePubxml
|
||||
# TODO: Comment the next line if you want to checkin your web deploy settings
|
||||
# but database connection strings (with potential passwords) will be unencrypted
|
||||
*.pubxml
|
||||
*.publishproj
|
||||
|
||||
# NuGet Packages
|
||||
*.nupkg
|
||||
# The packages folder can be ignored because of Package Restore
|
||||
**/packages/*
|
||||
# except build/, which is used as an MSBuild target.
|
||||
!**/packages/build/
|
||||
# Uncomment if necessary however generally it will be regenerated when needed
|
||||
#!**/packages/repositories.config
|
||||
|
||||
# Microsoft Azure Build Output
|
||||
csx/
|
||||
*.build.csdef
|
||||
|
||||
# Microsoft Azure Emulator
|
||||
ecf/
|
||||
rcf/
|
||||
|
||||
# Microsoft Azure ApplicationInsights config file
|
||||
ApplicationInsights.config
|
||||
|
||||
# Windows Store app package directory
|
||||
AppPackages/
|
||||
BundleArtifacts/
|
||||
|
||||
# Visual Studio cache files
|
||||
# files ending in .cache can be ignored
|
||||
*.[Cc]ache
|
||||
# but keep track of directories ending in .cache
|
||||
!*.[Cc]ache/
|
||||
|
||||
# Others
|
||||
ClientBin/
|
||||
~$*
|
||||
*~
|
||||
*.dbmdl
|
||||
*.dbproj.schemaview
|
||||
*.pfx
|
||||
*.publishsettings
|
||||
node_modules/
|
||||
orleans.codegen.cs
|
||||
|
||||
# RIA/Silverlight projects
|
||||
Generated_Code/
|
||||
|
||||
# Backup & report files from converting an old project file
|
||||
# to a newer Visual Studio version. Backup files are not needed,
|
||||
# because we have git ;-)
|
||||
_UpgradeReport_Files/
|
||||
Backup*/
|
||||
UpgradeLog*.XML
|
||||
UpgradeLog*.htm
|
||||
|
||||
# SQL Server files
|
||||
*.mdf
|
||||
*.ldf
|
||||
|
||||
# Business Intelligence projects
|
||||
*.rdl.data
|
||||
*.bim.layout
|
||||
*.bim_*.settings
|
||||
|
||||
# Microsoft Fakes
|
||||
FakesAssemblies/
|
||||
|
||||
# GhostDoc plugin setting file
|
||||
*.GhostDoc.xml
|
||||
|
||||
# Node.js Tools for Visual Studio
|
||||
.ntvs_analysis.dat
|
||||
|
||||
# Visual Studio 6 build log
|
||||
*.plg
|
||||
|
||||
# Visual Studio 6 workspace options file
|
||||
*.opt
|
||||
|
||||
# Visual Studio LightSwitch build output
|
||||
**/*.HTMLClient/GeneratedArtifacts
|
||||
**/*.DesktopClient/GeneratedArtifacts
|
||||
**/*.DesktopClient/ModelManifest.xml
|
||||
**/*.Server/GeneratedArtifacts
|
||||
**/*.Server/ModelManifest.xml
|
||||
_Pvt_Extensions
|
||||
|
||||
# Paket dependency manager
|
||||
.paket/paket.exe
|
||||
|
||||
# FAKE - F# Make
|
||||
.fake/
|
||||
|
@ -1,39 +1,39 @@
|
||||
using System;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Ocelot.DependencyInjection;
|
||||
using Ocelot.Middleware;
|
||||
using ConfigurationBuilder = Microsoft.Extensions.Configuration.ConfigurationBuilder;
|
||||
|
||||
namespace Ocelot.AcceptanceTests
|
||||
{
|
||||
public class AcceptanceTestsStartup
|
||||
{
|
||||
public AcceptanceTestsStartup(IHostingEnvironment env)
|
||||
{
|
||||
var builder = new ConfigurationBuilder()
|
||||
.SetBasePath(env.ContentRootPath)
|
||||
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
|
||||
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
|
||||
.AddJsonFile("configuration.json")
|
||||
.AddEnvironmentVariables();
|
||||
|
||||
Configuration = builder.Build();
|
||||
}
|
||||
|
||||
public IConfiguration Configuration { get; }
|
||||
|
||||
public virtual void ConfigureServices(IServiceCollection services)
|
||||
{
|
||||
services.AddOcelot(Configuration);
|
||||
}
|
||||
|
||||
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
|
||||
{
|
||||
app.UseOcelot().Wait();
|
||||
}
|
||||
}
|
||||
}
|
||||
using System;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Ocelot.DependencyInjection;
|
||||
using Ocelot.Middleware;
|
||||
using ConfigurationBuilder = Microsoft.Extensions.Configuration.ConfigurationBuilder;
|
||||
|
||||
namespace Ocelot.AcceptanceTests
|
||||
{
|
||||
public class AcceptanceTestsStartup
|
||||
{
|
||||
public AcceptanceTestsStartup(IHostingEnvironment env)
|
||||
{
|
||||
var builder = new ConfigurationBuilder()
|
||||
.SetBasePath(env.ContentRootPath)
|
||||
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
|
||||
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
|
||||
.AddJsonFile("configuration.json")
|
||||
.AddEnvironmentVariables();
|
||||
|
||||
Configuration = builder.Build();
|
||||
}
|
||||
|
||||
public IConfiguration Configuration { get; }
|
||||
|
||||
public virtual void ConfigureServices(IServiceCollection services)
|
||||
{
|
||||
services.AddOcelot(Configuration);
|
||||
}
|
||||
|
||||
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
|
||||
{
|
||||
app.UseOcelot().Wait();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,351 +1,381 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Security.Claims;
|
||||
using IdentityServer4.AccessTokenValidation;
|
||||
using IdentityServer4.Models;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Ocelot.Configuration.File;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.AcceptanceTests
|
||||
{
|
||||
using IdentityServer4.Test;
|
||||
|
||||
public class AuthenticationTests : IDisposable
|
||||
{
|
||||
private IWebHost _servicebuilder;
|
||||
private readonly Steps _steps;
|
||||
private IWebHost _identityServerBuilder;
|
||||
private string _identityServerRootUrl = "http://localhost:51888";
|
||||
private string _downstreamServicePath = "/";
|
||||
private string _downstreamServiceHost = "localhost";
|
||||
private int _downstreamServicePort = 51876;
|
||||
private string _downstreamServiceScheme = "http";
|
||||
private string _downstreamServiceUrl = "http://localhost:51876";
|
||||
private readonly Action<IdentityServerAuthenticationOptions> _options;
|
||||
|
||||
public AuthenticationTests()
|
||||
{
|
||||
_steps = new Steps();
|
||||
_options = o =>
|
||||
{
|
||||
o.Authority = _identityServerRootUrl;
|
||||
o.ApiName = "api";
|
||||
o.RequireHttpsMetadata = false;
|
||||
o.SupportedTokens = SupportedTokens.Both;
|
||||
o.ApiSecret = "secret";
|
||||
};
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_401_using_identity_server_access_token()
|
||||
{
|
||||
var configuration = new FileConfiguration
|
||||
{
|
||||
ReRoutes = new List<FileReRoute>
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamPathTemplate = _downstreamServicePath,
|
||||
DownstreamPort = _downstreamServicePort,
|
||||
DownstreamHost = _downstreamServiceHost,
|
||||
DownstreamScheme = _downstreamServiceScheme,
|
||||
UpstreamPathTemplate = "/",
|
||||
UpstreamHttpMethod = new List<string> { "Post" },
|
||||
AuthenticationOptions = new FileAuthenticationOptions
|
||||
{
|
||||
AuthenticationProviderKey = "Test"
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenThereIsAnIdentityServerOn(_identityServerRootUrl, "api", "api2", AccessTokenType.Jwt))
|
||||
.And(x => x.GivenThereIsAServiceRunningOn(_downstreamServiceUrl, 201, string.Empty))
|
||||
.And(x => _steps.GivenThereIsAConfiguration(configuration))
|
||||
.And(x => _steps.GivenOcelotIsRunning(_options, "Test"))
|
||||
.And(x => _steps.GivenThePostHasContent("postContent"))
|
||||
.When(x => _steps.WhenIPostUrlOnTheApiGateway("/"))
|
||||
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.Unauthorized))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_response_200_using_identity_server()
|
||||
{
|
||||
var configuration = new FileConfiguration
|
||||
{
|
||||
ReRoutes = new List<FileReRoute>
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamPathTemplate = _downstreamServicePath,
|
||||
DownstreamPort = _downstreamServicePort,
|
||||
DownstreamHost = _downstreamServiceHost,
|
||||
DownstreamScheme = _downstreamServiceScheme,
|
||||
UpstreamPathTemplate = "/",
|
||||
UpstreamHttpMethod = new List<string> { "Get" },
|
||||
AuthenticationOptions = new FileAuthenticationOptions
|
||||
{
|
||||
AuthenticationProviderKey = "Test"
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenThereIsAnIdentityServerOn(_identityServerRootUrl, "api", "api2", AccessTokenType.Jwt))
|
||||
.And(x => x.GivenThereIsAServiceRunningOn(_downstreamServiceUrl, 200, "Hello from Laura"))
|
||||
.And(x => _steps.GivenIHaveAToken(_identityServerRootUrl))
|
||||
.And(x => _steps.GivenThereIsAConfiguration(configuration))
|
||||
.And(x => _steps.GivenOcelotIsRunning(_options, "Test"))
|
||||
.And(x => _steps.GivenIHaveAddedATokenToMyRequest())
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
|
||||
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
|
||||
.And(x => _steps.ThenTheResponseBodyShouldBe("Hello from Laura"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_response_401_using_identity_server_with_token_requested_for_other_api()
|
||||
{
|
||||
var configuration = new FileConfiguration
|
||||
{
|
||||
ReRoutes = new List<FileReRoute>
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamPathTemplate = _downstreamServicePath,
|
||||
DownstreamPort = _downstreamServicePort,
|
||||
DownstreamHost = _downstreamServiceHost,
|
||||
DownstreamScheme = _downstreamServiceScheme,
|
||||
UpstreamPathTemplate = "/",
|
||||
UpstreamHttpMethod = new List<string> { "Get" },
|
||||
AuthenticationOptions = new FileAuthenticationOptions
|
||||
{
|
||||
AuthenticationProviderKey = "Test"
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenThereIsAnIdentityServerOn(_identityServerRootUrl, "api", "api2", AccessTokenType.Jwt))
|
||||
.And(x => x.GivenThereIsAServiceRunningOn(_downstreamServiceUrl, 200, "Hello from Laura"))
|
||||
.And(x => _steps.GivenIHaveATokenForApi2(_identityServerRootUrl))
|
||||
.And(x => _steps.GivenThereIsAConfiguration(configuration))
|
||||
.And(x => _steps.GivenOcelotIsRunning(_options, "Test"))
|
||||
.And(x => _steps.GivenIHaveAddedATokenToMyRequest())
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
|
||||
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.Unauthorized))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_201_using_identity_server_access_token()
|
||||
{
|
||||
var configuration = new FileConfiguration
|
||||
{
|
||||
ReRoutes = new List<FileReRoute>
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamPathTemplate = _downstreamServicePath,
|
||||
DownstreamPort = _downstreamServicePort,
|
||||
DownstreamHost = _downstreamServiceHost,
|
||||
DownstreamScheme = _downstreamServiceScheme,
|
||||
UpstreamPathTemplate = "/",
|
||||
UpstreamHttpMethod = new List<string> { "Post" },
|
||||
AuthenticationOptions = new FileAuthenticationOptions
|
||||
{
|
||||
AuthenticationProviderKey = "Test"
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenThereIsAnIdentityServerOn(_identityServerRootUrl, "api", "api2", AccessTokenType.Jwt))
|
||||
.And(x => x.GivenThereIsAServiceRunningOn(_downstreamServiceUrl, 201, string.Empty))
|
||||
.And(x => _steps.GivenIHaveAToken(_identityServerRootUrl))
|
||||
.And(x => _steps.GivenThereIsAConfiguration(configuration))
|
||||
.And(x => _steps.GivenOcelotIsRunning(_options, "Test"))
|
||||
.And(x => _steps.GivenIHaveAddedATokenToMyRequest())
|
||||
.And(x => _steps.GivenThePostHasContent("postContent"))
|
||||
.When(x => _steps.WhenIPostUrlOnTheApiGateway("/"))
|
||||
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.Created))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_201_using_identity_server_reference_token()
|
||||
{
|
||||
var configuration = new FileConfiguration
|
||||
{
|
||||
ReRoutes = new List<FileReRoute>
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamPathTemplate = _downstreamServicePath,
|
||||
DownstreamPort = _downstreamServicePort,
|
||||
DownstreamHost = _downstreamServiceHost,
|
||||
DownstreamScheme = _downstreamServiceScheme,
|
||||
UpstreamPathTemplate = "/",
|
||||
UpstreamHttpMethod = new List<string> { "Post" },
|
||||
AuthenticationOptions = new FileAuthenticationOptions
|
||||
{
|
||||
AuthenticationProviderKey = "Test"
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenThereIsAnIdentityServerOn(_identityServerRootUrl, "api", "api2", AccessTokenType.Reference))
|
||||
.And(x => x.GivenThereIsAServiceRunningOn(_downstreamServiceUrl, 201, string.Empty))
|
||||
.And(x => _steps.GivenIHaveAToken(_identityServerRootUrl))
|
||||
.And(x => _steps.GivenThereIsAConfiguration(configuration))
|
||||
.And(x => _steps.GivenOcelotIsRunning(_options, "Test"))
|
||||
.And(x => _steps.GivenIHaveAddedATokenToMyRequest())
|
||||
.And(x => _steps.GivenThePostHasContent("postContent"))
|
||||
.When(x => _steps.WhenIPostUrlOnTheApiGateway("/"))
|
||||
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.Created))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenThereIsAServiceRunningOn(string url, int statusCode, string responseBody)
|
||||
{
|
||||
_servicebuilder = new WebHostBuilder()
|
||||
.UseUrls(url)
|
||||
.UseKestrel()
|
||||
.UseContentRoot(Directory.GetCurrentDirectory())
|
||||
.UseIISIntegration()
|
||||
.UseUrls(url)
|
||||
.Configure(app =>
|
||||
{
|
||||
app.Run(async context =>
|
||||
{
|
||||
context.Response.StatusCode = statusCode;
|
||||
await context.Response.WriteAsync(responseBody);
|
||||
});
|
||||
})
|
||||
.Build();
|
||||
|
||||
_servicebuilder.Start();
|
||||
}
|
||||
|
||||
private void GivenThereIsAnIdentityServerOn(string url, string apiName, string api2Name, AccessTokenType tokenType)
|
||||
{
|
||||
_identityServerBuilder = new WebHostBuilder()
|
||||
.UseUrls(url)
|
||||
.UseKestrel()
|
||||
.UseContentRoot(Directory.GetCurrentDirectory())
|
||||
.UseIISIntegration()
|
||||
.UseUrls(url)
|
||||
.ConfigureServices(services =>
|
||||
{
|
||||
services.AddLogging();
|
||||
services.AddIdentityServer()
|
||||
.AddDeveloperSigningCredential()
|
||||
.AddInMemoryApiResources(new List<ApiResource>
|
||||
{
|
||||
new ApiResource
|
||||
{
|
||||
Name = apiName,
|
||||
Description = "My API",
|
||||
Enabled = true,
|
||||
DisplayName = "test",
|
||||
Scopes = new List<Scope>()
|
||||
{
|
||||
new Scope("api"),
|
||||
new Scope("api.readOnly"),
|
||||
new Scope("openid"),
|
||||
new Scope("offline_access")
|
||||
},
|
||||
ApiSecrets = new List<Secret>()
|
||||
{
|
||||
new Secret
|
||||
{
|
||||
Value = "secret".Sha256()
|
||||
}
|
||||
},
|
||||
UserClaims = new List<string>()
|
||||
{
|
||||
"CustomerId", "LocationId"
|
||||
}
|
||||
},
|
||||
new ApiResource
|
||||
{
|
||||
Name = api2Name,
|
||||
Description = "My second API",
|
||||
Enabled = true,
|
||||
DisplayName = "second test",
|
||||
Scopes = new List<Scope>()
|
||||
{
|
||||
new Scope("api2"),
|
||||
new Scope("api2.readOnly"),
|
||||
new Scope("openid"),
|
||||
new Scope("offline_access")
|
||||
},
|
||||
ApiSecrets = new List<Secret>()
|
||||
{
|
||||
new Secret
|
||||
{
|
||||
Value = "secret".Sha256()
|
||||
}
|
||||
},
|
||||
UserClaims = new List<string>()
|
||||
{
|
||||
"CustomerId", "LocationId"
|
||||
}
|
||||
},
|
||||
})
|
||||
.AddInMemoryClients(new List<Client>
|
||||
{
|
||||
new Client
|
||||
{
|
||||
ClientId = "client",
|
||||
AllowedGrantTypes = GrantTypes.ResourceOwnerPassword,
|
||||
ClientSecrets = new List<Secret> {new Secret("secret".Sha256())},
|
||||
AllowedScopes = new List<string> { apiName, api2Name, "api.readOnly", "openid", "offline_access" },
|
||||
AccessTokenType = tokenType,
|
||||
Enabled = true,
|
||||
RequireClientSecret = false
|
||||
}
|
||||
})
|
||||
.AddTestUsers(new List<TestUser>
|
||||
{
|
||||
new TestUser
|
||||
{
|
||||
Username = "test",
|
||||
Password = "test",
|
||||
SubjectId = "registered|1231231",
|
||||
Claims = new List<Claim>
|
||||
{
|
||||
new Claim("CustomerId", "123"),
|
||||
new Claim("LocationId", "321")
|
||||
}
|
||||
}
|
||||
});
|
||||
})
|
||||
.Configure(app =>
|
||||
{
|
||||
app.UseIdentityServer();
|
||||
})
|
||||
.Build();
|
||||
|
||||
_identityServerBuilder.Start();
|
||||
|
||||
_steps.VerifyIdentiryServerStarted(url);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_servicebuilder?.Dispose();
|
||||
_steps.Dispose();
|
||||
_identityServerBuilder?.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Security.Claims;
|
||||
using IdentityServer4.AccessTokenValidation;
|
||||
using IdentityServer4.Models;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Ocelot.Configuration.File;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.AcceptanceTests
|
||||
{
|
||||
using IdentityServer4.Test;
|
||||
|
||||
public class AuthenticationTests : IDisposable
|
||||
{
|
||||
private IWebHost _servicebuilder;
|
||||
private readonly Steps _steps;
|
||||
private IWebHost _identityServerBuilder;
|
||||
private string _identityServerRootUrl = "http://localhost:51888";
|
||||
private string _downstreamServicePath = "/";
|
||||
private string _downstreamServiceHost = "localhost";
|
||||
private int _downstreamServicePort = 51876;
|
||||
private string _downstreamServiceScheme = "http";
|
||||
private string _downstreamServiceUrl = "http://localhost:51876";
|
||||
private readonly Action<IdentityServerAuthenticationOptions> _options;
|
||||
|
||||
public AuthenticationTests()
|
||||
{
|
||||
_steps = new Steps();
|
||||
_options = o =>
|
||||
{
|
||||
o.Authority = _identityServerRootUrl;
|
||||
o.ApiName = "api";
|
||||
o.RequireHttpsMetadata = false;
|
||||
o.SupportedTokens = SupportedTokens.Both;
|
||||
o.ApiSecret = "secret";
|
||||
};
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_401_using_identity_server_access_token()
|
||||
{
|
||||
var configuration = new FileConfiguration
|
||||
{
|
||||
ReRoutes = new List<FileReRoute>
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamPathTemplate = _downstreamServicePath,
|
||||
DownstreamHostAndPorts = new List<FileHostAndPort>
|
||||
{
|
||||
new FileHostAndPort
|
||||
{
|
||||
Host =_downstreamServiceHost,
|
||||
Port = _downstreamServicePort,
|
||||
}
|
||||
},
|
||||
DownstreamScheme = _downstreamServiceScheme,
|
||||
UpstreamPathTemplate = "/",
|
||||
UpstreamHttpMethod = new List<string> { "Post" },
|
||||
AuthenticationOptions = new FileAuthenticationOptions
|
||||
{
|
||||
AuthenticationProviderKey = "Test"
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenThereIsAnIdentityServerOn(_identityServerRootUrl, "api", "api2", AccessTokenType.Jwt))
|
||||
.And(x => x.GivenThereIsAServiceRunningOn(_downstreamServiceUrl, 201, string.Empty))
|
||||
.And(x => _steps.GivenThereIsAConfiguration(configuration))
|
||||
.And(x => _steps.GivenOcelotIsRunning(_options, "Test"))
|
||||
.And(x => _steps.GivenThePostHasContent("postContent"))
|
||||
.When(x => _steps.WhenIPostUrlOnTheApiGateway("/"))
|
||||
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.Unauthorized))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_response_200_using_identity_server()
|
||||
{
|
||||
var configuration = new FileConfiguration
|
||||
{
|
||||
ReRoutes = new List<FileReRoute>
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamPathTemplate = _downstreamServicePath,
|
||||
DownstreamHostAndPorts = new List<FileHostAndPort>
|
||||
{
|
||||
new FileHostAndPort
|
||||
{
|
||||
Host =_downstreamServiceHost,
|
||||
Port = _downstreamServicePort,
|
||||
}
|
||||
},
|
||||
DownstreamScheme = _downstreamServiceScheme,
|
||||
UpstreamPathTemplate = "/",
|
||||
UpstreamHttpMethod = new List<string> { "Get" },
|
||||
AuthenticationOptions = new FileAuthenticationOptions
|
||||
{
|
||||
AuthenticationProviderKey = "Test"
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenThereIsAnIdentityServerOn(_identityServerRootUrl, "api", "api2", AccessTokenType.Jwt))
|
||||
.And(x => x.GivenThereIsAServiceRunningOn(_downstreamServiceUrl, 200, "Hello from Laura"))
|
||||
.And(x => _steps.GivenIHaveAToken(_identityServerRootUrl))
|
||||
.And(x => _steps.GivenThereIsAConfiguration(configuration))
|
||||
.And(x => _steps.GivenOcelotIsRunning(_options, "Test"))
|
||||
.And(x => _steps.GivenIHaveAddedATokenToMyRequest())
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
|
||||
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
|
||||
.And(x => _steps.ThenTheResponseBodyShouldBe("Hello from Laura"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_response_401_using_identity_server_with_token_requested_for_other_api()
|
||||
{
|
||||
var configuration = new FileConfiguration
|
||||
{
|
||||
ReRoutes = new List<FileReRoute>
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamPathTemplate = _downstreamServicePath,
|
||||
DownstreamHostAndPorts = new List<FileHostAndPort>
|
||||
{
|
||||
new FileHostAndPort
|
||||
{
|
||||
Host =_downstreamServiceHost,
|
||||
Port = _downstreamServicePort,
|
||||
}
|
||||
},
|
||||
DownstreamScheme = _downstreamServiceScheme,
|
||||
UpstreamPathTemplate = "/",
|
||||
UpstreamHttpMethod = new List<string> { "Get" },
|
||||
AuthenticationOptions = new FileAuthenticationOptions
|
||||
{
|
||||
AuthenticationProviderKey = "Test"
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenThereIsAnIdentityServerOn(_identityServerRootUrl, "api", "api2", AccessTokenType.Jwt))
|
||||
.And(x => x.GivenThereIsAServiceRunningOn(_downstreamServiceUrl, 200, "Hello from Laura"))
|
||||
.And(x => _steps.GivenIHaveATokenForApi2(_identityServerRootUrl))
|
||||
.And(x => _steps.GivenThereIsAConfiguration(configuration))
|
||||
.And(x => _steps.GivenOcelotIsRunning(_options, "Test"))
|
||||
.And(x => _steps.GivenIHaveAddedATokenToMyRequest())
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
|
||||
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.Unauthorized))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_201_using_identity_server_access_token()
|
||||
{
|
||||
var configuration = new FileConfiguration
|
||||
{
|
||||
ReRoutes = new List<FileReRoute>
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamPathTemplate = _downstreamServicePath,
|
||||
DownstreamHostAndPorts = new List<FileHostAndPort>
|
||||
{
|
||||
new FileHostAndPort
|
||||
{
|
||||
Host =_downstreamServiceHost,
|
||||
Port = _downstreamServicePort,
|
||||
}
|
||||
},
|
||||
DownstreamScheme = _downstreamServiceScheme,
|
||||
UpstreamPathTemplate = "/",
|
||||
UpstreamHttpMethod = new List<string> { "Post" },
|
||||
AuthenticationOptions = new FileAuthenticationOptions
|
||||
{
|
||||
AuthenticationProviderKey = "Test"
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenThereIsAnIdentityServerOn(_identityServerRootUrl, "api", "api2", AccessTokenType.Jwt))
|
||||
.And(x => x.GivenThereIsAServiceRunningOn(_downstreamServiceUrl, 201, string.Empty))
|
||||
.And(x => _steps.GivenIHaveAToken(_identityServerRootUrl))
|
||||
.And(x => _steps.GivenThereIsAConfiguration(configuration))
|
||||
.And(x => _steps.GivenOcelotIsRunning(_options, "Test"))
|
||||
.And(x => _steps.GivenIHaveAddedATokenToMyRequest())
|
||||
.And(x => _steps.GivenThePostHasContent("postContent"))
|
||||
.When(x => _steps.WhenIPostUrlOnTheApiGateway("/"))
|
||||
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.Created))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_201_using_identity_server_reference_token()
|
||||
{
|
||||
var configuration = new FileConfiguration
|
||||
{
|
||||
ReRoutes = new List<FileReRoute>
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamPathTemplate = _downstreamServicePath,
|
||||
DownstreamHostAndPorts = new List<FileHostAndPort>
|
||||
{
|
||||
new FileHostAndPort
|
||||
{
|
||||
Host =_downstreamServiceHost,
|
||||
Port = _downstreamServicePort,
|
||||
}
|
||||
},
|
||||
DownstreamScheme = _downstreamServiceScheme,
|
||||
UpstreamPathTemplate = "/",
|
||||
UpstreamHttpMethod = new List<string> { "Post" },
|
||||
AuthenticationOptions = new FileAuthenticationOptions
|
||||
{
|
||||
AuthenticationProviderKey = "Test"
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenThereIsAnIdentityServerOn(_identityServerRootUrl, "api", "api2", AccessTokenType.Reference))
|
||||
.And(x => x.GivenThereIsAServiceRunningOn(_downstreamServiceUrl, 201, string.Empty))
|
||||
.And(x => _steps.GivenIHaveAToken(_identityServerRootUrl))
|
||||
.And(x => _steps.GivenThereIsAConfiguration(configuration))
|
||||
.And(x => _steps.GivenOcelotIsRunning(_options, "Test"))
|
||||
.And(x => _steps.GivenIHaveAddedATokenToMyRequest())
|
||||
.And(x => _steps.GivenThePostHasContent("postContent"))
|
||||
.When(x => _steps.WhenIPostUrlOnTheApiGateway("/"))
|
||||
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.Created))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenThereIsAServiceRunningOn(string url, int statusCode, string responseBody)
|
||||
{
|
||||
_servicebuilder = new WebHostBuilder()
|
||||
.UseUrls(url)
|
||||
.UseKestrel()
|
||||
.UseContentRoot(Directory.GetCurrentDirectory())
|
||||
.UseIISIntegration()
|
||||
.UseUrls(url)
|
||||
.Configure(app =>
|
||||
{
|
||||
app.Run(async context =>
|
||||
{
|
||||
context.Response.StatusCode = statusCode;
|
||||
await context.Response.WriteAsync(responseBody);
|
||||
});
|
||||
})
|
||||
.Build();
|
||||
|
||||
_servicebuilder.Start();
|
||||
}
|
||||
|
||||
private void GivenThereIsAnIdentityServerOn(string url, string apiName, string api2Name, AccessTokenType tokenType)
|
||||
{
|
||||
_identityServerBuilder = new WebHostBuilder()
|
||||
.UseUrls(url)
|
||||
.UseKestrel()
|
||||
.UseContentRoot(Directory.GetCurrentDirectory())
|
||||
.UseIISIntegration()
|
||||
.UseUrls(url)
|
||||
.ConfigureServices(services =>
|
||||
{
|
||||
services.AddLogging();
|
||||
services.AddIdentityServer()
|
||||
.AddDeveloperSigningCredential()
|
||||
.AddInMemoryApiResources(new List<ApiResource>
|
||||
{
|
||||
new ApiResource
|
||||
{
|
||||
Name = apiName,
|
||||
Description = "My API",
|
||||
Enabled = true,
|
||||
DisplayName = "test",
|
||||
Scopes = new List<Scope>()
|
||||
{
|
||||
new Scope("api"),
|
||||
new Scope("api.readOnly"),
|
||||
new Scope("openid"),
|
||||
new Scope("offline_access")
|
||||
},
|
||||
ApiSecrets = new List<Secret>()
|
||||
{
|
||||
new Secret
|
||||
{
|
||||
Value = "secret".Sha256()
|
||||
}
|
||||
},
|
||||
UserClaims = new List<string>()
|
||||
{
|
||||
"CustomerId", "LocationId"
|
||||
}
|
||||
},
|
||||
new ApiResource
|
||||
{
|
||||
Name = api2Name,
|
||||
Description = "My second API",
|
||||
Enabled = true,
|
||||
DisplayName = "second test",
|
||||
Scopes = new List<Scope>()
|
||||
{
|
||||
new Scope("api2"),
|
||||
new Scope("api2.readOnly"),
|
||||
new Scope("openid"),
|
||||
new Scope("offline_access")
|
||||
},
|
||||
ApiSecrets = new List<Secret>()
|
||||
{
|
||||
new Secret
|
||||
{
|
||||
Value = "secret".Sha256()
|
||||
}
|
||||
},
|
||||
UserClaims = new List<string>()
|
||||
{
|
||||
"CustomerId", "LocationId"
|
||||
}
|
||||
},
|
||||
})
|
||||
.AddInMemoryClients(new List<Client>
|
||||
{
|
||||
new Client
|
||||
{
|
||||
ClientId = "client",
|
||||
AllowedGrantTypes = GrantTypes.ResourceOwnerPassword,
|
||||
ClientSecrets = new List<Secret> {new Secret("secret".Sha256())},
|
||||
AllowedScopes = new List<string> { apiName, api2Name, "api.readOnly", "openid", "offline_access" },
|
||||
AccessTokenType = tokenType,
|
||||
Enabled = true,
|
||||
RequireClientSecret = false
|
||||
}
|
||||
})
|
||||
.AddTestUsers(new List<TestUser>
|
||||
{
|
||||
new TestUser
|
||||
{
|
||||
Username = "test",
|
||||
Password = "test",
|
||||
SubjectId = "registered|1231231",
|
||||
Claims = new List<Claim>
|
||||
{
|
||||
new Claim("CustomerId", "123"),
|
||||
new Claim("LocationId", "321")
|
||||
}
|
||||
}
|
||||
});
|
||||
})
|
||||
.Configure(app =>
|
||||
{
|
||||
app.UseIdentityServer();
|
||||
})
|
||||
.Build();
|
||||
|
||||
_identityServerBuilder.Start();
|
||||
|
||||
_steps.VerifyIdentiryServerStarted(url);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_servicebuilder?.Dispose();
|
||||
_steps.Dispose();
|
||||
_identityServerBuilder?.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,324 +1,348 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Security.Claims;
|
||||
using IdentityServer4.AccessTokenValidation;
|
||||
using IdentityServer4.Models;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Ocelot.Configuration.File;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.AcceptanceTests
|
||||
{
|
||||
using IdentityServer4;
|
||||
using IdentityServer4.Test;
|
||||
|
||||
public class AuthorisationTests : IDisposable
|
||||
{
|
||||
private IWebHost _servicebuilder;
|
||||
private IWebHost _identityServerBuilder;
|
||||
private readonly Steps _steps;
|
||||
private Action<IdentityServerAuthenticationOptions> _options;
|
||||
private string _identityServerRootUrl = "http://localhost:51888";
|
||||
|
||||
public AuthorisationTests()
|
||||
{
|
||||
_steps = new Steps();
|
||||
_options = o =>
|
||||
{
|
||||
o.Authority = _identityServerRootUrl;
|
||||
o.ApiName = "api";
|
||||
o.RequireHttpsMetadata = false;
|
||||
o.SupportedTokens = SupportedTokens.Both;
|
||||
o.ApiSecret = "secret";
|
||||
};
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_response_200_authorising_route()
|
||||
{
|
||||
var configuration = new FileConfiguration
|
||||
{
|
||||
ReRoutes = new List<FileReRoute>
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamPathTemplate = "/",
|
||||
DownstreamPort = 51876,
|
||||
DownstreamScheme = "http",
|
||||
DownstreamHost = "localhost",
|
||||
UpstreamPathTemplate = "/",
|
||||
UpstreamHttpMethod = new List<string> { "Get" },
|
||||
AuthenticationOptions = new FileAuthenticationOptions
|
||||
{
|
||||
AuthenticationProviderKey = "Test"
|
||||
},
|
||||
AddHeadersToRequest =
|
||||
{
|
||||
{"CustomerId", "Claims[CustomerId] > value"},
|
||||
{"LocationId", "Claims[LocationId] > value"},
|
||||
{"UserType", "Claims[sub] > value[0] > |"},
|
||||
{"UserId", "Claims[sub] > value[1] > |"}
|
||||
},
|
||||
AddClaimsToRequest =
|
||||
{
|
||||
{"CustomerId", "Claims[CustomerId] > value"},
|
||||
{"UserType", "Claims[sub] > value[0] > |"},
|
||||
{"UserId", "Claims[sub] > value[1] > |"}
|
||||
},
|
||||
RouteClaimsRequirement =
|
||||
{
|
||||
{"UserType", "registered"}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenThereIsAnIdentityServerOn("http://localhost:51888", "api", AccessTokenType.Jwt))
|
||||
.And(x => x.GivenThereIsAServiceRunningOn("http://localhost:51876", 200, "Hello from Laura"))
|
||||
.And(x => _steps.GivenIHaveAToken("http://localhost:51888"))
|
||||
.And(x => _steps.GivenThereIsAConfiguration(configuration))
|
||||
.And(x => _steps.GivenOcelotIsRunning(_options, "Test"))
|
||||
.And(x => _steps.GivenIHaveAddedATokenToMyRequest())
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
|
||||
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
|
||||
.And(x => _steps.ThenTheResponseBodyShouldBe("Hello from Laura"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_response_403_authorising_route()
|
||||
{
|
||||
var configuration = new FileConfiguration
|
||||
{
|
||||
ReRoutes = new List<FileReRoute>
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamPathTemplate = "/",
|
||||
DownstreamPort = 51876,
|
||||
DownstreamScheme = "http",
|
||||
DownstreamHost = "localhost",
|
||||
UpstreamPathTemplate = "/",
|
||||
UpstreamHttpMethod = new List<string> { "Get" },
|
||||
AuthenticationOptions = new FileAuthenticationOptions
|
||||
{
|
||||
AuthenticationProviderKey = "Test"
|
||||
},
|
||||
AddHeadersToRequest =
|
||||
{
|
||||
{"CustomerId", "Claims[CustomerId] > value"},
|
||||
{"LocationId", "Claims[LocationId] > value"},
|
||||
{"UserType", "Claims[sub] > value[0] > |"},
|
||||
{"UserId", "Claims[sub] > value[1] > |"}
|
||||
},
|
||||
AddClaimsToRequest =
|
||||
{
|
||||
{"CustomerId", "Claims[CustomerId] > value"},
|
||||
{"UserId", "Claims[sub] > value[1] > |"}
|
||||
},
|
||||
RouteClaimsRequirement =
|
||||
{
|
||||
{"UserType", "registered"}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenThereIsAnIdentityServerOn("http://localhost:51888", "api", AccessTokenType.Jwt))
|
||||
.And(x => x.GivenThereIsAServiceRunningOn("http://localhost:51876", 200, "Hello from Laura"))
|
||||
.And(x => _steps.GivenIHaveAToken("http://localhost:51888"))
|
||||
.And(x => _steps.GivenThereIsAConfiguration(configuration))
|
||||
.And(x => _steps.GivenOcelotIsRunning(_options, "Test"))
|
||||
.And(x => _steps.GivenIHaveAddedATokenToMyRequest())
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
|
||||
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.Forbidden))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_response_200_using_identity_server_with_allowed_scope()
|
||||
{
|
||||
var configuration = new FileConfiguration
|
||||
{
|
||||
ReRoutes = new List<FileReRoute>
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamPathTemplate = "/",
|
||||
DownstreamPort = 51876,
|
||||
DownstreamHost = "localhost",
|
||||
DownstreamScheme = "http",
|
||||
UpstreamPathTemplate = "/",
|
||||
UpstreamHttpMethod = new List<string> { "Get" },
|
||||
AuthenticationOptions = new FileAuthenticationOptions
|
||||
{
|
||||
AuthenticationProviderKey = "Test",
|
||||
AllowedScopes = new List<string>{ "api", "api.readOnly", "openid", "offline_access" },
|
||||
},
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenThereIsAnIdentityServerOn("http://localhost:51888", "api", AccessTokenType.Jwt))
|
||||
.And(x => x.GivenThereIsAServiceRunningOn("http://localhost:51876", 200, "Hello from Laura"))
|
||||
.And(x => _steps.GivenIHaveATokenForApiReadOnlyScope("http://localhost:51888"))
|
||||
.And(x => _steps.GivenThereIsAConfiguration(configuration))
|
||||
.And(x => _steps.GivenOcelotIsRunning(_options, "Test"))
|
||||
.And(x => _steps.GivenIHaveAddedATokenToMyRequest())
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
|
||||
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_response_403_using_identity_server_with_scope_not_allowed()
|
||||
{
|
||||
var configuration = new FileConfiguration
|
||||
{
|
||||
ReRoutes = new List<FileReRoute>
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamPathTemplate = "/",
|
||||
DownstreamPort = 51876,
|
||||
DownstreamHost = "localhost",
|
||||
DownstreamScheme = "http",
|
||||
UpstreamPathTemplate = "/",
|
||||
UpstreamHttpMethod = new List<string> { "Get" },
|
||||
AuthenticationOptions = new FileAuthenticationOptions
|
||||
{
|
||||
AuthenticationProviderKey = "Test",
|
||||
AllowedScopes = new List<string>{ "api", "openid", "offline_access" },
|
||||
},
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenThereIsAnIdentityServerOn("http://localhost:51888", "api", AccessTokenType.Jwt))
|
||||
.And(x => x.GivenThereIsAServiceRunningOn("http://localhost:51876", 200, "Hello from Laura"))
|
||||
.And(x => _steps.GivenIHaveATokenForApiReadOnlyScope("http://localhost:51888"))
|
||||
.And(x => _steps.GivenThereIsAConfiguration(configuration))
|
||||
.And(x => _steps.GivenOcelotIsRunning(_options, "Test"))
|
||||
.And(x => _steps.GivenIHaveAddedATokenToMyRequest())
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
|
||||
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.Forbidden))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenThereIsAServiceRunningOn(string url, int statusCode, string responseBody)
|
||||
{
|
||||
_servicebuilder = new WebHostBuilder()
|
||||
.UseUrls(url)
|
||||
.UseKestrel()
|
||||
.UseContentRoot(Directory.GetCurrentDirectory())
|
||||
.UseIISIntegration()
|
||||
.UseUrls(url)
|
||||
.Configure(app =>
|
||||
{
|
||||
app.Run(async context =>
|
||||
{
|
||||
context.Response.StatusCode = statusCode;
|
||||
await context.Response.WriteAsync(responseBody);
|
||||
});
|
||||
})
|
||||
.Build();
|
||||
|
||||
_servicebuilder.Start();
|
||||
}
|
||||
|
||||
private void GivenThereIsAnIdentityServerOn(string url, string apiName, AccessTokenType tokenType)
|
||||
{
|
||||
_identityServerBuilder = new WebHostBuilder()
|
||||
.UseUrls(url)
|
||||
.UseKestrel()
|
||||
.UseContentRoot(Directory.GetCurrentDirectory())
|
||||
.UseIISIntegration()
|
||||
.UseUrls(url)
|
||||
.ConfigureServices(services =>
|
||||
{
|
||||
services.AddLogging();
|
||||
services.AddIdentityServer()
|
||||
.AddDeveloperSigningCredential()
|
||||
.AddInMemoryApiResources(new List<ApiResource>
|
||||
{
|
||||
new ApiResource
|
||||
{
|
||||
Name = apiName,
|
||||
Description = "My API",
|
||||
Enabled = true,
|
||||
DisplayName = "test",
|
||||
Scopes = new List<Scope>()
|
||||
{
|
||||
new Scope("api"),
|
||||
new Scope("api.readOnly"),
|
||||
new Scope("openid"),
|
||||
new Scope("offline_access")
|
||||
},
|
||||
ApiSecrets = new List<Secret>()
|
||||
{
|
||||
new Secret
|
||||
{
|
||||
Value = "secret".Sha256()
|
||||
}
|
||||
},
|
||||
UserClaims = new List<string>()
|
||||
{
|
||||
"CustomerId", "LocationId", "UserType", "UserId"
|
||||
}
|
||||
},
|
||||
|
||||
})
|
||||
.AddInMemoryClients(new List<Client>
|
||||
{
|
||||
new Client
|
||||
{
|
||||
ClientId = "client",
|
||||
AllowedGrantTypes = GrantTypes.ResourceOwnerPassword,
|
||||
ClientSecrets = new List<Secret> {new Secret("secret".Sha256())},
|
||||
AllowedScopes = new List<string> { apiName, "api.readOnly", "openid", "offline_access" },
|
||||
AccessTokenType = tokenType,
|
||||
Enabled = true,
|
||||
RequireClientSecret = false
|
||||
}
|
||||
})
|
||||
.AddTestUsers(new List<TestUser>
|
||||
{
|
||||
new TestUser
|
||||
{
|
||||
Username = "test",
|
||||
Password = "test",
|
||||
SubjectId = "registered|1231231",
|
||||
Claims = new List<Claim>
|
||||
{
|
||||
new Claim("CustomerId", "123"),
|
||||
new Claim("LocationId", "321")
|
||||
}
|
||||
}
|
||||
});
|
||||
})
|
||||
.Configure(app =>
|
||||
{
|
||||
app.UseIdentityServer();
|
||||
})
|
||||
.Build();
|
||||
|
||||
_identityServerBuilder.Start();
|
||||
|
||||
_steps.VerifyIdentiryServerStarted(url);
|
||||
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_servicebuilder?.Dispose();
|
||||
_steps.Dispose();
|
||||
_identityServerBuilder?.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Security.Claims;
|
||||
using IdentityServer4.AccessTokenValidation;
|
||||
using IdentityServer4.Models;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Ocelot.Configuration.File;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.AcceptanceTests
|
||||
{
|
||||
using IdentityServer4;
|
||||
using IdentityServer4.Test;
|
||||
|
||||
public class AuthorisationTests : IDisposable
|
||||
{
|
||||
private IWebHost _servicebuilder;
|
||||
private IWebHost _identityServerBuilder;
|
||||
private readonly Steps _steps;
|
||||
private Action<IdentityServerAuthenticationOptions> _options;
|
||||
private string _identityServerRootUrl = "http://localhost:51888";
|
||||
|
||||
public AuthorisationTests()
|
||||
{
|
||||
_steps = new Steps();
|
||||
_options = o =>
|
||||
{
|
||||
o.Authority = _identityServerRootUrl;
|
||||
o.ApiName = "api";
|
||||
o.RequireHttpsMetadata = false;
|
||||
o.SupportedTokens = SupportedTokens.Both;
|
||||
o.ApiSecret = "secret";
|
||||
};
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_response_200_authorising_route()
|
||||
{
|
||||
var configuration = new FileConfiguration
|
||||
{
|
||||
ReRoutes = new List<FileReRoute>
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamPathTemplate = "/",
|
||||
DownstreamHostAndPorts = new List<FileHostAndPort>
|
||||
{
|
||||
new FileHostAndPort
|
||||
{
|
||||
Host = "localhost",
|
||||
Port = 51876,
|
||||
}
|
||||
},
|
||||
DownstreamScheme = "http",
|
||||
UpstreamPathTemplate = "/",
|
||||
UpstreamHttpMethod = new List<string> { "Get" },
|
||||
AuthenticationOptions = new FileAuthenticationOptions
|
||||
{
|
||||
AuthenticationProviderKey = "Test"
|
||||
},
|
||||
AddHeadersToRequest =
|
||||
{
|
||||
{"CustomerId", "Claims[CustomerId] > value"},
|
||||
{"LocationId", "Claims[LocationId] > value"},
|
||||
{"UserType", "Claims[sub] > value[0] > |"},
|
||||
{"UserId", "Claims[sub] > value[1] > |"}
|
||||
},
|
||||
AddClaimsToRequest =
|
||||
{
|
||||
{"CustomerId", "Claims[CustomerId] > value"},
|
||||
{"UserType", "Claims[sub] > value[0] > |"},
|
||||
{"UserId", "Claims[sub] > value[1] > |"}
|
||||
},
|
||||
RouteClaimsRequirement =
|
||||
{
|
||||
{"UserType", "registered"}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenThereIsAnIdentityServerOn("http://localhost:51888", "api", AccessTokenType.Jwt))
|
||||
.And(x => x.GivenThereIsAServiceRunningOn("http://localhost:51876", 200, "Hello from Laura"))
|
||||
.And(x => _steps.GivenIHaveAToken("http://localhost:51888"))
|
||||
.And(x => _steps.GivenThereIsAConfiguration(configuration))
|
||||
.And(x => _steps.GivenOcelotIsRunning(_options, "Test"))
|
||||
.And(x => _steps.GivenIHaveAddedATokenToMyRequest())
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
|
||||
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
|
||||
.And(x => _steps.ThenTheResponseBodyShouldBe("Hello from Laura"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_response_403_authorising_route()
|
||||
{
|
||||
var configuration = new FileConfiguration
|
||||
{
|
||||
ReRoutes = new List<FileReRoute>
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamPathTemplate = "/",
|
||||
DownstreamHostAndPorts = new List<FileHostAndPort>
|
||||
{
|
||||
new FileHostAndPort
|
||||
{
|
||||
Host = "localhost",
|
||||
Port = 51876,
|
||||
}
|
||||
},
|
||||
DownstreamScheme = "http",
|
||||
UpstreamPathTemplate = "/",
|
||||
UpstreamHttpMethod = new List<string> { "Get" },
|
||||
AuthenticationOptions = new FileAuthenticationOptions
|
||||
{
|
||||
AuthenticationProviderKey = "Test"
|
||||
},
|
||||
AddHeadersToRequest =
|
||||
{
|
||||
{"CustomerId", "Claims[CustomerId] > value"},
|
||||
{"LocationId", "Claims[LocationId] > value"},
|
||||
{"UserType", "Claims[sub] > value[0] > |"},
|
||||
{"UserId", "Claims[sub] > value[1] > |"}
|
||||
},
|
||||
AddClaimsToRequest =
|
||||
{
|
||||
{"CustomerId", "Claims[CustomerId] > value"},
|
||||
{"UserId", "Claims[sub] > value[1] > |"}
|
||||
},
|
||||
RouteClaimsRequirement =
|
||||
{
|
||||
{"UserType", "registered"}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenThereIsAnIdentityServerOn("http://localhost:51888", "api", AccessTokenType.Jwt))
|
||||
.And(x => x.GivenThereIsAServiceRunningOn("http://localhost:51876", 200, "Hello from Laura"))
|
||||
.And(x => _steps.GivenIHaveAToken("http://localhost:51888"))
|
||||
.And(x => _steps.GivenThereIsAConfiguration(configuration))
|
||||
.And(x => _steps.GivenOcelotIsRunning(_options, "Test"))
|
||||
.And(x => _steps.GivenIHaveAddedATokenToMyRequest())
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
|
||||
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.Forbidden))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_response_200_using_identity_server_with_allowed_scope()
|
||||
{
|
||||
var configuration = new FileConfiguration
|
||||
{
|
||||
ReRoutes = new List<FileReRoute>
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamPathTemplate = "/",
|
||||
DownstreamHostAndPorts = new List<FileHostAndPort>
|
||||
{
|
||||
new FileHostAndPort
|
||||
{
|
||||
Host = "localhost",
|
||||
Port = 51876,
|
||||
}
|
||||
},
|
||||
DownstreamScheme = "http",
|
||||
UpstreamPathTemplate = "/",
|
||||
UpstreamHttpMethod = new List<string> { "Get" },
|
||||
AuthenticationOptions = new FileAuthenticationOptions
|
||||
{
|
||||
AuthenticationProviderKey = "Test",
|
||||
AllowedScopes = new List<string>{ "api", "api.readOnly", "openid", "offline_access" },
|
||||
},
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenThereIsAnIdentityServerOn("http://localhost:51888", "api", AccessTokenType.Jwt))
|
||||
.And(x => x.GivenThereIsAServiceRunningOn("http://localhost:51876", 200, "Hello from Laura"))
|
||||
.And(x => _steps.GivenIHaveATokenForApiReadOnlyScope("http://localhost:51888"))
|
||||
.And(x => _steps.GivenThereIsAConfiguration(configuration))
|
||||
.And(x => _steps.GivenOcelotIsRunning(_options, "Test"))
|
||||
.And(x => _steps.GivenIHaveAddedATokenToMyRequest())
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
|
||||
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_response_403_using_identity_server_with_scope_not_allowed()
|
||||
{
|
||||
var configuration = new FileConfiguration
|
||||
{
|
||||
ReRoutes = new List<FileReRoute>
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamPathTemplate = "/",
|
||||
DownstreamHostAndPorts = new List<FileHostAndPort>
|
||||
{
|
||||
new FileHostAndPort
|
||||
{
|
||||
Host = "localhost",
|
||||
Port = 51876,
|
||||
}
|
||||
},
|
||||
DownstreamScheme = "http",
|
||||
UpstreamPathTemplate = "/",
|
||||
UpstreamHttpMethod = new List<string> { "Get" },
|
||||
AuthenticationOptions = new FileAuthenticationOptions
|
||||
{
|
||||
AuthenticationProviderKey = "Test",
|
||||
AllowedScopes = new List<string>{ "api", "openid", "offline_access" },
|
||||
},
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenThereIsAnIdentityServerOn("http://localhost:51888", "api", AccessTokenType.Jwt))
|
||||
.And(x => x.GivenThereIsAServiceRunningOn("http://localhost:51876", 200, "Hello from Laura"))
|
||||
.And(x => _steps.GivenIHaveATokenForApiReadOnlyScope("http://localhost:51888"))
|
||||
.And(x => _steps.GivenThereIsAConfiguration(configuration))
|
||||
.And(x => _steps.GivenOcelotIsRunning(_options, "Test"))
|
||||
.And(x => _steps.GivenIHaveAddedATokenToMyRequest())
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
|
||||
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.Forbidden))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenThereIsAServiceRunningOn(string url, int statusCode, string responseBody)
|
||||
{
|
||||
_servicebuilder = new WebHostBuilder()
|
||||
.UseUrls(url)
|
||||
.UseKestrel()
|
||||
.UseContentRoot(Directory.GetCurrentDirectory())
|
||||
.UseIISIntegration()
|
||||
.UseUrls(url)
|
||||
.Configure(app =>
|
||||
{
|
||||
app.Run(async context =>
|
||||
{
|
||||
context.Response.StatusCode = statusCode;
|
||||
await context.Response.WriteAsync(responseBody);
|
||||
});
|
||||
})
|
||||
.Build();
|
||||
|
||||
_servicebuilder.Start();
|
||||
}
|
||||
|
||||
private void GivenThereIsAnIdentityServerOn(string url, string apiName, AccessTokenType tokenType)
|
||||
{
|
||||
_identityServerBuilder = new WebHostBuilder()
|
||||
.UseUrls(url)
|
||||
.UseKestrel()
|
||||
.UseContentRoot(Directory.GetCurrentDirectory())
|
||||
.UseIISIntegration()
|
||||
.UseUrls(url)
|
||||
.ConfigureServices(services =>
|
||||
{
|
||||
services.AddLogging();
|
||||
services.AddIdentityServer()
|
||||
.AddDeveloperSigningCredential()
|
||||
.AddInMemoryApiResources(new List<ApiResource>
|
||||
{
|
||||
new ApiResource
|
||||
{
|
||||
Name = apiName,
|
||||
Description = "My API",
|
||||
Enabled = true,
|
||||
DisplayName = "test",
|
||||
Scopes = new List<Scope>()
|
||||
{
|
||||
new Scope("api"),
|
||||
new Scope("api.readOnly"),
|
||||
new Scope("openid"),
|
||||
new Scope("offline_access")
|
||||
},
|
||||
ApiSecrets = new List<Secret>()
|
||||
{
|
||||
new Secret
|
||||
{
|
||||
Value = "secret".Sha256()
|
||||
}
|
||||
},
|
||||
UserClaims = new List<string>()
|
||||
{
|
||||
"CustomerId", "LocationId", "UserType", "UserId"
|
||||
}
|
||||
},
|
||||
|
||||
})
|
||||
.AddInMemoryClients(new List<Client>
|
||||
{
|
||||
new Client
|
||||
{
|
||||
ClientId = "client",
|
||||
AllowedGrantTypes = GrantTypes.ResourceOwnerPassword,
|
||||
ClientSecrets = new List<Secret> {new Secret("secret".Sha256())},
|
||||
AllowedScopes = new List<string> { apiName, "api.readOnly", "openid", "offline_access" },
|
||||
AccessTokenType = tokenType,
|
||||
Enabled = true,
|
||||
RequireClientSecret = false
|
||||
}
|
||||
})
|
||||
.AddTestUsers(new List<TestUser>
|
||||
{
|
||||
new TestUser
|
||||
{
|
||||
Username = "test",
|
||||
Password = "test",
|
||||
SubjectId = "registered|1231231",
|
||||
Claims = new List<Claim>
|
||||
{
|
||||
new Claim("CustomerId", "123"),
|
||||
new Claim("LocationId", "321")
|
||||
}
|
||||
}
|
||||
});
|
||||
})
|
||||
.Configure(app =>
|
||||
{
|
||||
app.UseIdentityServer();
|
||||
})
|
||||
.Build();
|
||||
|
||||
_identityServerBuilder.Start();
|
||||
|
||||
_steps.VerifyIdentiryServerStarted(url);
|
||||
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_servicebuilder?.Dispose();
|
||||
_steps.Dispose();
|
||||
_identityServerBuilder?.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,16 +1,16 @@
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace Ocelot.AcceptanceTests
|
||||
{
|
||||
class BearerToken
|
||||
{
|
||||
[JsonProperty("access_token")]
|
||||
public string AccessToken { get; set; }
|
||||
|
||||
[JsonProperty("expires_in")]
|
||||
public int ExpiresIn { get; set; }
|
||||
|
||||
[JsonProperty("token_type")]
|
||||
public string TokenType { get; set; }
|
||||
}
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace Ocelot.AcceptanceTests
|
||||
{
|
||||
class BearerToken
|
||||
{
|
||||
[JsonProperty("access_token")]
|
||||
public string AccessToken { get; set; }
|
||||
|
||||
[JsonProperty("expires_in")]
|
||||
public int ExpiresIn { get; set; }
|
||||
|
||||
[JsonProperty("token_type")]
|
||||
public string TokenType { get; set; }
|
||||
}
|
||||
}
|
@ -1,137 +1,137 @@
|
||||
using CacheManager.Core;
|
||||
using CacheManager.Core.Internal;
|
||||
using CacheManager.Core.Logging;
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Linq;
|
||||
using static CacheManager.Core.Utility.Guard;
|
||||
|
||||
namespace Ocelot.AcceptanceTests.Caching
|
||||
{
|
||||
public class InMemoryJsonHandle<TCacheValue> : BaseCacheHandle<TCacheValue>
|
||||
{
|
||||
private readonly ICacheSerializer _serializer;
|
||||
private readonly ConcurrentDictionary<string, Tuple<Type, byte[]>> _cache;
|
||||
|
||||
public InMemoryJsonHandle(
|
||||
ICacheManagerConfiguration managerConfiguration,
|
||||
CacheHandleConfiguration configuration,
|
||||
ICacheSerializer serializer,
|
||||
ILoggerFactory loggerFactory) : base(managerConfiguration, configuration)
|
||||
{
|
||||
_cache = new ConcurrentDictionary<string, Tuple<Type, byte[]>>();
|
||||
_serializer = serializer;
|
||||
Logger = loggerFactory.CreateLogger(this);
|
||||
}
|
||||
|
||||
public override int Count => _cache.Count;
|
||||
|
||||
protected override ILogger Logger { get; }
|
||||
|
||||
public override void Clear() => _cache.Clear();
|
||||
|
||||
public override void ClearRegion(string region)
|
||||
{
|
||||
NotNullOrWhiteSpace(region, nameof(region));
|
||||
|
||||
var key = string.Concat(region, ":");
|
||||
foreach (var item in _cache.Where(p => p.Key.StartsWith(key, StringComparison.OrdinalIgnoreCase)))
|
||||
{
|
||||
_cache.TryRemove(item.Key, out Tuple<Type, byte[]> val);
|
||||
}
|
||||
}
|
||||
|
||||
public override bool Exists(string key)
|
||||
{
|
||||
NotNullOrWhiteSpace(key, nameof(key));
|
||||
|
||||
return _cache.ContainsKey(key);
|
||||
}
|
||||
|
||||
public override bool Exists(string key, string region)
|
||||
{
|
||||
NotNullOrWhiteSpace(region, nameof(region));
|
||||
var fullKey = GetKey(key, region);
|
||||
return _cache.ContainsKey(fullKey);
|
||||
}
|
||||
|
||||
protected override bool AddInternalPrepared(CacheItem<TCacheValue> item)
|
||||
{
|
||||
NotNull(item, nameof(item));
|
||||
|
||||
var key = GetKey(item.Key, item.Region);
|
||||
|
||||
var serializedItem = _serializer.SerializeCacheItem(item);
|
||||
|
||||
return _cache.TryAdd(key, new Tuple<Type, byte[]>(item.Value.GetType(), serializedItem));
|
||||
}
|
||||
|
||||
protected override CacheItem<TCacheValue> GetCacheItemInternal(string key) => GetCacheItemInternal(key, null);
|
||||
|
||||
protected override CacheItem<TCacheValue> GetCacheItemInternal(string key, string region)
|
||||
{
|
||||
var fullKey = GetKey(key, region);
|
||||
|
||||
CacheItem<TCacheValue> deserializedResult = null;
|
||||
|
||||
if (_cache.TryGetValue(fullKey, out Tuple<Type, byte[]> result))
|
||||
{
|
||||
deserializedResult = _serializer.DeserializeCacheItem<TCacheValue>(result.Item2, result.Item1);
|
||||
|
||||
if (deserializedResult.ExpirationMode != ExpirationMode.None && IsExpired(deserializedResult, DateTime.UtcNow))
|
||||
{
|
||||
_cache.TryRemove(fullKey, out Tuple<Type, byte[]> removeResult);
|
||||
TriggerCacheSpecificRemove(key, region, CacheItemRemovedReason.Expired, deserializedResult.Value);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
return deserializedResult;
|
||||
}
|
||||
|
||||
protected override void PutInternalPrepared(CacheItem<TCacheValue> item)
|
||||
{
|
||||
NotNull(item, nameof(item));
|
||||
|
||||
var serializedItem = _serializer.SerializeCacheItem<TCacheValue>(item);
|
||||
|
||||
_cache[GetKey(item.Key, item.Region)] = new Tuple<Type, byte[]>(item.Value.GetType(), serializedItem);
|
||||
}
|
||||
|
||||
protected override bool RemoveInternal(string key) => RemoveInternal(key, null);
|
||||
|
||||
protected override bool RemoveInternal(string key, string region)
|
||||
{
|
||||
var fullKey = GetKey(key, region);
|
||||
return _cache.TryRemove(fullKey, out Tuple<Type, byte[]> val);
|
||||
}
|
||||
|
||||
private static string GetKey(string key, string region)
|
||||
{
|
||||
NotNullOrWhiteSpace(key, nameof(key));
|
||||
|
||||
if (string.IsNullOrWhiteSpace(region))
|
||||
{
|
||||
return key;
|
||||
}
|
||||
|
||||
return string.Concat(region, ":", key);
|
||||
}
|
||||
|
||||
private static bool IsExpired(CacheItem<TCacheValue> item, DateTime now)
|
||||
{
|
||||
if (item.ExpirationMode == ExpirationMode.Absolute
|
||||
&& item.CreatedUtc.Add(item.ExpirationTimeout) < now)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else if (item.ExpirationMode == ExpirationMode.Sliding
|
||||
&& item.LastAccessedUtc.Add(item.ExpirationTimeout) < now)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
using CacheManager.Core;
|
||||
using CacheManager.Core.Internal;
|
||||
using CacheManager.Core.Logging;
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Linq;
|
||||
using static CacheManager.Core.Utility.Guard;
|
||||
|
||||
namespace Ocelot.AcceptanceTests.Caching
|
||||
{
|
||||
public class InMemoryJsonHandle<TCacheValue> : BaseCacheHandle<TCacheValue>
|
||||
{
|
||||
private readonly ICacheSerializer _serializer;
|
||||
private readonly ConcurrentDictionary<string, Tuple<Type, byte[]>> _cache;
|
||||
|
||||
public InMemoryJsonHandle(
|
||||
ICacheManagerConfiguration managerConfiguration,
|
||||
CacheHandleConfiguration configuration,
|
||||
ICacheSerializer serializer,
|
||||
ILoggerFactory loggerFactory) : base(managerConfiguration, configuration)
|
||||
{
|
||||
_cache = new ConcurrentDictionary<string, Tuple<Type, byte[]>>();
|
||||
_serializer = serializer;
|
||||
Logger = loggerFactory.CreateLogger(this);
|
||||
}
|
||||
|
||||
public override int Count => _cache.Count;
|
||||
|
||||
protected override ILogger Logger { get; }
|
||||
|
||||
public override void Clear() => _cache.Clear();
|
||||
|
||||
public override void ClearRegion(string region)
|
||||
{
|
||||
NotNullOrWhiteSpace(region, nameof(region));
|
||||
|
||||
var key = string.Concat(region, ":");
|
||||
foreach (var item in _cache.Where(p => p.Key.StartsWith(key, StringComparison.OrdinalIgnoreCase)))
|
||||
{
|
||||
_cache.TryRemove(item.Key, out Tuple<Type, byte[]> val);
|
||||
}
|
||||
}
|
||||
|
||||
public override bool Exists(string key)
|
||||
{
|
||||
NotNullOrWhiteSpace(key, nameof(key));
|
||||
|
||||
return _cache.ContainsKey(key);
|
||||
}
|
||||
|
||||
public override bool Exists(string key, string region)
|
||||
{
|
||||
NotNullOrWhiteSpace(region, nameof(region));
|
||||
var fullKey = GetKey(key, region);
|
||||
return _cache.ContainsKey(fullKey);
|
||||
}
|
||||
|
||||
protected override bool AddInternalPrepared(CacheItem<TCacheValue> item)
|
||||
{
|
||||
NotNull(item, nameof(item));
|
||||
|
||||
var key = GetKey(item.Key, item.Region);
|
||||
|
||||
var serializedItem = _serializer.SerializeCacheItem(item);
|
||||
|
||||
return _cache.TryAdd(key, new Tuple<Type, byte[]>(item.Value.GetType(), serializedItem));
|
||||
}
|
||||
|
||||
protected override CacheItem<TCacheValue> GetCacheItemInternal(string key) => GetCacheItemInternal(key, null);
|
||||
|
||||
protected override CacheItem<TCacheValue> GetCacheItemInternal(string key, string region)
|
||||
{
|
||||
var fullKey = GetKey(key, region);
|
||||
|
||||
CacheItem<TCacheValue> deserializedResult = null;
|
||||
|
||||
if (_cache.TryGetValue(fullKey, out Tuple<Type, byte[]> result))
|
||||
{
|
||||
deserializedResult = _serializer.DeserializeCacheItem<TCacheValue>(result.Item2, result.Item1);
|
||||
|
||||
if (deserializedResult.ExpirationMode != ExpirationMode.None && IsExpired(deserializedResult, DateTime.UtcNow))
|
||||
{
|
||||
_cache.TryRemove(fullKey, out Tuple<Type, byte[]> removeResult);
|
||||
TriggerCacheSpecificRemove(key, region, CacheItemRemovedReason.Expired, deserializedResult.Value);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
return deserializedResult;
|
||||
}
|
||||
|
||||
protected override void PutInternalPrepared(CacheItem<TCacheValue> item)
|
||||
{
|
||||
NotNull(item, nameof(item));
|
||||
|
||||
var serializedItem = _serializer.SerializeCacheItem<TCacheValue>(item);
|
||||
|
||||
_cache[GetKey(item.Key, item.Region)] = new Tuple<Type, byte[]>(item.Value.GetType(), serializedItem);
|
||||
}
|
||||
|
||||
protected override bool RemoveInternal(string key) => RemoveInternal(key, null);
|
||||
|
||||
protected override bool RemoveInternal(string key, string region)
|
||||
{
|
||||
var fullKey = GetKey(key, region);
|
||||
return _cache.TryRemove(fullKey, out Tuple<Type, byte[]> val);
|
||||
}
|
||||
|
||||
private static string GetKey(string key, string region)
|
||||
{
|
||||
NotNullOrWhiteSpace(key, nameof(key));
|
||||
|
||||
if (string.IsNullOrWhiteSpace(region))
|
||||
{
|
||||
return key;
|
||||
}
|
||||
|
||||
return string.Concat(region, ":", key);
|
||||
}
|
||||
|
||||
private static bool IsExpired(CacheItem<TCacheValue> item, DateTime now)
|
||||
{
|
||||
if (item.ExpirationMode == ExpirationMode.Absolute
|
||||
&& item.CreatedUtc.Add(item.ExpirationTimeout) < now)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else if (item.ExpirationMode == ExpirationMode.Sliding
|
||||
&& item.LastAccessedUtc.Add(item.ExpirationTimeout) < now)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,172 +1,190 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Threading;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Ocelot.Configuration.File;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.AcceptanceTests
|
||||
{
|
||||
public class CachingTests : IDisposable
|
||||
{
|
||||
private IWebHost _builder;
|
||||
private readonly Steps _steps;
|
||||
|
||||
public CachingTests()
|
||||
{
|
||||
_steps = new Steps();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_cached_response()
|
||||
{
|
||||
var configuration = new FileConfiguration
|
||||
{
|
||||
ReRoutes = new List<FileReRoute>
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamPathTemplate = "/",
|
||||
DownstreamPort = 51879,
|
||||
DownstreamScheme = "http",
|
||||
DownstreamHost = "localhost",
|
||||
UpstreamPathTemplate = "/",
|
||||
UpstreamHttpMethod = new List<string> { "Get" },
|
||||
FileCacheOptions = new FileCacheOptions
|
||||
{
|
||||
TtlSeconds = 100
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:51879", 200, "Hello from Laura"))
|
||||
.And(x => _steps.GivenThereIsAConfiguration(configuration))
|
||||
.And(x => _steps.GivenOcelotIsRunning())
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
|
||||
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
|
||||
.And(x => _steps.ThenTheResponseBodyShouldBe("Hello from Laura"))
|
||||
.Given(x => x.GivenTheServiceNowReturns("http://localhost:51879", 200, "Hello from Tom"))
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
|
||||
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
|
||||
.And(x => _steps.ThenTheResponseBodyShouldBe("Hello from Laura"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_cached_response_when_using_jsonserialized_cache()
|
||||
{
|
||||
var configuration = new FileConfiguration
|
||||
{
|
||||
ReRoutes = new List<FileReRoute>
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamPathTemplate = "/",
|
||||
DownstreamPort = 51879,
|
||||
DownstreamScheme = "http",
|
||||
DownstreamHost = "localhost",
|
||||
UpstreamPathTemplate = "/",
|
||||
UpstreamHttpMethod = new List<string> { "Get" },
|
||||
FileCacheOptions = new FileCacheOptions
|
||||
{
|
||||
TtlSeconds = 100
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:51879", 200, "Hello from Laura"))
|
||||
.And(x => _steps.GivenThereIsAConfiguration(configuration))
|
||||
.And(x => _steps.GivenOcelotIsRunningUsingJsonSerializedCache())
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
|
||||
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
|
||||
.And(x => _steps.ThenTheResponseBodyShouldBe("Hello from Laura"))
|
||||
.Given(x => x.GivenTheServiceNowReturns("http://localhost:51879", 200, "Hello from Tom"))
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
|
||||
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
|
||||
.And(x => _steps.ThenTheResponseBodyShouldBe("Hello from Laura"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_not_return_cached_response_as_ttl_expires()
|
||||
{
|
||||
var configuration = new FileConfiguration
|
||||
{
|
||||
ReRoutes = new List<FileReRoute>
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamPathTemplate = "/",
|
||||
DownstreamPort = 51879,
|
||||
DownstreamScheme = "http",
|
||||
DownstreamHost = "localhost",
|
||||
UpstreamPathTemplate = "/",
|
||||
UpstreamHttpMethod = new List<string> { "Get" },
|
||||
FileCacheOptions = new FileCacheOptions
|
||||
{
|
||||
TtlSeconds = 1
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:51879", 200, "Hello from Laura"))
|
||||
.And(x => _steps.GivenThereIsAConfiguration(configuration))
|
||||
.And(x => _steps.GivenOcelotIsRunning())
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
|
||||
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
|
||||
.And(x => _steps.ThenTheResponseBodyShouldBe("Hello from Laura"))
|
||||
.Given(x => x.GivenTheServiceNowReturns("http://localhost:51879", 200, "Hello from Tom"))
|
||||
.And(x => x.GivenTheCacheExpires())
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
|
||||
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
|
||||
.And(x => _steps.ThenTheResponseBodyShouldBe("Hello from Tom"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenTheCacheExpires()
|
||||
{
|
||||
Thread.Sleep(1000);
|
||||
}
|
||||
|
||||
private void GivenTheServiceNowReturns(string url, int statusCode, string responseBody)
|
||||
{
|
||||
_builder.Dispose();
|
||||
GivenThereIsAServiceRunningOn(url, statusCode, responseBody);
|
||||
}
|
||||
|
||||
private void GivenThereIsAServiceRunningOn(string url, int statusCode, string responseBody)
|
||||
{
|
||||
_builder = new WebHostBuilder()
|
||||
.UseUrls(url)
|
||||
.UseKestrel()
|
||||
.UseContentRoot(Directory.GetCurrentDirectory())
|
||||
.UseIISIntegration()
|
||||
.UseUrls(url)
|
||||
.Configure(app =>
|
||||
{
|
||||
app.Run(async context =>
|
||||
{
|
||||
context.Response.StatusCode = statusCode;
|
||||
await context.Response.WriteAsync(responseBody);
|
||||
});
|
||||
})
|
||||
.Build();
|
||||
|
||||
_builder.Start();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_builder?.Dispose();
|
||||
_steps.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Threading;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Ocelot.Configuration.File;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.AcceptanceTests
|
||||
{
|
||||
public class CachingTests : IDisposable
|
||||
{
|
||||
private IWebHost _builder;
|
||||
private readonly Steps _steps;
|
||||
|
||||
public CachingTests()
|
||||
{
|
||||
_steps = new Steps();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_cached_response()
|
||||
{
|
||||
var configuration = new FileConfiguration
|
||||
{
|
||||
ReRoutes = new List<FileReRoute>
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamPathTemplate = "/",
|
||||
DownstreamHostAndPorts = new List<FileHostAndPort>
|
||||
{
|
||||
new FileHostAndPort
|
||||
{
|
||||
Host = "localhost",
|
||||
Port = 51879,
|
||||
}
|
||||
},
|
||||
DownstreamScheme = "http",
|
||||
UpstreamPathTemplate = "/",
|
||||
UpstreamHttpMethod = new List<string> { "Get" },
|
||||
FileCacheOptions = new FileCacheOptions
|
||||
{
|
||||
TtlSeconds = 100
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:51879", 200, "Hello from Laura"))
|
||||
.And(x => _steps.GivenThereIsAConfiguration(configuration))
|
||||
.And(x => _steps.GivenOcelotIsRunning())
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
|
||||
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
|
||||
.And(x => _steps.ThenTheResponseBodyShouldBe("Hello from Laura"))
|
||||
.Given(x => x.GivenTheServiceNowReturns("http://localhost:51879", 200, "Hello from Tom"))
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
|
||||
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
|
||||
.And(x => _steps.ThenTheResponseBodyShouldBe("Hello from Laura"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_cached_response_when_using_jsonserialized_cache()
|
||||
{
|
||||
var configuration = new FileConfiguration
|
||||
{
|
||||
ReRoutes = new List<FileReRoute>
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamPathTemplate = "/",
|
||||
DownstreamHostAndPorts = new List<FileHostAndPort>
|
||||
{
|
||||
new FileHostAndPort
|
||||
{
|
||||
Host = "localhost",
|
||||
Port = 51879,
|
||||
}
|
||||
},
|
||||
DownstreamScheme = "http",
|
||||
UpstreamPathTemplate = "/",
|
||||
UpstreamHttpMethod = new List<string> { "Get" },
|
||||
FileCacheOptions = new FileCacheOptions
|
||||
{
|
||||
TtlSeconds = 100
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:51879", 200, "Hello from Laura"))
|
||||
.And(x => _steps.GivenThereIsAConfiguration(configuration))
|
||||
.And(x => _steps.GivenOcelotIsRunningUsingJsonSerializedCache())
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
|
||||
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
|
||||
.And(x => _steps.ThenTheResponseBodyShouldBe("Hello from Laura"))
|
||||
.Given(x => x.GivenTheServiceNowReturns("http://localhost:51879", 200, "Hello from Tom"))
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
|
||||
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
|
||||
.And(x => _steps.ThenTheResponseBodyShouldBe("Hello from Laura"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_not_return_cached_response_as_ttl_expires()
|
||||
{
|
||||
var configuration = new FileConfiguration
|
||||
{
|
||||
ReRoutes = new List<FileReRoute>
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamPathTemplate = "/",
|
||||
DownstreamHostAndPorts = new List<FileHostAndPort>
|
||||
{
|
||||
new FileHostAndPort
|
||||
{
|
||||
Host = "localhost",
|
||||
Port = 51879,
|
||||
}
|
||||
},
|
||||
DownstreamScheme = "http",
|
||||
UpstreamPathTemplate = "/",
|
||||
UpstreamHttpMethod = new List<string> { "Get" },
|
||||
FileCacheOptions = new FileCacheOptions
|
||||
{
|
||||
TtlSeconds = 1
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:51879", 200, "Hello from Laura"))
|
||||
.And(x => _steps.GivenThereIsAConfiguration(configuration))
|
||||
.And(x => _steps.GivenOcelotIsRunning())
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
|
||||
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
|
||||
.And(x => _steps.ThenTheResponseBodyShouldBe("Hello from Laura"))
|
||||
.Given(x => x.GivenTheServiceNowReturns("http://localhost:51879", 200, "Hello from Tom"))
|
||||
.And(x => x.GivenTheCacheExpires())
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
|
||||
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
|
||||
.And(x => _steps.ThenTheResponseBodyShouldBe("Hello from Tom"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenTheCacheExpires()
|
||||
{
|
||||
Thread.Sleep(1000);
|
||||
}
|
||||
|
||||
private void GivenTheServiceNowReturns(string url, int statusCode, string responseBody)
|
||||
{
|
||||
_builder.Dispose();
|
||||
GivenThereIsAServiceRunningOn(url, statusCode, responseBody);
|
||||
}
|
||||
|
||||
private void GivenThereIsAServiceRunningOn(string url, int statusCode, string responseBody)
|
||||
{
|
||||
_builder = new WebHostBuilder()
|
||||
.UseUrls(url)
|
||||
.UseKestrel()
|
||||
.UseContentRoot(Directory.GetCurrentDirectory())
|
||||
.UseIISIntegration()
|
||||
.UseUrls(url)
|
||||
.Configure(app =>
|
||||
{
|
||||
app.Run(async context =>
|
||||
{
|
||||
context.Response.StatusCode = statusCode;
|
||||
await context.Response.WriteAsync(responseBody);
|
||||
});
|
||||
})
|
||||
.Build();
|
||||
|
||||
_builder.Start();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_builder?.Dispose();
|
||||
_steps.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,58 +1,58 @@
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Ocelot.Configuration.File;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.AcceptanceTests
|
||||
{
|
||||
public class CannotStartOcelotTests : IDisposable
|
||||
{
|
||||
private IWebHost _builder;
|
||||
private readonly Steps _steps;
|
||||
private string _downstreamPath;
|
||||
|
||||
public CannotStartOcelotTests()
|
||||
{
|
||||
_steps = new Steps();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_throw_exception_if_cannot_start()
|
||||
{
|
||||
var invalidConfig = new FileConfiguration()
|
||||
{
|
||||
ReRoutes = new List<FileReRoute>
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
UpstreamPathTemplate = "api",
|
||||
DownstreamPathTemplate = "test"
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Exception exception = null;
|
||||
_steps.GivenThereIsAConfiguration(invalidConfig);
|
||||
try
|
||||
{
|
||||
_steps.GivenOcelotIsRunning();
|
||||
}
|
||||
catch(Exception ex)
|
||||
{
|
||||
exception = ex;
|
||||
}
|
||||
|
||||
exception.ShouldNotBeNull();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_builder?.Dispose();
|
||||
_steps.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Ocelot.Configuration.File;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.AcceptanceTests
|
||||
{
|
||||
public class CannotStartOcelotTests : IDisposable
|
||||
{
|
||||
private IWebHost _builder;
|
||||
private readonly Steps _steps;
|
||||
private string _downstreamPath;
|
||||
|
||||
public CannotStartOcelotTests()
|
||||
{
|
||||
_steps = new Steps();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_throw_exception_if_cannot_start()
|
||||
{
|
||||
var invalidConfig = new FileConfiguration()
|
||||
{
|
||||
ReRoutes = new List<FileReRoute>
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
UpstreamPathTemplate = "api",
|
||||
DownstreamPathTemplate = "test"
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Exception exception = null;
|
||||
_steps.GivenThereIsAConfiguration(invalidConfig);
|
||||
try
|
||||
{
|
||||
_steps.GivenOcelotIsRunning();
|
||||
}
|
||||
catch(Exception ex)
|
||||
{
|
||||
exception = ex;
|
||||
}
|
||||
|
||||
exception.ShouldNotBeNull();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_builder?.Dispose();
|
||||
_steps.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
@ -1,219 +1,255 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Ocelot.Configuration.File;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.AcceptanceTests
|
||||
{
|
||||
public class CaseSensitiveRoutingTests : IDisposable
|
||||
{
|
||||
private IWebHost _builder;
|
||||
private readonly Steps _steps;
|
||||
|
||||
public CaseSensitiveRoutingTests()
|
||||
{
|
||||
_steps = new Steps();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_response_200_when_global_ignore_case_sensitivity_set()
|
||||
{
|
||||
var configuration = new FileConfiguration
|
||||
{
|
||||
ReRoutes = new List<FileReRoute>
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamPathTemplate = "/api/products/{productId}",
|
||||
DownstreamPort = 51879,
|
||||
DownstreamScheme = "http",
|
||||
DownstreamHost = "localhost",
|
||||
UpstreamPathTemplate = "/products/{productId}",
|
||||
UpstreamHttpMethod = new List<string> { "Get" },
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:51879", "/api/products/1", 200, "Some Product"))
|
||||
.And(x => _steps.GivenThereIsAConfiguration(configuration))
|
||||
.And(x => _steps.GivenOcelotIsRunning())
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/PRODUCTS/1"))
|
||||
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_response_200_when_reroute_ignore_case_sensitivity_set()
|
||||
{
|
||||
var configuration = new FileConfiguration
|
||||
{
|
||||
ReRoutes = new List<FileReRoute>
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamPathTemplate = "/api/products/{productId}",
|
||||
DownstreamPort = 51879,
|
||||
DownstreamScheme = "http",
|
||||
DownstreamHost = "localhost",
|
||||
UpstreamPathTemplate = "/products/{productId}",
|
||||
UpstreamHttpMethod = new List<string> { "Get" },
|
||||
ReRouteIsCaseSensitive = false,
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:51879", "/api/products/1", 200, "Some Product"))
|
||||
.And(x => _steps.GivenThereIsAConfiguration(configuration))
|
||||
.And(x => _steps.GivenOcelotIsRunning())
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/PRODUCTS/1"))
|
||||
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_response_404_when_reroute_respect_case_sensitivity_set()
|
||||
{
|
||||
var configuration = new FileConfiguration
|
||||
{
|
||||
ReRoutes = new List<FileReRoute>
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamPathTemplate = "/api/products/{productId}",
|
||||
DownstreamPort = 51879,
|
||||
DownstreamScheme = "http",
|
||||
DownstreamHost = "localhost",
|
||||
UpstreamPathTemplate = "/products/{productId}",
|
||||
UpstreamHttpMethod = new List<string> { "Get" },
|
||||
ReRouteIsCaseSensitive = true,
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:51879", "/api/products/1", 200, "Some Product"))
|
||||
.And(x => _steps.GivenThereIsAConfiguration(configuration))
|
||||
.And(x => _steps.GivenOcelotIsRunning())
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/PRODUCTS/1"))
|
||||
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.NotFound))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_response_200_when_reroute_respect_case_sensitivity_set()
|
||||
{
|
||||
var configuration = new FileConfiguration
|
||||
{
|
||||
ReRoutes = new List<FileReRoute>
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamPathTemplate = "/api/products/{productId}",
|
||||
DownstreamPort = 51879,
|
||||
DownstreamScheme = "http",
|
||||
DownstreamHost = "localhost",
|
||||
UpstreamPathTemplate = "/PRODUCTS/{productId}",
|
||||
UpstreamHttpMethod = new List<string> { "Get" },
|
||||
ReRouteIsCaseSensitive = true,
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:51879", "/api/products/1", 200, "Some Product"))
|
||||
.And(x => _steps.GivenThereIsAConfiguration(configuration))
|
||||
.And(x => _steps.GivenOcelotIsRunning())
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/PRODUCTS/1"))
|
||||
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_response_404_when_global_respect_case_sensitivity_set()
|
||||
{
|
||||
var configuration = new FileConfiguration
|
||||
{
|
||||
ReRoutes = new List<FileReRoute>
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamPathTemplate = "/api/products/{productId}",
|
||||
DownstreamPort = 51879,
|
||||
DownstreamScheme = "http",
|
||||
DownstreamHost = "localhost",
|
||||
UpstreamPathTemplate = "/products/{productId}",
|
||||
UpstreamHttpMethod = new List<string> { "Get" },
|
||||
ReRouteIsCaseSensitive = true,
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:51879", "/api/products/1", 200, "Some Product"))
|
||||
.And(x => _steps.GivenThereIsAConfiguration(configuration))
|
||||
.And(x => _steps.GivenOcelotIsRunning())
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/PRODUCTS/1"))
|
||||
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.NotFound))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_response_200_when_global_respect_case_sensitivity_set()
|
||||
{
|
||||
var configuration = new FileConfiguration
|
||||
{
|
||||
ReRoutes = new List<FileReRoute>
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamPathTemplate = "/api/products/{productId}",
|
||||
DownstreamPort = 51879,
|
||||
DownstreamScheme = "http",
|
||||
DownstreamHost = "localhost",
|
||||
UpstreamPathTemplate = "/PRODUCTS/{productId}",
|
||||
UpstreamHttpMethod = new List<string> { "Get" },
|
||||
ReRouteIsCaseSensitive = true,
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:51879", "/api/products/1", 200, "Some Product"))
|
||||
.And(x => _steps.GivenThereIsAConfiguration(configuration))
|
||||
.And(x => _steps.GivenOcelotIsRunning())
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/PRODUCTS/1"))
|
||||
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenThereIsAServiceRunningOn(string baseUrl, string basePath, int statusCode, string responseBody)
|
||||
{
|
||||
_builder = new WebHostBuilder()
|
||||
.UseUrls(baseUrl)
|
||||
.UseKestrel()
|
||||
.UseContentRoot(Directory.GetCurrentDirectory())
|
||||
.UseIISIntegration()
|
||||
.UseUrls(baseUrl)
|
||||
.Configure(app =>
|
||||
{
|
||||
app.UsePathBase(basePath);
|
||||
app.Run(async context =>
|
||||
{
|
||||
context.Response.StatusCode = statusCode;
|
||||
await context.Response.WriteAsync(responseBody);
|
||||
});
|
||||
})
|
||||
.Build();
|
||||
|
||||
_builder.Start();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_builder?.Dispose();
|
||||
_steps.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Ocelot.Configuration.File;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.AcceptanceTests
|
||||
{
|
||||
public class CaseSensitiveRoutingTests : IDisposable
|
||||
{
|
||||
private IWebHost _builder;
|
||||
private readonly Steps _steps;
|
||||
|
||||
public CaseSensitiveRoutingTests()
|
||||
{
|
||||
_steps = new Steps();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_response_200_when_global_ignore_case_sensitivity_set()
|
||||
{
|
||||
var configuration = new FileConfiguration
|
||||
{
|
||||
ReRoutes = new List<FileReRoute>
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamPathTemplate = "/api/products/{productId}",
|
||||
DownstreamHostAndPorts = new List<FileHostAndPort>
|
||||
{
|
||||
new FileHostAndPort
|
||||
{
|
||||
Host = "localhost",
|
||||
Port = 51879,
|
||||
}
|
||||
},
|
||||
DownstreamScheme = "http",
|
||||
UpstreamPathTemplate = "/products/{productId}",
|
||||
UpstreamHttpMethod = new List<string> { "Get" },
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:51879", "/api/products/1", 200, "Some Product"))
|
||||
.And(x => _steps.GivenThereIsAConfiguration(configuration))
|
||||
.And(x => _steps.GivenOcelotIsRunning())
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/PRODUCTS/1"))
|
||||
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_response_200_when_reroute_ignore_case_sensitivity_set()
|
||||
{
|
||||
var configuration = new FileConfiguration
|
||||
{
|
||||
ReRoutes = new List<FileReRoute>
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamPathTemplate = "/api/products/{productId}",
|
||||
DownstreamHostAndPorts = new List<FileHostAndPort>
|
||||
{
|
||||
new FileHostAndPort
|
||||
{
|
||||
Host = "localhost",
|
||||
Port = 51879,
|
||||
}
|
||||
},
|
||||
DownstreamScheme = "http",
|
||||
UpstreamPathTemplate = "/products/{productId}",
|
||||
UpstreamHttpMethod = new List<string> { "Get" },
|
||||
ReRouteIsCaseSensitive = false,
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:51879", "/api/products/1", 200, "Some Product"))
|
||||
.And(x => _steps.GivenThereIsAConfiguration(configuration))
|
||||
.And(x => _steps.GivenOcelotIsRunning())
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/PRODUCTS/1"))
|
||||
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_response_404_when_reroute_respect_case_sensitivity_set()
|
||||
{
|
||||
var configuration = new FileConfiguration
|
||||
{
|
||||
ReRoutes = new List<FileReRoute>
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamPathTemplate = "/api/products/{productId}",
|
||||
DownstreamHostAndPorts = new List<FileHostAndPort>
|
||||
{
|
||||
new FileHostAndPort
|
||||
{
|
||||
Host = "localhost",
|
||||
Port = 51879,
|
||||
}
|
||||
},
|
||||
DownstreamScheme = "http",
|
||||
UpstreamPathTemplate = "/products/{productId}",
|
||||
UpstreamHttpMethod = new List<string> { "Get" },
|
||||
ReRouteIsCaseSensitive = true,
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:51879", "/api/products/1", 200, "Some Product"))
|
||||
.And(x => _steps.GivenThereIsAConfiguration(configuration))
|
||||
.And(x => _steps.GivenOcelotIsRunning())
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/PRODUCTS/1"))
|
||||
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.NotFound))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_response_200_when_reroute_respect_case_sensitivity_set()
|
||||
{
|
||||
var configuration = new FileConfiguration
|
||||
{
|
||||
ReRoutes = new List<FileReRoute>
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamPathTemplate = "/api/products/{productId}",
|
||||
DownstreamHostAndPorts = new List<FileHostAndPort>
|
||||
{
|
||||
new FileHostAndPort
|
||||
{
|
||||
Host = "localhost",
|
||||
Port = 51879,
|
||||
}
|
||||
},
|
||||
DownstreamScheme = "http",
|
||||
UpstreamPathTemplate = "/PRODUCTS/{productId}",
|
||||
UpstreamHttpMethod = new List<string> { "Get" },
|
||||
ReRouteIsCaseSensitive = true,
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:51879", "/api/products/1", 200, "Some Product"))
|
||||
.And(x => _steps.GivenThereIsAConfiguration(configuration))
|
||||
.And(x => _steps.GivenOcelotIsRunning())
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/PRODUCTS/1"))
|
||||
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_response_404_when_global_respect_case_sensitivity_set()
|
||||
{
|
||||
var configuration = new FileConfiguration
|
||||
{
|
||||
ReRoutes = new List<FileReRoute>
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamPathTemplate = "/api/products/{productId}",
|
||||
DownstreamHostAndPorts = new List<FileHostAndPort>
|
||||
{
|
||||
new FileHostAndPort
|
||||
{
|
||||
Host = "localhost",
|
||||
Port = 51879,
|
||||
}
|
||||
},
|
||||
DownstreamScheme = "http",
|
||||
UpstreamPathTemplate = "/products/{productId}",
|
||||
UpstreamHttpMethod = new List<string> { "Get" },
|
||||
ReRouteIsCaseSensitive = true,
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:51879", "/api/products/1", 200, "Some Product"))
|
||||
.And(x => _steps.GivenThereIsAConfiguration(configuration))
|
||||
.And(x => _steps.GivenOcelotIsRunning())
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/PRODUCTS/1"))
|
||||
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.NotFound))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_response_200_when_global_respect_case_sensitivity_set()
|
||||
{
|
||||
var configuration = new FileConfiguration
|
||||
{
|
||||
ReRoutes = new List<FileReRoute>
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamPathTemplate = "/api/products/{productId}",
|
||||
DownstreamHostAndPorts = new List<FileHostAndPort>
|
||||
{
|
||||
new FileHostAndPort
|
||||
{
|
||||
Host = "localhost",
|
||||
Port = 51879,
|
||||
}
|
||||
},
|
||||
DownstreamScheme = "http",
|
||||
UpstreamPathTemplate = "/PRODUCTS/{productId}",
|
||||
UpstreamHttpMethod = new List<string> { "Get" },
|
||||
ReRouteIsCaseSensitive = true,
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:51879", "/api/products/1", 200, "Some Product"))
|
||||
.And(x => _steps.GivenThereIsAConfiguration(configuration))
|
||||
.And(x => _steps.GivenOcelotIsRunning())
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/PRODUCTS/1"))
|
||||
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenThereIsAServiceRunningOn(string baseUrl, string basePath, int statusCode, string responseBody)
|
||||
{
|
||||
_builder = new WebHostBuilder()
|
||||
.UseUrls(baseUrl)
|
||||
.UseKestrel()
|
||||
.UseContentRoot(Directory.GetCurrentDirectory())
|
||||
.UseIISIntegration()
|
||||
.UseUrls(baseUrl)
|
||||
.Configure(app =>
|
||||
{
|
||||
app.UsePathBase(basePath);
|
||||
app.Run(async context =>
|
||||
{
|
||||
context.Response.StatusCode = statusCode;
|
||||
await context.Response.WriteAsync(responseBody);
|
||||
});
|
||||
})
|
||||
.Build();
|
||||
|
||||
_builder.Start();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_builder?.Dispose();
|
||||
_steps.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,205 +1,211 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Security.Claims;
|
||||
using IdentityServer4.AccessTokenValidation;
|
||||
using IdentityServer4.Models;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Ocelot.Configuration.File;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
[assembly: CollectionBehavior(DisableTestParallelization = true)]
|
||||
namespace Ocelot.AcceptanceTests
|
||||
{
|
||||
using IdentityServer4;
|
||||
using IdentityServer4.Test;
|
||||
|
||||
public class ClaimsToHeadersForwardingTests : IDisposable
|
||||
{
|
||||
private IWebHost _servicebuilder;
|
||||
private IWebHost _identityServerBuilder;
|
||||
private readonly Steps _steps;
|
||||
private Action<IdentityServerAuthenticationOptions> _options;
|
||||
private string _identityServerRootUrl = "http://localhost:52888";
|
||||
|
||||
public ClaimsToHeadersForwardingTests()
|
||||
{
|
||||
_steps = new Steps();
|
||||
_options = o =>
|
||||
{
|
||||
o.Authority = _identityServerRootUrl;
|
||||
o.ApiName = "api";
|
||||
o.RequireHttpsMetadata = false;
|
||||
o.SupportedTokens = SupportedTokens.Both;
|
||||
o.ApiSecret = "secret";
|
||||
};
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_response_200_and_foward_claim_as_header()
|
||||
{
|
||||
var user = new TestUser()
|
||||
{
|
||||
Username = "test",
|
||||
Password = "test",
|
||||
SubjectId = "registered|1231231",
|
||||
Claims = new List<Claim>
|
||||
{
|
||||
new Claim("CustomerId", "123"),
|
||||
new Claim("LocationId", "1")
|
||||
}
|
||||
};
|
||||
|
||||
var configuration = new FileConfiguration
|
||||
{
|
||||
ReRoutes = new List<FileReRoute>
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamPathTemplate = "/",
|
||||
DownstreamPort = 52876,
|
||||
DownstreamScheme = "http",
|
||||
DownstreamHost = "localhost",
|
||||
UpstreamPathTemplate = "/",
|
||||
UpstreamHttpMethod = new List<string> { "Get" },
|
||||
AuthenticationOptions = new FileAuthenticationOptions
|
||||
{
|
||||
AuthenticationProviderKey = "Test",
|
||||
AllowedScopes = new List<string>
|
||||
{
|
||||
"openid", "offline_access", "api"
|
||||
},
|
||||
},
|
||||
AddHeadersToRequest =
|
||||
{
|
||||
{"CustomerId", "Claims[CustomerId] > value"},
|
||||
{"LocationId", "Claims[LocationId] > value"},
|
||||
{"UserType", "Claims[sub] > value[0] > |"},
|
||||
{"UserId", "Claims[sub] > value[1] > |"}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenThereIsAnIdentityServerOn("http://localhost:52888", "api", AccessTokenType.Jwt, user))
|
||||
.And(x => x.GivenThereIsAServiceRunningOn("http://localhost:52876", 200))
|
||||
.And(x => _steps.GivenIHaveAToken("http://localhost:52888"))
|
||||
.And(x => _steps.GivenThereIsAConfiguration(configuration))
|
||||
.And(x => _steps.GivenOcelotIsRunning(_options, "Test"))
|
||||
.And(x => _steps.GivenIHaveAddedATokenToMyRequest())
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
|
||||
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
|
||||
.And(x => _steps.ThenTheResponseBodyShouldBe("CustomerId: 123 LocationId: 1 UserType: registered UserId: 1231231"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenThereIsAServiceRunningOn(string url, int statusCode)
|
||||
{
|
||||
_servicebuilder = new WebHostBuilder()
|
||||
.UseUrls(url)
|
||||
.UseKestrel()
|
||||
.UseContentRoot(Directory.GetCurrentDirectory())
|
||||
.UseIISIntegration()
|
||||
.UseUrls(url)
|
||||
.Configure(app =>
|
||||
{
|
||||
app.Run(async context =>
|
||||
{
|
||||
var customerId = context.Request.Headers.First(x => x.Key == "CustomerId").Value.First();
|
||||
var locationId = context.Request.Headers.First(x => x.Key == "LocationId").Value.First();
|
||||
var userType = context.Request.Headers.First(x => x.Key == "UserType").Value.First();
|
||||
var userId = context.Request.Headers.First(x => x.Key == "UserId").Value.First();
|
||||
|
||||
var responseBody = $"CustomerId: {customerId} LocationId: {locationId} UserType: {userType} UserId: {userId}";
|
||||
context.Response.StatusCode = statusCode;
|
||||
await context.Response.WriteAsync(responseBody);
|
||||
});
|
||||
})
|
||||
.Build();
|
||||
|
||||
_servicebuilder.Start();
|
||||
}
|
||||
|
||||
private void GivenThereIsAnIdentityServerOn(string url, string apiName, AccessTokenType tokenType, TestUser user)
|
||||
{
|
||||
_identityServerBuilder = new WebHostBuilder()
|
||||
.UseUrls(url)
|
||||
.UseKestrel()
|
||||
.UseContentRoot(Directory.GetCurrentDirectory())
|
||||
.UseIISIntegration()
|
||||
.UseUrls(url)
|
||||
.ConfigureServices(services =>
|
||||
{
|
||||
services.AddLogging();
|
||||
services.AddIdentityServer()
|
||||
.AddDeveloperSigningCredential()
|
||||
.AddInMemoryApiResources(new List<ApiResource>
|
||||
{
|
||||
new ApiResource
|
||||
{
|
||||
Name = apiName,
|
||||
Description = "My API",
|
||||
Enabled = true,
|
||||
DisplayName = "test",
|
||||
Scopes = new List<Scope>()
|
||||
{
|
||||
new Scope("api"),
|
||||
new Scope("openid"),
|
||||
new Scope("offline_access")
|
||||
},
|
||||
ApiSecrets = new List<Secret>()
|
||||
{
|
||||
new Secret
|
||||
{
|
||||
Value = "secret".Sha256()
|
||||
}
|
||||
},
|
||||
UserClaims = new List<string>()
|
||||
{
|
||||
"CustomerId", "LocationId", "UserType", "UserId"
|
||||
}
|
||||
}
|
||||
})
|
||||
.AddInMemoryClients(new List<Client>
|
||||
{
|
||||
new Client
|
||||
{
|
||||
ClientId = "client",
|
||||
AllowedGrantTypes = GrantTypes.ResourceOwnerPassword,
|
||||
ClientSecrets = new List<Secret> {new Secret("secret".Sha256())},
|
||||
AllowedScopes = new List<string> { apiName, "openid", "offline_access" },
|
||||
AccessTokenType = tokenType,
|
||||
Enabled = true,
|
||||
RequireClientSecret = false
|
||||
}
|
||||
})
|
||||
.AddTestUsers(new List<TestUser>
|
||||
{
|
||||
user
|
||||
});
|
||||
})
|
||||
.Configure(app =>
|
||||
{
|
||||
app.UseIdentityServer();
|
||||
})
|
||||
.Build();
|
||||
|
||||
_identityServerBuilder.Start();
|
||||
|
||||
_steps.VerifyIdentiryServerStarted(url);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_servicebuilder?.Dispose();
|
||||
_steps.Dispose();
|
||||
_identityServerBuilder?.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Security.Claims;
|
||||
using IdentityServer4.AccessTokenValidation;
|
||||
using IdentityServer4.Models;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Ocelot.Configuration.File;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
[assembly: CollectionBehavior(DisableTestParallelization = true)]
|
||||
namespace Ocelot.AcceptanceTests
|
||||
{
|
||||
using IdentityServer4;
|
||||
using IdentityServer4.Test;
|
||||
|
||||
public class ClaimsToHeadersForwardingTests : IDisposable
|
||||
{
|
||||
private IWebHost _servicebuilder;
|
||||
private IWebHost _identityServerBuilder;
|
||||
private readonly Steps _steps;
|
||||
private Action<IdentityServerAuthenticationOptions> _options;
|
||||
private string _identityServerRootUrl = "http://localhost:52888";
|
||||
|
||||
public ClaimsToHeadersForwardingTests()
|
||||
{
|
||||
_steps = new Steps();
|
||||
_options = o =>
|
||||
{
|
||||
o.Authority = _identityServerRootUrl;
|
||||
o.ApiName = "api";
|
||||
o.RequireHttpsMetadata = false;
|
||||
o.SupportedTokens = SupportedTokens.Both;
|
||||
o.ApiSecret = "secret";
|
||||
};
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_response_200_and_foward_claim_as_header()
|
||||
{
|
||||
var user = new TestUser()
|
||||
{
|
||||
Username = "test",
|
||||
Password = "test",
|
||||
SubjectId = "registered|1231231",
|
||||
Claims = new List<Claim>
|
||||
{
|
||||
new Claim("CustomerId", "123"),
|
||||
new Claim("LocationId", "1")
|
||||
}
|
||||
};
|
||||
|
||||
var configuration = new FileConfiguration
|
||||
{
|
||||
ReRoutes = new List<FileReRoute>
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamPathTemplate = "/",
|
||||
DownstreamHostAndPorts = new List<FileHostAndPort>
|
||||
{
|
||||
new FileHostAndPort
|
||||
{
|
||||
Host = "localhost",
|
||||
Port = 52876,
|
||||
}
|
||||
},
|
||||
DownstreamScheme = "http",
|
||||
UpstreamPathTemplate = "/",
|
||||
UpstreamHttpMethod = new List<string> { "Get" },
|
||||
AuthenticationOptions = new FileAuthenticationOptions
|
||||
{
|
||||
AuthenticationProviderKey = "Test",
|
||||
AllowedScopes = new List<string>
|
||||
{
|
||||
"openid", "offline_access", "api"
|
||||
},
|
||||
},
|
||||
AddHeadersToRequest =
|
||||
{
|
||||
{"CustomerId", "Claims[CustomerId] > value"},
|
||||
{"LocationId", "Claims[LocationId] > value"},
|
||||
{"UserType", "Claims[sub] > value[0] > |"},
|
||||
{"UserId", "Claims[sub] > value[1] > |"}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenThereIsAnIdentityServerOn("http://localhost:52888", "api", AccessTokenType.Jwt, user))
|
||||
.And(x => x.GivenThereIsAServiceRunningOn("http://localhost:52876", 200))
|
||||
.And(x => _steps.GivenIHaveAToken("http://localhost:52888"))
|
||||
.And(x => _steps.GivenThereIsAConfiguration(configuration))
|
||||
.And(x => _steps.GivenOcelotIsRunning(_options, "Test"))
|
||||
.And(x => _steps.GivenIHaveAddedATokenToMyRequest())
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
|
||||
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
|
||||
.And(x => _steps.ThenTheResponseBodyShouldBe("CustomerId: 123 LocationId: 1 UserType: registered UserId: 1231231"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenThereIsAServiceRunningOn(string url, int statusCode)
|
||||
{
|
||||
_servicebuilder = new WebHostBuilder()
|
||||
.UseUrls(url)
|
||||
.UseKestrel()
|
||||
.UseContentRoot(Directory.GetCurrentDirectory())
|
||||
.UseIISIntegration()
|
||||
.UseUrls(url)
|
||||
.Configure(app =>
|
||||
{
|
||||
app.Run(async context =>
|
||||
{
|
||||
var customerId = context.Request.Headers.First(x => x.Key == "CustomerId").Value.First();
|
||||
var locationId = context.Request.Headers.First(x => x.Key == "LocationId").Value.First();
|
||||
var userType = context.Request.Headers.First(x => x.Key == "UserType").Value.First();
|
||||
var userId = context.Request.Headers.First(x => x.Key == "UserId").Value.First();
|
||||
|
||||
var responseBody = $"CustomerId: {customerId} LocationId: {locationId} UserType: {userType} UserId: {userId}";
|
||||
context.Response.StatusCode = statusCode;
|
||||
await context.Response.WriteAsync(responseBody);
|
||||
});
|
||||
})
|
||||
.Build();
|
||||
|
||||
_servicebuilder.Start();
|
||||
}
|
||||
|
||||
private void GivenThereIsAnIdentityServerOn(string url, string apiName, AccessTokenType tokenType, TestUser user)
|
||||
{
|
||||
_identityServerBuilder = new WebHostBuilder()
|
||||
.UseUrls(url)
|
||||
.UseKestrel()
|
||||
.UseContentRoot(Directory.GetCurrentDirectory())
|
||||
.UseIISIntegration()
|
||||
.UseUrls(url)
|
||||
.ConfigureServices(services =>
|
||||
{
|
||||
services.AddLogging();
|
||||
services.AddIdentityServer()
|
||||
.AddDeveloperSigningCredential()
|
||||
.AddInMemoryApiResources(new List<ApiResource>
|
||||
{
|
||||
new ApiResource
|
||||
{
|
||||
Name = apiName,
|
||||
Description = "My API",
|
||||
Enabled = true,
|
||||
DisplayName = "test",
|
||||
Scopes = new List<Scope>()
|
||||
{
|
||||
new Scope("api"),
|
||||
new Scope("openid"),
|
||||
new Scope("offline_access")
|
||||
},
|
||||
ApiSecrets = new List<Secret>()
|
||||
{
|
||||
new Secret
|
||||
{
|
||||
Value = "secret".Sha256()
|
||||
}
|
||||
},
|
||||
UserClaims = new List<string>()
|
||||
{
|
||||
"CustomerId", "LocationId", "UserType", "UserId"
|
||||
}
|
||||
}
|
||||
})
|
||||
.AddInMemoryClients(new List<Client>
|
||||
{
|
||||
new Client
|
||||
{
|
||||
ClientId = "client",
|
||||
AllowedGrantTypes = GrantTypes.ResourceOwnerPassword,
|
||||
ClientSecrets = new List<Secret> {new Secret("secret".Sha256())},
|
||||
AllowedScopes = new List<string> { apiName, "openid", "offline_access" },
|
||||
AccessTokenType = tokenType,
|
||||
Enabled = true,
|
||||
RequireClientSecret = false
|
||||
}
|
||||
})
|
||||
.AddTestUsers(new List<TestUser>
|
||||
{
|
||||
user
|
||||
});
|
||||
})
|
||||
.Configure(app =>
|
||||
{
|
||||
app.UseIdentityServer();
|
||||
})
|
||||
.Build();
|
||||
|
||||
_identityServerBuilder.Start();
|
||||
|
||||
_steps.VerifyIdentiryServerStarted(url);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_servicebuilder?.Dispose();
|
||||
_steps.Dispose();
|
||||
_identityServerBuilder?.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,212 +1,218 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Security.Claims;
|
||||
using IdentityServer4.AccessTokenValidation;
|
||||
using IdentityServer4.Models;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Primitives;
|
||||
using Ocelot.Configuration.File;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.AcceptanceTests
|
||||
{
|
||||
using IdentityServer4;
|
||||
using IdentityServer4.Test;
|
||||
|
||||
public class ClaimsToQueryStringForwardingTests : IDisposable
|
||||
{
|
||||
private IWebHost _servicebuilder;
|
||||
private IWebHost _identityServerBuilder;
|
||||
private readonly Steps _steps;
|
||||
private Action<IdentityServerAuthenticationOptions> _options;
|
||||
private string _identityServerRootUrl = "http://localhost:57888";
|
||||
|
||||
public ClaimsToQueryStringForwardingTests()
|
||||
{
|
||||
_steps = new Steps();
|
||||
_options = o =>
|
||||
{
|
||||
o.Authority = _identityServerRootUrl;
|
||||
o.ApiName = "api";
|
||||
o.RequireHttpsMetadata = false;
|
||||
o.SupportedTokens = SupportedTokens.Both;
|
||||
o.ApiSecret = "secret";
|
||||
};
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_response_200_and_foward_claim_as_query_string()
|
||||
{
|
||||
var user = new TestUser()
|
||||
{
|
||||
Username = "test",
|
||||
Password = "test",
|
||||
SubjectId = "registered|1231231",
|
||||
Claims = new List<Claim>
|
||||
{
|
||||
new Claim("CustomerId", "123"),
|
||||
new Claim("LocationId", "1")
|
||||
}
|
||||
};
|
||||
|
||||
var configuration = new FileConfiguration
|
||||
{
|
||||
ReRoutes = new List<FileReRoute>
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamPathTemplate = "/",
|
||||
DownstreamPort = 57876,
|
||||
DownstreamScheme = "http",
|
||||
DownstreamHost = "localhost",
|
||||
UpstreamPathTemplate = "/",
|
||||
UpstreamHttpMethod = new List<string> { "Get" },
|
||||
AuthenticationOptions = new FileAuthenticationOptions
|
||||
{
|
||||
AuthenticationProviderKey = "Test",
|
||||
AllowedScopes = new List<string>
|
||||
{
|
||||
"openid", "offline_access", "api"
|
||||
},
|
||||
},
|
||||
AddQueriesToRequest =
|
||||
{
|
||||
{"CustomerId", "Claims[CustomerId] > value"},
|
||||
{"LocationId", "Claims[LocationId] > value"},
|
||||
{"UserType", "Claims[sub] > value[0] > |"},
|
||||
{"UserId", "Claims[sub] > value[1] > |"}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenThereIsAnIdentityServerOn("http://localhost:57888", "api", AccessTokenType.Jwt, user))
|
||||
.And(x => x.GivenThereIsAServiceRunningOn("http://localhost:57876", 200))
|
||||
.And(x => _steps.GivenIHaveAToken("http://localhost:57888"))
|
||||
.And(x => _steps.GivenThereIsAConfiguration(configuration))
|
||||
.And(x => _steps.GivenOcelotIsRunning(_options, "Test"))
|
||||
.And(x => _steps.GivenIHaveAddedATokenToMyRequest())
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
|
||||
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
|
||||
.And(x => _steps.ThenTheResponseBodyShouldBe("CustomerId: 123 LocationId: 1 UserType: registered UserId: 1231231"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenThereIsAServiceRunningOn(string url, int statusCode)
|
||||
{
|
||||
_servicebuilder = new WebHostBuilder()
|
||||
.UseUrls(url)
|
||||
.UseKestrel()
|
||||
.UseContentRoot(Directory.GetCurrentDirectory())
|
||||
.UseIISIntegration()
|
||||
.UseUrls(url)
|
||||
.Configure(app =>
|
||||
{
|
||||
app.Run(async context =>
|
||||
{
|
||||
StringValues customerId;
|
||||
context.Request.Query.TryGetValue("CustomerId", out customerId);
|
||||
|
||||
StringValues locationId;
|
||||
context.Request.Query.TryGetValue("LocationId", out locationId);
|
||||
|
||||
StringValues userType;
|
||||
context.Request.Query.TryGetValue("UserType", out userType);
|
||||
|
||||
StringValues userId;
|
||||
context.Request.Query.TryGetValue("UserId", out userId);
|
||||
|
||||
var responseBody = $"CustomerId: {customerId} LocationId: {locationId} UserType: {userType} UserId: {userId}";
|
||||
context.Response.StatusCode = statusCode;
|
||||
await context.Response.WriteAsync(responseBody);
|
||||
});
|
||||
})
|
||||
.Build();
|
||||
|
||||
_servicebuilder.Start();
|
||||
}
|
||||
|
||||
private void GivenThereIsAnIdentityServerOn(string url, string apiName, AccessTokenType tokenType, TestUser user)
|
||||
{
|
||||
_identityServerBuilder = new WebHostBuilder()
|
||||
.UseUrls(url)
|
||||
.UseKestrel()
|
||||
.UseContentRoot(Directory.GetCurrentDirectory())
|
||||
.UseIISIntegration()
|
||||
.UseUrls(url)
|
||||
.ConfigureServices(services =>
|
||||
{
|
||||
services.AddLogging();
|
||||
services.AddIdentityServer()
|
||||
.AddDeveloperSigningCredential()
|
||||
.AddInMemoryApiResources(new List<ApiResource>
|
||||
{
|
||||
new ApiResource
|
||||
{
|
||||
Name = apiName,
|
||||
Description = "My API",
|
||||
Enabled = true,
|
||||
DisplayName = "test",
|
||||
Scopes = new List<Scope>()
|
||||
{
|
||||
new Scope("api"),
|
||||
new Scope("openid"),
|
||||
new Scope("offline_access")
|
||||
},
|
||||
ApiSecrets = new List<Secret>()
|
||||
{
|
||||
new Secret
|
||||
{
|
||||
Value = "secret".Sha256()
|
||||
}
|
||||
},
|
||||
UserClaims = new List<string>()
|
||||
{
|
||||
"CustomerId", "LocationId", "UserType", "UserId"
|
||||
}
|
||||
}
|
||||
})
|
||||
.AddInMemoryClients(new List<Client>
|
||||
{
|
||||
new Client
|
||||
{
|
||||
ClientId = "client",
|
||||
AllowedGrantTypes = GrantTypes.ResourceOwnerPassword,
|
||||
ClientSecrets = new List<Secret> {new Secret("secret".Sha256())},
|
||||
AllowedScopes = new List<string> { apiName, "openid", "offline_access" },
|
||||
AccessTokenType = tokenType,
|
||||
Enabled = true,
|
||||
RequireClientSecret = false
|
||||
}
|
||||
})
|
||||
.AddTestUsers(new List<TestUser>
|
||||
{
|
||||
user
|
||||
});
|
||||
})
|
||||
.Configure(app =>
|
||||
{
|
||||
app.UseIdentityServer();
|
||||
})
|
||||
.Build();
|
||||
|
||||
_identityServerBuilder.Start();
|
||||
|
||||
_steps.VerifyIdentiryServerStarted(url);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_servicebuilder?.Dispose();
|
||||
_steps.Dispose();
|
||||
_identityServerBuilder?.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Security.Claims;
|
||||
using IdentityServer4.AccessTokenValidation;
|
||||
using IdentityServer4.Models;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Primitives;
|
||||
using Ocelot.Configuration.File;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.AcceptanceTests
|
||||
{
|
||||
using IdentityServer4;
|
||||
using IdentityServer4.Test;
|
||||
|
||||
public class ClaimsToQueryStringForwardingTests : IDisposable
|
||||
{
|
||||
private IWebHost _servicebuilder;
|
||||
private IWebHost _identityServerBuilder;
|
||||
private readonly Steps _steps;
|
||||
private Action<IdentityServerAuthenticationOptions> _options;
|
||||
private string _identityServerRootUrl = "http://localhost:57888";
|
||||
|
||||
public ClaimsToQueryStringForwardingTests()
|
||||
{
|
||||
_steps = new Steps();
|
||||
_options = o =>
|
||||
{
|
||||
o.Authority = _identityServerRootUrl;
|
||||
o.ApiName = "api";
|
||||
o.RequireHttpsMetadata = false;
|
||||
o.SupportedTokens = SupportedTokens.Both;
|
||||
o.ApiSecret = "secret";
|
||||
};
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_response_200_and_foward_claim_as_query_string()
|
||||
{
|
||||
var user = new TestUser()
|
||||
{
|
||||
Username = "test",
|
||||
Password = "test",
|
||||
SubjectId = "registered|1231231",
|
||||
Claims = new List<Claim>
|
||||
{
|
||||
new Claim("CustomerId", "123"),
|
||||
new Claim("LocationId", "1")
|
||||
}
|
||||
};
|
||||
|
||||
var configuration = new FileConfiguration
|
||||
{
|
||||
ReRoutes = new List<FileReRoute>
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamPathTemplate = "/",
|
||||
DownstreamHostAndPorts = new List<FileHostAndPort>
|
||||
{
|
||||
new FileHostAndPort
|
||||
{
|
||||
Host = "localhost",
|
||||
Port = 57876,
|
||||
}
|
||||
},
|
||||
DownstreamScheme = "http",
|
||||
UpstreamPathTemplate = "/",
|
||||
UpstreamHttpMethod = new List<string> { "Get" },
|
||||
AuthenticationOptions = new FileAuthenticationOptions
|
||||
{
|
||||
AuthenticationProviderKey = "Test",
|
||||
AllowedScopes = new List<string>
|
||||
{
|
||||
"openid", "offline_access", "api"
|
||||
},
|
||||
},
|
||||
AddQueriesToRequest =
|
||||
{
|
||||
{"CustomerId", "Claims[CustomerId] > value"},
|
||||
{"LocationId", "Claims[LocationId] > value"},
|
||||
{"UserType", "Claims[sub] > value[0] > |"},
|
||||
{"UserId", "Claims[sub] > value[1] > |"}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenThereIsAnIdentityServerOn("http://localhost:57888", "api", AccessTokenType.Jwt, user))
|
||||
.And(x => x.GivenThereIsAServiceRunningOn("http://localhost:57876", 200))
|
||||
.And(x => _steps.GivenIHaveAToken("http://localhost:57888"))
|
||||
.And(x => _steps.GivenThereIsAConfiguration(configuration))
|
||||
.And(x => _steps.GivenOcelotIsRunning(_options, "Test"))
|
||||
.And(x => _steps.GivenIHaveAddedATokenToMyRequest())
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
|
||||
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
|
||||
.And(x => _steps.ThenTheResponseBodyShouldBe("CustomerId: 123 LocationId: 1 UserType: registered UserId: 1231231"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenThereIsAServiceRunningOn(string url, int statusCode)
|
||||
{
|
||||
_servicebuilder = new WebHostBuilder()
|
||||
.UseUrls(url)
|
||||
.UseKestrel()
|
||||
.UseContentRoot(Directory.GetCurrentDirectory())
|
||||
.UseIISIntegration()
|
||||
.UseUrls(url)
|
||||
.Configure(app =>
|
||||
{
|
||||
app.Run(async context =>
|
||||
{
|
||||
StringValues customerId;
|
||||
context.Request.Query.TryGetValue("CustomerId", out customerId);
|
||||
|
||||
StringValues locationId;
|
||||
context.Request.Query.TryGetValue("LocationId", out locationId);
|
||||
|
||||
StringValues userType;
|
||||
context.Request.Query.TryGetValue("UserType", out userType);
|
||||
|
||||
StringValues userId;
|
||||
context.Request.Query.TryGetValue("UserId", out userId);
|
||||
|
||||
var responseBody = $"CustomerId: {customerId} LocationId: {locationId} UserType: {userType} UserId: {userId}";
|
||||
context.Response.StatusCode = statusCode;
|
||||
await context.Response.WriteAsync(responseBody);
|
||||
});
|
||||
})
|
||||
.Build();
|
||||
|
||||
_servicebuilder.Start();
|
||||
}
|
||||
|
||||
private void GivenThereIsAnIdentityServerOn(string url, string apiName, AccessTokenType tokenType, TestUser user)
|
||||
{
|
||||
_identityServerBuilder = new WebHostBuilder()
|
||||
.UseUrls(url)
|
||||
.UseKestrel()
|
||||
.UseContentRoot(Directory.GetCurrentDirectory())
|
||||
.UseIISIntegration()
|
||||
.UseUrls(url)
|
||||
.ConfigureServices(services =>
|
||||
{
|
||||
services.AddLogging();
|
||||
services.AddIdentityServer()
|
||||
.AddDeveloperSigningCredential()
|
||||
.AddInMemoryApiResources(new List<ApiResource>
|
||||
{
|
||||
new ApiResource
|
||||
{
|
||||
Name = apiName,
|
||||
Description = "My API",
|
||||
Enabled = true,
|
||||
DisplayName = "test",
|
||||
Scopes = new List<Scope>()
|
||||
{
|
||||
new Scope("api"),
|
||||
new Scope("openid"),
|
||||
new Scope("offline_access")
|
||||
},
|
||||
ApiSecrets = new List<Secret>()
|
||||
{
|
||||
new Secret
|
||||
{
|
||||
Value = "secret".Sha256()
|
||||
}
|
||||
},
|
||||
UserClaims = new List<string>()
|
||||
{
|
||||
"CustomerId", "LocationId", "UserType", "UserId"
|
||||
}
|
||||
}
|
||||
})
|
||||
.AddInMemoryClients(new List<Client>
|
||||
{
|
||||
new Client
|
||||
{
|
||||
ClientId = "client",
|
||||
AllowedGrantTypes = GrantTypes.ResourceOwnerPassword,
|
||||
ClientSecrets = new List<Secret> {new Secret("secret".Sha256())},
|
||||
AllowedScopes = new List<string> { apiName, "openid", "offline_access" },
|
||||
AccessTokenType = tokenType,
|
||||
Enabled = true,
|
||||
RequireClientSecret = false
|
||||
}
|
||||
})
|
||||
.AddTestUsers(new List<TestUser>
|
||||
{
|
||||
user
|
||||
});
|
||||
})
|
||||
.Configure(app =>
|
||||
{
|
||||
app.UseIdentityServer();
|
||||
})
|
||||
.Build();
|
||||
|
||||
_identityServerBuilder.Start();
|
||||
|
||||
_steps.VerifyIdentiryServerStarted(url);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_servicebuilder?.Dispose();
|
||||
_steps.Dispose();
|
||||
_identityServerBuilder?.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,166 +1,178 @@
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Ocelot.Configuration.File;
|
||||
using Shouldly;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.AcceptanceTests
|
||||
{
|
||||
public class ClientRateLimitTests : IDisposable
|
||||
{
|
||||
private IWebHost _builder;
|
||||
private readonly Steps _steps;
|
||||
private int _counterOne;
|
||||
|
||||
|
||||
public ClientRateLimitTests()
|
||||
{
|
||||
_steps = new Steps();
|
||||
}
|
||||
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_builder?.Dispose();
|
||||
_steps.Dispose();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_call_withratelimiting()
|
||||
{
|
||||
var configuration = new FileConfiguration
|
||||
{
|
||||
ReRoutes = new List<FileReRoute>
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamPathTemplate = "/api/ClientRateLimit",
|
||||
DownstreamPort = 51879,
|
||||
DownstreamScheme = "http",
|
||||
DownstreamHost = "localhost",
|
||||
UpstreamPathTemplate = "/api/ClientRateLimit",
|
||||
UpstreamHttpMethod = new List<string> { "Get" },
|
||||
RequestIdKey = _steps.RequestIdKey,
|
||||
|
||||
RateLimitOptions = new FileRateLimitRule()
|
||||
{
|
||||
EnableRateLimiting = true,
|
||||
ClientWhitelist = new List<string>(),
|
||||
Limit = 3,
|
||||
Period = "1s",
|
||||
PeriodTimespan = 1000
|
||||
}
|
||||
}
|
||||
},
|
||||
GlobalConfiguration = new FileGlobalConfiguration()
|
||||
{
|
||||
RateLimitOptions = new FileRateLimitOptions()
|
||||
{
|
||||
ClientIdHeader = "ClientId",
|
||||
DisableRateLimitHeaders = false,
|
||||
QuotaExceededMessage = "",
|
||||
RateLimitCounterPrefix = "",
|
||||
HttpStatusCode = 428
|
||||
|
||||
},
|
||||
RequestIdKey ="oceclientrequest"
|
||||
}
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:51879", "/api/ClientRateLimit"))
|
||||
.And(x => _steps.GivenThereIsAConfiguration(configuration))
|
||||
.And(x => _steps.GivenOcelotIsRunning())
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGatewayMultipleTimesForRateLimit("/api/ClientRateLimit",1))
|
||||
.Then(x => _steps.ThenTheStatusCodeShouldBe(200))
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGatewayMultipleTimesForRateLimit("/api/ClientRateLimit", 2))
|
||||
.Then(x => _steps.ThenTheStatusCodeShouldBe(200))
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGatewayMultipleTimesForRateLimit("/api/ClientRateLimit",1))
|
||||
.Then(x => _steps.ThenTheStatusCodeShouldBe(428))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
|
||||
[Fact]
|
||||
public void should_call_middleware_withWhitelistClient()
|
||||
{
|
||||
var configuration = new FileConfiguration
|
||||
{
|
||||
ReRoutes = new List<FileReRoute>
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamPathTemplate = "/api/ClientRateLimit",
|
||||
DownstreamPort = 51879,
|
||||
DownstreamScheme = "http",
|
||||
DownstreamHost = "localhost",
|
||||
UpstreamPathTemplate = "/api/ClientRateLimit",
|
||||
UpstreamHttpMethod = new List<string> { "Get" },
|
||||
RequestIdKey = _steps.RequestIdKey,
|
||||
|
||||
RateLimitOptions = new FileRateLimitRule()
|
||||
{
|
||||
EnableRateLimiting = true,
|
||||
ClientWhitelist = new List<string>() { "ocelotclient1"},
|
||||
Limit = 3,
|
||||
Period = "1s",
|
||||
PeriodTimespan = 100
|
||||
}
|
||||
}
|
||||
},
|
||||
GlobalConfiguration = new FileGlobalConfiguration()
|
||||
{
|
||||
RateLimitOptions = new FileRateLimitOptions()
|
||||
{
|
||||
ClientIdHeader = "ClientId",
|
||||
DisableRateLimitHeaders = false,
|
||||
QuotaExceededMessage = "",
|
||||
RateLimitCounterPrefix = ""
|
||||
},
|
||||
RequestIdKey = "oceclientrequest"
|
||||
}
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:51879", "/api/ClientRateLimit"))
|
||||
.And(x => _steps.GivenThereIsAConfiguration(configuration))
|
||||
.And(x => _steps.GivenOcelotIsRunning())
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGatewayMultipleTimesForRateLimit("/api/ClientRateLimit", 4))
|
||||
.Then(x => _steps.ThenTheStatusCodeShouldBe(200))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
|
||||
private void GivenThereIsAServiceRunningOn(string baseUrl, string basePath)
|
||||
{
|
||||
_builder = new WebHostBuilder()
|
||||
.UseUrls(baseUrl)
|
||||
.UseKestrel()
|
||||
.UseContentRoot(Directory.GetCurrentDirectory())
|
||||
.UseIISIntegration()
|
||||
.UseUrls(baseUrl)
|
||||
.Configure(app =>
|
||||
{
|
||||
app.UsePathBase(basePath);
|
||||
app.Run(context =>
|
||||
{
|
||||
_counterOne++;
|
||||
context.Response.StatusCode = 200;
|
||||
context.Response.WriteAsync(_counterOne.ToString());
|
||||
return Task.CompletedTask;
|
||||
});
|
||||
})
|
||||
.Build();
|
||||
|
||||
_builder.Start();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Ocelot.Configuration.File;
|
||||
using Shouldly;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.AcceptanceTests
|
||||
{
|
||||
public class ClientRateLimitTests : IDisposable
|
||||
{
|
||||
private IWebHost _builder;
|
||||
private readonly Steps _steps;
|
||||
private int _counterOne;
|
||||
|
||||
|
||||
public ClientRateLimitTests()
|
||||
{
|
||||
_steps = new Steps();
|
||||
}
|
||||
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_builder?.Dispose();
|
||||
_steps.Dispose();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_call_withratelimiting()
|
||||
{
|
||||
var configuration = new FileConfiguration
|
||||
{
|
||||
ReRoutes = new List<FileReRoute>
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamPathTemplate = "/api/ClientRateLimit",
|
||||
DownstreamHostAndPorts = new List<FileHostAndPort>
|
||||
{
|
||||
new FileHostAndPort
|
||||
{
|
||||
Host = "localhost",
|
||||
Port = 51879,
|
||||
}
|
||||
},
|
||||
DownstreamScheme = "http",
|
||||
UpstreamPathTemplate = "/api/ClientRateLimit",
|
||||
UpstreamHttpMethod = new List<string> { "Get" },
|
||||
RequestIdKey = _steps.RequestIdKey,
|
||||
|
||||
RateLimitOptions = new FileRateLimitRule()
|
||||
{
|
||||
EnableRateLimiting = true,
|
||||
ClientWhitelist = new List<string>(),
|
||||
Limit = 3,
|
||||
Period = "1s",
|
||||
PeriodTimespan = 1000
|
||||
}
|
||||
}
|
||||
},
|
||||
GlobalConfiguration = new FileGlobalConfiguration()
|
||||
{
|
||||
RateLimitOptions = new FileRateLimitOptions()
|
||||
{
|
||||
ClientIdHeader = "ClientId",
|
||||
DisableRateLimitHeaders = false,
|
||||
QuotaExceededMessage = "",
|
||||
RateLimitCounterPrefix = "",
|
||||
HttpStatusCode = 428
|
||||
|
||||
},
|
||||
RequestIdKey ="oceclientrequest"
|
||||
}
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:51879", "/api/ClientRateLimit"))
|
||||
.And(x => _steps.GivenThereIsAConfiguration(configuration))
|
||||
.And(x => _steps.GivenOcelotIsRunning())
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGatewayMultipleTimesForRateLimit("/api/ClientRateLimit",1))
|
||||
.Then(x => _steps.ThenTheStatusCodeShouldBe(200))
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGatewayMultipleTimesForRateLimit("/api/ClientRateLimit", 2))
|
||||
.Then(x => _steps.ThenTheStatusCodeShouldBe(200))
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGatewayMultipleTimesForRateLimit("/api/ClientRateLimit",1))
|
||||
.Then(x => _steps.ThenTheStatusCodeShouldBe(428))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
|
||||
[Fact]
|
||||
public void should_call_middleware_withWhitelistClient()
|
||||
{
|
||||
var configuration = new FileConfiguration
|
||||
{
|
||||
ReRoutes = new List<FileReRoute>
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamPathTemplate = "/api/ClientRateLimit",
|
||||
DownstreamHostAndPorts = new List<FileHostAndPort>
|
||||
{
|
||||
new FileHostAndPort
|
||||
{
|
||||
Host = "localhost",
|
||||
Port = 51879,
|
||||
}
|
||||
},
|
||||
DownstreamScheme = "http",
|
||||
UpstreamPathTemplate = "/api/ClientRateLimit",
|
||||
UpstreamHttpMethod = new List<string> { "Get" },
|
||||
RequestIdKey = _steps.RequestIdKey,
|
||||
|
||||
RateLimitOptions = new FileRateLimitRule()
|
||||
{
|
||||
EnableRateLimiting = true,
|
||||
ClientWhitelist = new List<string>() { "ocelotclient1"},
|
||||
Limit = 3,
|
||||
Period = "1s",
|
||||
PeriodTimespan = 100
|
||||
}
|
||||
}
|
||||
},
|
||||
GlobalConfiguration = new FileGlobalConfiguration()
|
||||
{
|
||||
RateLimitOptions = new FileRateLimitOptions()
|
||||
{
|
||||
ClientIdHeader = "ClientId",
|
||||
DisableRateLimitHeaders = false,
|
||||
QuotaExceededMessage = "",
|
||||
RateLimitCounterPrefix = ""
|
||||
},
|
||||
RequestIdKey = "oceclientrequest"
|
||||
}
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:51879", "/api/ClientRateLimit"))
|
||||
.And(x => _steps.GivenThereIsAConfiguration(configuration))
|
||||
.And(x => _steps.GivenOcelotIsRunning())
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGatewayMultipleTimesForRateLimit("/api/ClientRateLimit", 4))
|
||||
.Then(x => _steps.ThenTheStatusCodeShouldBe(200))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
|
||||
private void GivenThereIsAServiceRunningOn(string baseUrl, string basePath)
|
||||
{
|
||||
_builder = new WebHostBuilder()
|
||||
.UseUrls(baseUrl)
|
||||
.UseKestrel()
|
||||
.UseContentRoot(Directory.GetCurrentDirectory())
|
||||
.UseIISIntegration()
|
||||
.UseUrls(baseUrl)
|
||||
.Configure(app =>
|
||||
{
|
||||
app.UsePathBase(basePath);
|
||||
app.Run(context =>
|
||||
{
|
||||
_counterOne++;
|
||||
context.Response.StatusCode = 200;
|
||||
context.Response.WriteAsync(_counterOne.ToString());
|
||||
return Task.CompletedTask;
|
||||
});
|
||||
})
|
||||
.Build();
|
||||
|
||||
_builder.Start();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -1,353 +1,383 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Newtonsoft.Json;
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.Configuration.Builder;
|
||||
using Ocelot.Configuration.File;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.AcceptanceTests
|
||||
{
|
||||
public class ConfigurationInConsulTests : IDisposable
|
||||
{
|
||||
private IWebHost _builder;
|
||||
private readonly Steps _steps;
|
||||
private IWebHost _fakeConsulBuilder;
|
||||
private FileConfiguration _config;
|
||||
|
||||
public ConfigurationInConsulTests()
|
||||
{
|
||||
_steps = new Steps();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_response_200_with_simple_url()
|
||||
{
|
||||
var configuration = new FileConfiguration
|
||||
{
|
||||
ReRoutes = new List<FileReRoute>
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamPathTemplate = "/",
|
||||
DownstreamScheme = "http",
|
||||
DownstreamHost = "localhost",
|
||||
DownstreamPort = 51779,
|
||||
UpstreamPathTemplate = "/",
|
||||
UpstreamHttpMethod = new List<string> { "Get" },
|
||||
}
|
||||
},
|
||||
GlobalConfiguration = new FileGlobalConfiguration()
|
||||
{
|
||||
ServiceDiscoveryProvider = new FileServiceDiscoveryProvider()
|
||||
{
|
||||
Host = "localhost",
|
||||
Port = 9500
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
var fakeConsulServiceDiscoveryUrl = "http://localhost:9500";
|
||||
|
||||
this.Given(x => GivenThereIsAFakeConsulServiceDiscoveryProvider(fakeConsulServiceDiscoveryUrl))
|
||||
.And(x => x.GivenThereIsAServiceRunningOn("http://localhost:51779", "", 200, "Hello from Laura"))
|
||||
.And(x => _steps.GivenThereIsAConfiguration(configuration))
|
||||
.And(x => _steps.GivenOcelotIsRunningUsingConsulToStoreConfig())
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
|
||||
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
|
||||
.And(x => _steps.ThenTheResponseBodyShouldBe("Hello from Laura"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_response_200_with_simple_url_when_using_jsonserialized_cache()
|
||||
{
|
||||
var configuration = new FileConfiguration
|
||||
{
|
||||
ReRoutes = new List<FileReRoute>
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamPathTemplate = "/",
|
||||
DownstreamScheme = "http",
|
||||
DownstreamHost = "localhost",
|
||||
DownstreamPort = 51779,
|
||||
UpstreamPathTemplate = "/",
|
||||
UpstreamHttpMethod = new List<string> { "Get" },
|
||||
}
|
||||
},
|
||||
GlobalConfiguration = new FileGlobalConfiguration()
|
||||
{
|
||||
ServiceDiscoveryProvider = new FileServiceDiscoveryProvider()
|
||||
{
|
||||
Host = "localhost",
|
||||
Port = 9502
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
var fakeConsulServiceDiscoveryUrl = "http://localhost:9502";
|
||||
|
||||
this.Given(x => GivenThereIsAFakeConsulServiceDiscoveryProvider(fakeConsulServiceDiscoveryUrl))
|
||||
.And(x => x.GivenThereIsAServiceRunningOn("http://localhost:51779", "", 200, "Hello from Laura"))
|
||||
.And(x => _steps.GivenThereIsAConfiguration(configuration))
|
||||
.And(x => _steps.GivenOcelotIsRunningUsingConsulToStoreConfigAndJsonSerializedCache())
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
|
||||
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
|
||||
.And(x => _steps.ThenTheResponseBodyShouldBe("Hello from Laura"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_load_configuration_out_of_consul()
|
||||
{
|
||||
var consulPort = 8500;
|
||||
var configuration = new FileConfiguration
|
||||
{
|
||||
GlobalConfiguration = new FileGlobalConfiguration()
|
||||
{
|
||||
ServiceDiscoveryProvider = new FileServiceDiscoveryProvider()
|
||||
{
|
||||
Host = "localhost",
|
||||
Port = consulPort
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
var fakeConsulServiceDiscoveryUrl = $"http://localhost:{consulPort}";
|
||||
|
||||
var consulConfig = new FileConfiguration
|
||||
{
|
||||
ReRoutes = new List<FileReRoute>
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamPathTemplate = "/status",
|
||||
DownstreamScheme = "http",
|
||||
DownstreamHost = "localhost",
|
||||
DownstreamPort = 51779,
|
||||
UpstreamPathTemplate = "/cs/status",
|
||||
UpstreamHttpMethod = new List<string> {"Get"}
|
||||
}
|
||||
},
|
||||
GlobalConfiguration = new FileGlobalConfiguration()
|
||||
{
|
||||
ServiceDiscoveryProvider = new FileServiceDiscoveryProvider()
|
||||
{
|
||||
Host = "localhost",
|
||||
Port = consulPort
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.Given(x => GivenTheConsulConfigurationIs(consulConfig))
|
||||
.And(x => GivenThereIsAFakeConsulServiceDiscoveryProvider(fakeConsulServiceDiscoveryUrl))
|
||||
.And(x => x.GivenThereIsAServiceRunningOn("http://localhost:51779", "/status", 200, "Hello from Laura"))
|
||||
.And(x => _steps.GivenThereIsAConfiguration(configuration))
|
||||
.And(x => _steps.GivenOcelotIsRunningUsingConsulToStoreConfig())
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/cs/status"))
|
||||
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
|
||||
.And(x => _steps.ThenTheResponseBodyShouldBe("Hello from Laura"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
|
||||
[Fact]
|
||||
public void should_load_configuration_out_of_consul_if_it_is_changed()
|
||||
{
|
||||
var consulPort = 8506;
|
||||
var configuration = new FileConfiguration
|
||||
{
|
||||
GlobalConfiguration = new FileGlobalConfiguration()
|
||||
{
|
||||
ServiceDiscoveryProvider = new FileServiceDiscoveryProvider()
|
||||
{
|
||||
Host = "localhost",
|
||||
Port = consulPort
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
var fakeConsulServiceDiscoveryUrl = $"http://localhost:{consulPort}";
|
||||
|
||||
var consulConfig = new FileConfiguration
|
||||
{
|
||||
ReRoutes = new List<FileReRoute>
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamPathTemplate = "/status",
|
||||
DownstreamScheme = "http",
|
||||
DownstreamHost = "localhost",
|
||||
DownstreamPort = 51779,
|
||||
UpstreamPathTemplate = "/cs/status",
|
||||
UpstreamHttpMethod = new List<string> {"Get"}
|
||||
}
|
||||
},
|
||||
GlobalConfiguration = new FileGlobalConfiguration()
|
||||
{
|
||||
ServiceDiscoveryProvider = new FileServiceDiscoveryProvider()
|
||||
{
|
||||
Host = "localhost",
|
||||
Port = consulPort
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
var secondConsulConfig = new FileConfiguration
|
||||
{
|
||||
ReRoutes = new List<FileReRoute>
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamPathTemplate = "/status",
|
||||
DownstreamScheme = "http",
|
||||
DownstreamHost = "localhost",
|
||||
DownstreamPort = 51779,
|
||||
UpstreamPathTemplate = "/cs/status/awesome",
|
||||
UpstreamHttpMethod = new List<string> {"Get"}
|
||||
}
|
||||
},
|
||||
GlobalConfiguration = new FileGlobalConfiguration()
|
||||
{
|
||||
ServiceDiscoveryProvider = new FileServiceDiscoveryProvider()
|
||||
{
|
||||
Host = "localhost",
|
||||
Port = consulPort
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.Given(x => GivenTheConsulConfigurationIs(consulConfig))
|
||||
.And(x => GivenThereIsAFakeConsulServiceDiscoveryProvider(fakeConsulServiceDiscoveryUrl))
|
||||
.And(x => x.GivenThereIsAServiceRunningOn("http://localhost:51779", "/status", 200, "Hello from Laura"))
|
||||
.And(x => _steps.GivenThereIsAConfiguration(configuration))
|
||||
.And(x => _steps.GivenOcelotIsRunningUsingConsulToStoreConfig())
|
||||
.And(x => _steps.WhenIGetUrlOnTheApiGateway("/cs/status"))
|
||||
.And(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
|
||||
.And(x => _steps.ThenTheResponseBodyShouldBe("Hello from Laura"))
|
||||
.And(x => GivenTheConsulConfigurationIs(secondConsulConfig))
|
||||
.And(x => GivenIWaitForTheConfigToReplicateToOcelot())
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/cs/status/awesome"))
|
||||
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
|
||||
.And(x => _steps.ThenTheResponseBodyShouldBe("Hello from Laura"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenIWaitForTheConfigToReplicateToOcelot()
|
||||
{
|
||||
Thread.Sleep(10000);
|
||||
}
|
||||
|
||||
private void GivenTheConsulConfigurationIs(FileConfiguration config)
|
||||
{
|
||||
_config = config;
|
||||
}
|
||||
|
||||
private void GivenThereIsAFakeConsulServiceDiscoveryProvider(string url)
|
||||
{
|
||||
_fakeConsulBuilder = new WebHostBuilder()
|
||||
.UseUrls(url)
|
||||
.UseKestrel()
|
||||
.UseContentRoot(Directory.GetCurrentDirectory())
|
||||
.UseIISIntegration()
|
||||
.UseUrls(url)
|
||||
.Configure(app =>
|
||||
{
|
||||
app.Run(async context =>
|
||||
{
|
||||
if (context.Request.Method.ToLower() == "get" && context.Request.Path.Value == "/v1/kv/OcelotConfiguration")
|
||||
{
|
||||
var json = JsonConvert.SerializeObject(_config);
|
||||
|
||||
var bytes = Encoding.UTF8.GetBytes(json);
|
||||
|
||||
var base64 = Convert.ToBase64String(bytes);
|
||||
|
||||
var kvp = new FakeConsulGetResponse(base64);
|
||||
|
||||
await context.Response.WriteJsonAsync(new FakeConsulGetResponse[] { kvp });
|
||||
}
|
||||
|
||||
else if (context.Request.Method.ToLower() == "put" && context.Request.Path.Value == "/v1/kv/OcelotConfiguration")
|
||||
{
|
||||
try
|
||||
{
|
||||
var reader = new StreamReader(context.Request.Body);
|
||||
|
||||
var json = reader.ReadToEnd();
|
||||
|
||||
_config = JsonConvert.DeserializeObject<FileConfiguration>(json);
|
||||
|
||||
var response = JsonConvert.SerializeObject(true);
|
||||
|
||||
await context.Response.WriteAsync(response);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
});
|
||||
})
|
||||
.Build();
|
||||
|
||||
_fakeConsulBuilder.Start();
|
||||
}
|
||||
|
||||
public class FakeConsulGetResponse
|
||||
{
|
||||
public FakeConsulGetResponse(string value)
|
||||
{
|
||||
Value = value;
|
||||
}
|
||||
|
||||
public int CreateIndex => 100;
|
||||
public int ModifyIndex => 200;
|
||||
public int LockIndex => 200;
|
||||
public string Key => "OcelotConfiguration";
|
||||
public int Flags => 0;
|
||||
public string Value { get; private set; }
|
||||
public string Session => "adf4238a-882b-9ddc-4a9d-5b6758e4159e";
|
||||
}
|
||||
|
||||
private void GivenThereIsAServiceRunningOn(string url, string basePath, int statusCode, string responseBody)
|
||||
{
|
||||
_builder = new WebHostBuilder()
|
||||
.UseUrls(url)
|
||||
.UseKestrel()
|
||||
.UseContentRoot(Directory.GetCurrentDirectory())
|
||||
.UseIISIntegration()
|
||||
.UseUrls(url)
|
||||
.Configure(app =>
|
||||
{
|
||||
app.UsePathBase(basePath);
|
||||
|
||||
app.Run(async context =>
|
||||
{
|
||||
context.Response.StatusCode = statusCode;
|
||||
await context.Response.WriteAsync(responseBody);
|
||||
});
|
||||
})
|
||||
.Build();
|
||||
|
||||
_builder.Start();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_builder?.Dispose();
|
||||
_steps.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Newtonsoft.Json;
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.Configuration.Builder;
|
||||
using Ocelot.Configuration.File;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.AcceptanceTests
|
||||
{
|
||||
public class ConfigurationInConsulTests : IDisposable
|
||||
{
|
||||
private IWebHost _builder;
|
||||
private readonly Steps _steps;
|
||||
private IWebHost _fakeConsulBuilder;
|
||||
private FileConfiguration _config;
|
||||
|
||||
public ConfigurationInConsulTests()
|
||||
{
|
||||
_steps = new Steps();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_response_200_with_simple_url()
|
||||
{
|
||||
var configuration = new FileConfiguration
|
||||
{
|
||||
ReRoutes = new List<FileReRoute>
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamPathTemplate = "/",
|
||||
DownstreamScheme = "http",
|
||||
DownstreamHostAndPorts = new List<FileHostAndPort>
|
||||
{
|
||||
new FileHostAndPort
|
||||
{
|
||||
Host = "localhost",
|
||||
Port = 51779,
|
||||
}
|
||||
},
|
||||
UpstreamPathTemplate = "/",
|
||||
UpstreamHttpMethod = new List<string> { "Get" },
|
||||
}
|
||||
},
|
||||
GlobalConfiguration = new FileGlobalConfiguration()
|
||||
{
|
||||
ServiceDiscoveryProvider = new FileServiceDiscoveryProvider()
|
||||
{
|
||||
Host = "localhost",
|
||||
Port = 9500
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
var fakeConsulServiceDiscoveryUrl = "http://localhost:9500";
|
||||
|
||||
this.Given(x => GivenThereIsAFakeConsulServiceDiscoveryProvider(fakeConsulServiceDiscoveryUrl))
|
||||
.And(x => x.GivenThereIsAServiceRunningOn("http://localhost:51779", "", 200, "Hello from Laura"))
|
||||
.And(x => _steps.GivenThereIsAConfiguration(configuration))
|
||||
.And(x => _steps.GivenOcelotIsRunningUsingConsulToStoreConfig())
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
|
||||
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
|
||||
.And(x => _steps.ThenTheResponseBodyShouldBe("Hello from Laura"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_response_200_with_simple_url_when_using_jsonserialized_cache()
|
||||
{
|
||||
var configuration = new FileConfiguration
|
||||
{
|
||||
ReRoutes = new List<FileReRoute>
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamPathTemplate = "/",
|
||||
DownstreamScheme = "http",
|
||||
DownstreamHostAndPorts = new List<FileHostAndPort>
|
||||
{
|
||||
new FileHostAndPort
|
||||
{
|
||||
Host = "localhost",
|
||||
Port = 51779,
|
||||
}
|
||||
},
|
||||
UpstreamPathTemplate = "/",
|
||||
UpstreamHttpMethod = new List<string> { "Get" },
|
||||
}
|
||||
},
|
||||
GlobalConfiguration = new FileGlobalConfiguration()
|
||||
{
|
||||
ServiceDiscoveryProvider = new FileServiceDiscoveryProvider()
|
||||
{
|
||||
Host = "localhost",
|
||||
Port = 9502
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
var fakeConsulServiceDiscoveryUrl = "http://localhost:9502";
|
||||
|
||||
this.Given(x => GivenThereIsAFakeConsulServiceDiscoveryProvider(fakeConsulServiceDiscoveryUrl))
|
||||
.And(x => x.GivenThereIsAServiceRunningOn("http://localhost:51779", "", 200, "Hello from Laura"))
|
||||
.And(x => _steps.GivenThereIsAConfiguration(configuration))
|
||||
.And(x => _steps.GivenOcelotIsRunningUsingConsulToStoreConfigAndJsonSerializedCache())
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
|
||||
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
|
||||
.And(x => _steps.ThenTheResponseBodyShouldBe("Hello from Laura"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_load_configuration_out_of_consul()
|
||||
{
|
||||
var consulPort = 8500;
|
||||
var configuration = new FileConfiguration
|
||||
{
|
||||
GlobalConfiguration = new FileGlobalConfiguration()
|
||||
{
|
||||
ServiceDiscoveryProvider = new FileServiceDiscoveryProvider()
|
||||
{
|
||||
Host = "localhost",
|
||||
Port = consulPort
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
var fakeConsulServiceDiscoveryUrl = $"http://localhost:{consulPort}";
|
||||
|
||||
var consulConfig = new FileConfiguration
|
||||
{
|
||||
ReRoutes = new List<FileReRoute>
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamPathTemplate = "/status",
|
||||
DownstreamScheme = "http",
|
||||
DownstreamHostAndPorts = new List<FileHostAndPort>
|
||||
{
|
||||
new FileHostAndPort
|
||||
{
|
||||
Host = "localhost",
|
||||
Port = 51779,
|
||||
}
|
||||
},
|
||||
UpstreamPathTemplate = "/cs/status",
|
||||
UpstreamHttpMethod = new List<string> {"Get"}
|
||||
}
|
||||
},
|
||||
GlobalConfiguration = new FileGlobalConfiguration()
|
||||
{
|
||||
ServiceDiscoveryProvider = new FileServiceDiscoveryProvider()
|
||||
{
|
||||
Host = "localhost",
|
||||
Port = consulPort
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.Given(x => GivenTheConsulConfigurationIs(consulConfig))
|
||||
.And(x => GivenThereIsAFakeConsulServiceDiscoveryProvider(fakeConsulServiceDiscoveryUrl))
|
||||
.And(x => x.GivenThereIsAServiceRunningOn("http://localhost:51779", "/status", 200, "Hello from Laura"))
|
||||
.And(x => _steps.GivenThereIsAConfiguration(configuration))
|
||||
.And(x => _steps.GivenOcelotIsRunningUsingConsulToStoreConfig())
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/cs/status"))
|
||||
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
|
||||
.And(x => _steps.ThenTheResponseBodyShouldBe("Hello from Laura"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
|
||||
[Fact]
|
||||
public void should_load_configuration_out_of_consul_if_it_is_changed()
|
||||
{
|
||||
var consulPort = 8506;
|
||||
var configuration = new FileConfiguration
|
||||
{
|
||||
GlobalConfiguration = new FileGlobalConfiguration()
|
||||
{
|
||||
ServiceDiscoveryProvider = new FileServiceDiscoveryProvider()
|
||||
{
|
||||
Host = "localhost",
|
||||
Port = consulPort
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
var fakeConsulServiceDiscoveryUrl = $"http://localhost:{consulPort}";
|
||||
|
||||
var consulConfig = new FileConfiguration
|
||||
{
|
||||
ReRoutes = new List<FileReRoute>
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamPathTemplate = "/status",
|
||||
DownstreamScheme = "http",
|
||||
DownstreamHostAndPorts = new List<FileHostAndPort>
|
||||
{
|
||||
new FileHostAndPort
|
||||
{
|
||||
Host = "localhost",
|
||||
Port = 51780,
|
||||
}
|
||||
},
|
||||
UpstreamPathTemplate = "/cs/status",
|
||||
UpstreamHttpMethod = new List<string> {"Get"}
|
||||
}
|
||||
},
|
||||
GlobalConfiguration = new FileGlobalConfiguration()
|
||||
{
|
||||
ServiceDiscoveryProvider = new FileServiceDiscoveryProvider()
|
||||
{
|
||||
Host = "localhost",
|
||||
Port = consulPort
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
var secondConsulConfig = new FileConfiguration
|
||||
{
|
||||
ReRoutes = new List<FileReRoute>
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamPathTemplate = "/status",
|
||||
DownstreamScheme = "http",
|
||||
DownstreamHostAndPorts = new List<FileHostAndPort>
|
||||
{
|
||||
new FileHostAndPort
|
||||
{
|
||||
Host = "localhost",
|
||||
Port = 51780,
|
||||
}
|
||||
},
|
||||
UpstreamPathTemplate = "/cs/status/awesome",
|
||||
UpstreamHttpMethod = new List<string> {"Get"}
|
||||
}
|
||||
},
|
||||
GlobalConfiguration = new FileGlobalConfiguration()
|
||||
{
|
||||
ServiceDiscoveryProvider = new FileServiceDiscoveryProvider()
|
||||
{
|
||||
Host = "localhost",
|
||||
Port = consulPort
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.Given(x => GivenTheConsulConfigurationIs(consulConfig))
|
||||
.And(x => GivenThereIsAFakeConsulServiceDiscoveryProvider(fakeConsulServiceDiscoveryUrl))
|
||||
.And(x => x.GivenThereIsAServiceRunningOn("http://localhost:51780", "/status", 200, "Hello from Laura"))
|
||||
.And(x => _steps.GivenThereIsAConfiguration(configuration))
|
||||
.And(x => _steps.GivenOcelotIsRunningUsingConsulToStoreConfig())
|
||||
.And(x => _steps.WhenIGetUrlOnTheApiGateway("/cs/status"))
|
||||
.And(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
|
||||
.And(x => _steps.ThenTheResponseBodyShouldBe("Hello from Laura"))
|
||||
.And(x => GivenTheConsulConfigurationIs(secondConsulConfig))
|
||||
.And(x => GivenIWaitForTheConfigToReplicateToOcelot())
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/cs/status/awesome"))
|
||||
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
|
||||
.And(x => _steps.ThenTheResponseBodyShouldBe("Hello from Laura"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenIWaitForTheConfigToReplicateToOcelot()
|
||||
{
|
||||
Thread.Sleep(10000);
|
||||
}
|
||||
|
||||
private void GivenTheConsulConfigurationIs(FileConfiguration config)
|
||||
{
|
||||
_config = config;
|
||||
}
|
||||
|
||||
private void GivenThereIsAFakeConsulServiceDiscoveryProvider(string url)
|
||||
{
|
||||
_fakeConsulBuilder = new WebHostBuilder()
|
||||
.UseUrls(url)
|
||||
.UseKestrel()
|
||||
.UseContentRoot(Directory.GetCurrentDirectory())
|
||||
.UseIISIntegration()
|
||||
.UseUrls(url)
|
||||
.Configure(app =>
|
||||
{
|
||||
app.Run(async context =>
|
||||
{
|
||||
if (context.Request.Method.ToLower() == "get" && context.Request.Path.Value == "/v1/kv/OcelotConfiguration")
|
||||
{
|
||||
var json = JsonConvert.SerializeObject(_config);
|
||||
|
||||
var bytes = Encoding.UTF8.GetBytes(json);
|
||||
|
||||
var base64 = Convert.ToBase64String(bytes);
|
||||
|
||||
var kvp = new FakeConsulGetResponse(base64);
|
||||
|
||||
await context.Response.WriteJsonAsync(new FakeConsulGetResponse[] { kvp });
|
||||
}
|
||||
|
||||
else if (context.Request.Method.ToLower() == "put" && context.Request.Path.Value == "/v1/kv/OcelotConfiguration")
|
||||
{
|
||||
try
|
||||
{
|
||||
var reader = new StreamReader(context.Request.Body);
|
||||
|
||||
var json = reader.ReadToEnd();
|
||||
|
||||
_config = JsonConvert.DeserializeObject<FileConfiguration>(json);
|
||||
|
||||
var response = JsonConvert.SerializeObject(true);
|
||||
|
||||
await context.Response.WriteAsync(response);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
});
|
||||
})
|
||||
.Build();
|
||||
|
||||
_fakeConsulBuilder.Start();
|
||||
}
|
||||
|
||||
public class FakeConsulGetResponse
|
||||
{
|
||||
public FakeConsulGetResponse(string value)
|
||||
{
|
||||
Value = value;
|
||||
}
|
||||
|
||||
public int CreateIndex => 100;
|
||||
public int ModifyIndex => 200;
|
||||
public int LockIndex => 200;
|
||||
public string Key => "OcelotConfiguration";
|
||||
public int Flags => 0;
|
||||
public string Value { get; private set; }
|
||||
public string Session => "adf4238a-882b-9ddc-4a9d-5b6758e4159e";
|
||||
}
|
||||
|
||||
private void GivenThereIsAServiceRunningOn(string url, string basePath, int statusCode, string responseBody)
|
||||
{
|
||||
_builder = new WebHostBuilder()
|
||||
.UseUrls(url)
|
||||
.UseKestrel()
|
||||
.UseContentRoot(Directory.GetCurrentDirectory())
|
||||
.UseIISIntegration()
|
||||
.UseUrls(url)
|
||||
.Configure(app =>
|
||||
{
|
||||
app.UsePathBase(basePath);
|
||||
|
||||
app.Run(async context =>
|
||||
{
|
||||
context.Response.StatusCode = statusCode;
|
||||
await context.Response.WriteAsync(responseBody);
|
||||
});
|
||||
})
|
||||
.Build();
|
||||
|
||||
_builder.Start();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_builder?.Dispose();
|
||||
_steps.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,40 +1,40 @@
|
||||
using System;
|
||||
using CacheManager.Core;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Ocelot.DependencyInjection;
|
||||
using Ocelot.Middleware;
|
||||
using ConfigurationBuilder = Microsoft.Extensions.Configuration.ConfigurationBuilder;
|
||||
|
||||
namespace Ocelot.AcceptanceTests
|
||||
{
|
||||
public class ConsulStartup
|
||||
{
|
||||
public ConsulStartup(IHostingEnvironment env)
|
||||
{
|
||||
var builder = new ConfigurationBuilder()
|
||||
.SetBasePath(env.ContentRootPath)
|
||||
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
|
||||
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
|
||||
.AddJsonFile("configuration.json")
|
||||
.AddEnvironmentVariables();
|
||||
|
||||
Config = builder.Build();
|
||||
}
|
||||
|
||||
public static IConfiguration Config { get; private set; }
|
||||
|
||||
public void ConfigureServices(IServiceCollection services)
|
||||
{
|
||||
services.AddOcelot(Config).AddStoreOcelotConfigurationInConsul();
|
||||
}
|
||||
|
||||
public void Configure(IApplicationBuilder app)
|
||||
{
|
||||
app.UseOcelot().Wait();
|
||||
}
|
||||
}
|
||||
}
|
||||
using System;
|
||||
using CacheManager.Core;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Ocelot.DependencyInjection;
|
||||
using Ocelot.Middleware;
|
||||
using ConfigurationBuilder = Microsoft.Extensions.Configuration.ConfigurationBuilder;
|
||||
|
||||
namespace Ocelot.AcceptanceTests
|
||||
{
|
||||
public class ConsulStartup
|
||||
{
|
||||
public ConsulStartup(IHostingEnvironment env)
|
||||
{
|
||||
var builder = new ConfigurationBuilder()
|
||||
.SetBasePath(env.ContentRootPath)
|
||||
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
|
||||
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
|
||||
.AddJsonFile("configuration.json")
|
||||
.AddEnvironmentVariables();
|
||||
|
||||
Config = builder.Build();
|
||||
}
|
||||
|
||||
public static IConfiguration Config { get; private set; }
|
||||
|
||||
public void ConfigureServices(IServiceCollection services)
|
||||
{
|
||||
services.AddOcelot(Config).AddStoreOcelotConfigurationInConsul();
|
||||
}
|
||||
|
||||
public void Configure(IApplicationBuilder app)
|
||||
{
|
||||
app.UseOcelot().Wait();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,284 +1,320 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Ocelot.Configuration.File;
|
||||
using Ocelot.Middleware;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.AcceptanceTests
|
||||
{
|
||||
public class CustomMiddlewareTests : IDisposable
|
||||
{
|
||||
private readonly string _configurationPath;
|
||||
private IWebHost _builder;
|
||||
private readonly Steps _steps;
|
||||
private int _counter;
|
||||
|
||||
public CustomMiddlewareTests()
|
||||
{
|
||||
_counter = 0;
|
||||
_steps = new Steps();;
|
||||
_configurationPath = "configuration.json";
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_call_pre_query_string_builder_middleware()
|
||||
{
|
||||
var configuration = new OcelotMiddlewareConfiguration
|
||||
{
|
||||
AuthorisationMiddleware = async (ctx, next) =>
|
||||
{
|
||||
_counter++;
|
||||
await next.Invoke();
|
||||
}
|
||||
};
|
||||
|
||||
var fileConfiguration = new FileConfiguration
|
||||
{
|
||||
ReRoutes = new List<FileReRoute>
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamPathTemplate = "/",
|
||||
DownstreamPort = 41879,
|
||||
DownstreamScheme = "http",
|
||||
DownstreamHost = "localhost",
|
||||
UpstreamPathTemplate = "/",
|
||||
UpstreamHttpMethod = new List<string> { "Get" },
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:41879", 200))
|
||||
.And(x => _steps.GivenThereIsAConfiguration(fileConfiguration, _configurationPath))
|
||||
.And(x => _steps.GivenOcelotIsRunning(configuration))
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
|
||||
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
|
||||
.And(x => x.ThenTheCounterIs(1))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_call_authorisation_middleware()
|
||||
{
|
||||
var configuration = new OcelotMiddlewareConfiguration
|
||||
{
|
||||
AuthorisationMiddleware = async (ctx, next) =>
|
||||
{
|
||||
_counter++;
|
||||
await next.Invoke();
|
||||
}
|
||||
};
|
||||
|
||||
var fileConfiguration = new FileConfiguration
|
||||
{
|
||||
ReRoutes = new List<FileReRoute>
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamPathTemplate = "/",
|
||||
DownstreamPort = 41879,
|
||||
DownstreamScheme = "http",
|
||||
DownstreamHost = "localhost",
|
||||
UpstreamPathTemplate = "/",
|
||||
UpstreamHttpMethod = new List<string> { "Get" },
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:41879", 200))
|
||||
.And(x => _steps.GivenThereIsAConfiguration(fileConfiguration, _configurationPath))
|
||||
.And(x => _steps.GivenOcelotIsRunning(configuration))
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
|
||||
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
|
||||
.And(x => x.ThenTheCounterIs(1))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_call_authentication_middleware()
|
||||
{
|
||||
var configuration = new OcelotMiddlewareConfiguration
|
||||
{
|
||||
AuthenticationMiddleware = async (ctx, next) =>
|
||||
{
|
||||
_counter++;
|
||||
await next.Invoke();
|
||||
}
|
||||
};
|
||||
|
||||
var fileConfiguration = new FileConfiguration
|
||||
{
|
||||
ReRoutes = new List<FileReRoute>
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamPathTemplate = "/41879/",
|
||||
DownstreamPort = 41879,
|
||||
DownstreamScheme = "http",
|
||||
DownstreamHost = "localhost",
|
||||
UpstreamPathTemplate = "/",
|
||||
UpstreamHttpMethod = new List<string> { "Get" },
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:41879", 200))
|
||||
.And(x => _steps.GivenThereIsAConfiguration(fileConfiguration, _configurationPath))
|
||||
.And(x => _steps.GivenOcelotIsRunning(configuration))
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
|
||||
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
|
||||
.And(x => x.ThenTheCounterIs(1))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_call_pre_error_middleware()
|
||||
{
|
||||
var configuration = new OcelotMiddlewareConfiguration
|
||||
{
|
||||
PreErrorResponderMiddleware = async (ctx, next) =>
|
||||
{
|
||||
_counter++;
|
||||
await next.Invoke();
|
||||
}
|
||||
};
|
||||
|
||||
var fileConfiguration = new FileConfiguration
|
||||
{
|
||||
ReRoutes = new List<FileReRoute>
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamPathTemplate = "/",
|
||||
DownstreamPort = 41879,
|
||||
DownstreamScheme = "http",
|
||||
DownstreamHost = "localhost",
|
||||
UpstreamPathTemplate = "/",
|
||||
UpstreamHttpMethod = new List<string> { "Get" },
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:41879", 200))
|
||||
.And(x => _steps.GivenThereIsAConfiguration(fileConfiguration, _configurationPath))
|
||||
.And(x => _steps.GivenOcelotIsRunning(configuration))
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
|
||||
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
|
||||
.And(x => x.ThenTheCounterIs(1))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_call_pre_authorisation_middleware()
|
||||
{
|
||||
var configuration = new OcelotMiddlewareConfiguration
|
||||
{
|
||||
PreAuthorisationMiddleware = async (ctx, next) =>
|
||||
{
|
||||
_counter++;
|
||||
await next.Invoke();
|
||||
}
|
||||
};
|
||||
|
||||
var fileConfiguration = new FileConfiguration
|
||||
{
|
||||
ReRoutes = new List<FileReRoute>
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamPathTemplate = "/",
|
||||
DownstreamPort = 41879,
|
||||
DownstreamScheme = "http",
|
||||
DownstreamHost = "localhost",
|
||||
UpstreamPathTemplate = "/",
|
||||
UpstreamHttpMethod = new List<string> { "Get" },
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:41879", 200))
|
||||
.And(x => _steps.GivenThereIsAConfiguration(fileConfiguration, _configurationPath))
|
||||
.And(x => _steps.GivenOcelotIsRunning(configuration))
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
|
||||
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
|
||||
.And(x => x.ThenTheCounterIs(1))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_call_pre_http_authentication_middleware()
|
||||
{
|
||||
var configuration = new OcelotMiddlewareConfiguration
|
||||
{
|
||||
PreAuthenticationMiddleware = async (ctx, next) =>
|
||||
{
|
||||
_counter++;
|
||||
await next.Invoke();
|
||||
}
|
||||
};
|
||||
|
||||
var fileConfiguration = new FileConfiguration
|
||||
{
|
||||
ReRoutes = new List<FileReRoute>
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamPathTemplate = "/",
|
||||
DownstreamPort = 41879,
|
||||
DownstreamScheme = "http",
|
||||
DownstreamHost = "localhost",
|
||||
UpstreamPathTemplate = "/",
|
||||
UpstreamHttpMethod = new List<string> { "Get" },
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:41879", 200))
|
||||
.And(x => _steps.GivenThereIsAConfiguration(fileConfiguration, _configurationPath))
|
||||
.And(x => _steps.GivenOcelotIsRunning(configuration))
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
|
||||
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
|
||||
.And(x => x.ThenTheCounterIs(1))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void ThenTheCounterIs(int expected)
|
||||
{
|
||||
_counter.ShouldBe(expected);
|
||||
}
|
||||
|
||||
private void GivenThereIsAServiceRunningOn(string url, int statusCode)
|
||||
{
|
||||
_builder = new WebHostBuilder()
|
||||
.UseUrls(url)
|
||||
.UseKestrel()
|
||||
.UseContentRoot(Directory.GetCurrentDirectory())
|
||||
.UseIISIntegration()
|
||||
.UseUrls(url)
|
||||
.Configure(app =>
|
||||
{
|
||||
app.Run(context =>
|
||||
{
|
||||
context.Response.StatusCode = statusCode;
|
||||
return Task.CompletedTask;
|
||||
});
|
||||
})
|
||||
.Build();
|
||||
|
||||
_builder.Start();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_builder?.Dispose();
|
||||
_steps.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Ocelot.Configuration.File;
|
||||
using Ocelot.Middleware;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.AcceptanceTests
|
||||
{
|
||||
public class CustomMiddlewareTests : IDisposable
|
||||
{
|
||||
private readonly string _configurationPath;
|
||||
private IWebHost _builder;
|
||||
private readonly Steps _steps;
|
||||
private int _counter;
|
||||
|
||||
public CustomMiddlewareTests()
|
||||
{
|
||||
_counter = 0;
|
||||
_steps = new Steps();;
|
||||
_configurationPath = "configuration.json";
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_call_pre_query_string_builder_middleware()
|
||||
{
|
||||
var configuration = new OcelotMiddlewareConfiguration
|
||||
{
|
||||
AuthorisationMiddleware = async (ctx, next) =>
|
||||
{
|
||||
_counter++;
|
||||
await next.Invoke();
|
||||
}
|
||||
};
|
||||
|
||||
var fileConfiguration = new FileConfiguration
|
||||
{
|
||||
ReRoutes = new List<FileReRoute>
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamPathTemplate = "/",
|
||||
DownstreamHostAndPorts = new List<FileHostAndPort>
|
||||
{
|
||||
new FileHostAndPort
|
||||
{
|
||||
Host = "localhost",
|
||||
Port = 41879,
|
||||
}
|
||||
},
|
||||
DownstreamScheme = "http",
|
||||
UpstreamPathTemplate = "/",
|
||||
UpstreamHttpMethod = new List<string> { "Get" },
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:41879", 200))
|
||||
.And(x => _steps.GivenThereIsAConfiguration(fileConfiguration, _configurationPath))
|
||||
.And(x => _steps.GivenOcelotIsRunning(configuration))
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
|
||||
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
|
||||
.And(x => x.ThenTheCounterIs(1))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_call_authorisation_middleware()
|
||||
{
|
||||
var configuration = new OcelotMiddlewareConfiguration
|
||||
{
|
||||
AuthorisationMiddleware = async (ctx, next) =>
|
||||
{
|
||||
_counter++;
|
||||
await next.Invoke();
|
||||
}
|
||||
};
|
||||
|
||||
var fileConfiguration = new FileConfiguration
|
||||
{
|
||||
ReRoutes = new List<FileReRoute>
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamPathTemplate = "/",
|
||||
DownstreamHostAndPorts = new List<FileHostAndPort>
|
||||
{
|
||||
new FileHostAndPort
|
||||
{
|
||||
Host = "localhost",
|
||||
Port = 41879,
|
||||
}
|
||||
},
|
||||
DownstreamScheme = "http",
|
||||
UpstreamPathTemplate = "/",
|
||||
UpstreamHttpMethod = new List<string> { "Get" },
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:41879", 200))
|
||||
.And(x => _steps.GivenThereIsAConfiguration(fileConfiguration, _configurationPath))
|
||||
.And(x => _steps.GivenOcelotIsRunning(configuration))
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
|
||||
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
|
||||
.And(x => x.ThenTheCounterIs(1))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_call_authentication_middleware()
|
||||
{
|
||||
var configuration = new OcelotMiddlewareConfiguration
|
||||
{
|
||||
AuthenticationMiddleware = async (ctx, next) =>
|
||||
{
|
||||
_counter++;
|
||||
await next.Invoke();
|
||||
}
|
||||
};
|
||||
|
||||
var fileConfiguration = new FileConfiguration
|
||||
{
|
||||
ReRoutes = new List<FileReRoute>
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamPathTemplate = "/41879/",
|
||||
DownstreamHostAndPorts = new List<FileHostAndPort>
|
||||
{
|
||||
new FileHostAndPort
|
||||
{
|
||||
Host = "localhost",
|
||||
Port = 41879,
|
||||
}
|
||||
},
|
||||
DownstreamScheme = "http",
|
||||
UpstreamPathTemplate = "/",
|
||||
UpstreamHttpMethod = new List<string> { "Get" },
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:41879", 200))
|
||||
.And(x => _steps.GivenThereIsAConfiguration(fileConfiguration, _configurationPath))
|
||||
.And(x => _steps.GivenOcelotIsRunning(configuration))
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
|
||||
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
|
||||
.And(x => x.ThenTheCounterIs(1))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_call_pre_error_middleware()
|
||||
{
|
||||
var configuration = new OcelotMiddlewareConfiguration
|
||||
{
|
||||
PreErrorResponderMiddleware = async (ctx, next) =>
|
||||
{
|
||||
_counter++;
|
||||
await next.Invoke();
|
||||
}
|
||||
};
|
||||
|
||||
var fileConfiguration = new FileConfiguration
|
||||
{
|
||||
ReRoutes = new List<FileReRoute>
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamPathTemplate = "/",
|
||||
DownstreamHostAndPorts = new List<FileHostAndPort>
|
||||
{
|
||||
new FileHostAndPort
|
||||
{
|
||||
Host = "localhost",
|
||||
Port = 41879,
|
||||
}
|
||||
},
|
||||
DownstreamScheme = "http",
|
||||
UpstreamPathTemplate = "/",
|
||||
UpstreamHttpMethod = new List<string> { "Get" },
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:41879", 200))
|
||||
.And(x => _steps.GivenThereIsAConfiguration(fileConfiguration, _configurationPath))
|
||||
.And(x => _steps.GivenOcelotIsRunning(configuration))
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
|
||||
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
|
||||
.And(x => x.ThenTheCounterIs(1))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_call_pre_authorisation_middleware()
|
||||
{
|
||||
var configuration = new OcelotMiddlewareConfiguration
|
||||
{
|
||||
PreAuthorisationMiddleware = async (ctx, next) =>
|
||||
{
|
||||
_counter++;
|
||||
await next.Invoke();
|
||||
}
|
||||
};
|
||||
|
||||
var fileConfiguration = new FileConfiguration
|
||||
{
|
||||
ReRoutes = new List<FileReRoute>
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamPathTemplate = "/",
|
||||
DownstreamHostAndPorts = new List<FileHostAndPort>
|
||||
{
|
||||
new FileHostAndPort
|
||||
{
|
||||
Host = "localhost",
|
||||
Port = 41879,
|
||||
}
|
||||
},
|
||||
DownstreamScheme = "http",
|
||||
UpstreamPathTemplate = "/",
|
||||
UpstreamHttpMethod = new List<string> { "Get" },
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:41879", 200))
|
||||
.And(x => _steps.GivenThereIsAConfiguration(fileConfiguration, _configurationPath))
|
||||
.And(x => _steps.GivenOcelotIsRunning(configuration))
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
|
||||
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
|
||||
.And(x => x.ThenTheCounterIs(1))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_call_pre_http_authentication_middleware()
|
||||
{
|
||||
var configuration = new OcelotMiddlewareConfiguration
|
||||
{
|
||||
PreAuthenticationMiddleware = async (ctx, next) =>
|
||||
{
|
||||
_counter++;
|
||||
await next.Invoke();
|
||||
}
|
||||
};
|
||||
|
||||
var fileConfiguration = new FileConfiguration
|
||||
{
|
||||
ReRoutes = new List<FileReRoute>
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamPathTemplate = "/",
|
||||
DownstreamHostAndPorts = new List<FileHostAndPort>
|
||||
{
|
||||
new FileHostAndPort
|
||||
{
|
||||
Host = "localhost",
|
||||
Port = 41879,
|
||||
}
|
||||
},
|
||||
DownstreamScheme = "http",
|
||||
UpstreamPathTemplate = "/",
|
||||
UpstreamHttpMethod = new List<string> { "Get" },
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:41879", 200))
|
||||
.And(x => _steps.GivenThereIsAConfiguration(fileConfiguration, _configurationPath))
|
||||
.And(x => _steps.GivenOcelotIsRunning(configuration))
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
|
||||
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
|
||||
.And(x => x.ThenTheCounterIs(1))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void ThenTheCounterIs(int expected)
|
||||
{
|
||||
_counter.ShouldBe(expected);
|
||||
}
|
||||
|
||||
private void GivenThereIsAServiceRunningOn(string url, int statusCode)
|
||||
{
|
||||
_builder = new WebHostBuilder()
|
||||
.UseUrls(url)
|
||||
.UseKestrel()
|
||||
.UseContentRoot(Directory.GetCurrentDirectory())
|
||||
.UseIISIntegration()
|
||||
.UseUrls(url)
|
||||
.Configure(app =>
|
||||
{
|
||||
app.Run(context =>
|
||||
{
|
||||
context.Response.StatusCode = statusCode;
|
||||
return Task.CompletedTask;
|
||||
});
|
||||
})
|
||||
.Build();
|
||||
|
||||
_builder.Start();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_builder?.Dispose();
|
||||
_steps.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,226 +1,250 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Ocelot.Configuration.File;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.AcceptanceTests
|
||||
{
|
||||
public class HeaderTests : IDisposable
|
||||
{
|
||||
private IWebHost _builder;
|
||||
private readonly Steps _steps;
|
||||
private string _downstreamPath;
|
||||
|
||||
public HeaderTests()
|
||||
{
|
||||
_steps = new Steps();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_transform_upstream_header()
|
||||
{
|
||||
var configuration = new FileConfiguration
|
||||
{
|
||||
ReRoutes = new List<FileReRoute>
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamPathTemplate = "/",
|
||||
DownstreamScheme = "http",
|
||||
DownstreamHost = "localhost",
|
||||
DownstreamPort = 51879,
|
||||
UpstreamPathTemplate = "/",
|
||||
UpstreamHttpMethod = new List<string> { "Get" },
|
||||
UpstreamHeaderTransform = new Dictionary<string,string>
|
||||
{
|
||||
{"Laz", "D, GP"}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:51879", "/", 200, "Laz"))
|
||||
.And(x => _steps.GivenThereIsAConfiguration(configuration))
|
||||
.And(x => _steps.GivenOcelotIsRunning())
|
||||
.And(x => _steps.GivenIAddAHeader("Laz", "D"))
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
|
||||
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
|
||||
.And(x => _steps.ThenTheResponseBodyShouldBe("GP"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_transform_downstream_header()
|
||||
{
|
||||
var configuration = new FileConfiguration
|
||||
{
|
||||
ReRoutes = new List<FileReRoute>
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamPathTemplate = "/",
|
||||
DownstreamScheme = "http",
|
||||
DownstreamHost = "localhost",
|
||||
DownstreamPort = 51879,
|
||||
UpstreamPathTemplate = "/",
|
||||
UpstreamHttpMethod = new List<string> { "Get" },
|
||||
DownstreamHeaderTransform = new Dictionary<string,string>
|
||||
{
|
||||
{"Location", "http://www.bbc.co.uk/, http://ocelot.com/"}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:51879", "/", 200, "Location", "http://www.bbc.co.uk/"))
|
||||
.And(x => _steps.GivenThereIsAConfiguration(configuration))
|
||||
.And(x => _steps.GivenOcelotIsRunning())
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
|
||||
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
|
||||
.And(x => _steps.ThenTheResponseHeaderIs("Location", "http://ocelot.com/"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_fix_issue_190()
|
||||
{
|
||||
var configuration = new FileConfiguration
|
||||
{
|
||||
ReRoutes = new List<FileReRoute>
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamPathTemplate = "/",
|
||||
DownstreamScheme = "http",
|
||||
DownstreamHost = "localhost",
|
||||
DownstreamPort = 6773,
|
||||
UpstreamPathTemplate = "/",
|
||||
UpstreamHttpMethod = new List<string> { "Get" },
|
||||
DownstreamHeaderTransform = new Dictionary<string,string>
|
||||
{
|
||||
{"Location", "http://localhost:6773, {BaseUrl}"}
|
||||
},
|
||||
HttpHandlerOptions = new FileHttpHandlerOptions
|
||||
{
|
||||
AllowAutoRedirect = false
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:6773", "/", 302, "Location", "http://localhost:6773/pay/Receive"))
|
||||
.And(x => _steps.GivenThereIsAConfiguration(configuration))
|
||||
.And(x => _steps.GivenOcelotIsRunning())
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
|
||||
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.Redirect))
|
||||
.And(x => _steps.ThenTheResponseHeaderIs("Location", "http://localhost:5000/pay/Receive"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_fix_issue_205()
|
||||
{
|
||||
var configuration = new FileConfiguration
|
||||
{
|
||||
ReRoutes = new List<FileReRoute>
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamPathTemplate = "/",
|
||||
DownstreamScheme = "http",
|
||||
DownstreamHost = "localhost",
|
||||
DownstreamPort = 6773,
|
||||
UpstreamPathTemplate = "/",
|
||||
UpstreamHttpMethod = new List<string> { "Get" },
|
||||
DownstreamHeaderTransform = new Dictionary<string,string>
|
||||
{
|
||||
{"Location", "{DownstreamBaseUrl}, {BaseUrl}"}
|
||||
},
|
||||
HttpHandlerOptions = new FileHttpHandlerOptions
|
||||
{
|
||||
AllowAutoRedirect = false
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:6773", "/", 302, "Location", "http://localhost:6773/pay/Receive"))
|
||||
.And(x => _steps.GivenThereIsAConfiguration(configuration))
|
||||
.And(x => _steps.GivenOcelotIsRunning())
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
|
||||
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.Redirect))
|
||||
.And(x => _steps.ThenTheResponseHeaderIs("Location", "http://localhost:5000/pay/Receive"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
|
||||
private void GivenThereIsAServiceRunningOn(string baseUrl, string basePath, int statusCode, string headerKey)
|
||||
{
|
||||
_builder = new WebHostBuilder()
|
||||
.UseUrls(baseUrl)
|
||||
.UseKestrel()
|
||||
.UseContentRoot(Directory.GetCurrentDirectory())
|
||||
.UseIISIntegration()
|
||||
.Configure(app =>
|
||||
{
|
||||
app.UsePathBase(basePath);
|
||||
app.Run(async context =>
|
||||
{
|
||||
if(context.Request.Headers.TryGetValue(headerKey, out var values))
|
||||
{
|
||||
var result = values.First();
|
||||
context.Response.StatusCode = statusCode;
|
||||
await context.Response.WriteAsync(result);
|
||||
}
|
||||
});
|
||||
})
|
||||
.Build();
|
||||
|
||||
_builder.Start();
|
||||
}
|
||||
|
||||
private void GivenThereIsAServiceRunningOn(string baseUrl, string basePath, int statusCode, string headerKey, string headerValue)
|
||||
{
|
||||
_builder = new WebHostBuilder()
|
||||
.UseUrls(baseUrl)
|
||||
.UseKestrel()
|
||||
.UseContentRoot(Directory.GetCurrentDirectory())
|
||||
.UseIISIntegration()
|
||||
.Configure(app =>
|
||||
{
|
||||
app.UsePathBase(basePath);
|
||||
app.Run(async context =>
|
||||
{
|
||||
context.Response.OnStarting(() => {
|
||||
context.Response.Headers.Add(headerKey, headerValue);
|
||||
context.Response.StatusCode = statusCode;
|
||||
return Task.CompletedTask;
|
||||
});
|
||||
});
|
||||
})
|
||||
.Build();
|
||||
|
||||
_builder.Start();
|
||||
}
|
||||
|
||||
internal void ThenTheDownstreamUrlPathShouldBe(string expectedDownstreamPath)
|
||||
{
|
||||
_downstreamPath.ShouldBe(expectedDownstreamPath);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_builder?.Dispose();
|
||||
_steps.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Ocelot.Configuration.File;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.AcceptanceTests
|
||||
{
|
||||
public class HeaderTests : IDisposable
|
||||
{
|
||||
private IWebHost _builder;
|
||||
private readonly Steps _steps;
|
||||
private string _downstreamPath;
|
||||
|
||||
public HeaderTests()
|
||||
{
|
||||
_steps = new Steps();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_transform_upstream_header()
|
||||
{
|
||||
var configuration = new FileConfiguration
|
||||
{
|
||||
ReRoutes = new List<FileReRoute>
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamPathTemplate = "/",
|
||||
DownstreamScheme = "http",
|
||||
DownstreamHostAndPorts = new List<FileHostAndPort>
|
||||
{
|
||||
new FileHostAndPort
|
||||
{
|
||||
Host = "localhost",
|
||||
Port = 51879,
|
||||
}
|
||||
},
|
||||
UpstreamPathTemplate = "/",
|
||||
UpstreamHttpMethod = new List<string> { "Get" },
|
||||
UpstreamHeaderTransform = new Dictionary<string,string>
|
||||
{
|
||||
{"Laz", "D, GP"}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:51879", "/", 200, "Laz"))
|
||||
.And(x => _steps.GivenThereIsAConfiguration(configuration))
|
||||
.And(x => _steps.GivenOcelotIsRunning())
|
||||
.And(x => _steps.GivenIAddAHeader("Laz", "D"))
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
|
||||
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
|
||||
.And(x => _steps.ThenTheResponseBodyShouldBe("GP"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_transform_downstream_header()
|
||||
{
|
||||
var configuration = new FileConfiguration
|
||||
{
|
||||
ReRoutes = new List<FileReRoute>
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamPathTemplate = "/",
|
||||
DownstreamScheme = "http",
|
||||
DownstreamHostAndPorts = new List<FileHostAndPort>
|
||||
{
|
||||
new FileHostAndPort
|
||||
{
|
||||
Host = "localhost",
|
||||
Port = 51879,
|
||||
}
|
||||
},
|
||||
UpstreamPathTemplate = "/",
|
||||
UpstreamHttpMethod = new List<string> { "Get" },
|
||||
DownstreamHeaderTransform = new Dictionary<string,string>
|
||||
{
|
||||
{"Location", "http://www.bbc.co.uk/, http://ocelot.com/"}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:51879", "/", 200, "Location", "http://www.bbc.co.uk/"))
|
||||
.And(x => _steps.GivenThereIsAConfiguration(configuration))
|
||||
.And(x => _steps.GivenOcelotIsRunning())
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
|
||||
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
|
||||
.And(x => _steps.ThenTheResponseHeaderIs("Location", "http://ocelot.com/"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_fix_issue_190()
|
||||
{
|
||||
var configuration = new FileConfiguration
|
||||
{
|
||||
ReRoutes = new List<FileReRoute>
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamPathTemplate = "/",
|
||||
DownstreamScheme = "http",
|
||||
DownstreamHostAndPorts = new List<FileHostAndPort>
|
||||
{
|
||||
new FileHostAndPort
|
||||
{
|
||||
Host = "localhost",
|
||||
Port = 6773,
|
||||
}
|
||||
},
|
||||
UpstreamPathTemplate = "/",
|
||||
UpstreamHttpMethod = new List<string> { "Get" },
|
||||
DownstreamHeaderTransform = new Dictionary<string,string>
|
||||
{
|
||||
{"Location", "http://localhost:6773, {BaseUrl}"}
|
||||
},
|
||||
HttpHandlerOptions = new FileHttpHandlerOptions
|
||||
{
|
||||
AllowAutoRedirect = false
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:6773", "/", 302, "Location", "http://localhost:6773/pay/Receive"))
|
||||
.And(x => _steps.GivenThereIsAConfiguration(configuration))
|
||||
.And(x => _steps.GivenOcelotIsRunning())
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
|
||||
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.Redirect))
|
||||
.And(x => _steps.ThenTheResponseHeaderIs("Location", "http://localhost:5000/pay/Receive"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_fix_issue_205()
|
||||
{
|
||||
var configuration = new FileConfiguration
|
||||
{
|
||||
ReRoutes = new List<FileReRoute>
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamPathTemplate = "/",
|
||||
DownstreamScheme = "http",
|
||||
DownstreamHostAndPorts = new List<FileHostAndPort>
|
||||
{
|
||||
new FileHostAndPort
|
||||
{
|
||||
Host = "localhost",
|
||||
Port = 6773,
|
||||
}
|
||||
},
|
||||
UpstreamPathTemplate = "/",
|
||||
UpstreamHttpMethod = new List<string> { "Get" },
|
||||
DownstreamHeaderTransform = new Dictionary<string,string>
|
||||
{
|
||||
{"Location", "{DownstreamBaseUrl}, {BaseUrl}"}
|
||||
},
|
||||
HttpHandlerOptions = new FileHttpHandlerOptions
|
||||
{
|
||||
AllowAutoRedirect = false
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:6773", "/", 302, "Location", "http://localhost:6773/pay/Receive"))
|
||||
.And(x => _steps.GivenThereIsAConfiguration(configuration))
|
||||
.And(x => _steps.GivenOcelotIsRunning())
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
|
||||
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.Redirect))
|
||||
.And(x => _steps.ThenTheResponseHeaderIs("Location", "http://localhost:5000/pay/Receive"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
|
||||
private void GivenThereIsAServiceRunningOn(string baseUrl, string basePath, int statusCode, string headerKey)
|
||||
{
|
||||
_builder = new WebHostBuilder()
|
||||
.UseUrls(baseUrl)
|
||||
.UseKestrel()
|
||||
.UseContentRoot(Directory.GetCurrentDirectory())
|
||||
.UseIISIntegration()
|
||||
.Configure(app =>
|
||||
{
|
||||
app.UsePathBase(basePath);
|
||||
app.Run(async context =>
|
||||
{
|
||||
if(context.Request.Headers.TryGetValue(headerKey, out var values))
|
||||
{
|
||||
var result = values.First();
|
||||
context.Response.StatusCode = statusCode;
|
||||
await context.Response.WriteAsync(result);
|
||||
}
|
||||
});
|
||||
})
|
||||
.Build();
|
||||
|
||||
_builder.Start();
|
||||
}
|
||||
|
||||
private void GivenThereIsAServiceRunningOn(string baseUrl, string basePath, int statusCode, string headerKey, string headerValue)
|
||||
{
|
||||
_builder = new WebHostBuilder()
|
||||
.UseUrls(baseUrl)
|
||||
.UseKestrel()
|
||||
.UseContentRoot(Directory.GetCurrentDirectory())
|
||||
.UseIISIntegration()
|
||||
.Configure(app =>
|
||||
{
|
||||
app.UsePathBase(basePath);
|
||||
app.Run(async context =>
|
||||
{
|
||||
context.Response.OnStarting(() => {
|
||||
context.Response.Headers.Add(headerKey, headerValue);
|
||||
context.Response.StatusCode = statusCode;
|
||||
return Task.CompletedTask;
|
||||
});
|
||||
});
|
||||
})
|
||||
.Build();
|
||||
|
||||
_builder.Start();
|
||||
}
|
||||
|
||||
internal void ThenTheDownstreamUrlPathShouldBe(string expectedDownstreamPath)
|
||||
{
|
||||
_downstreamPath.ShouldBe(expectedDownstreamPath);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_builder?.Dispose();
|
||||
_steps.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
178
test/Ocelot.AcceptanceTests/LoadBalancerTests.cs
Normal file
178
test/Ocelot.AcceptanceTests/LoadBalancerTests.cs
Normal file
@ -0,0 +1,178 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using Consul;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Ocelot.Configuration.File;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.AcceptanceTests
|
||||
{
|
||||
public class LoadBalancerTests : IDisposable
|
||||
{
|
||||
private IWebHost _builderOne;
|
||||
private IWebHost _builderTwo;
|
||||
private readonly Steps _steps;
|
||||
private int _counterOne;
|
||||
private int _counterTwo;
|
||||
private static readonly object _syncLock = new object();
|
||||
|
||||
public LoadBalancerTests()
|
||||
{
|
||||
_steps = new Steps();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_use_service_discovery_and_load_balance_request()
|
||||
{
|
||||
var downstreamServiceOneUrl = "http://localhost:50881";
|
||||
var downstreamServiceTwoUrl = "http://localhost:50882";
|
||||
|
||||
var configuration = new FileConfiguration
|
||||
{
|
||||
ReRoutes = new List<FileReRoute>
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamPathTemplate = "/",
|
||||
DownstreamScheme = "http",
|
||||
UpstreamPathTemplate = "/",
|
||||
UpstreamHttpMethod = new List<string> { "Get" },
|
||||
LoadBalancer = "LeastConnection",
|
||||
DownstreamHostAndPorts = new List<FileHostAndPort>
|
||||
{
|
||||
new FileHostAndPort
|
||||
{
|
||||
Host = "localhost",
|
||||
Port = 50881
|
||||
},
|
||||
new FileHostAndPort
|
||||
{
|
||||
Host = "localhost",
|
||||
Port = 50882
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
GlobalConfiguration = new FileGlobalConfiguration()
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenProductServiceOneIsRunning(downstreamServiceOneUrl, 200))
|
||||
.And(x => x.GivenProductServiceTwoIsRunning(downstreamServiceTwoUrl, 200))
|
||||
.And(x => _steps.GivenThereIsAConfiguration(configuration))
|
||||
.And(x => _steps.GivenOcelotIsRunning())
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGatewayMultipleTimes("/", 50))
|
||||
.Then(x => x.ThenTheTwoServicesShouldHaveBeenCalledTimes(50))
|
||||
.And(x => x.ThenBothServicesCalledRealisticAmountOfTimes(24, 26))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void ThenOnlyOneServiceHasBeenCalled()
|
||||
{
|
||||
_counterOne.ShouldBe(10);
|
||||
_counterTwo.ShouldBe(0);
|
||||
}
|
||||
|
||||
private void GivenIResetCounters()
|
||||
{
|
||||
_counterOne = 0;
|
||||
_counterTwo = 0;
|
||||
}
|
||||
|
||||
private void ThenBothServicesCalledRealisticAmountOfTimes(int bottom, int top)
|
||||
{
|
||||
_counterOne.ShouldBeInRange(bottom, top);
|
||||
_counterOne.ShouldBeInRange(bottom, top);
|
||||
}
|
||||
|
||||
private void ThenTheTwoServicesShouldHaveBeenCalledTimes(int expected)
|
||||
{
|
||||
var total = _counterOne + _counterTwo;
|
||||
total.ShouldBe(expected);
|
||||
}
|
||||
|
||||
private void GivenProductServiceOneIsRunning(string url, int statusCode)
|
||||
{
|
||||
_builderOne = new WebHostBuilder()
|
||||
.UseUrls(url)
|
||||
.UseKestrel()
|
||||
.UseContentRoot(Directory.GetCurrentDirectory())
|
||||
.UseIISIntegration()
|
||||
.UseUrls(url)
|
||||
.Configure(app =>
|
||||
{
|
||||
app.Run(async context =>
|
||||
{
|
||||
try
|
||||
{
|
||||
var response = string.Empty;
|
||||
lock (_syncLock)
|
||||
{
|
||||
_counterOne++;
|
||||
response = _counterOne.ToString();
|
||||
}
|
||||
context.Response.StatusCode = statusCode;
|
||||
await context.Response.WriteAsync(response);
|
||||
}
|
||||
catch (System.Exception exception)
|
||||
{
|
||||
await context.Response.WriteAsync(exception.StackTrace);
|
||||
}
|
||||
});
|
||||
})
|
||||
.Build();
|
||||
|
||||
_builderOne.Start();
|
||||
}
|
||||
|
||||
private void GivenProductServiceTwoIsRunning(string url, int statusCode)
|
||||
{
|
||||
_builderTwo = new WebHostBuilder()
|
||||
.UseUrls(url)
|
||||
.UseKestrel()
|
||||
.UseContentRoot(Directory.GetCurrentDirectory())
|
||||
.UseIISIntegration()
|
||||
.UseUrls(url)
|
||||
.Configure(app =>
|
||||
{
|
||||
app.Run(async context =>
|
||||
{
|
||||
try
|
||||
{
|
||||
var response = string.Empty;
|
||||
lock (_syncLock)
|
||||
{
|
||||
_counterTwo++;
|
||||
response = _counterTwo.ToString();
|
||||
}
|
||||
|
||||
context.Response.StatusCode = statusCode;
|
||||
await context.Response.WriteAsync(response);
|
||||
}
|
||||
catch (System.Exception exception)
|
||||
{
|
||||
await context.Response.WriteAsync(exception.StackTrace);
|
||||
}
|
||||
|
||||
});
|
||||
})
|
||||
.Build();
|
||||
|
||||
_builderTwo.Start();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_builderOne?.Dispose();
|
||||
_builderTwo?.Dispose();
|
||||
_steps.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
@ -1,53 +1,53 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<VersionPrefix>0.0.0-dev</VersionPrefix>
|
||||
<TargetFramework>netcoreapp2.0</TargetFramework>
|
||||
<RuntimeFrameworkVersion>2.0.0</RuntimeFrameworkVersion>
|
||||
<AssemblyName>Ocelot.AcceptanceTests</AssemblyName>
|
||||
<OutputType>Exe</OutputType>
|
||||
<PackageId>Ocelot.AcceptanceTests</PackageId>
|
||||
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
|
||||
<RuntimeIdentifiers>osx.10.11-x64;osx.10.12-x64;win7-x64;win10-x64</RuntimeIdentifiers>
|
||||
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
|
||||
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
|
||||
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Update="configuration.json;appsettings.json">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\src\Ocelot\Ocelot.csproj" />
|
||||
<ProjectReference Include="..\Ocelot.ManualTest\Ocelot.ManualTest.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="CacheManager.Serialization.Json" Version="1.1.1" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="2.0.0" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0-preview-20171031-01" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="2.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="2.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="2.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="2.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="2.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="2.0.0" />
|
||||
<PackageReference Include="Microsoft.DotNet.InternalAbstractions" Version="1.0.500-preview2-1-003177" />
|
||||
<PackageReference Include="Shouldly" Version="3.0.0-beta0003" />
|
||||
<PackageReference Include="TestStack.BDDfy" Version="4.3.2" />
|
||||
<PackageReference Include="Consul" Version="0.7.2.3" />
|
||||
<PackageReference Include="xunit" Version="2.3.1" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<VersionPrefix>0.0.0-dev</VersionPrefix>
|
||||
<TargetFramework>netcoreapp2.0</TargetFramework>
|
||||
<RuntimeFrameworkVersion>2.0.0</RuntimeFrameworkVersion>
|
||||
<AssemblyName>Ocelot.AcceptanceTests</AssemblyName>
|
||||
<OutputType>Exe</OutputType>
|
||||
<PackageId>Ocelot.AcceptanceTests</PackageId>
|
||||
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
|
||||
<RuntimeIdentifiers>osx.10.11-x64;osx.10.12-x64;win7-x64;win10-x64</RuntimeIdentifiers>
|
||||
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
|
||||
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
|
||||
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Update="configuration.json;appsettings.json">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\src\Ocelot\Ocelot.csproj" />
|
||||
<ProjectReference Include="..\Ocelot.ManualTest\Ocelot.ManualTest.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="CacheManager.Serialization.Json" Version="1.1.1" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="2.0.0" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0-preview-20171031-01" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="2.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="2.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="2.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="2.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="2.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="2.0.0" />
|
||||
<PackageReference Include="Microsoft.DotNet.InternalAbstractions" Version="1.0.500-preview2-1-003177" />
|
||||
<PackageReference Include="Shouldly" Version="3.0.0-beta0003" />
|
||||
<PackageReference Include="TestStack.BDDfy" Version="4.3.2" />
|
||||
<PackageReference Include="Consul" Version="0.7.2.3" />
|
||||
<PackageReference Include="xunit" Version="2.3.1" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
@ -1,19 +1,19 @@
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
// General Information about an assembly is controlled through the following
|
||||
// set of attributes. Change these attribute values to modify the information
|
||||
// associated with an assembly.
|
||||
[assembly: AssemblyConfiguration("")]
|
||||
[assembly: AssemblyCompany("")]
|
||||
[assembly: AssemblyProduct("Ocelot.AcceptanceTests")]
|
||||
[assembly: AssemblyTrademark("")]
|
||||
|
||||
// Setting ComVisible to false makes the types in this assembly not visible
|
||||
// to COM components. If you need to access a type in this assembly from
|
||||
// COM, set the ComVisible attribute to true on that type.
|
||||
[assembly: ComVisible(false)]
|
||||
|
||||
// The following GUID is for the ID of the typelib if this project is exposed to COM
|
||||
[assembly: Guid("f8c224fe-36be-45f5-9b0e-666d8f4a9b52")]
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
// General Information about an assembly is controlled through the following
|
||||
// set of attributes. Change these attribute values to modify the information
|
||||
// associated with an assembly.
|
||||
[assembly: AssemblyConfiguration("")]
|
||||
[assembly: AssemblyCompany("")]
|
||||
[assembly: AssemblyProduct("Ocelot.AcceptanceTests")]
|
||||
[assembly: AssemblyTrademark("")]
|
||||
|
||||
// Setting ComVisible to false makes the types in this assembly not visible
|
||||
// to COM components. If you need to access a type in this assembly from
|
||||
// COM, set the ComVisible attribute to true on that type.
|
||||
[assembly: ComVisible(false)]
|
||||
|
||||
// The following GUID is for the ID of the typelib if this project is exposed to COM
|
||||
[assembly: Guid("f8c224fe-36be-45f5-9b0e-666d8f4a9b52")]
|
||||
|
@ -1,207 +1,225 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Ocelot.Configuration.File;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.AcceptanceTests
|
||||
{
|
||||
public class QoSTests : IDisposable
|
||||
{
|
||||
private IWebHost _brokenService;
|
||||
private readonly Steps _steps;
|
||||
private int _requestCount;
|
||||
private IWebHost _workingService;
|
||||
|
||||
public QoSTests()
|
||||
{
|
||||
_steps = new Steps();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_open_circuit_breaker_then_close()
|
||||
{
|
||||
var configuration = new FileConfiguration
|
||||
{
|
||||
ReRoutes = new List<FileReRoute>
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamPathTemplate = "/",
|
||||
DownstreamScheme = "http",
|
||||
DownstreamHost = "localhost",
|
||||
DownstreamPort = 51879,
|
||||
UpstreamPathTemplate = "/",
|
||||
UpstreamHttpMethod = new List<string> { "Get" },
|
||||
QoSOptions = new FileQoSOptions
|
||||
{
|
||||
ExceptionsAllowedBeforeBreaking = 1,
|
||||
TimeoutValue = 500,
|
||||
DurationOfBreak = 1000
|
||||
},
|
||||
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenThereIsAPossiblyBrokenServiceRunningOn("http://localhost:51879", "Hello from Laura"))
|
||||
.Given(x => _steps.GivenThereIsAConfiguration(configuration))
|
||||
.Given(x => _steps.GivenOcelotIsRunning())
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
|
||||
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
|
||||
.And(x => _steps.ThenTheResponseBodyShouldBe("Hello from Laura"))
|
||||
.Given(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
|
||||
.Given(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.ServiceUnavailable))
|
||||
.Given(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
|
||||
.Given(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.ServiceUnavailable))
|
||||
.Given(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
|
||||
.Given(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.ServiceUnavailable))
|
||||
.Given(x => x.GivenIWaitMilliseconds(3000))
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
|
||||
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
|
||||
.And(x => _steps.ThenTheResponseBodyShouldBe("Hello from Laura"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void open_circuit_should_not_effect_different_reRoute()
|
||||
{
|
||||
var configuration = new FileConfiguration
|
||||
{
|
||||
ReRoutes = new List<FileReRoute>
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamPathTemplate = "/",
|
||||
DownstreamScheme = "http",
|
||||
DownstreamHost = "localhost",
|
||||
DownstreamPort = 51879,
|
||||
UpstreamPathTemplate = "/",
|
||||
UpstreamHttpMethod = new List<string> { "Get" },
|
||||
QoSOptions = new FileQoSOptions
|
||||
{
|
||||
ExceptionsAllowedBeforeBreaking = 1,
|
||||
TimeoutValue = 500,
|
||||
DurationOfBreak = 1000
|
||||
}
|
||||
},
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamPathTemplate = "/",
|
||||
DownstreamScheme = "http",
|
||||
DownstreamHost = "localhost",
|
||||
DownstreamPort = 51880,
|
||||
UpstreamPathTemplate = "/working",
|
||||
UpstreamHttpMethod = new List<string> { "Get" },
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenThereIsAPossiblyBrokenServiceRunningOn("http://localhost:51879", "Hello from Laura"))
|
||||
.And(x => x.GivenThereIsAServiceRunningOn("http://localhost:51880/", 200, "Hello from Tom"))
|
||||
.And(x => _steps.GivenThereIsAConfiguration(configuration))
|
||||
.And(x => _steps.GivenOcelotIsRunning())
|
||||
.And(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
|
||||
.And(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
|
||||
.And(x => _steps.ThenTheResponseBodyShouldBe("Hello from Laura"))
|
||||
.And(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
|
||||
.And(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.ServiceUnavailable))
|
||||
.And(x => _steps.WhenIGetUrlOnTheApiGateway("/working"))
|
||||
.And(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
|
||||
.And(x => _steps.ThenTheResponseBodyShouldBe("Hello from Tom"))
|
||||
.And(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
|
||||
.And(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.ServiceUnavailable))
|
||||
.And(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
|
||||
.And(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.ServiceUnavailable))
|
||||
.And(x => x.GivenIWaitMilliseconds(3000))
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
|
||||
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
|
||||
.And(x => _steps.ThenTheResponseBodyShouldBe("Hello from Laura"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenIWaitMilliseconds(int ms)
|
||||
{
|
||||
Thread.Sleep(ms);
|
||||
}
|
||||
|
||||
private void GivenThereIsAPossiblyBrokenServiceRunningOn(string url, string responseBody)
|
||||
{
|
||||
_brokenService = new WebHostBuilder()
|
||||
.UseUrls(url)
|
||||
.UseKestrel()
|
||||
.UseContentRoot(Directory.GetCurrentDirectory())
|
||||
.UseIISIntegration()
|
||||
.UseUrls(url)
|
||||
.Configure(app =>
|
||||
{
|
||||
app.Run(async context =>
|
||||
{
|
||||
//circuit starts closed
|
||||
if (_requestCount == 0)
|
||||
{
|
||||
_requestCount++;
|
||||
context.Response.StatusCode = 200;
|
||||
await context.Response.WriteAsync(responseBody);
|
||||
return;
|
||||
}
|
||||
|
||||
//request one times out and polly throws exception, circuit opens
|
||||
if (_requestCount == 1)
|
||||
{
|
||||
_requestCount++;
|
||||
await Task.Delay(1000);
|
||||
context.Response.StatusCode = 200;
|
||||
return;
|
||||
}
|
||||
|
||||
//after break closes we return 200 OK
|
||||
if (_requestCount == 2)
|
||||
{
|
||||
context.Response.StatusCode = 200;
|
||||
await context.Response.WriteAsync(responseBody);
|
||||
return;
|
||||
}
|
||||
});
|
||||
})
|
||||
.Build();
|
||||
|
||||
_brokenService.Start();
|
||||
}
|
||||
|
||||
private void GivenThereIsAServiceRunningOn(string url, int statusCode, string responseBody)
|
||||
{
|
||||
_workingService = new WebHostBuilder()
|
||||
.UseUrls(url)
|
||||
.UseKestrel()
|
||||
.UseContentRoot(Directory.GetCurrentDirectory())
|
||||
.UseIISIntegration()
|
||||
.UseUrls(url)
|
||||
.Configure(app =>
|
||||
{
|
||||
app.Run(async context =>
|
||||
{
|
||||
context.Response.StatusCode = statusCode;
|
||||
await context.Response.WriteAsync(responseBody);
|
||||
});
|
||||
})
|
||||
.Build();
|
||||
|
||||
_workingService.Start();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_workingService?.Dispose();
|
||||
_brokenService?.Dispose();
|
||||
_steps.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Ocelot.Configuration.File;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.AcceptanceTests
|
||||
{
|
||||
public class QoSTests : IDisposable
|
||||
{
|
||||
private IWebHost _brokenService;
|
||||
private readonly Steps _steps;
|
||||
private int _requestCount;
|
||||
private IWebHost _workingService;
|
||||
|
||||
public QoSTests()
|
||||
{
|
||||
_steps = new Steps();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_open_circuit_breaker_then_close()
|
||||
{
|
||||
var configuration = new FileConfiguration
|
||||
{
|
||||
ReRoutes = new List<FileReRoute>
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamPathTemplate = "/",
|
||||
DownstreamScheme = "http",
|
||||
DownstreamHostAndPorts = new List<FileHostAndPort>
|
||||
{
|
||||
new FileHostAndPort
|
||||
{
|
||||
Host = "localhost",
|
||||
Port = 51879,
|
||||
}
|
||||
},
|
||||
UpstreamPathTemplate = "/",
|
||||
UpstreamHttpMethod = new List<string> { "Get" },
|
||||
QoSOptions = new FileQoSOptions
|
||||
{
|
||||
ExceptionsAllowedBeforeBreaking = 1,
|
||||
TimeoutValue = 500,
|
||||
DurationOfBreak = 1000
|
||||
},
|
||||
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenThereIsAPossiblyBrokenServiceRunningOn("http://localhost:51879", "Hello from Laura"))
|
||||
.Given(x => _steps.GivenThereIsAConfiguration(configuration))
|
||||
.Given(x => _steps.GivenOcelotIsRunning())
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
|
||||
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
|
||||
.And(x => _steps.ThenTheResponseBodyShouldBe("Hello from Laura"))
|
||||
.Given(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
|
||||
.Given(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.ServiceUnavailable))
|
||||
.Given(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
|
||||
.Given(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.ServiceUnavailable))
|
||||
.Given(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
|
||||
.Given(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.ServiceUnavailable))
|
||||
.Given(x => x.GivenIWaitMilliseconds(3000))
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
|
||||
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
|
||||
.And(x => _steps.ThenTheResponseBodyShouldBe("Hello from Laura"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void open_circuit_should_not_effect_different_reRoute()
|
||||
{
|
||||
var configuration = new FileConfiguration
|
||||
{
|
||||
ReRoutes = new List<FileReRoute>
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamPathTemplate = "/",
|
||||
DownstreamScheme = "http",
|
||||
DownstreamHostAndPorts = new List<FileHostAndPort>
|
||||
{
|
||||
new FileHostAndPort
|
||||
{
|
||||
Host = "localhost",
|
||||
Port = 51879,
|
||||
}
|
||||
},
|
||||
UpstreamPathTemplate = "/",
|
||||
UpstreamHttpMethod = new List<string> { "Get" },
|
||||
QoSOptions = new FileQoSOptions
|
||||
{
|
||||
ExceptionsAllowedBeforeBreaking = 1,
|
||||
TimeoutValue = 500,
|
||||
DurationOfBreak = 1000
|
||||
}
|
||||
},
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamPathTemplate = "/",
|
||||
DownstreamScheme = "http",
|
||||
DownstreamHostAndPorts = new List<FileHostAndPort>
|
||||
{
|
||||
new FileHostAndPort
|
||||
{
|
||||
Host = "localhost",
|
||||
Port = 51880,
|
||||
}
|
||||
},
|
||||
UpstreamPathTemplate = "/working",
|
||||
UpstreamHttpMethod = new List<string> { "Get" },
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenThereIsAPossiblyBrokenServiceRunningOn("http://localhost:51879", "Hello from Laura"))
|
||||
.And(x => x.GivenThereIsAServiceRunningOn("http://localhost:51880/", 200, "Hello from Tom"))
|
||||
.And(x => _steps.GivenThereIsAConfiguration(configuration))
|
||||
.And(x => _steps.GivenOcelotIsRunning())
|
||||
.And(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
|
||||
.And(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
|
||||
.And(x => _steps.ThenTheResponseBodyShouldBe("Hello from Laura"))
|
||||
.And(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
|
||||
.And(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.ServiceUnavailable))
|
||||
.And(x => _steps.WhenIGetUrlOnTheApiGateway("/working"))
|
||||
.And(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
|
||||
.And(x => _steps.ThenTheResponseBodyShouldBe("Hello from Tom"))
|
||||
.And(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
|
||||
.And(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.ServiceUnavailable))
|
||||
.And(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
|
||||
.And(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.ServiceUnavailable))
|
||||
.And(x => x.GivenIWaitMilliseconds(3000))
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
|
||||
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
|
||||
.And(x => _steps.ThenTheResponseBodyShouldBe("Hello from Laura"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenIWaitMilliseconds(int ms)
|
||||
{
|
||||
Thread.Sleep(ms);
|
||||
}
|
||||
|
||||
private void GivenThereIsAPossiblyBrokenServiceRunningOn(string url, string responseBody)
|
||||
{
|
||||
_brokenService = new WebHostBuilder()
|
||||
.UseUrls(url)
|
||||
.UseKestrel()
|
||||
.UseContentRoot(Directory.GetCurrentDirectory())
|
||||
.UseIISIntegration()
|
||||
.UseUrls(url)
|
||||
.Configure(app =>
|
||||
{
|
||||
app.Run(async context =>
|
||||
{
|
||||
//circuit starts closed
|
||||
if (_requestCount == 0)
|
||||
{
|
||||
_requestCount++;
|
||||
context.Response.StatusCode = 200;
|
||||
await context.Response.WriteAsync(responseBody);
|
||||
return;
|
||||
}
|
||||
|
||||
//request one times out and polly throws exception, circuit opens
|
||||
if (_requestCount == 1)
|
||||
{
|
||||
_requestCount++;
|
||||
await Task.Delay(1000);
|
||||
context.Response.StatusCode = 200;
|
||||
return;
|
||||
}
|
||||
|
||||
//after break closes we return 200 OK
|
||||
if (_requestCount == 2)
|
||||
{
|
||||
context.Response.StatusCode = 200;
|
||||
await context.Response.WriteAsync(responseBody);
|
||||
return;
|
||||
}
|
||||
});
|
||||
})
|
||||
.Build();
|
||||
|
||||
_brokenService.Start();
|
||||
}
|
||||
|
||||
private void GivenThereIsAServiceRunningOn(string url, int statusCode, string responseBody)
|
||||
{
|
||||
_workingService = new WebHostBuilder()
|
||||
.UseUrls(url)
|
||||
.UseKestrel()
|
||||
.UseContentRoot(Directory.GetCurrentDirectory())
|
||||
.UseIISIntegration()
|
||||
.UseUrls(url)
|
||||
.Configure(app =>
|
||||
{
|
||||
app.Run(async context =>
|
||||
{
|
||||
context.Response.StatusCode = statusCode;
|
||||
await context.Response.WriteAsync(responseBody);
|
||||
});
|
||||
})
|
||||
.Build();
|
||||
|
||||
_workingService.Start();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_workingService?.Dispose();
|
||||
_brokenService?.Dispose();
|
||||
_steps.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,146 +1,164 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.Primitives;
|
||||
using Ocelot.Configuration.File;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.AcceptanceTests
|
||||
{
|
||||
public class RequestIdTests : IDisposable
|
||||
{
|
||||
private IWebHost _builder;
|
||||
private readonly Steps _steps;
|
||||
|
||||
public RequestIdTests()
|
||||
{
|
||||
_steps = new Steps();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_use_default_request_id_and_forward()
|
||||
{
|
||||
var configuration = new FileConfiguration
|
||||
{
|
||||
ReRoutes = new List<FileReRoute>
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamPathTemplate = "/",
|
||||
DownstreamPort = 51879,
|
||||
DownstreamScheme = "http",
|
||||
DownstreamHost = "localhost",
|
||||
UpstreamPathTemplate = "/",
|
||||
UpstreamHttpMethod = new List<string> { "Get" },
|
||||
RequestIdKey = _steps.RequestIdKey,
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:51879"))
|
||||
.And(x => _steps.GivenThereIsAConfiguration(configuration))
|
||||
.And(x => _steps.GivenOcelotIsRunning())
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
|
||||
.Then(x => _steps.ThenTheRequestIdIsReturned())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_use_request_id_and_forward()
|
||||
{
|
||||
var configuration = new FileConfiguration
|
||||
{
|
||||
ReRoutes = new List<FileReRoute>
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamPathTemplate = "/",
|
||||
DownstreamPort = 51879,
|
||||
DownstreamScheme = "http",
|
||||
DownstreamHost = "localhost",
|
||||
UpstreamPathTemplate = "/",
|
||||
UpstreamHttpMethod = new List<string> { "Get" },
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
var requestId = Guid.NewGuid().ToString();
|
||||
|
||||
this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:51879"))
|
||||
.And(x => _steps.GivenThereIsAConfiguration(configuration))
|
||||
.And(x => _steps.GivenOcelotIsRunning())
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/", requestId))
|
||||
.Then(x => _steps.ThenTheRequestIdIsReturned(requestId))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_use_global_request_id_and_forward()
|
||||
{
|
||||
var configuration = new FileConfiguration
|
||||
{
|
||||
ReRoutes = new List<FileReRoute>
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamPathTemplate = "/",
|
||||
DownstreamPort = 51879,
|
||||
DownstreamScheme = "http",
|
||||
DownstreamHost = "localhost",
|
||||
UpstreamPathTemplate = "/",
|
||||
UpstreamHttpMethod = new List<string> { "Get" },
|
||||
}
|
||||
},
|
||||
GlobalConfiguration = new FileGlobalConfiguration
|
||||
{
|
||||
RequestIdKey = _steps.RequestIdKey
|
||||
}
|
||||
};
|
||||
|
||||
var requestId = Guid.NewGuid().ToString();
|
||||
|
||||
this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:51879"))
|
||||
.And(x => _steps.GivenThereIsAConfiguration(configuration))
|
||||
.And(x => _steps.GivenOcelotIsRunning())
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/", requestId))
|
||||
.Then(x => _steps.ThenTheRequestIdIsReturned(requestId))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenThereIsAServiceRunningOn(string url)
|
||||
{
|
||||
_builder = new WebHostBuilder()
|
||||
.UseUrls(url)
|
||||
.UseKestrel()
|
||||
.UseContentRoot(Directory.GetCurrentDirectory())
|
||||
.UseIISIntegration()
|
||||
.UseUrls(url)
|
||||
.Configure(app =>
|
||||
{
|
||||
app.Run(context =>
|
||||
{
|
||||
StringValues requestId;
|
||||
context.Request.Headers.TryGetValue(_steps.RequestIdKey, out requestId);
|
||||
context.Response.Headers.Add(_steps.RequestIdKey, requestId.First());
|
||||
return Task.CompletedTask;
|
||||
});
|
||||
})
|
||||
.Build();
|
||||
|
||||
_builder.Start();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_builder?.Dispose();
|
||||
_steps.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.Primitives;
|
||||
using Ocelot.Configuration.File;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.AcceptanceTests
|
||||
{
|
||||
public class RequestIdTests : IDisposable
|
||||
{
|
||||
private IWebHost _builder;
|
||||
private readonly Steps _steps;
|
||||
|
||||
public RequestIdTests()
|
||||
{
|
||||
_steps = new Steps();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_use_default_request_id_and_forward()
|
||||
{
|
||||
var configuration = new FileConfiguration
|
||||
{
|
||||
ReRoutes = new List<FileReRoute>
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamPathTemplate = "/",
|
||||
DownstreamHostAndPorts = new List<FileHostAndPort>
|
||||
{
|
||||
new FileHostAndPort
|
||||
{
|
||||
Host = "localhost",
|
||||
Port = 51879,
|
||||
}
|
||||
},
|
||||
DownstreamScheme = "http",
|
||||
UpstreamPathTemplate = "/",
|
||||
UpstreamHttpMethod = new List<string> { "Get" },
|
||||
RequestIdKey = _steps.RequestIdKey,
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:51879"))
|
||||
.And(x => _steps.GivenThereIsAConfiguration(configuration))
|
||||
.And(x => _steps.GivenOcelotIsRunning())
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
|
||||
.Then(x => _steps.ThenTheRequestIdIsReturned())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_use_request_id_and_forward()
|
||||
{
|
||||
var configuration = new FileConfiguration
|
||||
{
|
||||
ReRoutes = new List<FileReRoute>
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamPathTemplate = "/",
|
||||
DownstreamHostAndPorts = new List<FileHostAndPort>
|
||||
{
|
||||
new FileHostAndPort
|
||||
{
|
||||
Host = "localhost",
|
||||
Port = 51879,
|
||||
}
|
||||
},
|
||||
DownstreamScheme = "http",
|
||||
UpstreamPathTemplate = "/",
|
||||
UpstreamHttpMethod = new List<string> { "Get" },
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
var requestId = Guid.NewGuid().ToString();
|
||||
|
||||
this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:51879"))
|
||||
.And(x => _steps.GivenThereIsAConfiguration(configuration))
|
||||
.And(x => _steps.GivenOcelotIsRunning())
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/", requestId))
|
||||
.Then(x => _steps.ThenTheRequestIdIsReturned(requestId))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_use_global_request_id_and_forward()
|
||||
{
|
||||
var configuration = new FileConfiguration
|
||||
{
|
||||
ReRoutes = new List<FileReRoute>
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamPathTemplate = "/",
|
||||
DownstreamHostAndPorts = new List<FileHostAndPort>
|
||||
{
|
||||
new FileHostAndPort
|
||||
{
|
||||
Host = "localhost",
|
||||
Port = 51879,
|
||||
}
|
||||
},
|
||||
DownstreamScheme = "http",
|
||||
UpstreamPathTemplate = "/",
|
||||
UpstreamHttpMethod = new List<string> { "Get" },
|
||||
}
|
||||
},
|
||||
GlobalConfiguration = new FileGlobalConfiguration
|
||||
{
|
||||
RequestIdKey = _steps.RequestIdKey
|
||||
}
|
||||
};
|
||||
|
||||
var requestId = Guid.NewGuid().ToString();
|
||||
|
||||
this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:51879"))
|
||||
.And(x => _steps.GivenThereIsAConfiguration(configuration))
|
||||
.And(x => _steps.GivenOcelotIsRunning())
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/", requestId))
|
||||
.Then(x => _steps.ThenTheRequestIdIsReturned(requestId))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenThereIsAServiceRunningOn(string url)
|
||||
{
|
||||
_builder = new WebHostBuilder()
|
||||
.UseUrls(url)
|
||||
.UseKestrel()
|
||||
.UseContentRoot(Directory.GetCurrentDirectory())
|
||||
.UseIISIntegration()
|
||||
.UseUrls(url)
|
||||
.Configure(app =>
|
||||
{
|
||||
app.Run(context =>
|
||||
{
|
||||
StringValues requestId;
|
||||
context.Request.Headers.TryGetValue(_steps.RequestIdKey, out requestId);
|
||||
context.Response.Headers.Add(_steps.RequestIdKey, requestId.First());
|
||||
return Task.CompletedTask;
|
||||
});
|
||||
})
|
||||
.Build();
|
||||
|
||||
_builder.Start();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_builder?.Dispose();
|
||||
_steps.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,76 +1,82 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Ocelot.Configuration.File;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.AcceptanceTests
|
||||
{
|
||||
public class ReturnsErrorTests : IDisposable
|
||||
{
|
||||
private IWebHost _servicebuilder;
|
||||
private readonly Steps _steps;
|
||||
|
||||
public ReturnsErrorTests()
|
||||
{
|
||||
_steps = new Steps();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_internal_server_error_if_downstream_service_returns_internal_server_error()
|
||||
{
|
||||
var configuration = new FileConfiguration
|
||||
{
|
||||
ReRoutes = new List<FileReRoute>
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamPathTemplate = "/",
|
||||
UpstreamPathTemplate = "/",
|
||||
UpstreamHttpMethod = new List<string> { "Get" },
|
||||
DownstreamPort = 53876,
|
||||
DownstreamScheme = "http",
|
||||
DownstreamHost = "localhost"
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:53876"))
|
||||
.And(x => _steps.GivenThereIsAConfiguration(configuration))
|
||||
.And(x => _steps.GivenOcelotIsRunning())
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
|
||||
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.InternalServerError))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenThereIsAServiceRunningOn(string url)
|
||||
{
|
||||
_servicebuilder = new WebHostBuilder()
|
||||
.UseUrls(url)
|
||||
.UseKestrel()
|
||||
.UseContentRoot(Directory.GetCurrentDirectory())
|
||||
.UseIISIntegration()
|
||||
.UseUrls(url)
|
||||
.Configure(app =>
|
||||
{
|
||||
app.Run(context =>
|
||||
{
|
||||
throw new Exception("BLAMMMM");
|
||||
});
|
||||
})
|
||||
.Build();
|
||||
|
||||
_servicebuilder.Start();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_servicebuilder?.Dispose();
|
||||
_steps.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Ocelot.Configuration.File;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.AcceptanceTests
|
||||
{
|
||||
public class ReturnsErrorTests : IDisposable
|
||||
{
|
||||
private IWebHost _servicebuilder;
|
||||
private readonly Steps _steps;
|
||||
|
||||
public ReturnsErrorTests()
|
||||
{
|
||||
_steps = new Steps();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_internal_server_error_if_downstream_service_returns_internal_server_error()
|
||||
{
|
||||
var configuration = new FileConfiguration
|
||||
{
|
||||
ReRoutes = new List<FileReRoute>
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamPathTemplate = "/",
|
||||
UpstreamPathTemplate = "/",
|
||||
UpstreamHttpMethod = new List<string> { "Get" },
|
||||
DownstreamHostAndPorts = new List<FileHostAndPort>
|
||||
{
|
||||
new FileHostAndPort
|
||||
{
|
||||
Host = "localhost",
|
||||
Port = 53876,
|
||||
}
|
||||
},
|
||||
DownstreamScheme = "http",
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:53876"))
|
||||
.And(x => _steps.GivenThereIsAConfiguration(configuration))
|
||||
.And(x => _steps.GivenOcelotIsRunning())
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
|
||||
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.InternalServerError))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenThereIsAServiceRunningOn(string url)
|
||||
{
|
||||
_servicebuilder = new WebHostBuilder()
|
||||
.UseUrls(url)
|
||||
.UseKestrel()
|
||||
.UseContentRoot(Directory.GetCurrentDirectory())
|
||||
.UseIISIntegration()
|
||||
.UseUrls(url)
|
||||
.Configure(app =>
|
||||
{
|
||||
app.Run(context =>
|
||||
{
|
||||
throw new Exception("BLAMMMM");
|
||||
});
|
||||
})
|
||||
.Build();
|
||||
|
||||
_servicebuilder.Start();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_servicebuilder?.Dispose();
|
||||
_steps.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -23,6 +23,8 @@ namespace Ocelot.AcceptanceTests
|
||||
private int _counterOne;
|
||||
private int _counterTwo;
|
||||
private static readonly object _syncLock = new object();
|
||||
private IWebHost _builder;
|
||||
private string _downstreamPath;
|
||||
|
||||
public ServiceDiscoveryTests()
|
||||
{
|
||||
@ -88,7 +90,7 @@ namespace Ocelot.AcceptanceTests
|
||||
|
||||
this.Given(x => x.GivenProductServiceOneIsRunning(downstreamServiceOneUrl, 200))
|
||||
.And(x => x.GivenProductServiceTwoIsRunning(downstreamServiceTwoUrl, 200))
|
||||
.And(x => x.GivenThereIsAFakeConsulServiceDiscoveryProvider(fakeConsulServiceDiscoveryUrl))
|
||||
.And(x => x.GivenThereIsAFakeConsulServiceDiscoveryProvider(fakeConsulServiceDiscoveryUrl, serviceName))
|
||||
.And(x => x.GivenTheServicesAreRegisteredWithConsul(serviceEntryOne, serviceEntryTwo))
|
||||
.And(x => _steps.GivenThereIsAConfiguration(configuration))
|
||||
.And(x => _steps.GivenOcelotIsRunning())
|
||||
@ -98,6 +100,62 @@ namespace Ocelot.AcceptanceTests
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
//test from issue 213
|
||||
[Fact]
|
||||
public void should_handle_request_to_consul_for_downstream_service_and_make_request()
|
||||
{
|
||||
var consulPort = 8505;
|
||||
var serviceName = "web";
|
||||
var downstreamServiceOneUrl = "http://localhost:8080";
|
||||
var fakeConsulServiceDiscoveryUrl = $"http://localhost:{consulPort}";
|
||||
var serviceEntryOne = new ServiceEntry()
|
||||
{
|
||||
Service = new AgentService()
|
||||
{
|
||||
Service = serviceName,
|
||||
Address = "localhost",
|
||||
Port = 8080,
|
||||
ID = "web_90_0_2_224_8080",
|
||||
Tags = new string[1]{"version-v1"}
|
||||
},
|
||||
};
|
||||
|
||||
var configuration = new FileConfiguration
|
||||
{
|
||||
ReRoutes = new List<FileReRoute>
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamPathTemplate = "/api/home",
|
||||
DownstreamScheme = "http",
|
||||
UpstreamPathTemplate = "/home",
|
||||
UpstreamHttpMethod = new List<string> { "Get", "Options" },
|
||||
ServiceName = serviceName,
|
||||
LoadBalancer = "LeastConnection",
|
||||
UseServiceDiscovery = true,
|
||||
}
|
||||
},
|
||||
GlobalConfiguration = new FileGlobalConfiguration()
|
||||
{
|
||||
ServiceDiscoveryProvider = new FileServiceDiscoveryProvider()
|
||||
{
|
||||
Host = "localhost",
|
||||
Port = consulPort
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenThereIsAServiceRunningOn(downstreamServiceOneUrl, "/api/home", 200, "Hello from Laura"))
|
||||
.And(x => x.GivenThereIsAFakeConsulServiceDiscoveryProvider(fakeConsulServiceDiscoveryUrl, serviceName))
|
||||
.And(x => x.GivenTheServicesAreRegisteredWithConsul(serviceEntryOne))
|
||||
.And(x => _steps.GivenThereIsAConfiguration(configuration))
|
||||
.And(x => _steps.GivenOcelotIsRunning())
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/home"))
|
||||
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
|
||||
.And(x => _steps.ThenTheResponseBodyShouldBe("Hello from Laura"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_send_request_to_service_after_it_becomes_available()
|
||||
{
|
||||
@ -156,7 +214,7 @@ namespace Ocelot.AcceptanceTests
|
||||
|
||||
this.Given(x => x.GivenProductServiceOneIsRunning(downstreamServiceOneUrl, 200))
|
||||
.And(x => x.GivenProductServiceTwoIsRunning(downstreamServiceTwoUrl, 200))
|
||||
.And(x => x.GivenThereIsAFakeConsulServiceDiscoveryProvider(fakeConsulServiceDiscoveryUrl))
|
||||
.And(x => x.GivenThereIsAFakeConsulServiceDiscoveryProvider(fakeConsulServiceDiscoveryUrl, serviceName))
|
||||
.And(x => x.GivenTheServicesAreRegisteredWithConsul(serviceEntryOne, serviceEntryTwo))
|
||||
.And(x => _steps.GivenThereIsAConfiguration(configuration))
|
||||
.And(x => _steps.GivenOcelotIsRunning())
|
||||
@ -217,7 +275,7 @@ namespace Ocelot.AcceptanceTests
|
||||
}
|
||||
}
|
||||
|
||||
private void GivenThereIsAFakeConsulServiceDiscoveryProvider(string url)
|
||||
private void GivenThereIsAFakeConsulServiceDiscoveryProvider(string url, string serviceName)
|
||||
{
|
||||
_fakeConsulBuilder = new WebHostBuilder()
|
||||
.UseUrls(url)
|
||||
@ -229,7 +287,7 @@ namespace Ocelot.AcceptanceTests
|
||||
{
|
||||
app.Run(async context =>
|
||||
{
|
||||
if(context.Request.Path.Value == "/v1/health/service/product")
|
||||
if(context.Request.Path.Value == $"/v1/health/service/{serviceName}")
|
||||
{
|
||||
await context.Response.WriteJsonAsync(_serviceEntries);
|
||||
}
|
||||
@ -310,6 +368,37 @@ namespace Ocelot.AcceptanceTests
|
||||
_builderTwo.Start();
|
||||
}
|
||||
|
||||
private void GivenThereIsAServiceRunningOn(string baseUrl, string basePath, int statusCode, string responseBody)
|
||||
{
|
||||
_builder = new WebHostBuilder()
|
||||
.UseUrls(baseUrl)
|
||||
.UseKestrel()
|
||||
.UseContentRoot(Directory.GetCurrentDirectory())
|
||||
.UseIISIntegration()
|
||||
.Configure(app =>
|
||||
{
|
||||
app.UsePathBase(basePath);
|
||||
app.Run(async context =>
|
||||
{
|
||||
_downstreamPath = !string.IsNullOrEmpty(context.Request.PathBase.Value) ? context.Request.PathBase.Value : context.Request.Path.Value;
|
||||
|
||||
if(_downstreamPath != basePath)
|
||||
{
|
||||
context.Response.StatusCode = statusCode;
|
||||
await context.Response.WriteAsync("downstream path didnt match base path");
|
||||
}
|
||||
else
|
||||
{
|
||||
context.Response.StatusCode = statusCode;
|
||||
await context.Response.WriteAsync(responseBody);
|
||||
}
|
||||
});
|
||||
})
|
||||
.Build();
|
||||
|
||||
_builder.Start();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_builderOne?.Dispose();
|
||||
@ -317,4 +406,4 @@ namespace Ocelot.AcceptanceTests
|
||||
_steps.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,29 +1,29 @@
|
||||
using CacheManager.Core;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Ocelot.DependencyInjection;
|
||||
using Ocelot.AcceptanceTests.Caching;
|
||||
|
||||
namespace Ocelot.AcceptanceTests
|
||||
{
|
||||
public class StartupWithConsulAndCustomCacheHandle : AcceptanceTestsStartup
|
||||
{
|
||||
public StartupWithConsulAndCustomCacheHandle(IHostingEnvironment env) : base(env) { }
|
||||
|
||||
public override void ConfigureServices(IServiceCollection services)
|
||||
{
|
||||
services.AddOcelot(Configuration)
|
||||
.AddCacheManager((x) =>
|
||||
{
|
||||
x.WithMicrosoftLogging(log =>
|
||||
{
|
||||
log.AddConsole(LogLevel.Debug);
|
||||
})
|
||||
.WithJsonSerializer()
|
||||
.WithHandle(typeof(InMemoryJsonHandle<>));
|
||||
})
|
||||
.AddStoreOcelotConfigurationInConsul();
|
||||
}
|
||||
}
|
||||
}
|
||||
using CacheManager.Core;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Ocelot.DependencyInjection;
|
||||
using Ocelot.AcceptanceTests.Caching;
|
||||
|
||||
namespace Ocelot.AcceptanceTests
|
||||
{
|
||||
public class StartupWithConsulAndCustomCacheHandle : AcceptanceTestsStartup
|
||||
{
|
||||
public StartupWithConsulAndCustomCacheHandle(IHostingEnvironment env) : base(env) { }
|
||||
|
||||
public override void ConfigureServices(IServiceCollection services)
|
||||
{
|
||||
services.AddOcelot(Configuration)
|
||||
.AddCacheManager((x) =>
|
||||
{
|
||||
x.WithMicrosoftLogging(log =>
|
||||
{
|
||||
log.AddConsole(LogLevel.Debug);
|
||||
})
|
||||
.WithJsonSerializer()
|
||||
.WithHandle(typeof(InMemoryJsonHandle<>));
|
||||
})
|
||||
.AddStoreOcelotConfigurationInConsul();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,28 +1,28 @@
|
||||
using CacheManager.Core;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Ocelot.DependencyInjection;
|
||||
using Ocelot.AcceptanceTests.Caching;
|
||||
|
||||
namespace Ocelot.AcceptanceTests
|
||||
{
|
||||
public class StartupWithCustomCacheHandle : AcceptanceTestsStartup
|
||||
{
|
||||
public StartupWithCustomCacheHandle(IHostingEnvironment env) : base(env) { }
|
||||
|
||||
public override void ConfigureServices(IServiceCollection services)
|
||||
{
|
||||
services.AddOcelot(Configuration)
|
||||
.AddCacheManager((x) =>
|
||||
{
|
||||
x.WithMicrosoftLogging(log =>
|
||||
{
|
||||
log.AddConsole(LogLevel.Debug);
|
||||
})
|
||||
.WithJsonSerializer()
|
||||
.WithHandle(typeof(InMemoryJsonHandle<>));
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
using CacheManager.Core;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Ocelot.DependencyInjection;
|
||||
using Ocelot.AcceptanceTests.Caching;
|
||||
|
||||
namespace Ocelot.AcceptanceTests
|
||||
{
|
||||
public class StartupWithCustomCacheHandle : AcceptanceTestsStartup
|
||||
{
|
||||
public StartupWithCustomCacheHandle(IHostingEnvironment env) : base(env) { }
|
||||
|
||||
public override void ConfigureServices(IServiceCollection services)
|
||||
{
|
||||
services.AddOcelot(Configuration)
|
||||
.AddCacheManager((x) =>
|
||||
{
|
||||
x.WithMicrosoftLogging(log =>
|
||||
{
|
||||
log.AddConsole(LogLevel.Debug);
|
||||
})
|
||||
.WithJsonSerializer()
|
||||
.WithHandle(typeof(InMemoryJsonHandle<>));
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,423 +1,429 @@
|
||||
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.Configuration.Repository;
|
||||
using Ocelot.DependencyInjection;
|
||||
using Ocelot.Middleware;
|
||||
using Ocelot.ServiceDiscovery;
|
||||
using Shouldly;
|
||||
using ConfigurationBuilder = Microsoft.Extensions.Configuration.ConfigurationBuilder;
|
||||
using Ocelot.AcceptanceTests.Caching;
|
||||
|
||||
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.ConfigureServices(s =>
|
||||
{
|
||||
s.AddSingleton(_webHostBuilder);
|
||||
});
|
||||
|
||||
_ocelotServer = new TestServer(_webHostBuilder
|
||||
.UseStartup<AcceptanceTestsStartup>());
|
||||
|
||||
_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.ConfigureServices(s =>
|
||||
{
|
||||
s.AddSingleton(_webHostBuilder);
|
||||
s.AddAuthentication()
|
||||
.AddIdentityServerAuthentication(authenticationProviderKey, options);
|
||||
});
|
||||
|
||||
_ocelotServer = new TestServer(_webHostBuilder
|
||||
.UseStartup<AcceptanceTestsStartup>());
|
||||
|
||||
_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.ConfigureServices(s =>
|
||||
{
|
||||
s.AddSingleton(_webHostBuilder);
|
||||
});
|
||||
|
||||
_ocelotServer = new TestServer(_webHostBuilder
|
||||
.UseStartup<StartupWithCustomCacheHandle>());
|
||||
|
||||
_ocelotClient = _ocelotServer.CreateClient();
|
||||
}
|
||||
|
||||
public void GivenOcelotIsRunningUsingConsulToStoreConfig()
|
||||
{
|
||||
_webHostBuilder = new WebHostBuilder();
|
||||
|
||||
_webHostBuilder.ConfigureServices(s =>
|
||||
{
|
||||
s.AddSingleton(_webHostBuilder);
|
||||
});
|
||||
|
||||
_ocelotServer = new TestServer(_webHostBuilder
|
||||
.UseStartup<ConsulStartup>());
|
||||
|
||||
_ocelotClient = _ocelotServer.CreateClient();
|
||||
}
|
||||
|
||||
public void GivenOcelotIsRunningUsingConsulToStoreConfigAndJsonSerializedCache()
|
||||
{
|
||||
_webHostBuilder = new WebHostBuilder();
|
||||
|
||||
_webHostBuilder.ConfigureServices(s =>
|
||||
{
|
||||
s.AddSingleton(_webHostBuilder);
|
||||
});
|
||||
|
||||
_ocelotServer = new TestServer(_webHostBuilder
|
||||
.UseStartup<StartupWithConsulAndCustomCacheHandle>());
|
||||
|
||||
_ocelotClient = _ocelotServer.CreateClient();
|
||||
}
|
||||
|
||||
internal void ThenTheResponseShouldBe(FileConfiguration expected)
|
||||
{
|
||||
var response = JsonConvert.DeserializeObject<FileConfiguration>(_response.Content.ReadAsStringAsync().Result);
|
||||
|
||||
response.GlobalConfiguration.RequestIdKey.ShouldBe(expected.GlobalConfiguration.RequestIdKey);
|
||||
response.GlobalConfiguration.ServiceDiscoveryProvider.Host.ShouldBe(expected.GlobalConfiguration.ServiceDiscoveryProvider.Host);
|
||||
response.GlobalConfiguration.ServiceDiscoveryProvider.Port.ShouldBe(expected.GlobalConfiguration.ServiceDiscoveryProvider.Port);
|
||||
|
||||
for (var i = 0; i < response.ReRoutes.Count; i++)
|
||||
{
|
||||
response.ReRoutes[i].DownstreamHost.ShouldBe(expected.ReRoutes[i].DownstreamHost);
|
||||
response.ReRoutes[i].DownstreamPathTemplate.ShouldBe(expected.ReRoutes[i].DownstreamPathTemplate);
|
||||
response.ReRoutes[i].DownstreamPort.ShouldBe(expected.ReRoutes[i].DownstreamPort);
|
||||
response.ReRoutes[i].DownstreamScheme.ShouldBe(expected.ReRoutes[i].DownstreamScheme);
|
||||
response.ReRoutes[i].UpstreamPathTemplate.ShouldBe(expected.ReRoutes[i].UpstreamPathTemplate);
|
||||
response.ReRoutes[i].UpstreamHttpMethod.ShouldBe(expected.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(OcelotMiddlewareConfiguration ocelotMiddlewareConfig)
|
||||
{
|
||||
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(ocelotMiddlewareConfig).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 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
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.Configuration.Repository;
|
||||
using Ocelot.DependencyInjection;
|
||||
using Ocelot.Middleware;
|
||||
using Ocelot.ServiceDiscovery;
|
||||
using Shouldly;
|
||||
using ConfigurationBuilder = Microsoft.Extensions.Configuration.ConfigurationBuilder;
|
||||
using Ocelot.AcceptanceTests.Caching;
|
||||
|
||||
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.ConfigureServices(s =>
|
||||
{
|
||||
s.AddSingleton(_webHostBuilder);
|
||||
});
|
||||
|
||||
_ocelotServer = new TestServer(_webHostBuilder
|
||||
.UseStartup<AcceptanceTestsStartup>());
|
||||
|
||||
_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.ConfigureServices(s =>
|
||||
{
|
||||
s.AddSingleton(_webHostBuilder);
|
||||
s.AddAuthentication()
|
||||
.AddIdentityServerAuthentication(authenticationProviderKey, options);
|
||||
});
|
||||
|
||||
_ocelotServer = new TestServer(_webHostBuilder
|
||||
.UseStartup<AcceptanceTestsStartup>());
|
||||
|
||||
_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.ConfigureServices(s =>
|
||||
{
|
||||
s.AddSingleton(_webHostBuilder);
|
||||
});
|
||||
|
||||
_ocelotServer = new TestServer(_webHostBuilder
|
||||
.UseStartup<StartupWithCustomCacheHandle>());
|
||||
|
||||
_ocelotClient = _ocelotServer.CreateClient();
|
||||
}
|
||||
|
||||
public void GivenOcelotIsRunningUsingConsulToStoreConfig()
|
||||
{
|
||||
_webHostBuilder = new WebHostBuilder();
|
||||
|
||||
_webHostBuilder.ConfigureServices(s =>
|
||||
{
|
||||
s.AddSingleton(_webHostBuilder);
|
||||
});
|
||||
|
||||
_ocelotServer = new TestServer(_webHostBuilder
|
||||
.UseStartup<ConsulStartup>());
|
||||
|
||||
_ocelotClient = _ocelotServer.CreateClient();
|
||||
}
|
||||
|
||||
public void GivenOcelotIsRunningUsingConsulToStoreConfigAndJsonSerializedCache()
|
||||
{
|
||||
_webHostBuilder = new WebHostBuilder();
|
||||
|
||||
_webHostBuilder.ConfigureServices(s =>
|
||||
{
|
||||
s.AddSingleton(_webHostBuilder);
|
||||
});
|
||||
|
||||
_ocelotServer = new TestServer(_webHostBuilder
|
||||
.UseStartup<StartupWithConsulAndCustomCacheHandle>());
|
||||
|
||||
_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(OcelotMiddlewareConfiguration ocelotMiddlewareConfig)
|
||||
{
|
||||
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(ocelotMiddlewareConfig).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 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,10 +1,10 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
namespace Ocelot.AcceptanceTests
|
||||
{
|
||||
public static class TestConfiguration
|
||||
{
|
||||
public static string ConfigurationPath => Path.Combine(AppContext.BaseDirectory, "configuration.json");
|
||||
}
|
||||
}
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
namespace Ocelot.AcceptanceTests
|
||||
{
|
||||
public static class TestConfiguration
|
||||
{
|
||||
public static string ConfigurationPath => Path.Combine(AppContext.BaseDirectory, "configuration.json");
|
||||
}
|
||||
}
|
||||
|
@ -1,230 +1,191 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using Consul;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Ocelot.Configuration.File;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.AcceptanceTests
|
||||
{
|
||||
public class TwoDownstreamServicesTests : IDisposable
|
||||
{
|
||||
private IWebHost _builderOne;
|
||||
private IWebHost _builderTwo;
|
||||
private IWebHost _fakeConsulBuilder;
|
||||
private readonly Steps _steps;
|
||||
private readonly List<ServiceEntry> _serviceEntries;
|
||||
private string _downstreamPathOne;
|
||||
private string _downstreamPathTwo;
|
||||
|
||||
public TwoDownstreamServicesTests()
|
||||
{
|
||||
_steps = new Steps();
|
||||
_serviceEntries = new List<ServiceEntry>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_fix_issue_194()
|
||||
{
|
||||
var consulPort = 8503;
|
||||
var downstreamServiceOneUrl = "http://localhost:8362";
|
||||
var downstreamServiceTwoUrl = "http://localhost:8330";
|
||||
var fakeConsulServiceDiscoveryUrl = $"http://localhost:{consulPort}";
|
||||
|
||||
// http://localhost:8362/api/user/info?id=1 is ok
|
||||
// http://localhost:3164/api/user/info?id=1 is ok
|
||||
// http://localhost:8330/api/product/info?id=1 is ok
|
||||
// http://localhost:3164/api/product/info?id=1 is 404
|
||||
|
||||
// is my configuration.json
|
||||
// {
|
||||
// "ReRoutes": [
|
||||
// //{
|
||||
// // "DownstreamPathTemplate": "/{product}",
|
||||
// // "DownstreamScheme": "http",
|
||||
// // "UpstreamPathTemplate": "/{product}",
|
||||
// // "UpstreamHttpMethod": [ "Get", "Post" ],
|
||||
// // "ServiceName": "api-product",
|
||||
// // "LoadBalancer": "LeastConnection",
|
||||
// // "UseServiceDiscovery": true
|
||||
// //},
|
||||
// //{
|
||||
// // "DownstreamPathTemplate": "/{user}",
|
||||
// // "DownstreamScheme": "http",
|
||||
// // "UpstreamPathTemplate": "/{user}",
|
||||
// // "UpstreamHttpMethod": [ "Get", "Post" ],
|
||||
// // "ServiceName": "api-user",
|
||||
// // "LoadBalancer": "LeastConnection",
|
||||
// // "UseServiceDiscovery": true
|
||||
// //},
|
||||
// {
|
||||
// "DownstreamPathTemplate": "/api/user/{user}",
|
||||
// "DownstreamScheme": "http",
|
||||
// "DownstreamHost": "localhost",
|
||||
// "DownstreamPort": 8362,
|
||||
// "UpstreamPathTemplate": "/api/user/{user}",
|
||||
// "UpstreamHttpMethod": [ "Get" ]
|
||||
// },
|
||||
// {
|
||||
// "DownstreamPathTemplate": "/api/product/{product}",
|
||||
// "DownstreamScheme": "http",
|
||||
// "DownstreamHost": "localhost",
|
||||
// "DownstreamPort": 8330,
|
||||
// "UpstreamPathTemplate": "//api/product/{product}",
|
||||
// "UpstreamHttpMethod": [ "Get" ]
|
||||
// }
|
||||
// ],
|
||||
// "GlobalConfiguration": {
|
||||
// "ServiceDiscoveryProvider": {
|
||||
// "Host": "localhost",
|
||||
// "Port": 8500
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
var configuration = new FileConfiguration
|
||||
{
|
||||
ReRoutes = new List<FileReRoute>
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamPathTemplate = "/api/user/{user}",
|
||||
DownstreamScheme = "http",
|
||||
DownstreamHost = "localhost",
|
||||
DownstreamPort = 8362,
|
||||
UpstreamPathTemplate = "/api/user/{user}",
|
||||
UpstreamHttpMethod = new List<string> { "Get" },
|
||||
},
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamPathTemplate = "/api/product/{product}",
|
||||
DownstreamScheme = "http",
|
||||
DownstreamHost = "localhost",
|
||||
DownstreamPort = 8330,
|
||||
UpstreamPathTemplate = "/api/product/{product}",
|
||||
UpstreamHttpMethod = new List<string> { "Get" },
|
||||
}
|
||||
},
|
||||
GlobalConfiguration = new FileGlobalConfiguration()
|
||||
{
|
||||
ServiceDiscoveryProvider = new FileServiceDiscoveryProvider()
|
||||
{
|
||||
Host = "localhost",
|
||||
Port = consulPort
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenProductServiceOneIsRunning(downstreamServiceOneUrl, "/api/user/info", 200, "user"))
|
||||
.And(x => x.GivenProductServiceTwoIsRunning(downstreamServiceTwoUrl, "/api/product/info", 200, "product"))
|
||||
.And(x => x.GivenThereIsAFakeConsulServiceDiscoveryProvider(fakeConsulServiceDiscoveryUrl))
|
||||
.And(x => _steps.GivenThereIsAConfiguration(configuration))
|
||||
.And(x => _steps.GivenOcelotIsRunning())
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/api/user/info?id=1"))
|
||||
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
|
||||
.And(x => _steps.ThenTheResponseBodyShouldBe("user"))
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/api/product/info?id=1"))
|
||||
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
|
||||
.And(x => _steps.ThenTheResponseBodyShouldBe("product"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenThereIsAFakeConsulServiceDiscoveryProvider(string url)
|
||||
{
|
||||
_fakeConsulBuilder = new WebHostBuilder()
|
||||
.UseUrls(url)
|
||||
.UseKestrel()
|
||||
.UseContentRoot(Directory.GetCurrentDirectory())
|
||||
.UseIISIntegration()
|
||||
.UseUrls(url)
|
||||
.Configure(app =>
|
||||
{
|
||||
app.Run(async context =>
|
||||
{
|
||||
if(context.Request.Path.Value == "/v1/health/service/product")
|
||||
{
|
||||
await context.Response.WriteJsonAsync(_serviceEntries);
|
||||
}
|
||||
});
|
||||
})
|
||||
.Build();
|
||||
|
||||
_fakeConsulBuilder.Start();
|
||||
}
|
||||
|
||||
private void GivenProductServiceOneIsRunning(string baseUrl, string basePath, int statusCode, string responseBody)
|
||||
{
|
||||
_builderOne = new WebHostBuilder()
|
||||
.UseUrls(baseUrl)
|
||||
.UseKestrel()
|
||||
.UseContentRoot(Directory.GetCurrentDirectory())
|
||||
.UseIISIntegration()
|
||||
.Configure(app =>
|
||||
{
|
||||
app.UsePathBase(basePath);
|
||||
app.Run(async context =>
|
||||
{
|
||||
_downstreamPathOne = !string.IsNullOrEmpty(context.Request.PathBase.Value) ? context.Request.PathBase.Value : context.Request.Path.Value;
|
||||
|
||||
if(_downstreamPathOne != basePath)
|
||||
{
|
||||
context.Response.StatusCode = statusCode;
|
||||
await context.Response.WriteAsync("downstream path didnt match base path");
|
||||
}
|
||||
else
|
||||
{
|
||||
context.Response.StatusCode = statusCode;
|
||||
await context.Response.WriteAsync(responseBody);
|
||||
}
|
||||
});
|
||||
})
|
||||
.Build();
|
||||
|
||||
_builderOne.Start();
|
||||
}
|
||||
|
||||
private void GivenProductServiceTwoIsRunning(string baseUrl, string basePath, int statusCode, string responseBody)
|
||||
{
|
||||
_builderTwo = new WebHostBuilder()
|
||||
.UseUrls(baseUrl)
|
||||
.UseKestrel()
|
||||
.UseContentRoot(Directory.GetCurrentDirectory())
|
||||
.UseIISIntegration()
|
||||
.Configure(app =>
|
||||
{
|
||||
app.UsePathBase(basePath);
|
||||
app.Run(async context =>
|
||||
{
|
||||
_downstreamPathTwo = !string.IsNullOrEmpty(context.Request.PathBase.Value) ? context.Request.PathBase.Value : context.Request.Path.Value;
|
||||
|
||||
if(_downstreamPathTwo != basePath)
|
||||
{
|
||||
context.Response.StatusCode = statusCode;
|
||||
await context.Response.WriteAsync("downstream path didnt match base path");
|
||||
}
|
||||
else
|
||||
{
|
||||
context.Response.StatusCode = statusCode;
|
||||
await context.Response.WriteAsync(responseBody);
|
||||
}
|
||||
});
|
||||
})
|
||||
.Build();
|
||||
|
||||
_builderTwo.Start();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_builderOne?.Dispose();
|
||||
_builderTwo?.Dispose();
|
||||
_steps.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using Consul;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Ocelot.Configuration.File;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.AcceptanceTests
|
||||
{
|
||||
public class TwoDownstreamServicesTests : IDisposable
|
||||
{
|
||||
private IWebHost _builderOne;
|
||||
private IWebHost _builderTwo;
|
||||
private IWebHost _fakeConsulBuilder;
|
||||
private readonly Steps _steps;
|
||||
private readonly List<ServiceEntry> _serviceEntries;
|
||||
private string _downstreamPathOne;
|
||||
private string _downstreamPathTwo;
|
||||
|
||||
public TwoDownstreamServicesTests()
|
||||
{
|
||||
_steps = new Steps();
|
||||
_serviceEntries = new List<ServiceEntry>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_fix_issue_194()
|
||||
{
|
||||
var consulPort = 8503;
|
||||
var downstreamServiceOneUrl = "http://localhost:8362";
|
||||
var downstreamServiceTwoUrl = "http://localhost:8330";
|
||||
var fakeConsulServiceDiscoveryUrl = $"http://localhost:{consulPort}";
|
||||
|
||||
var configuration = new FileConfiguration
|
||||
{
|
||||
ReRoutes = new List<FileReRoute>
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamPathTemplate = "/api/user/{user}",
|
||||
DownstreamScheme = "http",
|
||||
DownstreamHostAndPorts = new List<FileHostAndPort>
|
||||
{
|
||||
new FileHostAndPort
|
||||
{
|
||||
Host = "localhost",
|
||||
Port = 8362,
|
||||
}
|
||||
},
|
||||
UpstreamPathTemplate = "/api/user/{user}",
|
||||
UpstreamHttpMethod = new List<string> { "Get" },
|
||||
},
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamPathTemplate = "/api/product/{product}",
|
||||
DownstreamScheme = "http",
|
||||
DownstreamHostAndPorts = new List<FileHostAndPort>
|
||||
{
|
||||
new FileHostAndPort
|
||||
{
|
||||
Host = "localhost",
|
||||
Port = 8330,
|
||||
}
|
||||
},
|
||||
UpstreamPathTemplate = "/api/product/{product}",
|
||||
UpstreamHttpMethod = new List<string> { "Get" },
|
||||
}
|
||||
},
|
||||
GlobalConfiguration = new FileGlobalConfiguration()
|
||||
{
|
||||
ServiceDiscoveryProvider = new FileServiceDiscoveryProvider()
|
||||
{
|
||||
Host = "localhost",
|
||||
Port = consulPort
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenProductServiceOneIsRunning(downstreamServiceOneUrl, "/api/user/info", 200, "user"))
|
||||
.And(x => x.GivenProductServiceTwoIsRunning(downstreamServiceTwoUrl, "/api/product/info", 200, "product"))
|
||||
.And(x => x.GivenThereIsAFakeConsulServiceDiscoveryProvider(fakeConsulServiceDiscoveryUrl))
|
||||
.And(x => _steps.GivenThereIsAConfiguration(configuration))
|
||||
.And(x => _steps.GivenOcelotIsRunning())
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/api/user/info?id=1"))
|
||||
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
|
||||
.And(x => _steps.ThenTheResponseBodyShouldBe("user"))
|
||||
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/api/product/info?id=1"))
|
||||
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
|
||||
.And(x => _steps.ThenTheResponseBodyShouldBe("product"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenThereIsAFakeConsulServiceDiscoveryProvider(string url)
|
||||
{
|
||||
_fakeConsulBuilder = new WebHostBuilder()
|
||||
.UseUrls(url)
|
||||
.UseKestrel()
|
||||
.UseContentRoot(Directory.GetCurrentDirectory())
|
||||
.UseIISIntegration()
|
||||
.UseUrls(url)
|
||||
.Configure(app =>
|
||||
{
|
||||
app.Run(async context =>
|
||||
{
|
||||
if(context.Request.Path.Value == "/v1/health/service/product")
|
||||
{
|
||||
await context.Response.WriteJsonAsync(_serviceEntries);
|
||||
}
|
||||
});
|
||||
})
|
||||
.Build();
|
||||
|
||||
_fakeConsulBuilder.Start();
|
||||
}
|
||||
|
||||
private void GivenProductServiceOneIsRunning(string baseUrl, string basePath, int statusCode, string responseBody)
|
||||
{
|
||||
_builderOne = new WebHostBuilder()
|
||||
.UseUrls(baseUrl)
|
||||
.UseKestrel()
|
||||
.UseContentRoot(Directory.GetCurrentDirectory())
|
||||
.UseIISIntegration()
|
||||
.Configure(app =>
|
||||
{
|
||||
app.UsePathBase(basePath);
|
||||
app.Run(async context =>
|
||||
{
|
||||
_downstreamPathOne = !string.IsNullOrEmpty(context.Request.PathBase.Value) ? context.Request.PathBase.Value : context.Request.Path.Value;
|
||||
|
||||
if(_downstreamPathOne != basePath)
|
||||
{
|
||||
context.Response.StatusCode = statusCode;
|
||||
await context.Response.WriteAsync("downstream path didnt match base path");
|
||||
}
|
||||
else
|
||||
{
|
||||
context.Response.StatusCode = statusCode;
|
||||
await context.Response.WriteAsync(responseBody);
|
||||
}
|
||||
});
|
||||
})
|
||||
.Build();
|
||||
|
||||
_builderOne.Start();
|
||||
}
|
||||
|
||||
private void GivenProductServiceTwoIsRunning(string baseUrl, string basePath, int statusCode, string responseBody)
|
||||
{
|
||||
_builderTwo = new WebHostBuilder()
|
||||
.UseUrls(baseUrl)
|
||||
.UseKestrel()
|
||||
.UseContentRoot(Directory.GetCurrentDirectory())
|
||||
.UseIISIntegration()
|
||||
.Configure(app =>
|
||||
{
|
||||
app.UsePathBase(basePath);
|
||||
app.Run(async context =>
|
||||
{
|
||||
_downstreamPathTwo = !string.IsNullOrEmpty(context.Request.PathBase.Value) ? context.Request.PathBase.Value : context.Request.Path.Value;
|
||||
|
||||
if(_downstreamPathTwo != basePath)
|
||||
{
|
||||
context.Response.StatusCode = statusCode;
|
||||
await context.Response.WriteAsync("downstream path didnt match base path");
|
||||
}
|
||||
else
|
||||
{
|
||||
context.Response.StatusCode = statusCode;
|
||||
await context.Response.WriteAsync(responseBody);
|
||||
}
|
||||
});
|
||||
})
|
||||
.Build();
|
||||
|
||||
_builderTwo.Start();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_builderOne?.Dispose();
|
||||
_builderTwo?.Dispose();
|
||||
_steps.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,10 +1,10 @@
|
||||
{
|
||||
"Logging": {
|
||||
"IncludeScopes": true,
|
||||
"LogLevel": {
|
||||
"Default": "Error",
|
||||
"System": "Error",
|
||||
"Microsoft": "Error"
|
||||
}
|
||||
}
|
||||
}
|
||||
{
|
||||
"Logging": {
|
||||
"IncludeScopes": true,
|
||||
"LogLevel": {
|
||||
"Default": "Error",
|
||||
"System": "Error",
|
||||
"Microsoft": "Error"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1 +1,59 @@
|
||||
{"ReRoutes":[{"DownstreamPathTemplate":"41879/","UpstreamPathTemplate":"/","UpstreamHttpMethod":"Get","AuthenticationOptions":{"Provider":null,"ProviderRootUrl":null,"ApiName":null,"RequireHttps":false,"AllowedScopes":[],"ApiSecret":null},"AddHeadersToRequest":{},"AddClaimsToRequest":{},"RouteClaimsRequirement":{},"AddQueriesToRequest":{},"RequestIdKey":null,"FileCacheOptions":{"TtlSeconds":0},"ReRouteIsCaseSensitive":false,"ServiceName":null,"DownstreamScheme":"http","DownstreamHost":"localhost","DownstreamPort":41879,"QoSOptions":{"ExceptionsAllowedBeforeBreaking":0,"DurationOfBreak":0,"TimeoutValue":0},"LoadBalancer":null,"RateLimitOptions":{"ClientWhitelist":[],"EnableRateLimiting":false,"Period":null,"PeriodTimespan":0.0,"Limit":0}}],"GlobalConfiguration":{"RequestIdKey":null,"ServiceDiscoveryProvider":{"Provider":null,"Host":null,"Port":0},"AdministrationPath":null,"RateLimitOptions":{"ClientIdHeader":"ClientId","QuotaExceededMessage":null,"RateLimitCounterPrefix":"ocelot","DisableRateLimitHeaders":false,"HttpStatusCode":429}}}
|
||||
{
|
||||
"ReRoutes": [
|
||||
{
|
||||
"DownstreamPathTemplate": "41879/",
|
||||
"UpstreamPathTemplate": "/",
|
||||
"UpstreamHttpMethod": "Get",
|
||||
"AuthenticationOptions": {
|
||||
"Provider": null,
|
||||
"ProviderRootUrl": null,
|
||||
"ApiName": null,
|
||||
"RequireHttps": false,
|
||||
"AllowedScopes": [],
|
||||
"ApiSecret": null
|
||||
},
|
||||
"AddHeadersToRequest": {},
|
||||
"AddClaimsToRequest": {},
|
||||
"RouteClaimsRequirement": {},
|
||||
"AddQueriesToRequest": {},
|
||||
"RequestIdKey": null,
|
||||
"FileCacheOptions": {
|
||||
"TtlSeconds": 0
|
||||
},
|
||||
"ReRouteIsCaseSensitive": false,
|
||||
"ServiceName": null,
|
||||
"DownstreamScheme": "http",
|
||||
"DownstreamHost": "localhost",
|
||||
"DownstreamPort": 41879,
|
||||
"QoSOptions": {
|
||||
"ExceptionsAllowedBeforeBreaking": 0,
|
||||
"DurationOfBreak": 0,
|
||||
"TimeoutValue": 0
|
||||
},
|
||||
"LoadBalancer": null,
|
||||
"RateLimitOptions": {
|
||||
"ClientWhitelist": [],
|
||||
"EnableRateLimiting": false,
|
||||
"Period": null,
|
||||
"PeriodTimespan": 0,
|
||||
"Limit": 0
|
||||
}
|
||||
}
|
||||
],
|
||||
"GlobalConfiguration": {
|
||||
"RequestIdKey": null,
|
||||
"ServiceDiscoveryProvider": {
|
||||
"Provider": null,
|
||||
"Host": null,
|
||||
"Port": 0
|
||||
},
|
||||
"AdministrationPath": null,
|
||||
"RateLimitOptions": {
|
||||
"ClientIdHeader": "ClientId",
|
||||
"QuotaExceededMessage": null,
|
||||
"RateLimitCounterPrefix": "ocelot",
|
||||
"DisableRateLimitHeaders": false,
|
||||
"HttpStatusCode": 429
|
||||
}
|
||||
}
|
||||
}
|
@ -1,24 +1,24 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<VersionPrefix>0.0.0-dev</VersionPrefix>
|
||||
<TargetFramework>netcoreapp2.0</TargetFramework>
|
||||
<RuntimeFrameworkVersion>2.0.0</RuntimeFrameworkVersion>
|
||||
<AssemblyName>Ocelot.Benchmarks</AssemblyName>
|
||||
<OutputType>Exe</OutputType>
|
||||
<PackageId>Ocelot.Benchmarks</PackageId>
|
||||
<RuntimeIdentifiers>osx.10.11-x64;osx.10.12-x64;win7-x64;win10-x64</RuntimeIdentifiers>
|
||||
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
|
||||
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
|
||||
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\src\Ocelot\Ocelot.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="BenchmarkDotNet" Version="0.10.9" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<VersionPrefix>0.0.0-dev</VersionPrefix>
|
||||
<TargetFramework>netcoreapp2.0</TargetFramework>
|
||||
<RuntimeFrameworkVersion>2.0.0</RuntimeFrameworkVersion>
|
||||
<AssemblyName>Ocelot.Benchmarks</AssemblyName>
|
||||
<OutputType>Exe</OutputType>
|
||||
<PackageId>Ocelot.Benchmarks</PackageId>
|
||||
<RuntimeIdentifiers>osx.10.11-x64;osx.10.12-x64;win7-x64;win10-x64</RuntimeIdentifiers>
|
||||
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
|
||||
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
|
||||
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\src\Ocelot\Ocelot.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="BenchmarkDotNet" Version="0.10.9" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
@ -1,17 +1,17 @@
|
||||
using BenchmarkDotNet.Running;
|
||||
|
||||
namespace Ocelot.Benchmarks
|
||||
{
|
||||
public class Program
|
||||
{
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
var switcher = new BenchmarkSwitcher(new[] {
|
||||
typeof(UrlPathToUrlPathTemplateMatcherBenchmarks),
|
||||
|
||||
});
|
||||
|
||||
switcher.Run(args);
|
||||
}
|
||||
}
|
||||
}
|
||||
using BenchmarkDotNet.Running;
|
||||
|
||||
namespace Ocelot.Benchmarks
|
||||
{
|
||||
public class Program
|
||||
{
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
var switcher = new BenchmarkSwitcher(new[] {
|
||||
typeof(UrlPathToUrlPathTemplateMatcherBenchmarks),
|
||||
|
||||
});
|
||||
|
||||
switcher.Run(args);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,19 +1,19 @@
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
// General Information about an assembly is controlled through the following
|
||||
// set of attributes. Change these attribute values to modify the information
|
||||
// associated with an assembly.
|
||||
[assembly: AssemblyConfiguration("")]
|
||||
[assembly: AssemblyCompany("")]
|
||||
[assembly: AssemblyProduct("Ocelot.Benchmarks")]
|
||||
[assembly: AssemblyTrademark("")]
|
||||
|
||||
// Setting ComVisible to false makes the types in this assembly not visible
|
||||
// to COM components. If you need to access a type in this assembly from
|
||||
// COM, set the ComVisible attribute to true on that type.
|
||||
[assembly: ComVisible(false)]
|
||||
|
||||
// The following GUID is for the ID of the typelib if this project is exposed to COM
|
||||
[assembly: Guid("106b49e6-95f6-4a7b-b81c-96bfa74af035")]
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
// General Information about an assembly is controlled through the following
|
||||
// set of attributes. Change these attribute values to modify the information
|
||||
// associated with an assembly.
|
||||
[assembly: AssemblyConfiguration("")]
|
||||
[assembly: AssemblyCompany("")]
|
||||
[assembly: AssemblyProduct("Ocelot.Benchmarks")]
|
||||
[assembly: AssemblyTrademark("")]
|
||||
|
||||
// Setting ComVisible to false makes the types in this assembly not visible
|
||||
// to COM components. If you need to access a type in this assembly from
|
||||
// COM, set the ComVisible attribute to true on that type.
|
||||
[assembly: ComVisible(false)]
|
||||
|
||||
// The following GUID is for the ID of the typelib if this project is exposed to COM
|
||||
[assembly: Guid("106b49e6-95f6-4a7b-b81c-96bfa74af035")]
|
||||
|
@ -1,40 +1,40 @@
|
||||
using BenchmarkDotNet.Attributes;
|
||||
using BenchmarkDotNet.Columns;
|
||||
using BenchmarkDotNet.Configs;
|
||||
using Ocelot.DownstreamRouteFinder.UrlMatcher;
|
||||
|
||||
namespace Ocelot.Benchmarks
|
||||
{
|
||||
[Config(typeof(UrlPathToUrlPathTemplateMatcherBenchmarks))]
|
||||
public class UrlPathToUrlPathTemplateMatcherBenchmarks : ManualConfig
|
||||
{
|
||||
private RegExUrlMatcher _urlPathMatcher;
|
||||
private string _downstreamUrlPath;
|
||||
private string _downstreamUrlPathTemplate;
|
||||
|
||||
public UrlPathToUrlPathTemplateMatcherBenchmarks()
|
||||
{
|
||||
Add(StatisticColumn.AllStatistics);
|
||||
}
|
||||
|
||||
[Setup]
|
||||
public void SetUp()
|
||||
{
|
||||
_urlPathMatcher = new RegExUrlMatcher();
|
||||
_downstreamUrlPath = "api/product/products/1/variants/?soldout=false";
|
||||
_downstreamUrlPathTemplate = "api/product/products/{productId}/variants/";
|
||||
}
|
||||
|
||||
[Benchmark]
|
||||
public void Benchmark1()
|
||||
{
|
||||
_urlPathMatcher.Match(_downstreamUrlPath, _downstreamUrlPathTemplate);
|
||||
}
|
||||
|
||||
[Benchmark]
|
||||
public void Benchmark2()
|
||||
{
|
||||
_urlPathMatcher.Match(_downstreamUrlPath, _downstreamUrlPathTemplate);
|
||||
}
|
||||
}
|
||||
using BenchmarkDotNet.Attributes;
|
||||
using BenchmarkDotNet.Columns;
|
||||
using BenchmarkDotNet.Configs;
|
||||
using Ocelot.DownstreamRouteFinder.UrlMatcher;
|
||||
|
||||
namespace Ocelot.Benchmarks
|
||||
{
|
||||
[Config(typeof(UrlPathToUrlPathTemplateMatcherBenchmarks))]
|
||||
public class UrlPathToUrlPathTemplateMatcherBenchmarks : ManualConfig
|
||||
{
|
||||
private RegExUrlMatcher _urlPathMatcher;
|
||||
private string _downstreamUrlPath;
|
||||
private string _downstreamUrlPathTemplate;
|
||||
|
||||
public UrlPathToUrlPathTemplateMatcherBenchmarks()
|
||||
{
|
||||
Add(StatisticColumn.AllStatistics);
|
||||
}
|
||||
|
||||
[Setup]
|
||||
public void SetUp()
|
||||
{
|
||||
_urlPathMatcher = new RegExUrlMatcher();
|
||||
_downstreamUrlPath = "api/product/products/1/variants/?soldout=false";
|
||||
_downstreamUrlPathTemplate = "api/product/products/{productId}/variants/";
|
||||
}
|
||||
|
||||
[Benchmark]
|
||||
public void Benchmark1()
|
||||
{
|
||||
_urlPathMatcher.Match(_downstreamUrlPath, _downstreamUrlPathTemplate);
|
||||
}
|
||||
|
||||
[Benchmark]
|
||||
public void Benchmark2()
|
||||
{
|
||||
_urlPathMatcher.Match(_downstreamUrlPath, _downstreamUrlPathTemplate);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,415 +1,469 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Net.Http.Headers;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Newtonsoft.Json;
|
||||
using Ocelot.Cache;
|
||||
using Ocelot.Configuration.File;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
[assembly: CollectionBehavior(DisableTestParallelization = true)]
|
||||
namespace Ocelot.IntegrationTests
|
||||
{
|
||||
public class AdministrationTests : IDisposable
|
||||
{
|
||||
private readonly HttpClient _httpClient;
|
||||
private readonly HttpClient _httpClientTwo;
|
||||
private HttpResponseMessage _response;
|
||||
private IWebHost _builder;
|
||||
private IWebHostBuilder _webHostBuilder;
|
||||
private readonly string _ocelotBaseUrl;
|
||||
private BearerToken _token;
|
||||
private IWebHostBuilder _webHostBuilderTwo;
|
||||
private IWebHost _builderTwo;
|
||||
|
||||
public AdministrationTests()
|
||||
{
|
||||
_httpClient = new HttpClient();
|
||||
_httpClientTwo = new HttpClient();
|
||||
_ocelotBaseUrl = "http://localhost:5000";
|
||||
_httpClient.BaseAddress = new Uri(_ocelotBaseUrl);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_response_401_with_call_re_routes_controller()
|
||||
{
|
||||
var configuration = new FileConfiguration();
|
||||
|
||||
this.Given(x => GivenThereIsAConfiguration(configuration))
|
||||
.And(x => GivenOcelotIsRunning())
|
||||
.When(x => WhenIGetUrlOnTheApiGateway("/administration/configuration"))
|
||||
.Then(x => ThenTheStatusCodeShouldBe(HttpStatusCode.Unauthorized))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_response_200_with_call_re_routes_controller()
|
||||
{
|
||||
var configuration = new FileConfiguration();
|
||||
|
||||
this.Given(x => GivenThereIsAConfiguration(configuration))
|
||||
.And(x => GivenOcelotIsRunning())
|
||||
.And(x => GivenIHaveAnOcelotToken("/administration"))
|
||||
.And(x => GivenIHaveAddedATokenToMyRequest())
|
||||
.When(x => WhenIGetUrlOnTheApiGateway("/administration/configuration"))
|
||||
.Then(x => ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_be_able_to_use_token_from_ocelot_a_on_ocelot_b()
|
||||
{
|
||||
var configuration = new FileConfiguration();
|
||||
|
||||
this.Given(x => GivenThereIsAConfiguration(configuration))
|
||||
.And(x => GivenIdentityServerSigningEnvironmentalVariablesAreSet())
|
||||
.And(x => GivenOcelotIsRunning())
|
||||
.And(x => GivenIHaveAnOcelotToken("/administration"))
|
||||
.And(x => GivenAnotherOcelotIsRunning("http://localhost:5007"))
|
||||
.When(x => WhenIGetUrlOnTheSecondOcelot("/administration/configuration"))
|
||||
.Then(x => ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_file_configuration()
|
||||
{
|
||||
var configuration = new FileConfiguration
|
||||
{
|
||||
GlobalConfiguration = new FileGlobalConfiguration
|
||||
{
|
||||
RequestIdKey = "RequestId",
|
||||
ServiceDiscoveryProvider = new FileServiceDiscoveryProvider
|
||||
{
|
||||
Host = "127.0.0.1",
|
||||
}
|
||||
|
||||
},
|
||||
ReRoutes = new List<FileReRoute>()
|
||||
{
|
||||
new FileReRoute()
|
||||
{
|
||||
DownstreamHost = "localhost",
|
||||
DownstreamPort = 80,
|
||||
DownstreamScheme = "https",
|
||||
DownstreamPathTemplate = "/",
|
||||
UpstreamHttpMethod = new List<string> { "get" },
|
||||
UpstreamPathTemplate = "/",
|
||||
FileCacheOptions = new FileCacheOptions
|
||||
{
|
||||
TtlSeconds = 10,
|
||||
Region = "Geoff"
|
||||
}
|
||||
},
|
||||
new FileReRoute()
|
||||
{
|
||||
DownstreamHost = "localhost",
|
||||
DownstreamPort = 80,
|
||||
DownstreamScheme = "https",
|
||||
DownstreamPathTemplate = "/",
|
||||
UpstreamHttpMethod = new List<string> { "get" },
|
||||
UpstreamPathTemplate = "/test",
|
||||
FileCacheOptions = new FileCacheOptions
|
||||
{
|
||||
TtlSeconds = 10,
|
||||
Region = "Dave"
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.Given(x => GivenThereIsAConfiguration(configuration))
|
||||
.And(x => GivenOcelotIsRunning())
|
||||
.And(x => GivenIHaveAnOcelotToken("/administration"))
|
||||
.And(x => GivenIHaveAddedATokenToMyRequest())
|
||||
.When(x => WhenIGetUrlOnTheApiGateway("/administration/configuration"))
|
||||
.Then(x => ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
|
||||
.And(x => ThenTheResponseShouldBe(configuration))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_get_file_configuration_edit_and_post_updated_version()
|
||||
{
|
||||
var initialConfiguration = new FileConfiguration
|
||||
{
|
||||
GlobalConfiguration = new FileGlobalConfiguration
|
||||
{
|
||||
},
|
||||
ReRoutes = new List<FileReRoute>()
|
||||
{
|
||||
new FileReRoute()
|
||||
{
|
||||
DownstreamHost = "localhost",
|
||||
DownstreamPort = 80,
|
||||
DownstreamScheme = "https",
|
||||
DownstreamPathTemplate = "/",
|
||||
UpstreamHttpMethod = new List<string> { "get" },
|
||||
UpstreamPathTemplate = "/"
|
||||
},
|
||||
new FileReRoute()
|
||||
{
|
||||
DownstreamHost = "localhost",
|
||||
DownstreamPort = 80,
|
||||
DownstreamScheme = "https",
|
||||
DownstreamPathTemplate = "/",
|
||||
UpstreamHttpMethod = new List<string> { "get" },
|
||||
UpstreamPathTemplate = "/test"
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
var updatedConfiguration = new FileConfiguration
|
||||
{
|
||||
GlobalConfiguration = new FileGlobalConfiguration
|
||||
{
|
||||
},
|
||||
ReRoutes = new List<FileReRoute>()
|
||||
{
|
||||
new FileReRoute()
|
||||
{
|
||||
DownstreamHost = "127.0.0.1",
|
||||
DownstreamPort = 80,
|
||||
DownstreamScheme = "http",
|
||||
DownstreamPathTemplate = "/geoffrey",
|
||||
UpstreamHttpMethod = new List<string> { "get" },
|
||||
UpstreamPathTemplate = "/"
|
||||
},
|
||||
new FileReRoute()
|
||||
{
|
||||
DownstreamHost = "123.123.123",
|
||||
DownstreamPort = 443,
|
||||
DownstreamScheme = "https",
|
||||
DownstreamPathTemplate = "/blooper/{productId}",
|
||||
UpstreamHttpMethod = new List<string> { "post" },
|
||||
UpstreamPathTemplate = "/test"
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.Given(x => GivenThereIsAConfiguration(initialConfiguration))
|
||||
.And(x => GivenOcelotIsRunning())
|
||||
.And(x => GivenIHaveAnOcelotToken("/administration"))
|
||||
.And(x => GivenIHaveAddedATokenToMyRequest())
|
||||
.When(x => WhenIGetUrlOnTheApiGateway("/administration/configuration"))
|
||||
.When(x => WhenIPostOnTheApiGateway("/administration/configuration", updatedConfiguration))
|
||||
.Then(x => ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
|
||||
.And(x => ThenTheResponseShouldBe(updatedConfiguration))
|
||||
.When(x => WhenIGetUrlOnTheApiGateway("/administration/configuration"))
|
||||
.And(x => ThenTheResponseShouldBe(updatedConfiguration))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_clear_region()
|
||||
{
|
||||
var initialConfiguration = new FileConfiguration
|
||||
{
|
||||
GlobalConfiguration = new FileGlobalConfiguration
|
||||
{
|
||||
},
|
||||
ReRoutes = new List<FileReRoute>()
|
||||
{
|
||||
new FileReRoute()
|
||||
{
|
||||
DownstreamHost = "localhost",
|
||||
DownstreamPort = 80,
|
||||
DownstreamScheme = "https",
|
||||
DownstreamPathTemplate = "/",
|
||||
UpstreamHttpMethod = new List<string> { "get" },
|
||||
UpstreamPathTemplate = "/",
|
||||
FileCacheOptions = new FileCacheOptions
|
||||
{
|
||||
TtlSeconds = 10
|
||||
}
|
||||
},
|
||||
new FileReRoute()
|
||||
{
|
||||
DownstreamHost = "localhost",
|
||||
DownstreamPort = 80,
|
||||
DownstreamScheme = "https",
|
||||
DownstreamPathTemplate = "/",
|
||||
UpstreamHttpMethod = new List<string> { "get" },
|
||||
UpstreamPathTemplate = "/test",
|
||||
FileCacheOptions = new FileCacheOptions
|
||||
{
|
||||
TtlSeconds = 10
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
var regionToClear = "gettest";
|
||||
|
||||
this.Given(x => GivenThereIsAConfiguration(initialConfiguration))
|
||||
.And(x => GivenOcelotIsRunning())
|
||||
.And(x => GivenIHaveAnOcelotToken("/administration"))
|
||||
.And(x => GivenIHaveAddedATokenToMyRequest())
|
||||
.When(x => WhenIDeleteOnTheApiGateway($"/administration/outputcache/{regionToClear}"))
|
||||
.Then(x => ThenTheStatusCodeShouldBe(HttpStatusCode.NoContent))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenAnotherOcelotIsRunning(string baseUrl)
|
||||
{
|
||||
_httpClientTwo.BaseAddress = new Uri(baseUrl);
|
||||
|
||||
_webHostBuilderTwo = new WebHostBuilder()
|
||||
.UseUrls(baseUrl)
|
||||
.UseKestrel()
|
||||
.UseContentRoot(Directory.GetCurrentDirectory())
|
||||
.ConfigureServices(x => {
|
||||
x.AddSingleton(_webHostBuilderTwo);
|
||||
})
|
||||
.UseStartup<IntegrationTestsStartup>();
|
||||
|
||||
_builderTwo = _webHostBuilderTwo.Build();
|
||||
|
||||
_builderTwo.Start();
|
||||
}
|
||||
|
||||
private void GivenIdentityServerSigningEnvironmentalVariablesAreSet()
|
||||
{
|
||||
Environment.SetEnvironmentVariable("OCELOT_CERTIFICATE", "idsrv3test.pfx");
|
||||
Environment.SetEnvironmentVariable("OCELOT_CERTIFICATE_PASSWORD", "idsrv3test");
|
||||
}
|
||||
|
||||
private void WhenIGetUrlOnTheSecondOcelot(string url)
|
||||
{
|
||||
_httpClientTwo.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _token.AccessToken);
|
||||
_response = _httpClientTwo.GetAsync(url).Result;
|
||||
}
|
||||
|
||||
private void WhenIPostOnTheApiGateway(string url, FileConfiguration updatedConfiguration)
|
||||
{
|
||||
var json = JsonConvert.SerializeObject(updatedConfiguration);
|
||||
var content = new StringContent(json);
|
||||
content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
|
||||
_response = _httpClient.PostAsync(url, content).Result;
|
||||
}
|
||||
|
||||
private void ThenTheResponseShouldBe(List<string> expected)
|
||||
{
|
||||
var content = _response.Content.ReadAsStringAsync().Result;
|
||||
var result = JsonConvert.DeserializeObject<Regions>(content);
|
||||
result.Value.ShouldBe(expected);
|
||||
}
|
||||
|
||||
private void ThenTheResponseShouldBe(FileConfiguration expected)
|
||||
{
|
||||
var response = JsonConvert.DeserializeObject<FileConfiguration>(_response.Content.ReadAsStringAsync().Result);
|
||||
|
||||
response.GlobalConfiguration.RequestIdKey.ShouldBe(expected.GlobalConfiguration.RequestIdKey);
|
||||
response.GlobalConfiguration.ServiceDiscoveryProvider.Host.ShouldBe(expected.GlobalConfiguration.ServiceDiscoveryProvider.Host);
|
||||
response.GlobalConfiguration.ServiceDiscoveryProvider.Port.ShouldBe(expected.GlobalConfiguration.ServiceDiscoveryProvider.Port);
|
||||
|
||||
for (var i = 0; i < response.ReRoutes.Count; i++)
|
||||
{
|
||||
response.ReRoutes[i].DownstreamHost.ShouldBe(expected.ReRoutes[i].DownstreamHost);
|
||||
response.ReRoutes[i].DownstreamPathTemplate.ShouldBe(expected.ReRoutes[i].DownstreamPathTemplate);
|
||||
response.ReRoutes[i].DownstreamPort.ShouldBe(expected.ReRoutes[i].DownstreamPort);
|
||||
response.ReRoutes[i].DownstreamScheme.ShouldBe(expected.ReRoutes[i].DownstreamScheme);
|
||||
response.ReRoutes[i].UpstreamPathTemplate.ShouldBe(expected.ReRoutes[i].UpstreamPathTemplate);
|
||||
response.ReRoutes[i].UpstreamHttpMethod.ShouldBe(expected.ReRoutes[i].UpstreamHttpMethod);
|
||||
}
|
||||
}
|
||||
|
||||
private void GivenIHaveAddedATokenToMyRequest()
|
||||
{
|
||||
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _token.AccessToken);
|
||||
}
|
||||
|
||||
private 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>("grant_type", "client_credentials")
|
||||
};
|
||||
var content = new FormUrlEncodedContent(formData);
|
||||
|
||||
var response = _httpClient.PostAsync(tokenUrl, content).Result;
|
||||
var responseContent = response.Content.ReadAsStringAsync().Result;
|
||||
response.EnsureSuccessStatusCode();
|
||||
_token = JsonConvert.DeserializeObject<BearerToken>(responseContent);
|
||||
var configPath = $"{adminPath}/.well-known/openid-configuration";
|
||||
response = _httpClient.GetAsync(configPath).Result;
|
||||
response.EnsureSuccessStatusCode();
|
||||
}
|
||||
|
||||
private void GivenOcelotIsRunning()
|
||||
{
|
||||
_webHostBuilder = new WebHostBuilder()
|
||||
.UseUrls(_ocelotBaseUrl)
|
||||
.UseKestrel()
|
||||
.UseContentRoot(Directory.GetCurrentDirectory())
|
||||
.ConfigureServices(x => {
|
||||
x.AddSingleton(_webHostBuilder);
|
||||
})
|
||||
.UseStartup<IntegrationTestsStartup>();
|
||||
|
||||
_builder = _webHostBuilder.Build();
|
||||
|
||||
_builder.Start();
|
||||
}
|
||||
|
||||
private void GivenThereIsAConfiguration(FileConfiguration fileConfiguration)
|
||||
{
|
||||
var configurationPath = $"{Directory.GetCurrentDirectory()}/configuration.json";
|
||||
|
||||
var jsonConfiguration = JsonConvert.SerializeObject(fileConfiguration);
|
||||
|
||||
if (File.Exists(configurationPath))
|
||||
{
|
||||
File.Delete(configurationPath);
|
||||
}
|
||||
|
||||
File.WriteAllText(configurationPath, jsonConfiguration);
|
||||
|
||||
var text = File.ReadAllText(configurationPath);
|
||||
|
||||
configurationPath = $"{AppContext.BaseDirectory}/configuration.json";
|
||||
|
||||
if (File.Exists(configurationPath))
|
||||
{
|
||||
File.Delete(configurationPath);
|
||||
}
|
||||
|
||||
File.WriteAllText(configurationPath, jsonConfiguration);
|
||||
|
||||
text = File.ReadAllText(configurationPath);
|
||||
}
|
||||
|
||||
private void WhenIGetUrlOnTheApiGateway(string url)
|
||||
{
|
||||
_response = _httpClient.GetAsync(url).Result;
|
||||
}
|
||||
|
||||
private void WhenIDeleteOnTheApiGateway(string url)
|
||||
{
|
||||
_response = _httpClient.DeleteAsync(url).Result;
|
||||
}
|
||||
|
||||
private void ThenTheStatusCodeShouldBe(HttpStatusCode expectedHttpStatusCode)
|
||||
{
|
||||
_response.StatusCode.ShouldBe(expectedHttpStatusCode);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Environment.SetEnvironmentVariable("OCELOT_CERTIFICATE", "");
|
||||
Environment.SetEnvironmentVariable("OCELOT_CERTIFICATE_PASSWORD", "");
|
||||
_builder?.Dispose();
|
||||
_httpClient?.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Net.Http.Headers;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Newtonsoft.Json;
|
||||
using Ocelot.Cache;
|
||||
using Ocelot.Configuration.File;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
[assembly: CollectionBehavior(DisableTestParallelization = true)]
|
||||
namespace Ocelot.IntegrationTests
|
||||
{
|
||||
public class AdministrationTests : IDisposable
|
||||
{
|
||||
private readonly HttpClient _httpClient;
|
||||
private readonly HttpClient _httpClientTwo;
|
||||
private HttpResponseMessage _response;
|
||||
private IWebHost _builder;
|
||||
private IWebHostBuilder _webHostBuilder;
|
||||
private readonly string _ocelotBaseUrl;
|
||||
private BearerToken _token;
|
||||
private IWebHostBuilder _webHostBuilderTwo;
|
||||
private IWebHost _builderTwo;
|
||||
|
||||
public AdministrationTests()
|
||||
{
|
||||
_httpClient = new HttpClient();
|
||||
_httpClientTwo = new HttpClient();
|
||||
_ocelotBaseUrl = "http://localhost:5000";
|
||||
_httpClient.BaseAddress = new Uri(_ocelotBaseUrl);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_response_401_with_call_re_routes_controller()
|
||||
{
|
||||
var configuration = new FileConfiguration();
|
||||
|
||||
this.Given(x => GivenThereIsAConfiguration(configuration))
|
||||
.And(x => GivenOcelotIsRunning())
|
||||
.When(x => WhenIGetUrlOnTheApiGateway("/administration/configuration"))
|
||||
.Then(x => ThenTheStatusCodeShouldBe(HttpStatusCode.Unauthorized))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_response_200_with_call_re_routes_controller()
|
||||
{
|
||||
var configuration = new FileConfiguration();
|
||||
|
||||
this.Given(x => GivenThereIsAConfiguration(configuration))
|
||||
.And(x => GivenOcelotIsRunning())
|
||||
.And(x => GivenIHaveAnOcelotToken("/administration"))
|
||||
.And(x => GivenIHaveAddedATokenToMyRequest())
|
||||
.When(x => WhenIGetUrlOnTheApiGateway("/administration/configuration"))
|
||||
.Then(x => ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_be_able_to_use_token_from_ocelot_a_on_ocelot_b()
|
||||
{
|
||||
var configuration = new FileConfiguration();
|
||||
|
||||
this.Given(x => GivenThereIsAConfiguration(configuration))
|
||||
.And(x => GivenIdentityServerSigningEnvironmentalVariablesAreSet())
|
||||
.And(x => GivenOcelotIsRunning())
|
||||
.And(x => GivenIHaveAnOcelotToken("/administration"))
|
||||
.And(x => GivenAnotherOcelotIsRunning("http://localhost:5007"))
|
||||
.When(x => WhenIGetUrlOnTheSecondOcelot("/administration/configuration"))
|
||||
.Then(x => ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_file_configuration()
|
||||
{
|
||||
var configuration = new FileConfiguration
|
||||
{
|
||||
GlobalConfiguration = new FileGlobalConfiguration
|
||||
{
|
||||
RequestIdKey = "RequestId",
|
||||
ServiceDiscoveryProvider = new FileServiceDiscoveryProvider
|
||||
{
|
||||
Host = "127.0.0.1",
|
||||
}
|
||||
|
||||
},
|
||||
ReRoutes = new List<FileReRoute>()
|
||||
{
|
||||
new FileReRoute()
|
||||
{
|
||||
DownstreamHostAndPorts = new List<FileHostAndPort>
|
||||
{
|
||||
new FileHostAndPort
|
||||
{
|
||||
Host = "localhost",
|
||||
Port = 80,
|
||||
}
|
||||
},
|
||||
DownstreamScheme = "https",
|
||||
DownstreamPathTemplate = "/",
|
||||
UpstreamHttpMethod = new List<string> { "get" },
|
||||
UpstreamPathTemplate = "/",
|
||||
FileCacheOptions = new FileCacheOptions
|
||||
{
|
||||
TtlSeconds = 10,
|
||||
Region = "Geoff"
|
||||
}
|
||||
},
|
||||
new FileReRoute()
|
||||
{
|
||||
DownstreamHostAndPorts = new List<FileHostAndPort>
|
||||
{
|
||||
new FileHostAndPort
|
||||
{
|
||||
Host = "localhost",
|
||||
Port = 80,
|
||||
}
|
||||
},
|
||||
DownstreamScheme = "https",
|
||||
DownstreamPathTemplate = "/",
|
||||
UpstreamHttpMethod = new List<string> { "get" },
|
||||
UpstreamPathTemplate = "/test",
|
||||
FileCacheOptions = new FileCacheOptions
|
||||
{
|
||||
TtlSeconds = 10,
|
||||
Region = "Dave"
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.Given(x => GivenThereIsAConfiguration(configuration))
|
||||
.And(x => GivenOcelotIsRunning())
|
||||
.And(x => GivenIHaveAnOcelotToken("/administration"))
|
||||
.And(x => GivenIHaveAddedATokenToMyRequest())
|
||||
.When(x => WhenIGetUrlOnTheApiGateway("/administration/configuration"))
|
||||
.Then(x => ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
|
||||
.And(x => ThenTheResponseShouldBe(configuration))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_get_file_configuration_edit_and_post_updated_version()
|
||||
{
|
||||
var initialConfiguration = new FileConfiguration
|
||||
{
|
||||
GlobalConfiguration = new FileGlobalConfiguration
|
||||
{
|
||||
},
|
||||
ReRoutes = new List<FileReRoute>()
|
||||
{
|
||||
new FileReRoute()
|
||||
{
|
||||
DownstreamHostAndPorts = new List<FileHostAndPort>
|
||||
{
|
||||
new FileHostAndPort
|
||||
{
|
||||
Host = "localhost",
|
||||
Port = 80,
|
||||
}
|
||||
},
|
||||
DownstreamScheme = "https",
|
||||
DownstreamPathTemplate = "/",
|
||||
UpstreamHttpMethod = new List<string> { "get" },
|
||||
UpstreamPathTemplate = "/"
|
||||
},
|
||||
new FileReRoute()
|
||||
{
|
||||
DownstreamHostAndPorts = new List<FileHostAndPort>
|
||||
{
|
||||
new FileHostAndPort
|
||||
{
|
||||
Host = "localhost",
|
||||
Port = 80,
|
||||
}
|
||||
},
|
||||
DownstreamScheme = "https",
|
||||
DownstreamPathTemplate = "/",
|
||||
UpstreamHttpMethod = new List<string> { "get" },
|
||||
UpstreamPathTemplate = "/test"
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
var updatedConfiguration = new FileConfiguration
|
||||
{
|
||||
GlobalConfiguration = new FileGlobalConfiguration
|
||||
{
|
||||
},
|
||||
ReRoutes = new List<FileReRoute>()
|
||||
{
|
||||
new FileReRoute()
|
||||
{
|
||||
DownstreamHostAndPorts = new List<FileHostAndPort>
|
||||
{
|
||||
new FileHostAndPort
|
||||
{
|
||||
Host = "localhost",
|
||||
Port = 80,
|
||||
}
|
||||
},
|
||||
DownstreamScheme = "http",
|
||||
DownstreamPathTemplate = "/geoffrey",
|
||||
UpstreamHttpMethod = new List<string> { "get" },
|
||||
UpstreamPathTemplate = "/"
|
||||
},
|
||||
new FileReRoute()
|
||||
{
|
||||
DownstreamHostAndPorts = new List<FileHostAndPort>
|
||||
{
|
||||
new FileHostAndPort
|
||||
{
|
||||
Host = "123.123.123",
|
||||
Port = 443,
|
||||
}
|
||||
},
|
||||
DownstreamScheme = "https",
|
||||
DownstreamPathTemplate = "/blooper/{productId}",
|
||||
UpstreamHttpMethod = new List<string> { "post" },
|
||||
UpstreamPathTemplate = "/test"
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.Given(x => GivenThereIsAConfiguration(initialConfiguration))
|
||||
.And(x => GivenOcelotIsRunning())
|
||||
.And(x => GivenIHaveAnOcelotToken("/administration"))
|
||||
.And(x => GivenIHaveAddedATokenToMyRequest())
|
||||
.When(x => WhenIGetUrlOnTheApiGateway("/administration/configuration"))
|
||||
.When(x => WhenIPostOnTheApiGateway("/administration/configuration", updatedConfiguration))
|
||||
.Then(x => ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
|
||||
.And(x => ThenTheResponseShouldBe(updatedConfiguration))
|
||||
.When(x => WhenIGetUrlOnTheApiGateway("/administration/configuration"))
|
||||
.And(x => ThenTheResponseShouldBe(updatedConfiguration))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_clear_region()
|
||||
{
|
||||
var initialConfiguration = new FileConfiguration
|
||||
{
|
||||
GlobalConfiguration = new FileGlobalConfiguration
|
||||
{
|
||||
},
|
||||
ReRoutes = new List<FileReRoute>()
|
||||
{
|
||||
new FileReRoute()
|
||||
{
|
||||
DownstreamHostAndPorts = new List<FileHostAndPort>
|
||||
{
|
||||
new FileHostAndPort
|
||||
{
|
||||
Host = "localhost",
|
||||
Port = 80,
|
||||
}
|
||||
},
|
||||
DownstreamScheme = "https",
|
||||
DownstreamPathTemplate = "/",
|
||||
UpstreamHttpMethod = new List<string> { "get" },
|
||||
UpstreamPathTemplate = "/",
|
||||
FileCacheOptions = new FileCacheOptions
|
||||
{
|
||||
TtlSeconds = 10
|
||||
}
|
||||
},
|
||||
new FileReRoute()
|
||||
{
|
||||
DownstreamHostAndPorts = new List<FileHostAndPort>
|
||||
{
|
||||
new FileHostAndPort
|
||||
{
|
||||
Host = "localhost",
|
||||
Port = 80,
|
||||
}
|
||||
},
|
||||
DownstreamScheme = "https",
|
||||
DownstreamPathTemplate = "/",
|
||||
UpstreamHttpMethod = new List<string> { "get" },
|
||||
UpstreamPathTemplate = "/test",
|
||||
FileCacheOptions = new FileCacheOptions
|
||||
{
|
||||
TtlSeconds = 10
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
var regionToClear = "gettest";
|
||||
|
||||
this.Given(x => GivenThereIsAConfiguration(initialConfiguration))
|
||||
.And(x => GivenOcelotIsRunning())
|
||||
.And(x => GivenIHaveAnOcelotToken("/administration"))
|
||||
.And(x => GivenIHaveAddedATokenToMyRequest())
|
||||
.When(x => WhenIDeleteOnTheApiGateway($"/administration/outputcache/{regionToClear}"))
|
||||
.Then(x => ThenTheStatusCodeShouldBe(HttpStatusCode.NoContent))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenAnotherOcelotIsRunning(string baseUrl)
|
||||
{
|
||||
_httpClientTwo.BaseAddress = new Uri(baseUrl);
|
||||
|
||||
_webHostBuilderTwo = new WebHostBuilder()
|
||||
.UseUrls(baseUrl)
|
||||
.UseKestrel()
|
||||
.UseContentRoot(Directory.GetCurrentDirectory())
|
||||
.ConfigureServices(x => {
|
||||
x.AddSingleton(_webHostBuilderTwo);
|
||||
})
|
||||
.UseStartup<IntegrationTestsStartup>();
|
||||
|
||||
_builderTwo = _webHostBuilderTwo.Build();
|
||||
|
||||
_builderTwo.Start();
|
||||
}
|
||||
|
||||
private void GivenIdentityServerSigningEnvironmentalVariablesAreSet()
|
||||
{
|
||||
Environment.SetEnvironmentVariable("OCELOT_CERTIFICATE", "idsrv3test.pfx");
|
||||
Environment.SetEnvironmentVariable("OCELOT_CERTIFICATE_PASSWORD", "idsrv3test");
|
||||
}
|
||||
|
||||
private void WhenIGetUrlOnTheSecondOcelot(string url)
|
||||
{
|
||||
_httpClientTwo.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _token.AccessToken);
|
||||
_response = _httpClientTwo.GetAsync(url).Result;
|
||||
}
|
||||
|
||||
private void WhenIPostOnTheApiGateway(string url, FileConfiguration updatedConfiguration)
|
||||
{
|
||||
var json = JsonConvert.SerializeObject(updatedConfiguration);
|
||||
var content = new StringContent(json);
|
||||
content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
|
||||
_response = _httpClient.PostAsync(url, content).Result;
|
||||
}
|
||||
|
||||
private void ThenTheResponseShouldBe(List<string> expected)
|
||||
{
|
||||
var content = _response.Content.ReadAsStringAsync().Result;
|
||||
var result = JsonConvert.DeserializeObject<Regions>(content);
|
||||
result.Value.ShouldBe(expected);
|
||||
}
|
||||
|
||||
private 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);
|
||||
}
|
||||
}
|
||||
|
||||
private void GivenIHaveAddedATokenToMyRequest()
|
||||
{
|
||||
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _token.AccessToken);
|
||||
}
|
||||
|
||||
private 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>("grant_type", "client_credentials")
|
||||
};
|
||||
var content = new FormUrlEncodedContent(formData);
|
||||
|
||||
var response = _httpClient.PostAsync(tokenUrl, content).Result;
|
||||
var responseContent = response.Content.ReadAsStringAsync().Result;
|
||||
response.EnsureSuccessStatusCode();
|
||||
_token = JsonConvert.DeserializeObject<BearerToken>(responseContent);
|
||||
var configPath = $"{adminPath}/.well-known/openid-configuration";
|
||||
response = _httpClient.GetAsync(configPath).Result;
|
||||
response.EnsureSuccessStatusCode();
|
||||
}
|
||||
|
||||
private void GivenOcelotIsRunning()
|
||||
{
|
||||
_webHostBuilder = new WebHostBuilder()
|
||||
.UseUrls(_ocelotBaseUrl)
|
||||
.UseKestrel()
|
||||
.UseContentRoot(Directory.GetCurrentDirectory())
|
||||
.ConfigureServices(x => {
|
||||
x.AddSingleton(_webHostBuilder);
|
||||
})
|
||||
.UseStartup<IntegrationTestsStartup>();
|
||||
|
||||
_builder = _webHostBuilder.Build();
|
||||
|
||||
_builder.Start();
|
||||
}
|
||||
|
||||
private void GivenThereIsAConfiguration(FileConfiguration fileConfiguration)
|
||||
{
|
||||
var configurationPath = $"{Directory.GetCurrentDirectory()}/configuration.json";
|
||||
|
||||
var jsonConfiguration = JsonConvert.SerializeObject(fileConfiguration);
|
||||
|
||||
if (File.Exists(configurationPath))
|
||||
{
|
||||
File.Delete(configurationPath);
|
||||
}
|
||||
|
||||
File.WriteAllText(configurationPath, jsonConfiguration);
|
||||
|
||||
var text = File.ReadAllText(configurationPath);
|
||||
|
||||
configurationPath = $"{AppContext.BaseDirectory}/configuration.json";
|
||||
|
||||
if (File.Exists(configurationPath))
|
||||
{
|
||||
File.Delete(configurationPath);
|
||||
}
|
||||
|
||||
File.WriteAllText(configurationPath, jsonConfiguration);
|
||||
|
||||
text = File.ReadAllText(configurationPath);
|
||||
}
|
||||
|
||||
private void WhenIGetUrlOnTheApiGateway(string url)
|
||||
{
|
||||
_response = _httpClient.GetAsync(url).Result;
|
||||
}
|
||||
|
||||
private void WhenIDeleteOnTheApiGateway(string url)
|
||||
{
|
||||
_response = _httpClient.DeleteAsync(url).Result;
|
||||
}
|
||||
|
||||
private void ThenTheStatusCodeShouldBe(HttpStatusCode expectedHttpStatusCode)
|
||||
{
|
||||
_response.StatusCode.ShouldBe(expectedHttpStatusCode);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Environment.SetEnvironmentVariable("OCELOT_CERTIFICATE", "");
|
||||
Environment.SetEnvironmentVariable("OCELOT_CERTIFICATE_PASSWORD", "");
|
||||
_builder?.Dispose();
|
||||
_httpClient?.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,16 +1,16 @@
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace Ocelot.IntegrationTests
|
||||
{
|
||||
class BearerToken
|
||||
{
|
||||
[JsonProperty("access_token")]
|
||||
public string AccessToken { get; set; }
|
||||
|
||||
[JsonProperty("expires_in")]
|
||||
public int ExpiresIn { get; set; }
|
||||
|
||||
[JsonProperty("token_type")]
|
||||
public string TokenType { get; set; }
|
||||
}
|
||||
}
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace Ocelot.IntegrationTests
|
||||
{
|
||||
class BearerToken
|
||||
{
|
||||
[JsonProperty("access_token")]
|
||||
public string AccessToken { get; set; }
|
||||
|
||||
[JsonProperty("expires_in")]
|
||||
public int ExpiresIn { get; set; }
|
||||
|
||||
[JsonProperty("token_type")]
|
||||
public string TokenType { get; set; }
|
||||
}
|
||||
}
|
||||
|
@ -1,51 +1,51 @@
|
||||
using System;
|
||||
using CacheManager.Core;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Ocelot.DependencyInjection;
|
||||
using Ocelot.Middleware;
|
||||
using ConfigurationBuilder = Microsoft.Extensions.Configuration.ConfigurationBuilder;
|
||||
|
||||
namespace Ocelot.IntegrationTests
|
||||
{
|
||||
public class IntegrationTestsStartup
|
||||
{
|
||||
public IntegrationTestsStartup(IHostingEnvironment env)
|
||||
{
|
||||
var builder = new ConfigurationBuilder()
|
||||
.SetBasePath(env.ContentRootPath)
|
||||
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
|
||||
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
|
||||
.AddJsonFile("configuration.json")
|
||||
.AddEnvironmentVariables();
|
||||
|
||||
Configuration = builder.Build();
|
||||
}
|
||||
|
||||
public IConfiguration Configuration { get; }
|
||||
|
||||
public void ConfigureServices(IServiceCollection services)
|
||||
{
|
||||
Action<ConfigurationBuilderCachePart> settings = (x) =>
|
||||
{
|
||||
x.WithMicrosoftLogging(log =>
|
||||
{
|
||||
log.AddConsole(LogLevel.Debug);
|
||||
})
|
||||
.WithDictionaryHandle();
|
||||
};
|
||||
|
||||
services.AddOcelot(Configuration)
|
||||
.AddCacheManager(settings)
|
||||
.AddAdministration("/administration", "secret");
|
||||
}
|
||||
|
||||
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
|
||||
{
|
||||
app.UseOcelot().Wait();
|
||||
}
|
||||
}
|
||||
using System;
|
||||
using CacheManager.Core;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Ocelot.DependencyInjection;
|
||||
using Ocelot.Middleware;
|
||||
using ConfigurationBuilder = Microsoft.Extensions.Configuration.ConfigurationBuilder;
|
||||
|
||||
namespace Ocelot.IntegrationTests
|
||||
{
|
||||
public class IntegrationTestsStartup
|
||||
{
|
||||
public IntegrationTestsStartup(IHostingEnvironment env)
|
||||
{
|
||||
var builder = new ConfigurationBuilder()
|
||||
.SetBasePath(env.ContentRootPath)
|
||||
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
|
||||
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
|
||||
.AddJsonFile("configuration.json")
|
||||
.AddEnvironmentVariables();
|
||||
|
||||
Configuration = builder.Build();
|
||||
}
|
||||
|
||||
public IConfiguration Configuration { get; }
|
||||
|
||||
public void ConfigureServices(IServiceCollection services)
|
||||
{
|
||||
Action<ConfigurationBuilderCachePart> settings = (x) =>
|
||||
{
|
||||
x.WithMicrosoftLogging(log =>
|
||||
{
|
||||
log.AddConsole(LogLevel.Debug);
|
||||
})
|
||||
.WithDictionaryHandle();
|
||||
};
|
||||
|
||||
services.AddOcelot(Configuration)
|
||||
.AddCacheManager(settings)
|
||||
.AddAdministration("/administration", "secret");
|
||||
}
|
||||
|
||||
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
|
||||
{
|
||||
app.UseOcelot().Wait();
|
||||
}
|
||||
}
|
||||
}
|
@ -1,46 +1,46 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<VersionPrefix>0.0.0-dev</VersionPrefix>
|
||||
<TargetFramework>netcoreapp2.0</TargetFramework>
|
||||
<RuntimeFrameworkVersion>2.0.0</RuntimeFrameworkVersion>
|
||||
<AssemblyName>Ocelot.IntegrationTests</AssemblyName>
|
||||
<OutputType>Exe</OutputType>
|
||||
<PackageId>Ocelot.IntegrationTests</PackageId>
|
||||
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
|
||||
<RuntimeIdentifiers>win10-x64;osx.10.11-x64;osx.10.12-x64;win7-x64</RuntimeIdentifiers>
|
||||
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
|
||||
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
|
||||
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<None Update="peers.json;configuration.json;appsettings.json;idsrv3test.pfx">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}"/>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\src\Ocelot\Ocelot.csproj"/>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0"/>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0-preview-20171031-01"/>
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1"/>
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="2.0.0"/>
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="2.0.0"/>
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.0.0"/>
|
||||
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.0.0"/>
|
||||
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="2.0.0"/>
|
||||
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="2.0.0"/>
|
||||
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="2.0.0"/>
|
||||
<PackageReference Include="Microsoft.DotNet.InternalAbstractions" Version="1.0.500-preview2-1-003177"/>
|
||||
<PackageReference Include="xunit" Version="2.3.1"/>
|
||||
<PackageReference Include="IdentityServer4" Version="2.0.2"/>
|
||||
<PackageReference Include="Shouldly" Version="3.0.0-beta0003"/>
|
||||
<PackageReference Include="TestStack.BDDfy" Version="4.3.2"/>
|
||||
<PackageReference Include="Consul" Version="0.7.2.3"/>
|
||||
<PackageReference Include="Rafty" Version="0.4.2"/>
|
||||
<PackageReference Include="Microsoft.Data.SQLite" Version="2.0.0"/>
|
||||
</ItemGroup>
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<VersionPrefix>0.0.0-dev</VersionPrefix>
|
||||
<TargetFramework>netcoreapp2.0</TargetFramework>
|
||||
<RuntimeFrameworkVersion>2.0.0</RuntimeFrameworkVersion>
|
||||
<AssemblyName>Ocelot.IntegrationTests</AssemblyName>
|
||||
<OutputType>Exe</OutputType>
|
||||
<PackageId>Ocelot.IntegrationTests</PackageId>
|
||||
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
|
||||
<RuntimeIdentifiers>win10-x64;osx.10.11-x64;osx.10.12-x64;win7-x64</RuntimeIdentifiers>
|
||||
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
|
||||
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
|
||||
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<None Update="peers.json;configuration.json;appsettings.json;idsrv3test.pfx">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}"/>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\src\Ocelot\Ocelot.csproj"/>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0"/>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0-preview-20171031-01"/>
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1"/>
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="2.0.0"/>
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="2.0.0"/>
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.0.0"/>
|
||||
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.0.0"/>
|
||||
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="2.0.0"/>
|
||||
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="2.0.0"/>
|
||||
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="2.0.0"/>
|
||||
<PackageReference Include="Microsoft.DotNet.InternalAbstractions" Version="1.0.500-preview2-1-003177"/>
|
||||
<PackageReference Include="xunit" Version="2.3.1"/>
|
||||
<PackageReference Include="IdentityServer4" Version="2.0.2"/>
|
||||
<PackageReference Include="Shouldly" Version="3.0.0-beta0003"/>
|
||||
<PackageReference Include="TestStack.BDDfy" Version="4.3.2"/>
|
||||
<PackageReference Include="Consul" Version="0.7.2.3"/>
|
||||
<PackageReference Include="Rafty" Version="0.4.2"/>
|
||||
<PackageReference Include="Microsoft.Data.SQLite" Version="2.0.0"/>
|
||||
</ItemGroup>
|
||||
</Project>
|
@ -1,19 +1,19 @@
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
// General Information about an assembly is controlled through the following
|
||||
// set of attributes. Change these attribute values to modify the information
|
||||
// associated with an assembly.
|
||||
[assembly: AssemblyConfiguration("")]
|
||||
[assembly: AssemblyCompany("")]
|
||||
[assembly: AssemblyProduct("Ocelot.IntegrationTests")]
|
||||
[assembly: AssemblyTrademark("")]
|
||||
|
||||
// Setting ComVisible to false makes the types in this assembly not visible
|
||||
// to COM components. If you need to access a type in this assembly from
|
||||
// COM, set the ComVisible attribute to true on that type.
|
||||
[assembly: ComVisible(false)]
|
||||
|
||||
// The following GUID is for the ID of the typelib if this project is exposed to COM
|
||||
[assembly: Guid("d4575572-99ca-4530-8737-c296eda326f8")]
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
// General Information about an assembly is controlled through the following
|
||||
// set of attributes. Change these attribute values to modify the information
|
||||
// associated with an assembly.
|
||||
[assembly: AssemblyConfiguration("")]
|
||||
[assembly: AssemblyCompany("")]
|
||||
[assembly: AssemblyProduct("Ocelot.IntegrationTests")]
|
||||
[assembly: AssemblyTrademark("")]
|
||||
|
||||
// Setting ComVisible to false makes the types in this assembly not visible
|
||||
// to COM components. If you need to access a type in this assembly from
|
||||
// COM, set the ComVisible attribute to true on that type.
|
||||
[assembly: ComVisible(false)]
|
||||
|
||||
// The following GUID is for the ID of the typelib if this project is exposed to COM
|
||||
[assembly: Guid("d4575572-99ca-4530-8737-c296eda326f8")]
|
||||
|
@ -1,52 +1,52 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using Ocelot.DependencyInjection;
|
||||
using Ocelot.Middleware;
|
||||
using Ocelot.Raft;
|
||||
using Rafty.Concensus;
|
||||
using Rafty.FiniteStateMachine;
|
||||
using Rafty.Infrastructure;
|
||||
using Rafty.Log;
|
||||
using ConfigurationBuilder = Microsoft.Extensions.Configuration.ConfigurationBuilder;
|
||||
|
||||
namespace Ocelot.IntegrationTests
|
||||
{
|
||||
public class RaftStartup
|
||||
{
|
||||
public RaftStartup(IHostingEnvironment env)
|
||||
{
|
||||
var builder = new ConfigurationBuilder()
|
||||
.SetBasePath(env.ContentRootPath)
|
||||
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
|
||||
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
|
||||
.AddJsonFile("peers.json", optional: true, reloadOnChange: true)
|
||||
.AddJsonFile("configuration.json")
|
||||
.AddEnvironmentVariables();
|
||||
|
||||
Configuration = builder.Build();
|
||||
}
|
||||
|
||||
public IConfiguration Configuration { get; }
|
||||
|
||||
public virtual void ConfigureServices(IServiceCollection services)
|
||||
{
|
||||
services
|
||||
.AddOcelot(Configuration)
|
||||
.AddAdministration("/administration", "secret")
|
||||
.AddRafty();
|
||||
}
|
||||
|
||||
public virtual void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
|
||||
{
|
||||
app.UseOcelot().Wait();
|
||||
}
|
||||
}
|
||||
}
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using Ocelot.DependencyInjection;
|
||||
using Ocelot.Middleware;
|
||||
using Ocelot.Raft;
|
||||
using Rafty.Concensus;
|
||||
using Rafty.FiniteStateMachine;
|
||||
using Rafty.Infrastructure;
|
||||
using Rafty.Log;
|
||||
using ConfigurationBuilder = Microsoft.Extensions.Configuration.ConfigurationBuilder;
|
||||
|
||||
namespace Ocelot.IntegrationTests
|
||||
{
|
||||
public class RaftStartup
|
||||
{
|
||||
public RaftStartup(IHostingEnvironment env)
|
||||
{
|
||||
var builder = new ConfigurationBuilder()
|
||||
.SetBasePath(env.ContentRootPath)
|
||||
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
|
||||
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
|
||||
.AddJsonFile("peers.json", optional: true, reloadOnChange: true)
|
||||
.AddJsonFile("configuration.json")
|
||||
.AddEnvironmentVariables();
|
||||
|
||||
Configuration = builder.Build();
|
||||
}
|
||||
|
||||
public IConfiguration Configuration { get; }
|
||||
|
||||
public virtual void ConfigureServices(IServiceCollection services)
|
||||
{
|
||||
services
|
||||
.AddOcelot(Configuration)
|
||||
.AddAdministration("/administration", "secret")
|
||||
.AddRafty();
|
||||
}
|
||||
|
||||
public virtual void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
|
||||
{
|
||||
app.UseOcelot().Wait();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,431 +1,372 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Newtonsoft.Json;
|
||||
using Ocelot.Configuration.File;
|
||||
using Ocelot.Raft;
|
||||
using Rafty.Concensus;
|
||||
using Rafty.FiniteStateMachine;
|
||||
using Rafty.Infrastructure;
|
||||
using Shouldly;
|
||||
using Xunit;
|
||||
using static Rafty.Infrastructure.Wait;
|
||||
using Microsoft.Data.Sqlite;
|
||||
|
||||
namespace Ocelot.IntegrationTests
|
||||
{
|
||||
public class RaftTests : IDisposable
|
||||
{
|
||||
private List<IWebHost> _builders;
|
||||
private List<IWebHostBuilder> _webHostBuilders;
|
||||
private List<Thread> _threads;
|
||||
private FilePeers _peers;
|
||||
private HttpClient _httpClient;
|
||||
private HttpClient _httpClientForAssertions;
|
||||
private string _ocelotBaseUrl;
|
||||
private BearerToken _token;
|
||||
private HttpResponseMessage _response;
|
||||
private static object _lock = new object();
|
||||
|
||||
public RaftTests()
|
||||
{
|
||||
_httpClientForAssertions = new HttpClient();
|
||||
_httpClient = new HttpClient();
|
||||
_ocelotBaseUrl = "http://localhost:5000";
|
||||
_httpClient.BaseAddress = new Uri(_ocelotBaseUrl);
|
||||
_webHostBuilders = new List<IWebHostBuilder>();
|
||||
_builders = new List<IWebHost>();
|
||||
_threads = new List<Thread>();
|
||||
}
|
||||
public void Dispose()
|
||||
{
|
||||
foreach (var builder in _builders)
|
||||
{
|
||||
builder?.Dispose();
|
||||
}
|
||||
|
||||
foreach (var peer in _peers.Peers)
|
||||
{
|
||||
File.Delete(peer.HostAndPort.Replace("/","").Replace(":",""));
|
||||
File.Delete($"{peer.HostAndPort.Replace("/","").Replace(":","")}.db");
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_persist_command_to_five_servers()
|
||||
{
|
||||
var configuration = new FileConfiguration
|
||||
{
|
||||
GlobalConfiguration = new FileGlobalConfiguration
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
var updatedConfiguration = new FileConfiguration
|
||||
{
|
||||
GlobalConfiguration = new FileGlobalConfiguration
|
||||
{
|
||||
},
|
||||
ReRoutes = new List<FileReRoute>()
|
||||
{
|
||||
new FileReRoute()
|
||||
{
|
||||
DownstreamHost = "127.0.0.1",
|
||||
DownstreamPort = 80,
|
||||
DownstreamScheme = "http",
|
||||
DownstreamPathTemplate = "/geoffrey",
|
||||
UpstreamHttpMethod = new List<string> { "get" },
|
||||
UpstreamPathTemplate = "/"
|
||||
},
|
||||
new FileReRoute()
|
||||
{
|
||||
DownstreamHost = "123.123.123",
|
||||
DownstreamPort = 443,
|
||||
DownstreamScheme = "https",
|
||||
DownstreamPathTemplate = "/blooper/{productId}",
|
||||
UpstreamHttpMethod = new List<string> { "post" },
|
||||
UpstreamPathTemplate = "/test"
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
var command = new UpdateFileConfiguration(updatedConfiguration);
|
||||
GivenThereIsAConfiguration(configuration);
|
||||
GivenFiveServersAreRunning();
|
||||
GivenALeaderIsElected();
|
||||
GivenIHaveAnOcelotToken("/administration");
|
||||
WhenISendACommandIntoTheCluster(command);
|
||||
ThenTheCommandIsReplicatedToAllStateMachines(command);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_persist_command_to_five_servers_when_using_administration_api()
|
||||
{
|
||||
var configuration = new FileConfiguration
|
||||
{
|
||||
};
|
||||
|
||||
var updatedConfiguration = new FileConfiguration
|
||||
{
|
||||
ReRoutes = new List<FileReRoute>()
|
||||
{
|
||||
new FileReRoute()
|
||||
{
|
||||
DownstreamHost = "127.0.0.1",
|
||||
DownstreamPort = 80,
|
||||
DownstreamScheme = "http",
|
||||
DownstreamPathTemplate = "/geoffrey",
|
||||
UpstreamHttpMethod = new List<string> { "get" },
|
||||
UpstreamPathTemplate = "/"
|
||||
},
|
||||
new FileReRoute()
|
||||
{
|
||||
DownstreamHost = "123.123.123",
|
||||
DownstreamPort = 443,
|
||||
DownstreamScheme = "https",
|
||||
DownstreamPathTemplate = "/blooper/{productId}",
|
||||
UpstreamHttpMethod = new List<string> { "post" },
|
||||
UpstreamPathTemplate = "/test"
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
var command = new UpdateFileConfiguration(updatedConfiguration);
|
||||
GivenThereIsAConfiguration(configuration);
|
||||
GivenFiveServersAreRunning();
|
||||
GivenALeaderIsElected();
|
||||
GivenIHaveAnOcelotToken("/administration");
|
||||
GivenIHaveAddedATokenToMyRequest();
|
||||
WhenIPostOnTheApiGateway("/administration/configuration", updatedConfiguration);
|
||||
ThenTheCommandIsReplicatedToAllStateMachines(command);
|
||||
}
|
||||
|
||||
private void WhenISendACommandIntoTheCluster(UpdateFileConfiguration command)
|
||||
{
|
||||
var p = _peers.Peers.First();
|
||||
var json = JsonConvert.SerializeObject(command,new JsonSerializerSettings() {
|
||||
TypeNameHandling = TypeNameHandling.All
|
||||
});
|
||||
var httpContent = new StringContent(json);
|
||||
httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
|
||||
using(var httpClient = new HttpClient())
|
||||
{
|
||||
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _token.AccessToken);
|
||||
var response = httpClient.PostAsync($"{p.HostAndPort}/administration/raft/command", httpContent).GetAwaiter().GetResult();
|
||||
response.EnsureSuccessStatusCode();
|
||||
var content = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
|
||||
var result = JsonConvert.DeserializeObject<OkResponse<UpdateFileConfiguration>>(content);
|
||||
result.Command.Configuration.ReRoutes.Count.ShouldBe(2);
|
||||
}
|
||||
|
||||
//dirty sleep to make sure command replicated...
|
||||
var stopwatch = Stopwatch.StartNew();
|
||||
while(stopwatch.ElapsedMilliseconds < 10000)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private void ThenTheCommandIsReplicatedToAllStateMachines(UpdateFileConfiguration expected)
|
||||
{
|
||||
//dirty sleep to give a chance to replicate...
|
||||
var stopwatch = Stopwatch.StartNew();
|
||||
while(stopwatch.ElapsedMilliseconds < 2000)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
bool CommandCalledOnAllStateMachines()
|
||||
{
|
||||
try
|
||||
{
|
||||
var passed = 0;
|
||||
foreach (var peer in _peers.Peers)
|
||||
{
|
||||
var path = $"{peer.HostAndPort.Replace("/","").Replace(":","")}.db";
|
||||
using(var connection = new SqliteConnection($"Data Source={path};"))
|
||||
{
|
||||
connection.Open();
|
||||
var sql = @"select count(id) from logs";
|
||||
using(var command = new SqliteCommand(sql, connection))
|
||||
{
|
||||
var index = Convert.ToInt32(command.ExecuteScalar());
|
||||
index.ShouldBe(1);
|
||||
}
|
||||
}
|
||||
_httpClientForAssertions.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _token.AccessToken);
|
||||
var result = _httpClientForAssertions.GetAsync($"{peer.HostAndPort}/administration/configuration").Result;
|
||||
var json = result.Content.ReadAsStringAsync().Result;
|
||||
var response = JsonConvert.DeserializeObject<FileConfiguration>(json, new JsonSerializerSettings{TypeNameHandling = TypeNameHandling.All});
|
||||
response.GlobalConfiguration.RequestIdKey.ShouldBe(expected.Configuration.GlobalConfiguration.RequestIdKey);
|
||||
response.GlobalConfiguration.ServiceDiscoveryProvider.Host.ShouldBe(expected.Configuration.GlobalConfiguration.ServiceDiscoveryProvider.Host);
|
||||
response.GlobalConfiguration.ServiceDiscoveryProvider.Port.ShouldBe(expected.Configuration.GlobalConfiguration.ServiceDiscoveryProvider.Port);
|
||||
|
||||
for (var i = 0; i < response.ReRoutes.Count; i++)
|
||||
{
|
||||
response.ReRoutes[i].DownstreamHost.ShouldBe(expected.Configuration.ReRoutes[i].DownstreamHost);
|
||||
response.ReRoutes[i].DownstreamPathTemplate.ShouldBe(expected.Configuration.ReRoutes[i].DownstreamPathTemplate);
|
||||
response.ReRoutes[i].DownstreamPort.ShouldBe(expected.Configuration.ReRoutes[i].DownstreamPort);
|
||||
response.ReRoutes[i].DownstreamScheme.ShouldBe(expected.Configuration.ReRoutes[i].DownstreamScheme);
|
||||
response.ReRoutes[i].UpstreamPathTemplate.ShouldBe(expected.Configuration.ReRoutes[i].UpstreamPathTemplate);
|
||||
response.ReRoutes[i].UpstreamHttpMethod.ShouldBe(expected.Configuration.ReRoutes[i].UpstreamHttpMethod);
|
||||
}
|
||||
passed++;
|
||||
}
|
||||
|
||||
return passed == 5;
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
Console.WriteLine(e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
var commandOnAllStateMachines = WaitFor(20000).Until(() => CommandCalledOnAllStateMachines());
|
||||
commandOnAllStateMachines.ShouldBeTrue();
|
||||
}
|
||||
|
||||
private void ThenTheResponseShouldBe(FileConfiguration expected)
|
||||
{
|
||||
var response = JsonConvert.DeserializeObject<FileConfiguration>(_response.Content.ReadAsStringAsync().Result);
|
||||
|
||||
response.GlobalConfiguration.RequestIdKey.ShouldBe(expected.GlobalConfiguration.RequestIdKey);
|
||||
response.GlobalConfiguration.ServiceDiscoveryProvider.Host.ShouldBe(expected.GlobalConfiguration.ServiceDiscoveryProvider.Host);
|
||||
response.GlobalConfiguration.ServiceDiscoveryProvider.Port.ShouldBe(expected.GlobalConfiguration.ServiceDiscoveryProvider.Port);
|
||||
|
||||
for (var i = 0; i < response.ReRoutes.Count; i++)
|
||||
{
|
||||
response.ReRoutes[i].DownstreamHost.ShouldBe(expected.ReRoutes[i].DownstreamHost);
|
||||
response.ReRoutes[i].DownstreamPathTemplate.ShouldBe(expected.ReRoutes[i].DownstreamPathTemplate);
|
||||
response.ReRoutes[i].DownstreamPort.ShouldBe(expected.ReRoutes[i].DownstreamPort);
|
||||
response.ReRoutes[i].DownstreamScheme.ShouldBe(expected.ReRoutes[i].DownstreamScheme);
|
||||
response.ReRoutes[i].UpstreamPathTemplate.ShouldBe(expected.ReRoutes[i].UpstreamPathTemplate);
|
||||
response.ReRoutes[i].UpstreamHttpMethod.ShouldBe(expected.ReRoutes[i].UpstreamHttpMethod);
|
||||
}
|
||||
}
|
||||
|
||||
private void WhenIGetUrlOnTheApiGateway(string url)
|
||||
{
|
||||
_response = _httpClient.GetAsync(url).Result;
|
||||
}
|
||||
|
||||
private void WhenIPostOnTheApiGateway(string url, FileConfiguration updatedConfiguration)
|
||||
{
|
||||
var json = JsonConvert.SerializeObject(updatedConfiguration);
|
||||
var content = new StringContent(json);
|
||||
content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
|
||||
_response = _httpClient.PostAsync(url, content).Result;
|
||||
}
|
||||
|
||||
private void GivenIHaveAddedATokenToMyRequest()
|
||||
{
|
||||
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _token.AccessToken);
|
||||
}
|
||||
|
||||
private 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>("grant_type", "client_credentials")
|
||||
};
|
||||
var content = new FormUrlEncodedContent(formData);
|
||||
|
||||
var response = _httpClient.PostAsync(tokenUrl, content).Result;
|
||||
var responseContent = response.Content.ReadAsStringAsync().Result;
|
||||
response.EnsureSuccessStatusCode();
|
||||
_token = JsonConvert.DeserializeObject<BearerToken>(responseContent);
|
||||
var configPath = $"{adminPath}/.well-known/openid-configuration";
|
||||
response = _httpClient.GetAsync(configPath).Result;
|
||||
response.EnsureSuccessStatusCode();
|
||||
}
|
||||
|
||||
private void GivenThereIsAConfiguration(FileConfiguration fileConfiguration)
|
||||
{
|
||||
var configurationPath = $"{Directory.GetCurrentDirectory()}/configuration.json";
|
||||
|
||||
var jsonConfiguration = JsonConvert.SerializeObject(fileConfiguration);
|
||||
|
||||
if (File.Exists(configurationPath))
|
||||
{
|
||||
File.Delete(configurationPath);
|
||||
}
|
||||
|
||||
File.WriteAllText(configurationPath, jsonConfiguration);
|
||||
|
||||
var text = File.ReadAllText(configurationPath);
|
||||
|
||||
configurationPath = $"{AppContext.BaseDirectory}/configuration.json";
|
||||
|
||||
if (File.Exists(configurationPath))
|
||||
{
|
||||
File.Delete(configurationPath);
|
||||
}
|
||||
|
||||
File.WriteAllText(configurationPath, jsonConfiguration);
|
||||
|
||||
text = File.ReadAllText(configurationPath);
|
||||
}
|
||||
|
||||
private void GivenAServerIsRunning(string url)
|
||||
{
|
||||
lock(_lock)
|
||||
{
|
||||
IWebHostBuilder webHostBuilder = new WebHostBuilder();
|
||||
webHostBuilder.UseUrls(url)
|
||||
.UseKestrel()
|
||||
.UseContentRoot(Directory.GetCurrentDirectory())
|
||||
.ConfigureServices(x =>
|
||||
{
|
||||
x.AddSingleton(webHostBuilder);
|
||||
x.AddSingleton(new NodeId(url));
|
||||
})
|
||||
.UseStartup<RaftStartup>();
|
||||
|
||||
var builder = webHostBuilder.Build();
|
||||
builder.Start();
|
||||
|
||||
_webHostBuilders.Add(webHostBuilder);
|
||||
_builders.Add(builder);
|
||||
}
|
||||
}
|
||||
|
||||
private void GivenFiveServersAreRunning()
|
||||
{
|
||||
var bytes = File.ReadAllText("peers.json");
|
||||
_peers = JsonConvert.DeserializeObject<FilePeers>(bytes);
|
||||
|
||||
foreach (var peer in _peers.Peers)
|
||||
{
|
||||
var thread = new Thread(() => GivenAServerIsRunning(peer.HostAndPort));
|
||||
thread.Start();
|
||||
_threads.Add(thread);
|
||||
}
|
||||
}
|
||||
|
||||
private void GivenALeaderIsElected()
|
||||
{
|
||||
//dirty sleep to make sure we have a leader
|
||||
var stopwatch = Stopwatch.StartNew();
|
||||
while(stopwatch.ElapsedMilliseconds < 20000)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private void WhenISendACommandIntoTheCluster(FakeCommand command)
|
||||
{
|
||||
var p = _peers.Peers.First();
|
||||
var json = JsonConvert.SerializeObject(command,new JsonSerializerSettings() {
|
||||
TypeNameHandling = TypeNameHandling.All
|
||||
});
|
||||
var httpContent = new StringContent(json);
|
||||
httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
|
||||
using(var httpClient = new HttpClient())
|
||||
{
|
||||
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _token.AccessToken);
|
||||
var response = httpClient.PostAsync($"{p.HostAndPort}/administration/raft/command", httpContent).GetAwaiter().GetResult();
|
||||
response.EnsureSuccessStatusCode();
|
||||
var content = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
|
||||
var result = JsonConvert.DeserializeObject<OkResponse<FakeCommand>>(content);
|
||||
result.Command.Value.ShouldBe(command.Value);
|
||||
}
|
||||
|
||||
//dirty sleep to make sure command replicated...
|
||||
var stopwatch = Stopwatch.StartNew();
|
||||
while(stopwatch.ElapsedMilliseconds < 10000)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private void ThenTheCommandIsReplicatedToAllStateMachines(FakeCommand command)
|
||||
{
|
||||
//dirty sleep to give a chance to replicate...
|
||||
var stopwatch = Stopwatch.StartNew();
|
||||
while(stopwatch.ElapsedMilliseconds < 2000)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
bool CommandCalledOnAllStateMachines()
|
||||
{
|
||||
try
|
||||
{
|
||||
var passed = 0;
|
||||
foreach (var peer in _peers.Peers)
|
||||
{
|
||||
string fsmData;
|
||||
fsmData = File.ReadAllText(peer.HostAndPort.Replace("/","").Replace(":",""));
|
||||
fsmData.ShouldNotBeNullOrEmpty();
|
||||
var fakeCommand = JsonConvert.DeserializeObject<FakeCommand>(fsmData);
|
||||
fakeCommand.Value.ShouldBe(command.Value);
|
||||
passed++;
|
||||
}
|
||||
|
||||
return passed == 5;
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
var commandOnAllStateMachines = WaitFor(20000).Until(() => CommandCalledOnAllStateMachines());
|
||||
commandOnAllStateMachines.ShouldBeTrue();
|
||||
}
|
||||
}
|
||||
}
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Threading;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Newtonsoft.Json;
|
||||
using Ocelot.Configuration.File;
|
||||
using Ocelot.Raft;
|
||||
using Rafty.Concensus;
|
||||
using Rafty.Infrastructure;
|
||||
using Shouldly;
|
||||
using Xunit;
|
||||
using static Rafty.Infrastructure.Wait;
|
||||
using Microsoft.Data.Sqlite;
|
||||
|
||||
namespace Ocelot.IntegrationTests
|
||||
{
|
||||
public class RaftTests : IDisposable
|
||||
{
|
||||
private readonly List<IWebHost> _builders;
|
||||
private readonly List<IWebHostBuilder> _webHostBuilders;
|
||||
private readonly List<Thread> _threads;
|
||||
private FilePeers _peers;
|
||||
private readonly HttpClient _httpClient;
|
||||
private readonly HttpClient _httpClientForAssertions;
|
||||
private string _ocelotBaseUrl;
|
||||
private BearerToken _token;
|
||||
private HttpResponseMessage _response;
|
||||
private static readonly object _lock = new object();
|
||||
|
||||
public RaftTests()
|
||||
{
|
||||
_httpClientForAssertions = new HttpClient();
|
||||
_httpClient = new HttpClient();
|
||||
_ocelotBaseUrl = "http://localhost:5000";
|
||||
_httpClient.BaseAddress = new Uri(_ocelotBaseUrl);
|
||||
_webHostBuilders = new List<IWebHostBuilder>();
|
||||
_builders = new List<IWebHost>();
|
||||
_threads = new List<Thread>();
|
||||
}
|
||||
public void Dispose()
|
||||
{
|
||||
foreach (var builder in _builders)
|
||||
{
|
||||
builder?.Dispose();
|
||||
}
|
||||
|
||||
foreach (var peer in _peers.Peers)
|
||||
{
|
||||
File.Delete(peer.HostAndPort.Replace("/","").Replace(":",""));
|
||||
File.Delete($"{peer.HostAndPort.Replace("/","").Replace(":","")}.db");
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_persist_command_to_five_servers()
|
||||
{
|
||||
var configuration = new FileConfiguration
|
||||
{
|
||||
GlobalConfiguration = new FileGlobalConfiguration
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
var updatedConfiguration = new FileConfiguration
|
||||
{
|
||||
GlobalConfiguration = new FileGlobalConfiguration
|
||||
{
|
||||
},
|
||||
ReRoutes = new List<FileReRoute>()
|
||||
{
|
||||
new FileReRoute()
|
||||
{
|
||||
DownstreamHostAndPorts = new List<FileHostAndPort>
|
||||
{
|
||||
new FileHostAndPort
|
||||
{
|
||||
Host = "127.0.0.1",
|
||||
Port = 80,
|
||||
}
|
||||
},
|
||||
DownstreamScheme = "http",
|
||||
DownstreamPathTemplate = "/geoffrey",
|
||||
UpstreamHttpMethod = new List<string> { "get" },
|
||||
UpstreamPathTemplate = "/"
|
||||
},
|
||||
new FileReRoute()
|
||||
{
|
||||
DownstreamHostAndPorts = new List<FileHostAndPort>
|
||||
{
|
||||
new FileHostAndPort
|
||||
{
|
||||
Host = "123.123.123",
|
||||
Port = 443,
|
||||
}
|
||||
},
|
||||
DownstreamScheme = "https",
|
||||
DownstreamPathTemplate = "/blooper/{productId}",
|
||||
UpstreamHttpMethod = new List<string> { "post" },
|
||||
UpstreamPathTemplate = "/test"
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
var command = new UpdateFileConfiguration(updatedConfiguration);
|
||||
GivenThereIsAConfiguration(configuration);
|
||||
GivenFiveServersAreRunning();
|
||||
GivenALeaderIsElected();
|
||||
GivenIHaveAnOcelotToken("/administration");
|
||||
WhenISendACommandIntoTheCluster(command);
|
||||
ThenTheCommandIsReplicatedToAllStateMachines(command);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_persist_command_to_five_servers_when_using_administration_api()
|
||||
{
|
||||
var configuration = new FileConfiguration
|
||||
{
|
||||
};
|
||||
|
||||
var updatedConfiguration = new FileConfiguration
|
||||
{
|
||||
ReRoutes = new List<FileReRoute>()
|
||||
{
|
||||
new FileReRoute()
|
||||
{
|
||||
DownstreamHostAndPorts = new List<FileHostAndPort>
|
||||
{
|
||||
new FileHostAndPort
|
||||
{
|
||||
Host = "127.0.0.1",
|
||||
Port = 80,
|
||||
}
|
||||
},
|
||||
DownstreamScheme = "http",
|
||||
DownstreamPathTemplate = "/geoffrey",
|
||||
UpstreamHttpMethod = new List<string> { "get" },
|
||||
UpstreamPathTemplate = "/"
|
||||
},
|
||||
new FileReRoute()
|
||||
{
|
||||
DownstreamHostAndPorts = new List<FileHostAndPort>
|
||||
{
|
||||
new FileHostAndPort
|
||||
{
|
||||
Host = "123.123.123",
|
||||
Port = 443,
|
||||
}
|
||||
},
|
||||
DownstreamScheme = "https",
|
||||
DownstreamPathTemplate = "/blooper/{productId}",
|
||||
UpstreamHttpMethod = new List<string> { "post" },
|
||||
UpstreamPathTemplate = "/test"
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
var command = new UpdateFileConfiguration(updatedConfiguration);
|
||||
GivenThereIsAConfiguration(configuration);
|
||||
GivenFiveServersAreRunning();
|
||||
GivenALeaderIsElected();
|
||||
GivenIHaveAnOcelotToken("/administration");
|
||||
GivenIHaveAddedATokenToMyRequest();
|
||||
WhenIPostOnTheApiGateway("/administration/configuration", updatedConfiguration);
|
||||
ThenTheCommandIsReplicatedToAllStateMachines(command);
|
||||
}
|
||||
|
||||
private void WhenISendACommandIntoTheCluster(UpdateFileConfiguration command)
|
||||
{
|
||||
var p = _peers.Peers.First();
|
||||
var json = JsonConvert.SerializeObject(command,new JsonSerializerSettings() {
|
||||
TypeNameHandling = TypeNameHandling.All
|
||||
});
|
||||
var httpContent = new StringContent(json);
|
||||
httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
|
||||
using(var httpClient = new HttpClient())
|
||||
{
|
||||
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _token.AccessToken);
|
||||
var response = httpClient.PostAsync($"{p.HostAndPort}/administration/raft/command", httpContent).GetAwaiter().GetResult();
|
||||
response.EnsureSuccessStatusCode();
|
||||
var content = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
|
||||
var result = JsonConvert.DeserializeObject<OkResponse<UpdateFileConfiguration>>(content);
|
||||
result.Command.Configuration.ReRoutes.Count.ShouldBe(2);
|
||||
}
|
||||
|
||||
//dirty sleep to make sure command replicated...
|
||||
var stopwatch = Stopwatch.StartNew();
|
||||
while(stopwatch.ElapsedMilliseconds < 10000)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private void ThenTheCommandIsReplicatedToAllStateMachines(UpdateFileConfiguration expecteds)
|
||||
{
|
||||
//dirty sleep to give a chance to replicate...
|
||||
var stopwatch = Stopwatch.StartNew();
|
||||
while(stopwatch.ElapsedMilliseconds < 2000)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
bool CommandCalledOnAllStateMachines()
|
||||
{
|
||||
try
|
||||
{
|
||||
var passed = 0;
|
||||
foreach (var peer in _peers.Peers)
|
||||
{
|
||||
var path = $"{peer.HostAndPort.Replace("/","").Replace(":","")}.db";
|
||||
using(var connection = new SqliteConnection($"Data Source={path};"))
|
||||
{
|
||||
connection.Open();
|
||||
var sql = @"select count(id) from logs";
|
||||
using(var command = new SqliteCommand(sql, connection))
|
||||
{
|
||||
var index = Convert.ToInt32(command.ExecuteScalar());
|
||||
index.ShouldBe(1);
|
||||
}
|
||||
}
|
||||
_httpClientForAssertions.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _token.AccessToken);
|
||||
var result = _httpClientForAssertions.GetAsync($"{peer.HostAndPort}/administration/configuration").Result;
|
||||
var json = result.Content.ReadAsStringAsync().Result;
|
||||
var response = JsonConvert.DeserializeObject<FileConfiguration>(json, new JsonSerializerSettings{TypeNameHandling = TypeNameHandling.All});
|
||||
response.GlobalConfiguration.RequestIdKey.ShouldBe(expecteds.Configuration.GlobalConfiguration.RequestIdKey);
|
||||
response.GlobalConfiguration.ServiceDiscoveryProvider.Host.ShouldBe(expecteds.Configuration.GlobalConfiguration.ServiceDiscoveryProvider.Host);
|
||||
response.GlobalConfiguration.ServiceDiscoveryProvider.Port.ShouldBe(expecteds.Configuration.GlobalConfiguration.ServiceDiscoveryProvider.Port);
|
||||
|
||||
for (var i = 0; i < response.ReRoutes.Count; i++)
|
||||
{
|
||||
for (var j = 0; j < response.ReRoutes[i].DownstreamHostAndPorts.Count; j++)
|
||||
{
|
||||
var res = response.ReRoutes[i].DownstreamHostAndPorts[j];
|
||||
var expected = expecteds.Configuration.ReRoutes[i].DownstreamHostAndPorts[j];
|
||||
res.Host.ShouldBe(expected.Host);
|
||||
res.Port.ShouldBe(expected.Port);
|
||||
}
|
||||
|
||||
response.ReRoutes[i].DownstreamPathTemplate.ShouldBe(expecteds.Configuration.ReRoutes[i].DownstreamPathTemplate);
|
||||
response.ReRoutes[i].DownstreamScheme.ShouldBe(expecteds.Configuration.ReRoutes[i].DownstreamScheme);
|
||||
response.ReRoutes[i].UpstreamPathTemplate.ShouldBe(expecteds.Configuration.ReRoutes[i].UpstreamPathTemplate);
|
||||
response.ReRoutes[i].UpstreamHttpMethod.ShouldBe(expecteds.Configuration.ReRoutes[i].UpstreamHttpMethod);
|
||||
}
|
||||
passed++;
|
||||
}
|
||||
|
||||
return passed == 5;
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
Console.WriteLine(e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
var commandOnAllStateMachines = WaitFor(20000).Until(() => CommandCalledOnAllStateMachines());
|
||||
commandOnAllStateMachines.ShouldBeTrue();
|
||||
}
|
||||
|
||||
private void WhenIPostOnTheApiGateway(string url, FileConfiguration updatedConfiguration)
|
||||
{
|
||||
var json = JsonConvert.SerializeObject(updatedConfiguration);
|
||||
var content = new StringContent(json);
|
||||
content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
|
||||
_response = _httpClient.PostAsync(url, content).Result;
|
||||
}
|
||||
|
||||
private void GivenIHaveAddedATokenToMyRequest()
|
||||
{
|
||||
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _token.AccessToken);
|
||||
}
|
||||
|
||||
private 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>("grant_type", "client_credentials")
|
||||
};
|
||||
var content = new FormUrlEncodedContent(formData);
|
||||
|
||||
var response = _httpClient.PostAsync(tokenUrl, content).Result;
|
||||
var responseContent = response.Content.ReadAsStringAsync().Result;
|
||||
response.EnsureSuccessStatusCode();
|
||||
_token = JsonConvert.DeserializeObject<BearerToken>(responseContent);
|
||||
var configPath = $"{adminPath}/.well-known/openid-configuration";
|
||||
response = _httpClient.GetAsync(configPath).Result;
|
||||
response.EnsureSuccessStatusCode();
|
||||
}
|
||||
|
||||
private void GivenThereIsAConfiguration(FileConfiguration fileConfiguration)
|
||||
{
|
||||
var configurationPath = $"{Directory.GetCurrentDirectory()}/configuration.json";
|
||||
|
||||
var jsonConfiguration = JsonConvert.SerializeObject(fileConfiguration);
|
||||
|
||||
if (File.Exists(configurationPath))
|
||||
{
|
||||
File.Delete(configurationPath);
|
||||
}
|
||||
|
||||
File.WriteAllText(configurationPath, jsonConfiguration);
|
||||
|
||||
var text = File.ReadAllText(configurationPath);
|
||||
|
||||
configurationPath = $"{AppContext.BaseDirectory}/configuration.json";
|
||||
|
||||
if (File.Exists(configurationPath))
|
||||
{
|
||||
File.Delete(configurationPath);
|
||||
}
|
||||
|
||||
File.WriteAllText(configurationPath, jsonConfiguration);
|
||||
|
||||
text = File.ReadAllText(configurationPath);
|
||||
}
|
||||
|
||||
private void GivenAServerIsRunning(string url)
|
||||
{
|
||||
lock(_lock)
|
||||
{
|
||||
IWebHostBuilder webHostBuilder = new WebHostBuilder();
|
||||
webHostBuilder.UseUrls(url)
|
||||
.UseKestrel()
|
||||
.UseContentRoot(Directory.GetCurrentDirectory())
|
||||
.ConfigureServices(x =>
|
||||
{
|
||||
x.AddSingleton(webHostBuilder);
|
||||
x.AddSingleton(new NodeId(url));
|
||||
})
|
||||
.UseStartup<RaftStartup>();
|
||||
|
||||
var builder = webHostBuilder.Build();
|
||||
builder.Start();
|
||||
|
||||
_webHostBuilders.Add(webHostBuilder);
|
||||
_builders.Add(builder);
|
||||
}
|
||||
}
|
||||
|
||||
private void GivenFiveServersAreRunning()
|
||||
{
|
||||
var bytes = File.ReadAllText("peers.json");
|
||||
_peers = JsonConvert.DeserializeObject<FilePeers>(bytes);
|
||||
|
||||
foreach (var peer in _peers.Peers)
|
||||
{
|
||||
var thread = new Thread(() => GivenAServerIsRunning(peer.HostAndPort));
|
||||
thread.Start();
|
||||
_threads.Add(thread);
|
||||
}
|
||||
}
|
||||
|
||||
private void GivenALeaderIsElected()
|
||||
{
|
||||
//dirty sleep to make sure we have a leader
|
||||
var stopwatch = Stopwatch.StartNew();
|
||||
while(stopwatch.ElapsedMilliseconds < 20000)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,184 +1,190 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Net.Http;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Newtonsoft.Json;
|
||||
using Ocelot.Configuration.File;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using System.Threading.Tasks;
|
||||
using System.Collections.Concurrent;
|
||||
|
||||
namespace Ocelot.IntegrationTests
|
||||
{
|
||||
public class ThreadSafeHeadersTests : IDisposable
|
||||
{
|
||||
private readonly HttpClient _httpClient;
|
||||
private IWebHost _builder;
|
||||
private IWebHostBuilder _webHostBuilder;
|
||||
private readonly string _ocelotBaseUrl;
|
||||
private IWebHost _downstreamBuilder;
|
||||
private readonly Random _random;
|
||||
private readonly ConcurrentBag<ThreadSafeHeadersTestResult> _results;
|
||||
|
||||
public ThreadSafeHeadersTests()
|
||||
{
|
||||
_results = new ConcurrentBag<ThreadSafeHeadersTestResult>();
|
||||
_random = new Random();
|
||||
_httpClient = new HttpClient();
|
||||
_ocelotBaseUrl = "http://localhost:5001";
|
||||
_httpClient.BaseAddress = new Uri(_ocelotBaseUrl);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_same_response_for_each_different_header_under_load_to_downsteam_service()
|
||||
{
|
||||
var configuration = new FileConfiguration
|
||||
{
|
||||
ReRoutes = new List<FileReRoute>
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamPathTemplate = "/",
|
||||
DownstreamScheme = "http",
|
||||
DownstreamHost = "localhost",
|
||||
DownstreamPort = 51879,
|
||||
UpstreamPathTemplate = "/",
|
||||
UpstreamHttpMethod = new List<string> { "Get" },
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.Given(x => GivenThereIsAConfiguration(configuration))
|
||||
.And(x => GivenThereIsAServiceRunningOn("http://localhost:51879"))
|
||||
.And(x => GivenOcelotIsRunning())
|
||||
.When(x => WhenIGetUrlOnTheApiGatewayMultipleTimesWithDifferentHeaderValues("/", 300))
|
||||
.Then(x => ThenTheSameHeaderValuesAreReturnedByTheDownstreamService())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenThereIsAServiceRunningOn(string url)
|
||||
{
|
||||
_downstreamBuilder = new WebHostBuilder()
|
||||
.UseUrls(url)
|
||||
.UseKestrel()
|
||||
.UseContentRoot(Directory.GetCurrentDirectory())
|
||||
.UseIISIntegration()
|
||||
.UseUrls(url)
|
||||
.Configure(app =>
|
||||
{
|
||||
app.Run(async context =>
|
||||
{
|
||||
var header = context.Request.Headers["ThreadSafeHeadersTest"];
|
||||
|
||||
context.Response.StatusCode = 200;
|
||||
await context.Response.WriteAsync(header[0]);
|
||||
});
|
||||
})
|
||||
.Build();
|
||||
|
||||
_downstreamBuilder.Start();
|
||||
}
|
||||
|
||||
private void GivenOcelotIsRunning()
|
||||
{
|
||||
_webHostBuilder = new WebHostBuilder()
|
||||
.UseUrls(_ocelotBaseUrl)
|
||||
.UseKestrel()
|
||||
.UseContentRoot(Directory.GetCurrentDirectory())
|
||||
.ConfigureServices(x =>
|
||||
{
|
||||
x.AddSingleton(_webHostBuilder);
|
||||
})
|
||||
.UseStartup<IntegrationTestsStartup>();
|
||||
|
||||
_builder = _webHostBuilder.Build();
|
||||
|
||||
_builder.Start();
|
||||
}
|
||||
|
||||
private void GivenThereIsAConfiguration(FileConfiguration fileConfiguration)
|
||||
{
|
||||
var configurationPath = $"{Directory.GetCurrentDirectory()}/configuration.json";
|
||||
|
||||
var jsonConfiguration = JsonConvert.SerializeObject(fileConfiguration);
|
||||
|
||||
if (File.Exists(configurationPath))
|
||||
{
|
||||
File.Delete(configurationPath);
|
||||
}
|
||||
|
||||
File.WriteAllText(configurationPath, jsonConfiguration);
|
||||
|
||||
var text = File.ReadAllText(configurationPath);
|
||||
|
||||
configurationPath = $"{AppContext.BaseDirectory}/configuration.json";
|
||||
|
||||
if (File.Exists(configurationPath))
|
||||
{
|
||||
File.Delete(configurationPath);
|
||||
}
|
||||
|
||||
File.WriteAllText(configurationPath, jsonConfiguration);
|
||||
|
||||
text = File.ReadAllText(configurationPath);
|
||||
}
|
||||
|
||||
private void WhenIGetUrlOnTheApiGatewayMultipleTimesWithDifferentHeaderValues(string url, int times)
|
||||
{
|
||||
var tasks = new Task[times];
|
||||
|
||||
for (int i = 0; i < times; i++)
|
||||
{
|
||||
var urlCopy = url;
|
||||
var random = _random.Next(0, 50);
|
||||
tasks[i] = GetForThreadSafeHeadersTest(urlCopy, random);
|
||||
}
|
||||
|
||||
Task.WaitAll(tasks);
|
||||
}
|
||||
|
||||
private async Task GetForThreadSafeHeadersTest(string url, int random)
|
||||
{
|
||||
var request = new HttpRequestMessage(HttpMethod.Get, url);
|
||||
request.Headers.Add("ThreadSafeHeadersTest", new List<string> { random.ToString() });
|
||||
var response = await _httpClient.SendAsync(request);
|
||||
var content = await response.Content.ReadAsStringAsync();
|
||||
int result = int.Parse(content);
|
||||
var tshtr = new ThreadSafeHeadersTestResult(result, random);
|
||||
_results.Add(tshtr);
|
||||
}
|
||||
|
||||
private void ThenTheSameHeaderValuesAreReturnedByTheDownstreamService()
|
||||
{
|
||||
foreach(var result in _results)
|
||||
{
|
||||
result.Result.ShouldBe(result.Random);
|
||||
}
|
||||
}
|
||||
public void Dispose()
|
||||
{
|
||||
_builder?.Dispose();
|
||||
_httpClient?.Dispose();
|
||||
_downstreamBuilder?.Dispose();
|
||||
}
|
||||
|
||||
class ThreadSafeHeadersTestResult
|
||||
{
|
||||
public ThreadSafeHeadersTestResult(int result, int random)
|
||||
{
|
||||
Result = result;
|
||||
Random = random;
|
||||
|
||||
}
|
||||
|
||||
public int Result { get; private set; }
|
||||
public int Random { get; private set; }
|
||||
}
|
||||
}
|
||||
}
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Net.Http;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Newtonsoft.Json;
|
||||
using Ocelot.Configuration.File;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using System.Threading.Tasks;
|
||||
using System.Collections.Concurrent;
|
||||
|
||||
namespace Ocelot.IntegrationTests
|
||||
{
|
||||
public class ThreadSafeHeadersTests : IDisposable
|
||||
{
|
||||
private readonly HttpClient _httpClient;
|
||||
private IWebHost _builder;
|
||||
private IWebHostBuilder _webHostBuilder;
|
||||
private readonly string _ocelotBaseUrl;
|
||||
private IWebHost _downstreamBuilder;
|
||||
private readonly Random _random;
|
||||
private readonly ConcurrentBag<ThreadSafeHeadersTestResult> _results;
|
||||
|
||||
public ThreadSafeHeadersTests()
|
||||
{
|
||||
_results = new ConcurrentBag<ThreadSafeHeadersTestResult>();
|
||||
_random = new Random();
|
||||
_httpClient = new HttpClient();
|
||||
_ocelotBaseUrl = "http://localhost:5001";
|
||||
_httpClient.BaseAddress = new Uri(_ocelotBaseUrl);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_same_response_for_each_different_header_under_load_to_downsteam_service()
|
||||
{
|
||||
var configuration = new FileConfiguration
|
||||
{
|
||||
ReRoutes = new List<FileReRoute>
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamPathTemplate = "/",
|
||||
DownstreamScheme = "http",
|
||||
DownstreamHostAndPorts = new List<FileHostAndPort>
|
||||
{
|
||||
new FileHostAndPort
|
||||
{
|
||||
Host = "localhost",
|
||||
Port = 51879,
|
||||
}
|
||||
},
|
||||
UpstreamPathTemplate = "/",
|
||||
UpstreamHttpMethod = new List<string> { "Get" },
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.Given(x => GivenThereIsAConfiguration(configuration))
|
||||
.And(x => GivenThereIsAServiceRunningOn("http://localhost:51879"))
|
||||
.And(x => GivenOcelotIsRunning())
|
||||
.When(x => WhenIGetUrlOnTheApiGatewayMultipleTimesWithDifferentHeaderValues("/", 300))
|
||||
.Then(x => ThenTheSameHeaderValuesAreReturnedByTheDownstreamService())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenThereIsAServiceRunningOn(string url)
|
||||
{
|
||||
_downstreamBuilder = new WebHostBuilder()
|
||||
.UseUrls(url)
|
||||
.UseKestrel()
|
||||
.UseContentRoot(Directory.GetCurrentDirectory())
|
||||
.UseIISIntegration()
|
||||
.UseUrls(url)
|
||||
.Configure(app =>
|
||||
{
|
||||
app.Run(async context =>
|
||||
{
|
||||
var header = context.Request.Headers["ThreadSafeHeadersTest"];
|
||||
|
||||
context.Response.StatusCode = 200;
|
||||
await context.Response.WriteAsync(header[0]);
|
||||
});
|
||||
})
|
||||
.Build();
|
||||
|
||||
_downstreamBuilder.Start();
|
||||
}
|
||||
|
||||
private void GivenOcelotIsRunning()
|
||||
{
|
||||
_webHostBuilder = new WebHostBuilder()
|
||||
.UseUrls(_ocelotBaseUrl)
|
||||
.UseKestrel()
|
||||
.UseContentRoot(Directory.GetCurrentDirectory())
|
||||
.ConfigureServices(x =>
|
||||
{
|
||||
x.AddSingleton(_webHostBuilder);
|
||||
})
|
||||
.UseStartup<IntegrationTestsStartup>();
|
||||
|
||||
_builder = _webHostBuilder.Build();
|
||||
|
||||
_builder.Start();
|
||||
}
|
||||
|
||||
private void GivenThereIsAConfiguration(FileConfiguration fileConfiguration)
|
||||
{
|
||||
var configurationPath = $"{Directory.GetCurrentDirectory()}/configuration.json";
|
||||
|
||||
var jsonConfiguration = JsonConvert.SerializeObject(fileConfiguration);
|
||||
|
||||
if (File.Exists(configurationPath))
|
||||
{
|
||||
File.Delete(configurationPath);
|
||||
}
|
||||
|
||||
File.WriteAllText(configurationPath, jsonConfiguration);
|
||||
|
||||
var text = File.ReadAllText(configurationPath);
|
||||
|
||||
configurationPath = $"{AppContext.BaseDirectory}/configuration.json";
|
||||
|
||||
if (File.Exists(configurationPath))
|
||||
{
|
||||
File.Delete(configurationPath);
|
||||
}
|
||||
|
||||
File.WriteAllText(configurationPath, jsonConfiguration);
|
||||
|
||||
text = File.ReadAllText(configurationPath);
|
||||
}
|
||||
|
||||
private void WhenIGetUrlOnTheApiGatewayMultipleTimesWithDifferentHeaderValues(string url, int times)
|
||||
{
|
||||
var tasks = new Task[times];
|
||||
|
||||
for (int i = 0; i < times; i++)
|
||||
{
|
||||
var urlCopy = url;
|
||||
var random = _random.Next(0, 50);
|
||||
tasks[i] = GetForThreadSafeHeadersTest(urlCopy, random);
|
||||
}
|
||||
|
||||
Task.WaitAll(tasks);
|
||||
}
|
||||
|
||||
private async Task GetForThreadSafeHeadersTest(string url, int random)
|
||||
{
|
||||
var request = new HttpRequestMessage(HttpMethod.Get, url);
|
||||
request.Headers.Add("ThreadSafeHeadersTest", new List<string> { random.ToString() });
|
||||
var response = await _httpClient.SendAsync(request);
|
||||
var content = await response.Content.ReadAsStringAsync();
|
||||
int result = int.Parse(content);
|
||||
var tshtr = new ThreadSafeHeadersTestResult(result, random);
|
||||
_results.Add(tshtr);
|
||||
}
|
||||
|
||||
private void ThenTheSameHeaderValuesAreReturnedByTheDownstreamService()
|
||||
{
|
||||
foreach(var result in _results)
|
||||
{
|
||||
result.Result.ShouldBe(result.Random);
|
||||
}
|
||||
}
|
||||
public void Dispose()
|
||||
{
|
||||
_builder?.Dispose();
|
||||
_httpClient?.Dispose();
|
||||
_downstreamBuilder?.Dispose();
|
||||
}
|
||||
|
||||
class ThreadSafeHeadersTestResult
|
||||
{
|
||||
public ThreadSafeHeadersTestResult(int result, int random)
|
||||
{
|
||||
Result = result;
|
||||
Random = random;
|
||||
|
||||
}
|
||||
|
||||
public int Result { get; private set; }
|
||||
public int Random { get; private set; }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,10 +1,10 @@
|
||||
{
|
||||
"Logging": {
|
||||
"IncludeScopes": true,
|
||||
"LogLevel": {
|
||||
"Default": "Error",
|
||||
"System": "Error",
|
||||
"Microsoft": "Error"
|
||||
}
|
||||
}
|
||||
}
|
||||
{
|
||||
"Logging": {
|
||||
"IncludeScopes": true,
|
||||
"LogLevel": {
|
||||
"Default": "Error",
|
||||
"System": "Error",
|
||||
"Microsoft": "Error"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,18 +1,18 @@
|
||||
{
|
||||
"Peers": [{
|
||||
"HostAndPort": "http://localhost:5000"
|
||||
},
|
||||
{
|
||||
"HostAndPort": "http://localhost:5002"
|
||||
},
|
||||
{
|
||||
"HostAndPort": "http://localhost:5003"
|
||||
},
|
||||
{
|
||||
"HostAndPort": "http://localhost:5004"
|
||||
},
|
||||
{
|
||||
"HostAndPort": "http://localhost:5001"
|
||||
}
|
||||
]
|
||||
{
|
||||
"Peers": [{
|
||||
"HostAndPort": "http://localhost:5000"
|
||||
},
|
||||
{
|
||||
"HostAndPort": "http://localhost:5002"
|
||||
},
|
||||
{
|
||||
"HostAndPort": "http://localhost:5003"
|
||||
},
|
||||
{
|
||||
"HostAndPort": "http://localhost:5004"
|
||||
},
|
||||
{
|
||||
"HostAndPort": "http://localhost:5001"
|
||||
}
|
||||
]
|
||||
}
|
@ -1,40 +1,40 @@
|
||||
using System;
|
||||
using CacheManager.Core;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Ocelot.DependencyInjection;
|
||||
using Ocelot.Middleware;
|
||||
using ConfigurationBuilder = Microsoft.Extensions.Configuration.ConfigurationBuilder;
|
||||
|
||||
namespace Ocelot.ManualTest
|
||||
{
|
||||
public class ManualTestStartup
|
||||
{
|
||||
public void ConfigureServices(IServiceCollection services)
|
||||
{
|
||||
Action<ConfigurationBuilderCachePart> settings = (x) =>
|
||||
{
|
||||
x.WithDictionaryHandle();
|
||||
};
|
||||
|
||||
services.AddAuthentication()
|
||||
.AddJwtBearer("TestKey", x =>
|
||||
{
|
||||
x.Authority = "test";
|
||||
x.Audience = "test";
|
||||
});
|
||||
|
||||
services.AddOcelot()
|
||||
.AddCacheManager(settings)
|
||||
.AddAdministration("/administration", "secret");
|
||||
}
|
||||
|
||||
public void Configure(IApplicationBuilder app)
|
||||
{
|
||||
app.UseOcelot().Wait();
|
||||
}
|
||||
}
|
||||
}
|
||||
using System;
|
||||
using CacheManager.Core;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Ocelot.DependencyInjection;
|
||||
using Ocelot.Middleware;
|
||||
using ConfigurationBuilder = Microsoft.Extensions.Configuration.ConfigurationBuilder;
|
||||
|
||||
namespace Ocelot.ManualTest
|
||||
{
|
||||
public class ManualTestStartup
|
||||
{
|
||||
public void ConfigureServices(IServiceCollection services)
|
||||
{
|
||||
Action<ConfigurationBuilderCachePart> settings = (x) =>
|
||||
{
|
||||
x.WithDictionaryHandle();
|
||||
};
|
||||
|
||||
services.AddAuthentication()
|
||||
.AddJwtBearer("TestKey", x =>
|
||||
{
|
||||
x.Authority = "test";
|
||||
x.Audience = "test";
|
||||
});
|
||||
|
||||
services.AddOcelot()
|
||||
.AddCacheManager(settings)
|
||||
.AddAdministration("/administration", "secret");
|
||||
}
|
||||
|
||||
public void Configure(IApplicationBuilder app)
|
||||
{
|
||||
app.UseOcelot().Wait();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,43 +1,43 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<VersionPrefix>0.0.0-dev</VersionPrefix>
|
||||
<TargetFramework>netcoreapp2.0</TargetFramework>
|
||||
<RuntimeFrameworkVersion>2.0.0</RuntimeFrameworkVersion>
|
||||
<PreserveCompilationContext>true</PreserveCompilationContext>
|
||||
<AssemblyName>Ocelot.ManualTest</AssemblyName>
|
||||
<OutputType>Exe</OutputType>
|
||||
<PackageId>Ocelot.ManualTest</PackageId>
|
||||
<RuntimeIdentifiers>osx.10.11-x64;osx.10.12-x64;win7-x64;win10-x64</RuntimeIdentifiers>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Update="Views;Areas\**\Views">
|
||||
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Update="configuration.json;appsettings.json;idsrv3test.pfx">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\src\Ocelot\Ocelot.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="2.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="2.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="2.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="2.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="2.0.0" />
|
||||
<PackageReference Include="Consul" Version="0.7.2.3" />
|
||||
<PackageReference Include="Polly" Version="5.3.1" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<VersionPrefix>0.0.0-dev</VersionPrefix>
|
||||
<TargetFramework>netcoreapp2.0</TargetFramework>
|
||||
<RuntimeFrameworkVersion>2.0.0</RuntimeFrameworkVersion>
|
||||
<PreserveCompilationContext>true</PreserveCompilationContext>
|
||||
<AssemblyName>Ocelot.ManualTest</AssemblyName>
|
||||
<OutputType>Exe</OutputType>
|
||||
<PackageId>Ocelot.ManualTest</PackageId>
|
||||
<RuntimeIdentifiers>osx.10.11-x64;osx.10.12-x64;win7-x64;win10-x64</RuntimeIdentifiers>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Update="Views;Areas\**\Views">
|
||||
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Update="configuration.json;appsettings.json;idsrv3test.pfx">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\src\Ocelot\Ocelot.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="2.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="2.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="2.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="2.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="2.0.0" />
|
||||
<PackageReference Include="Consul" Version="0.7.2.3" />
|
||||
<PackageReference Include="Polly" Version="5.3.1" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
@ -1,371 +1,371 @@
|
||||
{
|
||||
"id": "4dbde9fe-89f5-be35-bb9f-d3b438e16375",
|
||||
"name": "Ocelot",
|
||||
"description": "",
|
||||
"order": [
|
||||
"a1c95935-ed18-d5dc-bcb8-a3db8ba1934f",
|
||||
"ea0ed57a-2cb9-8acc-47dd-006b8db2f1b2",
|
||||
"c4494401-3985-a5bf-71fb-6e4171384ac6",
|
||||
"09af8dda-a9cb-20d2-5ee3-0a3023773a1a",
|
||||
"e8825dc3-4137-99a7-0000-ef5786610dc3",
|
||||
"fddfc4fa-5114-69e3-4744-203ed71a526b",
|
||||
"c45d30d7-d9c4-fa05-8110-d6e769bb6ff9",
|
||||
"4684c2fa-f38c-c193-5f55-bf563a1978c6",
|
||||
"37bfa9f1-fe29-6a68-e558-66d125d2c96f",
|
||||
"5f308240-79e3-cf74-7a6b-fe462f0d54f1",
|
||||
"178f16da-c61b-c881-1c33-9d64a56851a4"
|
||||
],
|
||||
"folders": [],
|
||||
"folders_order": [],
|
||||
"timestamp": 0,
|
||||
"owner": "212120",
|
||||
"public": false,
|
||||
"requests": [
|
||||
{
|
||||
"folder": null,
|
||||
"id": "09af8dda-a9cb-20d2-5ee3-0a3023773a1a",
|
||||
"name": "GET http://localhost:5000/comments?postId=1",
|
||||
"dataMode": "params",
|
||||
"data": null,
|
||||
"rawModeData": null,
|
||||
"descriptionFormat": "html",
|
||||
"description": "",
|
||||
"headers": "",
|
||||
"method": "GET",
|
||||
"pathVariables": {},
|
||||
"url": "http://localhost:5000/comments?postId=1",
|
||||
"preRequestScript": null,
|
||||
"tests": null,
|
||||
"currentHelper": "normal",
|
||||
"helperAttributes": "{}",
|
||||
"queryParams": [],
|
||||
"headerData": [],
|
||||
"pathVariableData": [],
|
||||
"responses": [],
|
||||
"collectionId": "4dbde9fe-89f5-be35-bb9f-d3b438e16375"
|
||||
},
|
||||
{
|
||||
"id": "178f16da-c61b-c881-1c33-9d64a56851a4",
|
||||
"headers": "Authorization: Bearer {{AccessToken}}\n",
|
||||
"headerData": [
|
||||
{
|
||||
"key": "Authorization",
|
||||
"value": "Bearer {{AccessToken}}",
|
||||
"enabled": true,
|
||||
"description": ""
|
||||
}
|
||||
],
|
||||
"url": "http://localhost:5000/administration/configuration",
|
||||
"folder": null,
|
||||
"queryParams": [],
|
||||
"preRequestScript": null,
|
||||
"pathVariables": {},
|
||||
"pathVariableData": [],
|
||||
"method": "GET",
|
||||
"data": null,
|
||||
"dataMode": "params",
|
||||
"tests": null,
|
||||
"currentHelper": "normal",
|
||||
"helperAttributes": "{}",
|
||||
"time": 1508849878025,
|
||||
"name": "GET http://localhost:5000/admin/configuration",
|
||||
"description": "",
|
||||
"collectionId": "4dbde9fe-89f5-be35-bb9f-d3b438e16375",
|
||||
"responses": [],
|
||||
"isFromCollection": true,
|
||||
"collectionRequestId": "178f16da-c61b-c881-1c33-9d64a56851a4",
|
||||
"rawModeData": null,
|
||||
"descriptionFormat": null
|
||||
},
|
||||
{
|
||||
"id": "37bfa9f1-fe29-6a68-e558-66d125d2c96f",
|
||||
"headers": "",
|
||||
"headerData": [],
|
||||
"url": "http://localhost:5000/administration/connect/token",
|
||||
"folder": null,
|
||||
"queryParams": [],
|
||||
"preRequestScript": null,
|
||||
"pathVariables": {},
|
||||
"pathVariableData": [],
|
||||
"method": "POST",
|
||||
"data": [
|
||||
{
|
||||
"key": "client_id",
|
||||
"value": "admin",
|
||||
"type": "text",
|
||||
"enabled": true
|
||||
},
|
||||
{
|
||||
"key": "client_secret",
|
||||
"value": "secret",
|
||||
"type": "text",
|
||||
"enabled": true
|
||||
},
|
||||
{
|
||||
"key": "scope",
|
||||
"value": "admin",
|
||||
"type": "text",
|
||||
"enabled": true
|
||||
},
|
||||
{
|
||||
"key": "username",
|
||||
"value": "admin",
|
||||
"type": "text",
|
||||
"enabled": true
|
||||
},
|
||||
{
|
||||
"key": "password",
|
||||
"value": "secret",
|
||||
"type": "text",
|
||||
"enabled": true
|
||||
},
|
||||
{
|
||||
"key": "grant_type",
|
||||
"value": "password",
|
||||
"type": "text",
|
||||
"enabled": true
|
||||
}
|
||||
],
|
||||
"dataMode": "params",
|
||||
"tests": "var jsonData = JSON.parse(responseBody);\npostman.setGlobalVariable(\"AccessToken\", jsonData.access_token);\npostman.setGlobalVariable(\"RefreshToken\", jsonData.refresh_token);",
|
||||
"currentHelper": "normal",
|
||||
"helperAttributes": "{}",
|
||||
"time": 1506359585080,
|
||||
"name": "POST http://localhost:5000/admin/connect/token copy",
|
||||
"description": "",
|
||||
"collectionId": "4dbde9fe-89f5-be35-bb9f-d3b438e16375",
|
||||
"responses": [],
|
||||
"isFromCollection": true,
|
||||
"collectionRequestId": "37bfa9f1-fe29-6a68-e558-66d125d2c96f",
|
||||
"rawModeData": null,
|
||||
"descriptionFormat": null
|
||||
},
|
||||
{
|
||||
"folder": null,
|
||||
"id": "4684c2fa-f38c-c193-5f55-bf563a1978c6",
|
||||
"name": "DELETE http://localhost:5000/posts/1",
|
||||
"dataMode": "params",
|
||||
"data": null,
|
||||
"rawModeData": null,
|
||||
"descriptionFormat": "html",
|
||||
"description": "",
|
||||
"headers": "",
|
||||
"method": "DELETE",
|
||||
"pathVariables": {},
|
||||
"url": "http://localhost:5000/posts/1",
|
||||
"preRequestScript": null,
|
||||
"tests": null,
|
||||
"currentHelper": "normal",
|
||||
"helperAttributes": "{}",
|
||||
"queryParams": [],
|
||||
"headerData": [],
|
||||
"pathVariableData": [],
|
||||
"responses": [],
|
||||
"collectionId": "4dbde9fe-89f5-be35-bb9f-d3b438e16375"
|
||||
},
|
||||
{
|
||||
"id": "5f308240-79e3-cf74-7a6b-fe462f0d54f1",
|
||||
"headers": "Authorization: Bearer {{AccessToken}}\n",
|
||||
"headerData": [
|
||||
{
|
||||
"key": "Authorization",
|
||||
"value": "Bearer {{AccessToken}}",
|
||||
"description": "",
|
||||
"enabled": true
|
||||
}
|
||||
],
|
||||
"url": "http://localhost:5000/administration/.well-known/openid-configuration",
|
||||
"folder": null,
|
||||
"queryParams": [],
|
||||
"preRequestScript": null,
|
||||
"pathVariables": {},
|
||||
"pathVariableData": [],
|
||||
"method": "GET",
|
||||
"data": null,
|
||||
"dataMode": "params",
|
||||
"tests": null,
|
||||
"currentHelper": "normal",
|
||||
"helperAttributes": {},
|
||||
"time": 1508849923518,
|
||||
"name": "GET http://localhost:5000/admin/.well-known/openid-configuration",
|
||||
"description": "",
|
||||
"collectionId": "4dbde9fe-89f5-be35-bb9f-d3b438e16375",
|
||||
"responses": []
|
||||
},
|
||||
{
|
||||
"folder": null,
|
||||
"id": "a1c95935-ed18-d5dc-bcb8-a3db8ba1934f",
|
||||
"name": "GET http://localhost:5000/posts",
|
||||
"dataMode": "params",
|
||||
"data": [
|
||||
{
|
||||
"key": "client_id",
|
||||
"value": "admin",
|
||||
"type": "text",
|
||||
"enabled": true
|
||||
},
|
||||
{
|
||||
"key": "client_secret",
|
||||
"value": "secret",
|
||||
"type": "text",
|
||||
"enabled": true
|
||||
},
|
||||
{
|
||||
"key": "scope",
|
||||
"value": "admin",
|
||||
"type": "text",
|
||||
"enabled": true
|
||||
},
|
||||
{
|
||||
"key": "username",
|
||||
"value": "admin",
|
||||
"type": "text",
|
||||
"enabled": true
|
||||
},
|
||||
{
|
||||
"key": "password",
|
||||
"value": "admin",
|
||||
"type": "text",
|
||||
"enabled": true
|
||||
},
|
||||
{
|
||||
"key": "grant_type",
|
||||
"value": "password",
|
||||
"type": "text",
|
||||
"enabled": true
|
||||
}
|
||||
],
|
||||
"rawModeData": null,
|
||||
"descriptionFormat": "html",
|
||||
"description": "",
|
||||
"headers": "",
|
||||
"method": "POST",
|
||||
"pathVariables": {},
|
||||
"url": "http://localhost:5000/admin/configuration",
|
||||
"preRequestScript": null,
|
||||
"tests": null,
|
||||
"currentHelper": "normal",
|
||||
"helperAttributes": "{}",
|
||||
"queryParams": [],
|
||||
"headerData": [],
|
||||
"pathVariableData": [],
|
||||
"responses": [],
|
||||
"collectionId": "4dbde9fe-89f5-be35-bb9f-d3b438e16375"
|
||||
},
|
||||
{
|
||||
"folder": null,
|
||||
"id": "c4494401-3985-a5bf-71fb-6e4171384ac6",
|
||||
"name": "GET http://localhost:5000/posts/1/comments",
|
||||
"dataMode": "params",
|
||||
"data": null,
|
||||
"rawModeData": null,
|
||||
"descriptionFormat": "html",
|
||||
"description": "",
|
||||
"headers": "",
|
||||
"method": "GET",
|
||||
"pathVariables": {},
|
||||
"url": "http://localhost:5000/posts/1/comments",
|
||||
"preRequestScript": null,
|
||||
"tests": null,
|
||||
"currentHelper": "normal",
|
||||
"helperAttributes": "{}",
|
||||
"queryParams": [],
|
||||
"headerData": [],
|
||||
"pathVariableData": [],
|
||||
"responses": [],
|
||||
"collectionId": "4dbde9fe-89f5-be35-bb9f-d3b438e16375"
|
||||
},
|
||||
{
|
||||
"folder": null,
|
||||
"id": "c45d30d7-d9c4-fa05-8110-d6e769bb6ff9",
|
||||
"name": "PATCH http://localhost:5000/posts/1",
|
||||
"dataMode": "raw",
|
||||
"data": [],
|
||||
"rawModeData": "{\n \"title\": \"gfdgsgsdgsdfgsdfgdfg\",\n}",
|
||||
"descriptionFormat": "html",
|
||||
"description": "",
|
||||
"headers": "",
|
||||
"method": "PATCH",
|
||||
"pathVariables": {},
|
||||
"url": "http://localhost:5000/posts/1",
|
||||
"preRequestScript": null,
|
||||
"tests": null,
|
||||
"currentHelper": "normal",
|
||||
"helperAttributes": "{}",
|
||||
"queryParams": [],
|
||||
"headerData": [],
|
||||
"pathVariableData": [],
|
||||
"responses": [],
|
||||
"collectionId": "4dbde9fe-89f5-be35-bb9f-d3b438e16375"
|
||||
},
|
||||
{
|
||||
"folder": null,
|
||||
"id": "e8825dc3-4137-99a7-0000-ef5786610dc3",
|
||||
"name": "POST http://localhost:5000/posts/1",
|
||||
"dataMode": "raw",
|
||||
"data": [],
|
||||
"rawModeData": "{\n \"userId\": 1,\n \"title\": \"test\",\n \"body\": \"test\"\n}",
|
||||
"descriptionFormat": "html",
|
||||
"description": "",
|
||||
"headers": "",
|
||||
"method": "POST",
|
||||
"pathVariables": {},
|
||||
"url": "http://localhost:5000/posts",
|
||||
"preRequestScript": null,
|
||||
"tests": null,
|
||||
"currentHelper": "normal",
|
||||
"helperAttributes": "{}",
|
||||
"queryParams": [],
|
||||
"headerData": [],
|
||||
"pathVariableData": [],
|
||||
"responses": [],
|
||||
"collectionId": "4dbde9fe-89f5-be35-bb9f-d3b438e16375"
|
||||
},
|
||||
{
|
||||
"folder": null,
|
||||
"id": "ea0ed57a-2cb9-8acc-47dd-006b8db2f1b2",
|
||||
"name": "GET http://localhost:5000/posts/1",
|
||||
"dataMode": "params",
|
||||
"data": null,
|
||||
"rawModeData": null,
|
||||
"descriptionFormat": "html",
|
||||
"description": "",
|
||||
"headers": "",
|
||||
"method": "GET",
|
||||
"pathVariables": {},
|
||||
"url": "http://localhost:5000/posts/1",
|
||||
"preRequestScript": null,
|
||||
"tests": null,
|
||||
"currentHelper": "normal",
|
||||
"helperAttributes": "{}",
|
||||
"queryParams": [],
|
||||
"headerData": [],
|
||||
"pathVariableData": [],
|
||||
"responses": [],
|
||||
"collectionId": "4dbde9fe-89f5-be35-bb9f-d3b438e16375"
|
||||
},
|
||||
{
|
||||
"folder": null,
|
||||
"id": "fddfc4fa-5114-69e3-4744-203ed71a526b",
|
||||
"name": "PUT http://localhost:5000/posts/1",
|
||||
"dataMode": "raw",
|
||||
"data": [],
|
||||
"rawModeData": "{\n \"userId\": 1,\n \"title\": \"test\",\n \"body\": \"test\"\n}",
|
||||
"descriptionFormat": "html",
|
||||
"description": "",
|
||||
"headers": "",
|
||||
"method": "PUT",
|
||||
"pathVariables": {},
|
||||
"url": "http://localhost:5000/posts/1",
|
||||
"preRequestScript": null,
|
||||
"tests": null,
|
||||
"currentHelper": "normal",
|
||||
"helperAttributes": "{}",
|
||||
"queryParams": [],
|
||||
"headerData": [],
|
||||
"pathVariableData": [],
|
||||
"responses": [],
|
||||
"collectionId": "4dbde9fe-89f5-be35-bb9f-d3b438e16375"
|
||||
}
|
||||
]
|
||||
{
|
||||
"id": "4dbde9fe-89f5-be35-bb9f-d3b438e16375",
|
||||
"name": "Ocelot",
|
||||
"description": "",
|
||||
"order": [
|
||||
"a1c95935-ed18-d5dc-bcb8-a3db8ba1934f",
|
||||
"ea0ed57a-2cb9-8acc-47dd-006b8db2f1b2",
|
||||
"c4494401-3985-a5bf-71fb-6e4171384ac6",
|
||||
"09af8dda-a9cb-20d2-5ee3-0a3023773a1a",
|
||||
"e8825dc3-4137-99a7-0000-ef5786610dc3",
|
||||
"fddfc4fa-5114-69e3-4744-203ed71a526b",
|
||||
"c45d30d7-d9c4-fa05-8110-d6e769bb6ff9",
|
||||
"4684c2fa-f38c-c193-5f55-bf563a1978c6",
|
||||
"37bfa9f1-fe29-6a68-e558-66d125d2c96f",
|
||||
"5f308240-79e3-cf74-7a6b-fe462f0d54f1",
|
||||
"178f16da-c61b-c881-1c33-9d64a56851a4"
|
||||
],
|
||||
"folders": [],
|
||||
"folders_order": [],
|
||||
"timestamp": 0,
|
||||
"owner": "212120",
|
||||
"public": false,
|
||||
"requests": [
|
||||
{
|
||||
"folder": null,
|
||||
"id": "09af8dda-a9cb-20d2-5ee3-0a3023773a1a",
|
||||
"name": "GET http://localhost:5000/comments?postId=1",
|
||||
"dataMode": "params",
|
||||
"data": null,
|
||||
"rawModeData": null,
|
||||
"descriptionFormat": "html",
|
||||
"description": "",
|
||||
"headers": "",
|
||||
"method": "GET",
|
||||
"pathVariables": {},
|
||||
"url": "http://localhost:5000/comments?postId=1",
|
||||
"preRequestScript": null,
|
||||
"tests": null,
|
||||
"currentHelper": "normal",
|
||||
"helperAttributes": "{}",
|
||||
"queryParams": [],
|
||||
"headerData": [],
|
||||
"pathVariableData": [],
|
||||
"responses": [],
|
||||
"collectionId": "4dbde9fe-89f5-be35-bb9f-d3b438e16375"
|
||||
},
|
||||
{
|
||||
"id": "178f16da-c61b-c881-1c33-9d64a56851a4",
|
||||
"headers": "Authorization: Bearer {{AccessToken}}\n",
|
||||
"headerData": [
|
||||
{
|
||||
"key": "Authorization",
|
||||
"value": "Bearer {{AccessToken}}",
|
||||
"enabled": true,
|
||||
"description": ""
|
||||
}
|
||||
],
|
||||
"url": "http://localhost:5000/administration/configuration",
|
||||
"folder": null,
|
||||
"queryParams": [],
|
||||
"preRequestScript": null,
|
||||
"pathVariables": {},
|
||||
"pathVariableData": [],
|
||||
"method": "GET",
|
||||
"data": null,
|
||||
"dataMode": "params",
|
||||
"tests": null,
|
||||
"currentHelper": "normal",
|
||||
"helperAttributes": "{}",
|
||||
"time": 1508849878025,
|
||||
"name": "GET http://localhost:5000/admin/configuration",
|
||||
"description": "",
|
||||
"collectionId": "4dbde9fe-89f5-be35-bb9f-d3b438e16375",
|
||||
"responses": [],
|
||||
"isFromCollection": true,
|
||||
"collectionRequestId": "178f16da-c61b-c881-1c33-9d64a56851a4",
|
||||
"rawModeData": null,
|
||||
"descriptionFormat": null
|
||||
},
|
||||
{
|
||||
"id": "37bfa9f1-fe29-6a68-e558-66d125d2c96f",
|
||||
"headers": "",
|
||||
"headerData": [],
|
||||
"url": "http://localhost:5000/administration/connect/token",
|
||||
"folder": null,
|
||||
"queryParams": [],
|
||||
"preRequestScript": null,
|
||||
"pathVariables": {},
|
||||
"pathVariableData": [],
|
||||
"method": "POST",
|
||||
"data": [
|
||||
{
|
||||
"key": "client_id",
|
||||
"value": "admin",
|
||||
"type": "text",
|
||||
"enabled": true
|
||||
},
|
||||
{
|
||||
"key": "client_secret",
|
||||
"value": "secret",
|
||||
"type": "text",
|
||||
"enabled": true
|
||||
},
|
||||
{
|
||||
"key": "scope",
|
||||
"value": "admin",
|
||||
"type": "text",
|
||||
"enabled": true
|
||||
},
|
||||
{
|
||||
"key": "username",
|
||||
"value": "admin",
|
||||
"type": "text",
|
||||
"enabled": true
|
||||
},
|
||||
{
|
||||
"key": "password",
|
||||
"value": "secret",
|
||||
"type": "text",
|
||||
"enabled": true
|
||||
},
|
||||
{
|
||||
"key": "grant_type",
|
||||
"value": "password",
|
||||
"type": "text",
|
||||
"enabled": true
|
||||
}
|
||||
],
|
||||
"dataMode": "params",
|
||||
"tests": "var jsonData = JSON.parse(responseBody);\npostman.setGlobalVariable(\"AccessToken\", jsonData.access_token);\npostman.setGlobalVariable(\"RefreshToken\", jsonData.refresh_token);",
|
||||
"currentHelper": "normal",
|
||||
"helperAttributes": "{}",
|
||||
"time": 1506359585080,
|
||||
"name": "POST http://localhost:5000/admin/connect/token copy",
|
||||
"description": "",
|
||||
"collectionId": "4dbde9fe-89f5-be35-bb9f-d3b438e16375",
|
||||
"responses": [],
|
||||
"isFromCollection": true,
|
||||
"collectionRequestId": "37bfa9f1-fe29-6a68-e558-66d125d2c96f",
|
||||
"rawModeData": null,
|
||||
"descriptionFormat": null
|
||||
},
|
||||
{
|
||||
"folder": null,
|
||||
"id": "4684c2fa-f38c-c193-5f55-bf563a1978c6",
|
||||
"name": "DELETE http://localhost:5000/posts/1",
|
||||
"dataMode": "params",
|
||||
"data": null,
|
||||
"rawModeData": null,
|
||||
"descriptionFormat": "html",
|
||||
"description": "",
|
||||
"headers": "",
|
||||
"method": "DELETE",
|
||||
"pathVariables": {},
|
||||
"url": "http://localhost:5000/posts/1",
|
||||
"preRequestScript": null,
|
||||
"tests": null,
|
||||
"currentHelper": "normal",
|
||||
"helperAttributes": "{}",
|
||||
"queryParams": [],
|
||||
"headerData": [],
|
||||
"pathVariableData": [],
|
||||
"responses": [],
|
||||
"collectionId": "4dbde9fe-89f5-be35-bb9f-d3b438e16375"
|
||||
},
|
||||
{
|
||||
"id": "5f308240-79e3-cf74-7a6b-fe462f0d54f1",
|
||||
"headers": "Authorization: Bearer {{AccessToken}}\n",
|
||||
"headerData": [
|
||||
{
|
||||
"key": "Authorization",
|
||||
"value": "Bearer {{AccessToken}}",
|
||||
"description": "",
|
||||
"enabled": true
|
||||
}
|
||||
],
|
||||
"url": "http://localhost:5000/administration/.well-known/openid-configuration",
|
||||
"folder": null,
|
||||
"queryParams": [],
|
||||
"preRequestScript": null,
|
||||
"pathVariables": {},
|
||||
"pathVariableData": [],
|
||||
"method": "GET",
|
||||
"data": null,
|
||||
"dataMode": "params",
|
||||
"tests": null,
|
||||
"currentHelper": "normal",
|
||||
"helperAttributes": {},
|
||||
"time": 1508849923518,
|
||||
"name": "GET http://localhost:5000/admin/.well-known/openid-configuration",
|
||||
"description": "",
|
||||
"collectionId": "4dbde9fe-89f5-be35-bb9f-d3b438e16375",
|
||||
"responses": []
|
||||
},
|
||||
{
|
||||
"folder": null,
|
||||
"id": "a1c95935-ed18-d5dc-bcb8-a3db8ba1934f",
|
||||
"name": "GET http://localhost:5000/posts",
|
||||
"dataMode": "params",
|
||||
"data": [
|
||||
{
|
||||
"key": "client_id",
|
||||
"value": "admin",
|
||||
"type": "text",
|
||||
"enabled": true
|
||||
},
|
||||
{
|
||||
"key": "client_secret",
|
||||
"value": "secret",
|
||||
"type": "text",
|
||||
"enabled": true
|
||||
},
|
||||
{
|
||||
"key": "scope",
|
||||
"value": "admin",
|
||||
"type": "text",
|
||||
"enabled": true
|
||||
},
|
||||
{
|
||||
"key": "username",
|
||||
"value": "admin",
|
||||
"type": "text",
|
||||
"enabled": true
|
||||
},
|
||||
{
|
||||
"key": "password",
|
||||
"value": "admin",
|
||||
"type": "text",
|
||||
"enabled": true
|
||||
},
|
||||
{
|
||||
"key": "grant_type",
|
||||
"value": "password",
|
||||
"type": "text",
|
||||
"enabled": true
|
||||
}
|
||||
],
|
||||
"rawModeData": null,
|
||||
"descriptionFormat": "html",
|
||||
"description": "",
|
||||
"headers": "",
|
||||
"method": "POST",
|
||||
"pathVariables": {},
|
||||
"url": "http://localhost:5000/admin/configuration",
|
||||
"preRequestScript": null,
|
||||
"tests": null,
|
||||
"currentHelper": "normal",
|
||||
"helperAttributes": "{}",
|
||||
"queryParams": [],
|
||||
"headerData": [],
|
||||
"pathVariableData": [],
|
||||
"responses": [],
|
||||
"collectionId": "4dbde9fe-89f5-be35-bb9f-d3b438e16375"
|
||||
},
|
||||
{
|
||||
"folder": null,
|
||||
"id": "c4494401-3985-a5bf-71fb-6e4171384ac6",
|
||||
"name": "GET http://localhost:5000/posts/1/comments",
|
||||
"dataMode": "params",
|
||||
"data": null,
|
||||
"rawModeData": null,
|
||||
"descriptionFormat": "html",
|
||||
"description": "",
|
||||
"headers": "",
|
||||
"method": "GET",
|
||||
"pathVariables": {},
|
||||
"url": "http://localhost:5000/posts/1/comments",
|
||||
"preRequestScript": null,
|
||||
"tests": null,
|
||||
"currentHelper": "normal",
|
||||
"helperAttributes": "{}",
|
||||
"queryParams": [],
|
||||
"headerData": [],
|
||||
"pathVariableData": [],
|
||||
"responses": [],
|
||||
"collectionId": "4dbde9fe-89f5-be35-bb9f-d3b438e16375"
|
||||
},
|
||||
{
|
||||
"folder": null,
|
||||
"id": "c45d30d7-d9c4-fa05-8110-d6e769bb6ff9",
|
||||
"name": "PATCH http://localhost:5000/posts/1",
|
||||
"dataMode": "raw",
|
||||
"data": [],
|
||||
"rawModeData": "{\n \"title\": \"gfdgsgsdgsdfgsdfgdfg\",\n}",
|
||||
"descriptionFormat": "html",
|
||||
"description": "",
|
||||
"headers": "",
|
||||
"method": "PATCH",
|
||||
"pathVariables": {},
|
||||
"url": "http://localhost:5000/posts/1",
|
||||
"preRequestScript": null,
|
||||
"tests": null,
|
||||
"currentHelper": "normal",
|
||||
"helperAttributes": "{}",
|
||||
"queryParams": [],
|
||||
"headerData": [],
|
||||
"pathVariableData": [],
|
||||
"responses": [],
|
||||
"collectionId": "4dbde9fe-89f5-be35-bb9f-d3b438e16375"
|
||||
},
|
||||
{
|
||||
"folder": null,
|
||||
"id": "e8825dc3-4137-99a7-0000-ef5786610dc3",
|
||||
"name": "POST http://localhost:5000/posts/1",
|
||||
"dataMode": "raw",
|
||||
"data": [],
|
||||
"rawModeData": "{\n \"userId\": 1,\n \"title\": \"test\",\n \"body\": \"test\"\n}",
|
||||
"descriptionFormat": "html",
|
||||
"description": "",
|
||||
"headers": "",
|
||||
"method": "POST",
|
||||
"pathVariables": {},
|
||||
"url": "http://localhost:5000/posts",
|
||||
"preRequestScript": null,
|
||||
"tests": null,
|
||||
"currentHelper": "normal",
|
||||
"helperAttributes": "{}",
|
||||
"queryParams": [],
|
||||
"headerData": [],
|
||||
"pathVariableData": [],
|
||||
"responses": [],
|
||||
"collectionId": "4dbde9fe-89f5-be35-bb9f-d3b438e16375"
|
||||
},
|
||||
{
|
||||
"folder": null,
|
||||
"id": "ea0ed57a-2cb9-8acc-47dd-006b8db2f1b2",
|
||||
"name": "GET http://localhost:5000/posts/1",
|
||||
"dataMode": "params",
|
||||
"data": null,
|
||||
"rawModeData": null,
|
||||
"descriptionFormat": "html",
|
||||
"description": "",
|
||||
"headers": "",
|
||||
"method": "GET",
|
||||
"pathVariables": {},
|
||||
"url": "http://localhost:5000/posts/1",
|
||||
"preRequestScript": null,
|
||||
"tests": null,
|
||||
"currentHelper": "normal",
|
||||
"helperAttributes": "{}",
|
||||
"queryParams": [],
|
||||
"headerData": [],
|
||||
"pathVariableData": [],
|
||||
"responses": [],
|
||||
"collectionId": "4dbde9fe-89f5-be35-bb9f-d3b438e16375"
|
||||
},
|
||||
{
|
||||
"folder": null,
|
||||
"id": "fddfc4fa-5114-69e3-4744-203ed71a526b",
|
||||
"name": "PUT http://localhost:5000/posts/1",
|
||||
"dataMode": "raw",
|
||||
"data": [],
|
||||
"rawModeData": "{\n \"userId\": 1,\n \"title\": \"test\",\n \"body\": \"test\"\n}",
|
||||
"descriptionFormat": "html",
|
||||
"description": "",
|
||||
"headers": "",
|
||||
"method": "PUT",
|
||||
"pathVariables": {},
|
||||
"url": "http://localhost:5000/posts/1",
|
||||
"preRequestScript": null,
|
||||
"tests": null,
|
||||
"currentHelper": "normal",
|
||||
"helperAttributes": "{}",
|
||||
"queryParams": [],
|
||||
"headerData": [],
|
||||
"pathVariableData": [],
|
||||
"responses": [],
|
||||
"collectionId": "4dbde9fe-89f5-be35-bb9f-d3b438e16375"
|
||||
}
|
||||
]
|
||||
}
|
@ -1,39 +1,39 @@
|
||||
using System.IO;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
namespace Ocelot.ManualTest
|
||||
{
|
||||
public class Program
|
||||
{
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
IWebHostBuilder builder = new WebHostBuilder();
|
||||
builder.ConfigureServices(s => {
|
||||
s.AddSingleton(builder);
|
||||
});
|
||||
builder.UseKestrel()
|
||||
.UseContentRoot(Directory.GetCurrentDirectory())
|
||||
.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();
|
||||
})
|
||||
.ConfigureLogging((hostingContext, logging) =>
|
||||
{
|
||||
logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
|
||||
logging.AddConsole();
|
||||
})
|
||||
.UseIISIntegration()
|
||||
.UseStartup<ManualTestStartup>();
|
||||
var host = builder.Build();
|
||||
host.Run();
|
||||
}
|
||||
}
|
||||
}
|
||||
using System.IO;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
namespace Ocelot.ManualTest
|
||||
{
|
||||
public class Program
|
||||
{
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
IWebHostBuilder builder = new WebHostBuilder();
|
||||
builder.ConfigureServices(s => {
|
||||
s.AddSingleton(builder);
|
||||
});
|
||||
builder.UseKestrel()
|
||||
.UseContentRoot(Directory.GetCurrentDirectory())
|
||||
.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();
|
||||
})
|
||||
.ConfigureLogging((hostingContext, logging) =>
|
||||
{
|
||||
logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
|
||||
logging.AddConsole();
|
||||
})
|
||||
.UseIISIntegration()
|
||||
.UseStartup<ManualTestStartup>();
|
||||
var host = builder.Build();
|
||||
host.Run();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,26 +1,26 @@
|
||||
{
|
||||
"iisSettings": {
|
||||
"windowsAuthentication": false,
|
||||
"anonymousAuthentication": true,
|
||||
"iisExpress": {
|
||||
"applicationUrl": "http://localhost:24620/",
|
||||
"sslPort": 0
|
||||
}
|
||||
},
|
||||
"profiles": {
|
||||
"IIS Express": {
|
||||
"commandName": "IISExpress",
|
||||
"launchBrowser": true,
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
},
|
||||
"Ocelot.ManualTest": {
|
||||
"commandName": "Project",
|
||||
"launchUrl": "http://localhost:5000",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
}
|
||||
}
|
||||
{
|
||||
"iisSettings": {
|
||||
"windowsAuthentication": false,
|
||||
"anonymousAuthentication": true,
|
||||
"iisExpress": {
|
||||
"applicationUrl": "http://localhost:24620/",
|
||||
"sslPort": 0
|
||||
}
|
||||
},
|
||||
"profiles": {
|
||||
"IIS Express": {
|
||||
"commandName": "IISExpress",
|
||||
"launchBrowser": true,
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
},
|
||||
"Ocelot.ManualTest": {
|
||||
"commandName": "Project",
|
||||
"launchUrl": "http://localhost:5000",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,10 +1,10 @@
|
||||
{
|
||||
"Logging": {
|
||||
"IncludeScopes": false,
|
||||
"LogLevel": {
|
||||
"Default": "Debug",
|
||||
"System": "Error",
|
||||
"Microsoft": "Error"
|
||||
}
|
||||
}
|
||||
}
|
||||
{
|
||||
"Logging": {
|
||||
"IncludeScopes": false,
|
||||
"LogLevel": {
|
||||
"Default": "Debug",
|
||||
"System": "Error",
|
||||
"Microsoft": "Error"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,311 +1,311 @@
|
||||
{
|
||||
"ReRoutes": [
|
||||
{
|
||||
"DownstreamPathTemplate": "/",
|
||||
"DownstreamScheme": "http",
|
||||
"DownstreamHost": "localhost",
|
||||
"DownstreamPort": 52876,
|
||||
"UpstreamPathTemplate": "/identityserverexample",
|
||||
"UpstreamHttpMethod": [ "Get" ],
|
||||
"QoSOptions": {
|
||||
"ExceptionsAllowedBeforeBreaking": 3,
|
||||
"DurationOfBreak": 10,
|
||||
"TimeoutValue": 5000
|
||||
},
|
||||
"AuthenticationOptions": {
|
||||
"AuthenticationProviderKey": "TestKey",
|
||||
"AllowedScopes": [
|
||||
"openid",
|
||||
"offline_access"
|
||||
]
|
||||
},
|
||||
"AddHeadersToRequest": {
|
||||
"CustomerId": "Claims[CustomerId] > value",
|
||||
"LocationId": "Claims[LocationId] > value",
|
||||
"UserType": "Claims[sub] > value[0] > |",
|
||||
"UserId": "Claims[sub] > value[1] > |"
|
||||
},
|
||||
"AddClaimsToRequest": {
|
||||
"CustomerId": "Claims[CustomerId] > value",
|
||||
"LocationId": "Claims[LocationId] > value",
|
||||
"UserType": "Claims[sub] > value[0] > |",
|
||||
"UserId": "Claims[sub] > value[1] > |"
|
||||
},
|
||||
"AddQueriesToRequest": {
|
||||
"CustomerId": "Claims[CustomerId] > value",
|
||||
"LocationId": "Claims[LocationId] > value",
|
||||
"UserType": "Claims[sub] > value[0] > |",
|
||||
"UserId": "Claims[sub] > value[1] > |"
|
||||
},
|
||||
"RouteClaimsRequirement": {
|
||||
"UserType": "registered"
|
||||
},
|
||||
"RequestIdKey": "OcRequestId"
|
||||
},
|
||||
{
|
||||
"DownstreamPathTemplate": "/posts",
|
||||
"DownstreamScheme": "https",
|
||||
"DownstreamHost": "jsonplaceholder.typicode.com",
|
||||
"DownstreamPort": 443,
|
||||
"UpstreamPathTemplate": "/posts",
|
||||
"UpstreamHttpMethod": [ "Get" ],
|
||||
"QoSOptions": {
|
||||
"ExceptionsAllowedBeforeBreaking": 3,
|
||||
"DurationOfBreak": 10,
|
||||
"TimeoutValue": 5000
|
||||
}
|
||||
},
|
||||
{
|
||||
"DownstreamPathTemplate": "/posts/{postId}",
|
||||
"DownstreamScheme": "http",
|
||||
"DownstreamHost": "jsonplaceholder.typicode.com",
|
||||
"DownstreamPort": 80,
|
||||
"UpstreamPathTemplate": "/posts/{postId}",
|
||||
"UpstreamHttpMethod": [ "Get" ],
|
||||
"RequestIdKey": "ReRouteRequestId",
|
||||
"QoSOptions": {
|
||||
"ExceptionsAllowedBeforeBreaking": 3,
|
||||
"DurationOfBreak": 10,
|
||||
"TimeoutValue": 5000
|
||||
}
|
||||
},
|
||||
{
|
||||
"DownstreamPathTemplate": "/posts/{postId}/comments",
|
||||
"DownstreamScheme": "http",
|
||||
"DownstreamHost": "jsonplaceholder.typicode.com",
|
||||
"DownstreamPort": 80,
|
||||
"UpstreamPathTemplate": "/posts/{postId}/comments",
|
||||
"UpstreamHttpMethod": [ "Get" ],
|
||||
"QoSOptions": {
|
||||
"ExceptionsAllowedBeforeBreaking": 3,
|
||||
"DurationOfBreak": 10,
|
||||
"TimeoutValue": 5000
|
||||
}
|
||||
},
|
||||
{
|
||||
"DownstreamPathTemplate": "/comments",
|
||||
"DownstreamScheme": "http",
|
||||
"DownstreamHost": "jsonplaceholder.typicode.com",
|
||||
"DownstreamPort": 80,
|
||||
"UpstreamPathTemplate": "/comments",
|
||||
"UpstreamHttpMethod": [ "Get" ],
|
||||
"QoSOptions": {
|
||||
"ExceptionsAllowedBeforeBreaking": 3,
|
||||
"DurationOfBreak": 10,
|
||||
"TimeoutValue": 5000
|
||||
}
|
||||
},
|
||||
{
|
||||
"DownstreamPathTemplate": "/posts",
|
||||
"DownstreamScheme": "http",
|
||||
"DownstreamHost": "jsonplaceholder.typicode.com",
|
||||
"DownstreamPort": 80,
|
||||
"UpstreamPathTemplate": "/posts",
|
||||
"UpstreamHttpMethod": [ "Post" ],
|
||||
"QoSOptions": {
|
||||
"ExceptionsAllowedBeforeBreaking": 3,
|
||||
"DurationOfBreak": 10,
|
||||
"TimeoutValue": 5000
|
||||
}
|
||||
},
|
||||
{
|
||||
"DownstreamPathTemplate": "/posts/{postId}",
|
||||
"DownstreamScheme": "http",
|
||||
"DownstreamHost": "jsonplaceholder.typicode.com",
|
||||
"DownstreamPort": 80,
|
||||
"UpstreamPathTemplate": "/posts/{postId}",
|
||||
"UpstreamHttpMethod": [ "Put" ],
|
||||
"QoSOptions": {
|
||||
"ExceptionsAllowedBeforeBreaking": 3,
|
||||
"DurationOfBreak": 10,
|
||||
"TimeoutValue": 5000
|
||||
}
|
||||
},
|
||||
{
|
||||
"DownstreamPathTemplate": "/posts/{postId}",
|
||||
"DownstreamScheme": "http",
|
||||
"DownstreamHost": "jsonplaceholder.typicode.com",
|
||||
"DownstreamPort": 80,
|
||||
"UpstreamPathTemplate": "/posts/{postId}",
|
||||
"UpstreamHttpMethod": [ "Patch" ],
|
||||
"QoSOptions": {
|
||||
"ExceptionsAllowedBeforeBreaking": 3,
|
||||
"DurationOfBreak": 10,
|
||||
"TimeoutValue": 5000
|
||||
}
|
||||
},
|
||||
{
|
||||
"DownstreamPathTemplate": "/posts/{postId}",
|
||||
"DownstreamScheme": "http",
|
||||
"DownstreamHost": "jsonplaceholder.typicode.com",
|
||||
"DownstreamPort": 80,
|
||||
"UpstreamPathTemplate": "/posts/{postId}",
|
||||
"UpstreamHttpMethod": [ "Delete" ],
|
||||
"QoSOptions": {
|
||||
"ExceptionsAllowedBeforeBreaking": 3,
|
||||
"DurationOfBreak": 10,
|
||||
"TimeoutValue": 5000
|
||||
}
|
||||
},
|
||||
{
|
||||
"DownstreamPathTemplate": "/api/products",
|
||||
"DownstreamScheme": "http",
|
||||
"DownstreamHost": "jsonplaceholder.typicode.com",
|
||||
"DownstreamPort": 80,
|
||||
"UpstreamPathTemplate": "/products",
|
||||
"UpstreamHttpMethod": [ "Get" ],
|
||||
"QoSOptions": {
|
||||
"ExceptionsAllowedBeforeBreaking": 3,
|
||||
"DurationOfBreak": 10,
|
||||
"TimeoutValue": 5000
|
||||
},
|
||||
"FileCacheOptions": { "TtlSeconds": 15 }
|
||||
},
|
||||
{
|
||||
"DownstreamPathTemplate": "/api/products/{productId}",
|
||||
"DownstreamScheme": "http",
|
||||
"DownstreamHost": "jsonplaceholder.typicode.com",
|
||||
"DownstreamPort": 80,
|
||||
"UpstreamPathTemplate": "/products/{productId}",
|
||||
"UpstreamHttpMethod": [ "Get" ],
|
||||
"FileCacheOptions": { "TtlSeconds": 15 }
|
||||
},
|
||||
{
|
||||
"DownstreamPathTemplate": "/api/products",
|
||||
"DownstreamScheme": "http",
|
||||
"DownstreamHost": "products20161126090340.azurewebsites.net",
|
||||
"DownstreamPort": 80,
|
||||
"UpstreamPathTemplate": "/products",
|
||||
"UpstreamHttpMethod": [ "Post" ],
|
||||
"QoSOptions": {
|
||||
"ExceptionsAllowedBeforeBreaking": 3,
|
||||
"DurationOfBreak": 10,
|
||||
"TimeoutValue": 5000
|
||||
}
|
||||
},
|
||||
{
|
||||
"DownstreamPathTemplate": "/api/products/{productId}",
|
||||
"DownstreamScheme": "http",
|
||||
"DownstreamHost": "products20161126090340.azurewebsites.net",
|
||||
"DownstreamPort": 80,
|
||||
"UpstreamPathTemplate": "/products/{productId}",
|
||||
"UpstreamHttpMethod": [ "Put" ],
|
||||
"QoSOptions": {
|
||||
"ExceptionsAllowedBeforeBreaking": 3,
|
||||
"DurationOfBreak": 10,
|
||||
"TimeoutValue": 5000
|
||||
},
|
||||
"FileCacheOptions": { "TtlSeconds": 15 }
|
||||
},
|
||||
{
|
||||
"DownstreamPathTemplate": "/api/products/{productId}",
|
||||
"DownstreamScheme": "http",
|
||||
"DownstreamHost": "products20161126090340.azurewebsites.net",
|
||||
"DownstreamPort": 80,
|
||||
"UpstreamPathTemplate": "/products/{productId}",
|
||||
"UpstreamHttpMethod": [ "Delete" ],
|
||||
"QoSOptions": {
|
||||
"ExceptionsAllowedBeforeBreaking": 3,
|
||||
"DurationOfBreak": 10,
|
||||
"TimeoutValue": 5000
|
||||
},
|
||||
"FileCacheOptions": { "TtlSeconds": 15 }
|
||||
},
|
||||
{
|
||||
"DownstreamPathTemplate": "/api/customers",
|
||||
"DownstreamScheme": "http",
|
||||
"DownstreamHost": "customers20161126090811.azurewebsites.net",
|
||||
"DownstreamPort": 80,
|
||||
"UpstreamPathTemplate": "/customers",
|
||||
"UpstreamHttpMethod": [ "Get" ],
|
||||
"QoSOptions": {
|
||||
"ExceptionsAllowedBeforeBreaking": 3,
|
||||
"DurationOfBreak": 10,
|
||||
"TimeoutValue": 5000
|
||||
},
|
||||
"FileCacheOptions": { "TtlSeconds": 15 }
|
||||
},
|
||||
{
|
||||
"DownstreamPathTemplate": "/api/customers/{customerId}",
|
||||
"DownstreamScheme": "http",
|
||||
"DownstreamHost": "customers20161126090811.azurewebsites.net",
|
||||
"DownstreamPort": 80,
|
||||
"UpstreamPathTemplate": "/customers/{customerId}",
|
||||
"UpstreamHttpMethod": [ "Get" ],
|
||||
"QoSOptions": {
|
||||
"ExceptionsAllowedBeforeBreaking": 3,
|
||||
"DurationOfBreak": 10,
|
||||
"TimeoutValue": 5000
|
||||
},
|
||||
"FileCacheOptions": { "TtlSeconds": 15 }
|
||||
},
|
||||
{
|
||||
"DownstreamPathTemplate": "/api/customers",
|
||||
"DownstreamScheme": "http",
|
||||
"DownstreamHost": "customers20161126090811.azurewebsites.net",
|
||||
"DownstreamPort": 80,
|
||||
"UpstreamPathTemplate": "/customers",
|
||||
"UpstreamHttpMethod": [ "Post" ],
|
||||
"QoSOptions": {
|
||||
"ExceptionsAllowedBeforeBreaking": 3,
|
||||
"DurationOfBreak": 10,
|
||||
"TimeoutValue": 5000
|
||||
},
|
||||
"FileCacheOptions": { "TtlSeconds": 15 }
|
||||
},
|
||||
{
|
||||
"DownstreamPathTemplate": "/api/customers/{customerId}",
|
||||
"DownstreamScheme": "http",
|
||||
"DownstreamHost": "customers20161126090811.azurewebsites.net",
|
||||
"DownstreamPort": 80,
|
||||
"UpstreamPathTemplate": "/customers/{customerId}",
|
||||
"UpstreamHttpMethod": [ "Put" ],
|
||||
"QoSOptions": {
|
||||
"ExceptionsAllowedBeforeBreaking": 3,
|
||||
"DurationOfBreak": 10,
|
||||
"TimeoutValue": 5000
|
||||
},
|
||||
"FileCacheOptions": { "TtlSeconds": 15 }
|
||||
},
|
||||
{
|
||||
"DownstreamPathTemplate": "/api/customers/{customerId}",
|
||||
"DownstreamScheme": "http",
|
||||
"DownstreamHost": "customers20161126090811.azurewebsites.net",
|
||||
"DownstreamPort": 80,
|
||||
"UpstreamPathTemplate": "/customers/{customerId}",
|
||||
"UpstreamHttpMethod": [ "Delete" ],
|
||||
"QoSOptions": {
|
||||
"ExceptionsAllowedBeforeBreaking": 3,
|
||||
"DurationOfBreak": 10,
|
||||
"TimeoutValue": 5000
|
||||
},
|
||||
"FileCacheOptions": { "TtlSeconds": 15 }
|
||||
},
|
||||
{
|
||||
"DownstreamPathTemplate": "/posts",
|
||||
"DownstreamScheme": "http",
|
||||
"DownstreamHost": "jsonplaceholder.typicode.com",
|
||||
"DownstreamPort": 80,
|
||||
"UpstreamPathTemplate": "/posts/",
|
||||
"UpstreamHttpMethod": [ "Get" ],
|
||||
"QoSOptions": {
|
||||
"ExceptionsAllowedBeforeBreaking": 3,
|
||||
"DurationOfBreak": 10,
|
||||
"TimeoutValue": 5000
|
||||
},
|
||||
"FileCacheOptions": { "TtlSeconds": 15 }
|
||||
},
|
||||
{
|
||||
"DownstreamPathTemplate": "/",
|
||||
"DownstreamScheme": "http",
|
||||
"DownstreamHost": "www.bbc.co.uk",
|
||||
"DownstreamPort": 80,
|
||||
"UpstreamPathTemplate": "/bbc/",
|
||||
"UpstreamHttpMethod": [ "Get" ]
|
||||
}
|
||||
],
|
||||
|
||||
"GlobalConfiguration": {
|
||||
"RequestIdKey": "OcRequestId"
|
||||
}
|
||||
{
|
||||
"ReRoutes": [
|
||||
{
|
||||
"DownstreamPathTemplate": "/",
|
||||
"DownstreamScheme": "http",
|
||||
"DownstreamHost": "localhost",
|
||||
"DownstreamPort": 52876,
|
||||
"UpstreamPathTemplate": "/identityserverexample",
|
||||
"UpstreamHttpMethod": [ "Get" ],
|
||||
"QoSOptions": {
|
||||
"ExceptionsAllowedBeforeBreaking": 3,
|
||||
"DurationOfBreak": 10,
|
||||
"TimeoutValue": 5000
|
||||
},
|
||||
"AuthenticationOptions": {
|
||||
"AuthenticationProviderKey": "TestKey",
|
||||
"AllowedScopes": [
|
||||
"openid",
|
||||
"offline_access"
|
||||
]
|
||||
},
|
||||
"AddHeadersToRequest": {
|
||||
"CustomerId": "Claims[CustomerId] > value",
|
||||
"LocationId": "Claims[LocationId] > value",
|
||||
"UserType": "Claims[sub] > value[0] > |",
|
||||
"UserId": "Claims[sub] > value[1] > |"
|
||||
},
|
||||
"AddClaimsToRequest": {
|
||||
"CustomerId": "Claims[CustomerId] > value",
|
||||
"LocationId": "Claims[LocationId] > value",
|
||||
"UserType": "Claims[sub] > value[0] > |",
|
||||
"UserId": "Claims[sub] > value[1] > |"
|
||||
},
|
||||
"AddQueriesToRequest": {
|
||||
"CustomerId": "Claims[CustomerId] > value",
|
||||
"LocationId": "Claims[LocationId] > value",
|
||||
"UserType": "Claims[sub] > value[0] > |",
|
||||
"UserId": "Claims[sub] > value[1] > |"
|
||||
},
|
||||
"RouteClaimsRequirement": {
|
||||
"UserType": "registered"
|
||||
},
|
||||
"RequestIdKey": "OcRequestId"
|
||||
},
|
||||
{
|
||||
"DownstreamPathTemplate": "/posts",
|
||||
"DownstreamScheme": "https",
|
||||
"DownstreamHost": "jsonplaceholder.typicode.com",
|
||||
"DownstreamPort": 443,
|
||||
"UpstreamPathTemplate": "/posts",
|
||||
"UpstreamHttpMethod": [ "Get" ],
|
||||
"QoSOptions": {
|
||||
"ExceptionsAllowedBeforeBreaking": 3,
|
||||
"DurationOfBreak": 10,
|
||||
"TimeoutValue": 5000
|
||||
}
|
||||
},
|
||||
{
|
||||
"DownstreamPathTemplate": "/posts/{postId}",
|
||||
"DownstreamScheme": "http",
|
||||
"DownstreamHost": "jsonplaceholder.typicode.com",
|
||||
"DownstreamPort": 80,
|
||||
"UpstreamPathTemplate": "/posts/{postId}",
|
||||
"UpstreamHttpMethod": [ "Get" ],
|
||||
"RequestIdKey": "ReRouteRequestId",
|
||||
"QoSOptions": {
|
||||
"ExceptionsAllowedBeforeBreaking": 3,
|
||||
"DurationOfBreak": 10,
|
||||
"TimeoutValue": 5000
|
||||
}
|
||||
},
|
||||
{
|
||||
"DownstreamPathTemplate": "/posts/{postId}/comments",
|
||||
"DownstreamScheme": "http",
|
||||
"DownstreamHost": "jsonplaceholder.typicode.com",
|
||||
"DownstreamPort": 80,
|
||||
"UpstreamPathTemplate": "/posts/{postId}/comments",
|
||||
"UpstreamHttpMethod": [ "Get" ],
|
||||
"QoSOptions": {
|
||||
"ExceptionsAllowedBeforeBreaking": 3,
|
||||
"DurationOfBreak": 10,
|
||||
"TimeoutValue": 5000
|
||||
}
|
||||
},
|
||||
{
|
||||
"DownstreamPathTemplate": "/comments",
|
||||
"DownstreamScheme": "http",
|
||||
"DownstreamHost": "jsonplaceholder.typicode.com",
|
||||
"DownstreamPort": 80,
|
||||
"UpstreamPathTemplate": "/comments",
|
||||
"UpstreamHttpMethod": [ "Get" ],
|
||||
"QoSOptions": {
|
||||
"ExceptionsAllowedBeforeBreaking": 3,
|
||||
"DurationOfBreak": 10,
|
||||
"TimeoutValue": 5000
|
||||
}
|
||||
},
|
||||
{
|
||||
"DownstreamPathTemplate": "/posts",
|
||||
"DownstreamScheme": "http",
|
||||
"DownstreamHost": "jsonplaceholder.typicode.com",
|
||||
"DownstreamPort": 80,
|
||||
"UpstreamPathTemplate": "/posts",
|
||||
"UpstreamHttpMethod": [ "Post" ],
|
||||
"QoSOptions": {
|
||||
"ExceptionsAllowedBeforeBreaking": 3,
|
||||
"DurationOfBreak": 10,
|
||||
"TimeoutValue": 5000
|
||||
}
|
||||
},
|
||||
{
|
||||
"DownstreamPathTemplate": "/posts/{postId}",
|
||||
"DownstreamScheme": "http",
|
||||
"DownstreamHost": "jsonplaceholder.typicode.com",
|
||||
"DownstreamPort": 80,
|
||||
"UpstreamPathTemplate": "/posts/{postId}",
|
||||
"UpstreamHttpMethod": [ "Put" ],
|
||||
"QoSOptions": {
|
||||
"ExceptionsAllowedBeforeBreaking": 3,
|
||||
"DurationOfBreak": 10,
|
||||
"TimeoutValue": 5000
|
||||
}
|
||||
},
|
||||
{
|
||||
"DownstreamPathTemplate": "/posts/{postId}",
|
||||
"DownstreamScheme": "http",
|
||||
"DownstreamHost": "jsonplaceholder.typicode.com",
|
||||
"DownstreamPort": 80,
|
||||
"UpstreamPathTemplate": "/posts/{postId}",
|
||||
"UpstreamHttpMethod": [ "Patch" ],
|
||||
"QoSOptions": {
|
||||
"ExceptionsAllowedBeforeBreaking": 3,
|
||||
"DurationOfBreak": 10,
|
||||
"TimeoutValue": 5000
|
||||
}
|
||||
},
|
||||
{
|
||||
"DownstreamPathTemplate": "/posts/{postId}",
|
||||
"DownstreamScheme": "http",
|
||||
"DownstreamHost": "jsonplaceholder.typicode.com",
|
||||
"DownstreamPort": 80,
|
||||
"UpstreamPathTemplate": "/posts/{postId}",
|
||||
"UpstreamHttpMethod": [ "Delete" ],
|
||||
"QoSOptions": {
|
||||
"ExceptionsAllowedBeforeBreaking": 3,
|
||||
"DurationOfBreak": 10,
|
||||
"TimeoutValue": 5000
|
||||
}
|
||||
},
|
||||
{
|
||||
"DownstreamPathTemplate": "/api/products",
|
||||
"DownstreamScheme": "http",
|
||||
"DownstreamHost": "jsonplaceholder.typicode.com",
|
||||
"DownstreamPort": 80,
|
||||
"UpstreamPathTemplate": "/products",
|
||||
"UpstreamHttpMethod": [ "Get" ],
|
||||
"QoSOptions": {
|
||||
"ExceptionsAllowedBeforeBreaking": 3,
|
||||
"DurationOfBreak": 10,
|
||||
"TimeoutValue": 5000
|
||||
},
|
||||
"FileCacheOptions": { "TtlSeconds": 15 }
|
||||
},
|
||||
{
|
||||
"DownstreamPathTemplate": "/api/products/{productId}",
|
||||
"DownstreamScheme": "http",
|
||||
"DownstreamHost": "jsonplaceholder.typicode.com",
|
||||
"DownstreamPort": 80,
|
||||
"UpstreamPathTemplate": "/products/{productId}",
|
||||
"UpstreamHttpMethod": [ "Get" ],
|
||||
"FileCacheOptions": { "TtlSeconds": 15 }
|
||||
},
|
||||
{
|
||||
"DownstreamPathTemplate": "/api/products",
|
||||
"DownstreamScheme": "http",
|
||||
"DownstreamHost": "products20161126090340.azurewebsites.net",
|
||||
"DownstreamPort": 80,
|
||||
"UpstreamPathTemplate": "/products",
|
||||
"UpstreamHttpMethod": [ "Post" ],
|
||||
"QoSOptions": {
|
||||
"ExceptionsAllowedBeforeBreaking": 3,
|
||||
"DurationOfBreak": 10,
|
||||
"TimeoutValue": 5000
|
||||
}
|
||||
},
|
||||
{
|
||||
"DownstreamPathTemplate": "/api/products/{productId}",
|
||||
"DownstreamScheme": "http",
|
||||
"DownstreamHost": "products20161126090340.azurewebsites.net",
|
||||
"DownstreamPort": 80,
|
||||
"UpstreamPathTemplate": "/products/{productId}",
|
||||
"UpstreamHttpMethod": [ "Put" ],
|
||||
"QoSOptions": {
|
||||
"ExceptionsAllowedBeforeBreaking": 3,
|
||||
"DurationOfBreak": 10,
|
||||
"TimeoutValue": 5000
|
||||
},
|
||||
"FileCacheOptions": { "TtlSeconds": 15 }
|
||||
},
|
||||
{
|
||||
"DownstreamPathTemplate": "/api/products/{productId}",
|
||||
"DownstreamScheme": "http",
|
||||
"DownstreamHost": "products20161126090340.azurewebsites.net",
|
||||
"DownstreamPort": 80,
|
||||
"UpstreamPathTemplate": "/products/{productId}",
|
||||
"UpstreamHttpMethod": [ "Delete" ],
|
||||
"QoSOptions": {
|
||||
"ExceptionsAllowedBeforeBreaking": 3,
|
||||
"DurationOfBreak": 10,
|
||||
"TimeoutValue": 5000
|
||||
},
|
||||
"FileCacheOptions": { "TtlSeconds": 15 }
|
||||
},
|
||||
{
|
||||
"DownstreamPathTemplate": "/api/customers",
|
||||
"DownstreamScheme": "http",
|
||||
"DownstreamHost": "customers20161126090811.azurewebsites.net",
|
||||
"DownstreamPort": 80,
|
||||
"UpstreamPathTemplate": "/customers",
|
||||
"UpstreamHttpMethod": [ "Get" ],
|
||||
"QoSOptions": {
|
||||
"ExceptionsAllowedBeforeBreaking": 3,
|
||||
"DurationOfBreak": 10,
|
||||
"TimeoutValue": 5000
|
||||
},
|
||||
"FileCacheOptions": { "TtlSeconds": 15 }
|
||||
},
|
||||
{
|
||||
"DownstreamPathTemplate": "/api/customers/{customerId}",
|
||||
"DownstreamScheme": "http",
|
||||
"DownstreamHost": "customers20161126090811.azurewebsites.net",
|
||||
"DownstreamPort": 80,
|
||||
"UpstreamPathTemplate": "/customers/{customerId}",
|
||||
"UpstreamHttpMethod": [ "Get" ],
|
||||
"QoSOptions": {
|
||||
"ExceptionsAllowedBeforeBreaking": 3,
|
||||
"DurationOfBreak": 10,
|
||||
"TimeoutValue": 5000
|
||||
},
|
||||
"FileCacheOptions": { "TtlSeconds": 15 }
|
||||
},
|
||||
{
|
||||
"DownstreamPathTemplate": "/api/customers",
|
||||
"DownstreamScheme": "http",
|
||||
"DownstreamHost": "customers20161126090811.azurewebsites.net",
|
||||
"DownstreamPort": 80,
|
||||
"UpstreamPathTemplate": "/customers",
|
||||
"UpstreamHttpMethod": [ "Post" ],
|
||||
"QoSOptions": {
|
||||
"ExceptionsAllowedBeforeBreaking": 3,
|
||||
"DurationOfBreak": 10,
|
||||
"TimeoutValue": 5000
|
||||
},
|
||||
"FileCacheOptions": { "TtlSeconds": 15 }
|
||||
},
|
||||
{
|
||||
"DownstreamPathTemplate": "/api/customers/{customerId}",
|
||||
"DownstreamScheme": "http",
|
||||
"DownstreamHost": "customers20161126090811.azurewebsites.net",
|
||||
"DownstreamPort": 80,
|
||||
"UpstreamPathTemplate": "/customers/{customerId}",
|
||||
"UpstreamHttpMethod": [ "Put" ],
|
||||
"QoSOptions": {
|
||||
"ExceptionsAllowedBeforeBreaking": 3,
|
||||
"DurationOfBreak": 10,
|
||||
"TimeoutValue": 5000
|
||||
},
|
||||
"FileCacheOptions": { "TtlSeconds": 15 }
|
||||
},
|
||||
{
|
||||
"DownstreamPathTemplate": "/api/customers/{customerId}",
|
||||
"DownstreamScheme": "http",
|
||||
"DownstreamHost": "customers20161126090811.azurewebsites.net",
|
||||
"DownstreamPort": 80,
|
||||
"UpstreamPathTemplate": "/customers/{customerId}",
|
||||
"UpstreamHttpMethod": [ "Delete" ],
|
||||
"QoSOptions": {
|
||||
"ExceptionsAllowedBeforeBreaking": 3,
|
||||
"DurationOfBreak": 10,
|
||||
"TimeoutValue": 5000
|
||||
},
|
||||
"FileCacheOptions": { "TtlSeconds": 15 }
|
||||
},
|
||||
{
|
||||
"DownstreamPathTemplate": "/posts",
|
||||
"DownstreamScheme": "http",
|
||||
"DownstreamHost": "jsonplaceholder.typicode.com",
|
||||
"DownstreamPort": 80,
|
||||
"UpstreamPathTemplate": "/posts/",
|
||||
"UpstreamHttpMethod": [ "Get" ],
|
||||
"QoSOptions": {
|
||||
"ExceptionsAllowedBeforeBreaking": 3,
|
||||
"DurationOfBreak": 10,
|
||||
"TimeoutValue": 5000
|
||||
},
|
||||
"FileCacheOptions": { "TtlSeconds": 15 }
|
||||
},
|
||||
{
|
||||
"DownstreamPathTemplate": "/",
|
||||
"DownstreamScheme": "http",
|
||||
"DownstreamHost": "www.bbc.co.uk",
|
||||
"DownstreamPort": 80,
|
||||
"UpstreamPathTemplate": "/bbc/",
|
||||
"UpstreamHttpMethod": [ "Get" ]
|
||||
}
|
||||
],
|
||||
|
||||
"GlobalConfiguration": {
|
||||
"RequestIdKey": "OcRequestId"
|
||||
}
|
||||
}
|
@ -1,14 +1,14 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<configuration>
|
||||
|
||||
<!--
|
||||
Configure your application settings in appsettings.json. Learn more at http://go.microsoft.com/fwlink/?LinkId=786380
|
||||
-->
|
||||
|
||||
<system.webServer>
|
||||
<handlers>
|
||||
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified"/>
|
||||
</handlers>
|
||||
<aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false"/>
|
||||
</system.webServer>
|
||||
</configuration>
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<configuration>
|
||||
|
||||
<!--
|
||||
Configure your application settings in appsettings.json. Learn more at http://go.microsoft.com/fwlink/?LinkId=786380
|
||||
-->
|
||||
|
||||
<system.webServer>
|
||||
<handlers>
|
||||
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified"/>
|
||||
</handlers>
|
||||
<aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false"/>
|
||||
</system.webServer>
|
||||
</configuration>
|
||||
|
@ -1,70 +1,70 @@
|
||||
namespace Ocelot.UnitTests.Authentication
|
||||
{
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Moq;
|
||||
using Ocelot.Authentication.Middleware;
|
||||
using Ocelot.Configuration.Builder;
|
||||
using Ocelot.DownstreamRouteFinder;
|
||||
using Ocelot.DownstreamRouteFinder.UrlMatcher;
|
||||
using Ocelot.Logging;
|
||||
using Ocelot.Responses;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
public class AuthenticationMiddlewareTests : ServerHostedMiddlewareTest
|
||||
{
|
||||
private OkResponse<DownstreamRoute> _downstreamRoute;
|
||||
|
||||
public AuthenticationMiddlewareTests()
|
||||
{
|
||||
GivenTheTestServerIsConfigured();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_call_next_middleware_if_route_is_not_authenticated()
|
||||
{
|
||||
this.Given(x => x.GivenTheDownStreamRouteIs(
|
||||
new DownstreamRoute(
|
||||
new List<PlaceholderNameAndValue>(),
|
||||
new ReRouteBuilder().WithUpstreamHttpMethod(new List<string> { "Get" }).Build())))
|
||||
.When(x => x.WhenICallTheMiddleware())
|
||||
.Then(x => x.ThenTheUserIsAuthenticated())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
protected override void GivenTheTestServerServicesAreConfigured(IServiceCollection services)
|
||||
{
|
||||
services.AddSingleton<IOcelotLoggerFactory, AspDotNetLoggerFactory>();
|
||||
services.AddLogging();
|
||||
services.AddSingleton(ScopedRepository.Object);
|
||||
}
|
||||
|
||||
protected override void GivenTheTestServerPipelineIsConfigured(IApplicationBuilder app)
|
||||
{
|
||||
app.UseAuthenticationMiddleware();
|
||||
|
||||
app.Run(async x =>
|
||||
{
|
||||
await x.Response.WriteAsync("The user is authenticated");
|
||||
});
|
||||
}
|
||||
|
||||
private void ThenTheUserIsAuthenticated()
|
||||
{
|
||||
var content = ResponseMessage.Content.ReadAsStringAsync().Result;
|
||||
content.ShouldBe("The user is authenticated");
|
||||
}
|
||||
|
||||
private void GivenTheDownStreamRouteIs(DownstreamRoute downstreamRoute)
|
||||
{
|
||||
_downstreamRoute = new OkResponse<DownstreamRoute>(downstreamRoute);
|
||||
ScopedRepository
|
||||
.Setup(x => x.Get<DownstreamRoute>(It.IsAny<string>()))
|
||||
.Returns(_downstreamRoute);
|
||||
}
|
||||
}
|
||||
}
|
||||
namespace Ocelot.UnitTests.Authentication
|
||||
{
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Moq;
|
||||
using Ocelot.Authentication.Middleware;
|
||||
using Ocelot.Configuration.Builder;
|
||||
using Ocelot.DownstreamRouteFinder;
|
||||
using Ocelot.DownstreamRouteFinder.UrlMatcher;
|
||||
using Ocelot.Logging;
|
||||
using Ocelot.Responses;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
public class AuthenticationMiddlewareTests : ServerHostedMiddlewareTest
|
||||
{
|
||||
private OkResponse<DownstreamRoute> _downstreamRoute;
|
||||
|
||||
public AuthenticationMiddlewareTests()
|
||||
{
|
||||
GivenTheTestServerIsConfigured();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_call_next_middleware_if_route_is_not_authenticated()
|
||||
{
|
||||
this.Given(x => x.GivenTheDownStreamRouteIs(
|
||||
new DownstreamRoute(
|
||||
new List<PlaceholderNameAndValue>(),
|
||||
new ReRouteBuilder().WithUpstreamHttpMethod(new List<string> { "Get" }).Build())))
|
||||
.When(x => x.WhenICallTheMiddleware())
|
||||
.Then(x => x.ThenTheUserIsAuthenticated())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
protected override void GivenTheTestServerServicesAreConfigured(IServiceCollection services)
|
||||
{
|
||||
services.AddSingleton<IOcelotLoggerFactory, AspDotNetLoggerFactory>();
|
||||
services.AddLogging();
|
||||
services.AddSingleton(ScopedRepository.Object);
|
||||
}
|
||||
|
||||
protected override void GivenTheTestServerPipelineIsConfigured(IApplicationBuilder app)
|
||||
{
|
||||
app.UseAuthenticationMiddleware();
|
||||
|
||||
app.Run(async x =>
|
||||
{
|
||||
await x.Response.WriteAsync("The user is authenticated");
|
||||
});
|
||||
}
|
||||
|
||||
private void ThenTheUserIsAuthenticated()
|
||||
{
|
||||
var content = ResponseMessage.Content.ReadAsStringAsync().Result;
|
||||
content.ShouldBe("The user is authenticated");
|
||||
}
|
||||
|
||||
private void GivenTheDownStreamRouteIs(DownstreamRoute downstreamRoute)
|
||||
{
|
||||
_downstreamRoute = new OkResponse<DownstreamRoute>(downstreamRoute);
|
||||
ScopedRepository
|
||||
.Setup(x => x.Get<DownstreamRoute>(It.IsAny<string>()))
|
||||
.Returns(_downstreamRoute);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,82 +1,82 @@
|
||||
namespace Ocelot.UnitTests.Authorization
|
||||
{
|
||||
using System.Collections.Generic;
|
||||
using System.Security.Claims;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Moq;
|
||||
using Ocelot.Authorisation;
|
||||
using Ocelot.Authorisation.Middleware;
|
||||
using Ocelot.Configuration.Builder;
|
||||
using Ocelot.DownstreamRouteFinder;
|
||||
using Ocelot.DownstreamRouteFinder.UrlMatcher;
|
||||
using Ocelot.Logging;
|
||||
using Ocelot.Responses;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
public class AuthorisationMiddlewareTests : ServerHostedMiddlewareTest
|
||||
{
|
||||
private readonly Mock<IClaimsAuthoriser> _authService;
|
||||
private readonly Mock<IScopesAuthoriser> _authScopesService;
|
||||
private OkResponse<DownstreamRoute> _downstreamRoute;
|
||||
|
||||
public AuthorisationMiddlewareTests()
|
||||
{
|
||||
_authService = new Mock<IClaimsAuthoriser>();
|
||||
_authScopesService = new Mock<IScopesAuthoriser>();
|
||||
|
||||
GivenTheTestServerIsConfigured();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_call_authorisation_service()
|
||||
{
|
||||
this.Given(x => x.GivenTheDownStreamRouteIs(new DownstreamRoute(new List<PlaceholderNameAndValue>(),
|
||||
new ReRouteBuilder()
|
||||
.WithIsAuthorised(true)
|
||||
.WithUpstreamHttpMethod(new List<string> { "Get" })
|
||||
.Build())))
|
||||
.And(x => x.GivenTheAuthServiceReturns(new OkResponse<bool>(true)))
|
||||
.When(x => x.WhenICallTheMiddleware())
|
||||
.Then(x => x.ThenTheAuthServiceIsCalledCorrectly())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
protected override void GivenTheTestServerServicesAreConfigured(IServiceCollection services)
|
||||
{
|
||||
services.AddSingleton<IOcelotLoggerFactory, AspDotNetLoggerFactory>();
|
||||
services.AddLogging();
|
||||
services.AddSingleton(_authService.Object);
|
||||
services.AddSingleton(_authScopesService.Object);
|
||||
services.AddSingleton(ScopedRepository.Object);
|
||||
}
|
||||
|
||||
protected override void GivenTheTestServerPipelineIsConfigured(IApplicationBuilder app)
|
||||
{
|
||||
app.UseAuthorisationMiddleware();
|
||||
}
|
||||
|
||||
private void GivenTheDownStreamRouteIs(DownstreamRoute downstreamRoute)
|
||||
{
|
||||
_downstreamRoute = new OkResponse<DownstreamRoute>(downstreamRoute);
|
||||
ScopedRepository
|
||||
.Setup(x => x.Get<DownstreamRoute>(It.IsAny<string>()))
|
||||
.Returns(_downstreamRoute);
|
||||
}
|
||||
|
||||
private void GivenTheAuthServiceReturns(Response<bool> expected)
|
||||
{
|
||||
_authService
|
||||
.Setup(x => x.Authorise(It.IsAny<ClaimsPrincipal>(), It.IsAny<Dictionary<string, string>>()))
|
||||
.Returns(expected);
|
||||
}
|
||||
|
||||
private void ThenTheAuthServiceIsCalledCorrectly()
|
||||
{
|
||||
_authService
|
||||
.Verify(x => x.Authorise(It.IsAny<ClaimsPrincipal>(),
|
||||
It.IsAny<Dictionary<string, string>>()), Times.Once);
|
||||
}
|
||||
}
|
||||
}
|
||||
namespace Ocelot.UnitTests.Authorization
|
||||
{
|
||||
using System.Collections.Generic;
|
||||
using System.Security.Claims;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Moq;
|
||||
using Ocelot.Authorisation;
|
||||
using Ocelot.Authorisation.Middleware;
|
||||
using Ocelot.Configuration.Builder;
|
||||
using Ocelot.DownstreamRouteFinder;
|
||||
using Ocelot.DownstreamRouteFinder.UrlMatcher;
|
||||
using Ocelot.Logging;
|
||||
using Ocelot.Responses;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
public class AuthorisationMiddlewareTests : ServerHostedMiddlewareTest
|
||||
{
|
||||
private readonly Mock<IClaimsAuthoriser> _authService;
|
||||
private readonly Mock<IScopesAuthoriser> _authScopesService;
|
||||
private OkResponse<DownstreamRoute> _downstreamRoute;
|
||||
|
||||
public AuthorisationMiddlewareTests()
|
||||
{
|
||||
_authService = new Mock<IClaimsAuthoriser>();
|
||||
_authScopesService = new Mock<IScopesAuthoriser>();
|
||||
|
||||
GivenTheTestServerIsConfigured();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_call_authorisation_service()
|
||||
{
|
||||
this.Given(x => x.GivenTheDownStreamRouteIs(new DownstreamRoute(new List<PlaceholderNameAndValue>(),
|
||||
new ReRouteBuilder()
|
||||
.WithIsAuthorised(true)
|
||||
.WithUpstreamHttpMethod(new List<string> { "Get" })
|
||||
.Build())))
|
||||
.And(x => x.GivenTheAuthServiceReturns(new OkResponse<bool>(true)))
|
||||
.When(x => x.WhenICallTheMiddleware())
|
||||
.Then(x => x.ThenTheAuthServiceIsCalledCorrectly())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
protected override void GivenTheTestServerServicesAreConfigured(IServiceCollection services)
|
||||
{
|
||||
services.AddSingleton<IOcelotLoggerFactory, AspDotNetLoggerFactory>();
|
||||
services.AddLogging();
|
||||
services.AddSingleton(_authService.Object);
|
||||
services.AddSingleton(_authScopesService.Object);
|
||||
services.AddSingleton(ScopedRepository.Object);
|
||||
}
|
||||
|
||||
protected override void GivenTheTestServerPipelineIsConfigured(IApplicationBuilder app)
|
||||
{
|
||||
app.UseAuthorisationMiddleware();
|
||||
}
|
||||
|
||||
private void GivenTheDownStreamRouteIs(DownstreamRoute downstreamRoute)
|
||||
{
|
||||
_downstreamRoute = new OkResponse<DownstreamRoute>(downstreamRoute);
|
||||
ScopedRepository
|
||||
.Setup(x => x.Get<DownstreamRoute>(It.IsAny<string>()))
|
||||
.Returns(_downstreamRoute);
|
||||
}
|
||||
|
||||
private void GivenTheAuthServiceReturns(Response<bool> expected)
|
||||
{
|
||||
_authService
|
||||
.Setup(x => x.Authorise(It.IsAny<ClaimsPrincipal>(), It.IsAny<Dictionary<string, string>>()))
|
||||
.Returns(expected);
|
||||
}
|
||||
|
||||
private void ThenTheAuthServiceIsCalledCorrectly()
|
||||
{
|
||||
_authService
|
||||
.Verify(x => x.Authorise(It.IsAny<ClaimsPrincipal>(),
|
||||
It.IsAny<Dictionary<string, string>>()), Times.Once);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,79 +1,79 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Security.Claims;
|
||||
using Ocelot.Authorisation;
|
||||
using Ocelot.Responses;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests.Authorization
|
||||
{
|
||||
using Ocelot.Infrastructure.Claims.Parser;
|
||||
|
||||
public class ClaimsAuthoriserTests
|
||||
{
|
||||
private readonly ClaimsAuthoriser _claimsAuthoriser;
|
||||
private ClaimsPrincipal _claimsPrincipal;
|
||||
private Dictionary<string, string> _requirement;
|
||||
private Response<bool> _result;
|
||||
|
||||
public ClaimsAuthoriserTests()
|
||||
{
|
||||
_claimsAuthoriser = new ClaimsAuthoriser(new ClaimsParser());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_authorise_user()
|
||||
{
|
||||
this.Given(x => x.GivenAClaimsPrincipal(new ClaimsPrincipal(new ClaimsIdentity(new List<Claim>
|
||||
{
|
||||
new Claim("UserType", "registered")
|
||||
}))))
|
||||
.And(x => x.GivenARouteClaimsRequirement(new Dictionary<string, string>
|
||||
{
|
||||
{"UserType", "registered"}
|
||||
}))
|
||||
.When(x => x.WhenICallTheAuthoriser())
|
||||
.Then(x => x.ThenTheUserIsAuthorised())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_not_authorise_user()
|
||||
{
|
||||
this.Given(x => x.GivenAClaimsPrincipal(new ClaimsPrincipal(new ClaimsIdentity(new List<Claim>()))))
|
||||
.And(x => x.GivenARouteClaimsRequirement(new Dictionary<string, string>
|
||||
{
|
||||
{ "UserType", "registered" }
|
||||
}))
|
||||
.When(x => x.WhenICallTheAuthoriser())
|
||||
.Then(x => x.ThenTheUserIsntAuthorised())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenAClaimsPrincipal(ClaimsPrincipal claimsPrincipal)
|
||||
{
|
||||
_claimsPrincipal = claimsPrincipal;
|
||||
}
|
||||
|
||||
private void GivenARouteClaimsRequirement(Dictionary<string, string> requirement)
|
||||
{
|
||||
_requirement = requirement;
|
||||
}
|
||||
|
||||
private void WhenICallTheAuthoriser()
|
||||
{
|
||||
_result = _claimsAuthoriser.Authorise(_claimsPrincipal, _requirement);
|
||||
}
|
||||
|
||||
private void ThenTheUserIsAuthorised()
|
||||
{
|
||||
_result.Data.ShouldBe(true);
|
||||
}
|
||||
|
||||
private void ThenTheUserIsntAuthorised()
|
||||
{
|
||||
_result.Data.ShouldBe(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
using System.Collections.Generic;
|
||||
using System.Security.Claims;
|
||||
using Ocelot.Authorisation;
|
||||
using Ocelot.Responses;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests.Authorization
|
||||
{
|
||||
using Ocelot.Infrastructure.Claims.Parser;
|
||||
|
||||
public class ClaimsAuthoriserTests
|
||||
{
|
||||
private readonly ClaimsAuthoriser _claimsAuthoriser;
|
||||
private ClaimsPrincipal _claimsPrincipal;
|
||||
private Dictionary<string, string> _requirement;
|
||||
private Response<bool> _result;
|
||||
|
||||
public ClaimsAuthoriserTests()
|
||||
{
|
||||
_claimsAuthoriser = new ClaimsAuthoriser(new ClaimsParser());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_authorise_user()
|
||||
{
|
||||
this.Given(x => x.GivenAClaimsPrincipal(new ClaimsPrincipal(new ClaimsIdentity(new List<Claim>
|
||||
{
|
||||
new Claim("UserType", "registered")
|
||||
}))))
|
||||
.And(x => x.GivenARouteClaimsRequirement(new Dictionary<string, string>
|
||||
{
|
||||
{"UserType", "registered"}
|
||||
}))
|
||||
.When(x => x.WhenICallTheAuthoriser())
|
||||
.Then(x => x.ThenTheUserIsAuthorised())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_not_authorise_user()
|
||||
{
|
||||
this.Given(x => x.GivenAClaimsPrincipal(new ClaimsPrincipal(new ClaimsIdentity(new List<Claim>()))))
|
||||
.And(x => x.GivenARouteClaimsRequirement(new Dictionary<string, string>
|
||||
{
|
||||
{ "UserType", "registered" }
|
||||
}))
|
||||
.When(x => x.WhenICallTheAuthoriser())
|
||||
.Then(x => x.ThenTheUserIsntAuthorised())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenAClaimsPrincipal(ClaimsPrincipal claimsPrincipal)
|
||||
{
|
||||
_claimsPrincipal = claimsPrincipal;
|
||||
}
|
||||
|
||||
private void GivenARouteClaimsRequirement(Dictionary<string, string> requirement)
|
||||
{
|
||||
_requirement = requirement;
|
||||
}
|
||||
|
||||
private void WhenICallTheAuthoriser()
|
||||
{
|
||||
_result = _claimsAuthoriser.Authorise(_claimsPrincipal, _requirement);
|
||||
}
|
||||
|
||||
private void ThenTheUserIsAuthorised()
|
||||
{
|
||||
_result.Data.ShouldBe(true);
|
||||
}
|
||||
|
||||
private void ThenTheUserIsntAuthorised()
|
||||
{
|
||||
_result.Data.ShouldBe(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,103 +1,103 @@
|
||||
using System;
|
||||
using CacheManager.Core;
|
||||
using Moq;
|
||||
using Ocelot.Cache;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests.Cache
|
||||
{
|
||||
public class CacheManagerCacheTests
|
||||
{
|
||||
private OcelotCacheManagerCache<string> _ocelotOcelotCacheManager;
|
||||
private Mock<ICacheManager<string>> _mockCacheManager;
|
||||
private string _key;
|
||||
private string _value;
|
||||
private string _resultGet;
|
||||
private TimeSpan _ttlSeconds;
|
||||
private string _region;
|
||||
|
||||
public CacheManagerCacheTests()
|
||||
{
|
||||
_mockCacheManager = new Mock<ICacheManager<string>>();
|
||||
_ocelotOcelotCacheManager = new OcelotCacheManagerCache<string>(_mockCacheManager.Object);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_get_from_cache()
|
||||
{
|
||||
this.Given(x => x.GivenTheFollowingIsCached("someKey", "someRegion", "someValue"))
|
||||
.When(x => x.WhenIGetFromTheCache())
|
||||
.Then(x => x.ThenTheResultIs("someValue"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_add_to_cache()
|
||||
{
|
||||
this.When(x => x.WhenIAddToTheCache("someKey", "someValue", TimeSpan.FromSeconds(1)))
|
||||
.Then(x => x.ThenTheCacheIsCalledCorrectly())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_delete_key_from_cache()
|
||||
{
|
||||
this.Given(_ => GivenTheFollowingRegion("fookey"))
|
||||
.When(_ => WhenIDeleteTheRegion("fookey"))
|
||||
.Then(_ => ThenTheRegionIsDeleted("fookey"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void WhenIDeleteTheRegion(string region)
|
||||
{
|
||||
_ocelotOcelotCacheManager.ClearRegion(region);
|
||||
}
|
||||
|
||||
private void ThenTheRegionIsDeleted(string region)
|
||||
{
|
||||
_mockCacheManager
|
||||
.Verify(x => x.ClearRegion(region), Times.Once);
|
||||
}
|
||||
|
||||
private void GivenTheFollowingRegion(string key)
|
||||
{
|
||||
_ocelotOcelotCacheManager.Add(key, "doesnt matter", TimeSpan.FromSeconds(10), "region");
|
||||
}
|
||||
|
||||
private void WhenIAddToTheCache(string key, string value, TimeSpan ttlSeconds)
|
||||
{
|
||||
_key = key;
|
||||
_value = value;
|
||||
_ttlSeconds = ttlSeconds;
|
||||
_ocelotOcelotCacheManager.Add(_key, _value, _ttlSeconds, "region");
|
||||
}
|
||||
|
||||
private void ThenTheCacheIsCalledCorrectly()
|
||||
{
|
||||
_mockCacheManager
|
||||
.Verify(x => x.Add(It.IsAny<CacheItem<string>>()), Times.Once);
|
||||
}
|
||||
|
||||
private void ThenTheResultIs(string expected)
|
||||
{
|
||||
_resultGet.ShouldBe(expected);
|
||||
}
|
||||
|
||||
private void WhenIGetFromTheCache()
|
||||
{
|
||||
_resultGet = _ocelotOcelotCacheManager.Get(_key, _region);
|
||||
}
|
||||
|
||||
private void GivenTheFollowingIsCached(string key, string region, string value)
|
||||
{
|
||||
_key = key;
|
||||
_value = value;
|
||||
_region = region;
|
||||
_mockCacheManager
|
||||
.Setup(x => x.Get<string>(It.IsAny<string>(), It.IsAny<string>()))
|
||||
.Returns(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
using System;
|
||||
using CacheManager.Core;
|
||||
using Moq;
|
||||
using Ocelot.Cache;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests.Cache
|
||||
{
|
||||
public class CacheManagerCacheTests
|
||||
{
|
||||
private OcelotCacheManagerCache<string> _ocelotOcelotCacheManager;
|
||||
private Mock<ICacheManager<string>> _mockCacheManager;
|
||||
private string _key;
|
||||
private string _value;
|
||||
private string _resultGet;
|
||||
private TimeSpan _ttlSeconds;
|
||||
private string _region;
|
||||
|
||||
public CacheManagerCacheTests()
|
||||
{
|
||||
_mockCacheManager = new Mock<ICacheManager<string>>();
|
||||
_ocelotOcelotCacheManager = new OcelotCacheManagerCache<string>(_mockCacheManager.Object);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_get_from_cache()
|
||||
{
|
||||
this.Given(x => x.GivenTheFollowingIsCached("someKey", "someRegion", "someValue"))
|
||||
.When(x => x.WhenIGetFromTheCache())
|
||||
.Then(x => x.ThenTheResultIs("someValue"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_add_to_cache()
|
||||
{
|
||||
this.When(x => x.WhenIAddToTheCache("someKey", "someValue", TimeSpan.FromSeconds(1)))
|
||||
.Then(x => x.ThenTheCacheIsCalledCorrectly())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_delete_key_from_cache()
|
||||
{
|
||||
this.Given(_ => GivenTheFollowingRegion("fookey"))
|
||||
.When(_ => WhenIDeleteTheRegion("fookey"))
|
||||
.Then(_ => ThenTheRegionIsDeleted("fookey"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void WhenIDeleteTheRegion(string region)
|
||||
{
|
||||
_ocelotOcelotCacheManager.ClearRegion(region);
|
||||
}
|
||||
|
||||
private void ThenTheRegionIsDeleted(string region)
|
||||
{
|
||||
_mockCacheManager
|
||||
.Verify(x => x.ClearRegion(region), Times.Once);
|
||||
}
|
||||
|
||||
private void GivenTheFollowingRegion(string key)
|
||||
{
|
||||
_ocelotOcelotCacheManager.Add(key, "doesnt matter", TimeSpan.FromSeconds(10), "region");
|
||||
}
|
||||
|
||||
private void WhenIAddToTheCache(string key, string value, TimeSpan ttlSeconds)
|
||||
{
|
||||
_key = key;
|
||||
_value = value;
|
||||
_ttlSeconds = ttlSeconds;
|
||||
_ocelotOcelotCacheManager.Add(_key, _value, _ttlSeconds, "region");
|
||||
}
|
||||
|
||||
private void ThenTheCacheIsCalledCorrectly()
|
||||
{
|
||||
_mockCacheManager
|
||||
.Verify(x => x.Add(It.IsAny<CacheItem<string>>()), Times.Once);
|
||||
}
|
||||
|
||||
private void ThenTheResultIs(string expected)
|
||||
{
|
||||
_resultGet.ShouldBe(expected);
|
||||
}
|
||||
|
||||
private void WhenIGetFromTheCache()
|
||||
{
|
||||
_resultGet = _ocelotOcelotCacheManager.Get(_key, _region);
|
||||
}
|
||||
|
||||
private void GivenTheFollowingIsCached(string key, string region, string value)
|
||||
{
|
||||
_key = key;
|
||||
_value = value;
|
||||
_region = region;
|
||||
_mockCacheManager
|
||||
.Setup(x => x.Get<string>(It.IsAny<string>(), It.IsAny<string>()))
|
||||
.Returns(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,130 +1,130 @@
|
||||
namespace Ocelot.UnitTests.Cache
|
||||
{
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Net.Http;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Moq;
|
||||
using Ocelot.Cache;
|
||||
using Ocelot.Cache.Middleware;
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.Configuration.Builder;
|
||||
using Ocelot.DownstreamRouteFinder;
|
||||
using Ocelot.DownstreamRouteFinder.UrlMatcher;
|
||||
using Ocelot.Logging;
|
||||
using Ocelot.Responses;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
public class OutputCacheMiddlewareTests : ServerHostedMiddlewareTest
|
||||
{
|
||||
private readonly Mock<IOcelotCache<CachedResponse>> _cacheManager;
|
||||
private CachedResponse _response;
|
||||
|
||||
public OutputCacheMiddlewareTests()
|
||||
{
|
||||
_cacheManager = new Mock<IOcelotCache<CachedResponse>>();
|
||||
|
||||
ScopedRepository
|
||||
.Setup(sr => sr.Get<HttpRequestMessage>("DownstreamRequest"))
|
||||
.Returns(new OkResponse<HttpRequestMessage>(new HttpRequestMessage(HttpMethod.Get, "https://some.url/blah?abcd=123")));
|
||||
|
||||
GivenTheTestServerIsConfigured();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_returned_cached_item_when_it_is_in_cache()
|
||||
{
|
||||
var cachedResponse = new CachedResponse();
|
||||
this.Given(x => x.GivenThereIsACachedResponse(cachedResponse))
|
||||
.And(x => x.GivenTheDownstreamRouteIs())
|
||||
.And(x => x.GivenThereIsADownstreamUrl())
|
||||
.When(x => x.WhenICallTheMiddleware())
|
||||
.Then(x => x.ThenTheCacheGetIsCalledCorrectly())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_continue_with_pipeline_and_cache_response()
|
||||
{
|
||||
this.Given(x => x.GivenResponseIsNotCached())
|
||||
.And(x => x.GivenTheDownstreamRouteIs())
|
||||
.And(x => x.GivenThereAreNoErrors())
|
||||
.And(x => x.GivenThereIsADownstreamUrl())
|
||||
.When(x => x.WhenICallTheMiddleware())
|
||||
.Then(x => x.ThenTheCacheAddIsCalledCorrectly())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
protected override void GivenTheTestServerServicesAreConfigured(IServiceCollection services)
|
||||
{
|
||||
services.AddSingleton<IOcelotLoggerFactory, AspDotNetLoggerFactory>();
|
||||
services.AddLogging();
|
||||
services.AddSingleton(_cacheManager.Object);
|
||||
services.AddSingleton(ScopedRepository.Object);
|
||||
services.AddSingleton<IRegionCreator, RegionCreator>();
|
||||
}
|
||||
|
||||
protected override void GivenTheTestServerPipelineIsConfigured(IApplicationBuilder app)
|
||||
{
|
||||
app.UseOutputCacheMiddleware();
|
||||
}
|
||||
|
||||
private void GivenThereIsACachedResponse(CachedResponse response)
|
||||
{
|
||||
_response = response;
|
||||
_cacheManager
|
||||
.Setup(x => x.Get(It.IsAny<string>(), It.IsAny<string>()))
|
||||
.Returns(_response);
|
||||
}
|
||||
|
||||
private void GivenResponseIsNotCached()
|
||||
{
|
||||
ScopedRepository
|
||||
.Setup(x => x.Get<HttpResponseMessage>("HttpResponseMessage"))
|
||||
.Returns(new OkResponse<HttpResponseMessage>(new HttpResponseMessage()));
|
||||
}
|
||||
|
||||
private void GivenTheDownstreamRouteIs()
|
||||
{
|
||||
var reRoute = new ReRouteBuilder()
|
||||
.WithIsCached(true)
|
||||
.WithCacheOptions(new CacheOptions(100, "kanken"))
|
||||
.WithUpstreamHttpMethod(new List<string> { "Get" })
|
||||
.Build();
|
||||
|
||||
var downstreamRoute = new DownstreamRoute(new List<PlaceholderNameAndValue>(), reRoute);
|
||||
|
||||
ScopedRepository
|
||||
.Setup(x => x.Get<DownstreamRoute>(It.IsAny<string>()))
|
||||
.Returns(new OkResponse<DownstreamRoute>(downstreamRoute));
|
||||
}
|
||||
|
||||
private void GivenThereAreNoErrors()
|
||||
{
|
||||
ScopedRepository
|
||||
.Setup(x => x.Get<bool>("OcelotMiddlewareError"))
|
||||
.Returns(new OkResponse<bool>(false));
|
||||
}
|
||||
|
||||
private void GivenThereIsADownstreamUrl()
|
||||
{
|
||||
ScopedRepository
|
||||
.Setup(x => x.Get<string>("DownstreamUrl"))
|
||||
.Returns(new OkResponse<string>("anything"));
|
||||
}
|
||||
|
||||
private void ThenTheCacheGetIsCalledCorrectly()
|
||||
{
|
||||
_cacheManager
|
||||
.Verify(x => x.Get(It.IsAny<string>(), It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
|
||||
private void ThenTheCacheAddIsCalledCorrectly()
|
||||
{
|
||||
_cacheManager
|
||||
.Verify(x => x.Add(It.IsAny<string>(), It.IsAny<CachedResponse>(), It.IsAny<TimeSpan>(), It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
}
|
||||
}
|
||||
namespace Ocelot.UnitTests.Cache
|
||||
{
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Net.Http;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Moq;
|
||||
using Ocelot.Cache;
|
||||
using Ocelot.Cache.Middleware;
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.Configuration.Builder;
|
||||
using Ocelot.DownstreamRouteFinder;
|
||||
using Ocelot.DownstreamRouteFinder.UrlMatcher;
|
||||
using Ocelot.Logging;
|
||||
using Ocelot.Responses;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
public class OutputCacheMiddlewareTests : ServerHostedMiddlewareTest
|
||||
{
|
||||
private readonly Mock<IOcelotCache<CachedResponse>> _cacheManager;
|
||||
private CachedResponse _response;
|
||||
|
||||
public OutputCacheMiddlewareTests()
|
||||
{
|
||||
_cacheManager = new Mock<IOcelotCache<CachedResponse>>();
|
||||
|
||||
ScopedRepository
|
||||
.Setup(sr => sr.Get<HttpRequestMessage>("DownstreamRequest"))
|
||||
.Returns(new OkResponse<HttpRequestMessage>(new HttpRequestMessage(HttpMethod.Get, "https://some.url/blah?abcd=123")));
|
||||
|
||||
GivenTheTestServerIsConfigured();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_returned_cached_item_when_it_is_in_cache()
|
||||
{
|
||||
var cachedResponse = new CachedResponse();
|
||||
this.Given(x => x.GivenThereIsACachedResponse(cachedResponse))
|
||||
.And(x => x.GivenTheDownstreamRouteIs())
|
||||
.And(x => x.GivenThereIsADownstreamUrl())
|
||||
.When(x => x.WhenICallTheMiddleware())
|
||||
.Then(x => x.ThenTheCacheGetIsCalledCorrectly())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_continue_with_pipeline_and_cache_response()
|
||||
{
|
||||
this.Given(x => x.GivenResponseIsNotCached())
|
||||
.And(x => x.GivenTheDownstreamRouteIs())
|
||||
.And(x => x.GivenThereAreNoErrors())
|
||||
.And(x => x.GivenThereIsADownstreamUrl())
|
||||
.When(x => x.WhenICallTheMiddleware())
|
||||
.Then(x => x.ThenTheCacheAddIsCalledCorrectly())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
protected override void GivenTheTestServerServicesAreConfigured(IServiceCollection services)
|
||||
{
|
||||
services.AddSingleton<IOcelotLoggerFactory, AspDotNetLoggerFactory>();
|
||||
services.AddLogging();
|
||||
services.AddSingleton(_cacheManager.Object);
|
||||
services.AddSingleton(ScopedRepository.Object);
|
||||
services.AddSingleton<IRegionCreator, RegionCreator>();
|
||||
}
|
||||
|
||||
protected override void GivenTheTestServerPipelineIsConfigured(IApplicationBuilder app)
|
||||
{
|
||||
app.UseOutputCacheMiddleware();
|
||||
}
|
||||
|
||||
private void GivenThereIsACachedResponse(CachedResponse response)
|
||||
{
|
||||
_response = response;
|
||||
_cacheManager
|
||||
.Setup(x => x.Get(It.IsAny<string>(), It.IsAny<string>()))
|
||||
.Returns(_response);
|
||||
}
|
||||
|
||||
private void GivenResponseIsNotCached()
|
||||
{
|
||||
ScopedRepository
|
||||
.Setup(x => x.Get<HttpResponseMessage>("HttpResponseMessage"))
|
||||
.Returns(new OkResponse<HttpResponseMessage>(new HttpResponseMessage()));
|
||||
}
|
||||
|
||||
private void GivenTheDownstreamRouteIs()
|
||||
{
|
||||
var reRoute = new ReRouteBuilder()
|
||||
.WithIsCached(true)
|
||||
.WithCacheOptions(new CacheOptions(100, "kanken"))
|
||||
.WithUpstreamHttpMethod(new List<string> { "Get" })
|
||||
.Build();
|
||||
|
||||
var downstreamRoute = new DownstreamRoute(new List<PlaceholderNameAndValue>(), reRoute);
|
||||
|
||||
ScopedRepository
|
||||
.Setup(x => x.Get<DownstreamRoute>(It.IsAny<string>()))
|
||||
.Returns(new OkResponse<DownstreamRoute>(downstreamRoute));
|
||||
}
|
||||
|
||||
private void GivenThereAreNoErrors()
|
||||
{
|
||||
ScopedRepository
|
||||
.Setup(x => x.Get<bool>("OcelotMiddlewareError"))
|
||||
.Returns(new OkResponse<bool>(false));
|
||||
}
|
||||
|
||||
private void GivenThereIsADownstreamUrl()
|
||||
{
|
||||
ScopedRepository
|
||||
.Setup(x => x.Get<string>("DownstreamUrl"))
|
||||
.Returns(new OkResponse<string>("anything"));
|
||||
}
|
||||
|
||||
private void ThenTheCacheGetIsCalledCorrectly()
|
||||
{
|
||||
_cacheManager
|
||||
.Verify(x => x.Get(It.IsAny<string>(), It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
|
||||
private void ThenTheCacheAddIsCalledCorrectly()
|
||||
{
|
||||
_cacheManager
|
||||
.Verify(x => x.Add(It.IsAny<string>(), It.IsAny<CachedResponse>(), It.IsAny<TimeSpan>(), It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,65 +1,65 @@
|
||||
using System.Collections.Generic;
|
||||
using Ocelot.Cache;
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.Configuration.Builder;
|
||||
using Ocelot.Configuration.File;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests.Cache
|
||||
{
|
||||
public class RegionCreatorTests
|
||||
{
|
||||
private string _result;
|
||||
private FileReRoute _reRoute;
|
||||
|
||||
[Fact]
|
||||
public void should_create_region()
|
||||
{
|
||||
var reRoute = new FileReRoute
|
||||
{
|
||||
UpstreamHttpMethod = new List<string> { "Get" },
|
||||
UpstreamPathTemplate = "/testdummy"
|
||||
};
|
||||
|
||||
this.Given(_ => GivenTheReRoute(reRoute))
|
||||
.When(_ => WhenICreateTheRegion())
|
||||
.Then(_ => ThenTheRegionIs("Gettestdummy"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_use_region()
|
||||
{
|
||||
var reRoute = new FileReRoute
|
||||
{
|
||||
FileCacheOptions = new FileCacheOptions
|
||||
{
|
||||
Region = "region"
|
||||
}
|
||||
};
|
||||
|
||||
this.Given(_ => GivenTheReRoute(reRoute))
|
||||
.When(_ => WhenICreateTheRegion())
|
||||
.Then(_ => ThenTheRegionIs("region"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenTheReRoute(FileReRoute reRoute)
|
||||
{
|
||||
_reRoute = reRoute;
|
||||
}
|
||||
|
||||
private void WhenICreateTheRegion()
|
||||
{
|
||||
RegionCreator regionCreator = new RegionCreator();
|
||||
_result = regionCreator.Create(_reRoute);
|
||||
}
|
||||
|
||||
private void ThenTheRegionIs(string expected)
|
||||
{
|
||||
_result.ShouldBe(expected);
|
||||
}
|
||||
}
|
||||
using System.Collections.Generic;
|
||||
using Ocelot.Cache;
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.Configuration.Builder;
|
||||
using Ocelot.Configuration.File;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests.Cache
|
||||
{
|
||||
public class RegionCreatorTests
|
||||
{
|
||||
private string _result;
|
||||
private FileReRoute _reRoute;
|
||||
|
||||
[Fact]
|
||||
public void should_create_region()
|
||||
{
|
||||
var reRoute = new FileReRoute
|
||||
{
|
||||
UpstreamHttpMethod = new List<string> { "Get" },
|
||||
UpstreamPathTemplate = "/testdummy"
|
||||
};
|
||||
|
||||
this.Given(_ => GivenTheReRoute(reRoute))
|
||||
.When(_ => WhenICreateTheRegion())
|
||||
.Then(_ => ThenTheRegionIs("Gettestdummy"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_use_region()
|
||||
{
|
||||
var reRoute = new FileReRoute
|
||||
{
|
||||
FileCacheOptions = new FileCacheOptions
|
||||
{
|
||||
Region = "region"
|
||||
}
|
||||
};
|
||||
|
||||
this.Given(_ => GivenTheReRoute(reRoute))
|
||||
.When(_ => WhenICreateTheRegion())
|
||||
.Then(_ => ThenTheRegionIs("region"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenTheReRoute(FileReRoute reRoute)
|
||||
{
|
||||
_reRoute = reRoute;
|
||||
}
|
||||
|
||||
private void WhenICreateTheRegion()
|
||||
{
|
||||
RegionCreator regionCreator = new RegionCreator();
|
||||
_result = regionCreator.Create(_reRoute);
|
||||
}
|
||||
|
||||
private void ThenTheRegionIs(string expected)
|
||||
{
|
||||
_result.ShouldBe(expected);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,144 +1,144 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Security.Claims;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Moq;
|
||||
using Ocelot.Claims;
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.Errors;
|
||||
using Ocelot.Infrastructure.Claims.Parser;
|
||||
using Ocelot.Responses;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests.Claims
|
||||
{
|
||||
public class AddClaimsToRequestTests
|
||||
{
|
||||
private readonly AddClaimsToRequest _addClaimsToRequest;
|
||||
private readonly Mock<IClaimsParser> _parser;
|
||||
private List<ClaimToThing> _claimsToThings;
|
||||
private HttpContext _context;
|
||||
private Response _result;
|
||||
private Response<string> _claimValue;
|
||||
|
||||
public AddClaimsToRequestTests()
|
||||
{
|
||||
_parser = new Mock<IClaimsParser>();
|
||||
_addClaimsToRequest = new AddClaimsToRequest(_parser.Object);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_add_claims_to_context()
|
||||
{
|
||||
var context = new DefaultHttpContext
|
||||
{
|
||||
User = new ClaimsPrincipal(new ClaimsIdentity(new List<Claim>
|
||||
{
|
||||
new Claim("test", "data")
|
||||
}))
|
||||
};
|
||||
|
||||
this.Given(
|
||||
x => x.GivenClaimsToThings(new List<ClaimToThing>
|
||||
{
|
||||
new ClaimToThing("claim-key", "", "", 0)
|
||||
}))
|
||||
.Given(x => x.GivenHttpContext(context))
|
||||
.And(x => x.GivenTheClaimParserReturns(new OkResponse<string>("value")))
|
||||
.When(x => x.WhenIAddClaimsToTheRequest())
|
||||
.Then(x => x.ThenTheResultIsSuccess())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void if_claims_exists_should_replace_it()
|
||||
{
|
||||
var context = new DefaultHttpContext
|
||||
{
|
||||
User = new ClaimsPrincipal(new ClaimsIdentity(new List<Claim>
|
||||
{
|
||||
new Claim("existing-key", "data"),
|
||||
new Claim("new-key", "data")
|
||||
})),
|
||||
};
|
||||
|
||||
this.Given(
|
||||
x => x.GivenClaimsToThings(new List<ClaimToThing>
|
||||
{
|
||||
new ClaimToThing("existing-key", "new-key", "", 0)
|
||||
}))
|
||||
.Given(x => x.GivenHttpContext(context))
|
||||
.And(x => x.GivenTheClaimParserReturns(new OkResponse<string>("value")))
|
||||
.When(x => x.WhenIAddClaimsToTheRequest())
|
||||
.Then(x => x.ThenTheResultIsSuccess())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_error()
|
||||
{
|
||||
this.Given(
|
||||
x => x.GivenClaimsToThings(new List<ClaimToThing>
|
||||
{
|
||||
new ClaimToThing("", "", "", 0)
|
||||
}))
|
||||
.Given(x => x.GivenHttpContext(new DefaultHttpContext()))
|
||||
.And(x => x.GivenTheClaimParserReturns(new ErrorResponse<string>(new List<Error>
|
||||
{
|
||||
new AnyError()
|
||||
})))
|
||||
.When(x => x.WhenIAddClaimsToTheRequest())
|
||||
.Then(x => x.ThenTheResultIsError())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
|
||||
private void GivenClaimsToThings(List<ClaimToThing> configuration)
|
||||
{
|
||||
_claimsToThings = configuration;
|
||||
}
|
||||
|
||||
private void GivenHttpContext(HttpContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
private void GivenTheClaimParserReturns(Response<string> claimValue)
|
||||
{
|
||||
_claimValue = claimValue;
|
||||
_parser
|
||||
.Setup(
|
||||
x =>
|
||||
x.GetValue(It.IsAny<IEnumerable<Claim>>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<int>()))
|
||||
.Returns(_claimValue);
|
||||
}
|
||||
|
||||
private void WhenIAddClaimsToTheRequest()
|
||||
{
|
||||
_result = _addClaimsToRequest.SetClaimsOnContext(_claimsToThings, _context);
|
||||
}
|
||||
|
||||
private void ThenTheResultIsSuccess()
|
||||
{
|
||||
_result.IsError.ShouldBe(false);
|
||||
}
|
||||
|
||||
private void ThenTheResultIsError()
|
||||
{
|
||||
|
||||
_result.IsError.ShouldBe(true);
|
||||
}
|
||||
|
||||
class AnyError : Error
|
||||
{
|
||||
public AnyError()
|
||||
: base("blahh", OcelotErrorCode.UnknownError)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
using System.Collections.Generic;
|
||||
using System.Security.Claims;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Moq;
|
||||
using Ocelot.Claims;
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.Errors;
|
||||
using Ocelot.Infrastructure.Claims.Parser;
|
||||
using Ocelot.Responses;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests.Claims
|
||||
{
|
||||
public class AddClaimsToRequestTests
|
||||
{
|
||||
private readonly AddClaimsToRequest _addClaimsToRequest;
|
||||
private readonly Mock<IClaimsParser> _parser;
|
||||
private List<ClaimToThing> _claimsToThings;
|
||||
private HttpContext _context;
|
||||
private Response _result;
|
||||
private Response<string> _claimValue;
|
||||
|
||||
public AddClaimsToRequestTests()
|
||||
{
|
||||
_parser = new Mock<IClaimsParser>();
|
||||
_addClaimsToRequest = new AddClaimsToRequest(_parser.Object);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_add_claims_to_context()
|
||||
{
|
||||
var context = new DefaultHttpContext
|
||||
{
|
||||
User = new ClaimsPrincipal(new ClaimsIdentity(new List<Claim>
|
||||
{
|
||||
new Claim("test", "data")
|
||||
}))
|
||||
};
|
||||
|
||||
this.Given(
|
||||
x => x.GivenClaimsToThings(new List<ClaimToThing>
|
||||
{
|
||||
new ClaimToThing("claim-key", "", "", 0)
|
||||
}))
|
||||
.Given(x => x.GivenHttpContext(context))
|
||||
.And(x => x.GivenTheClaimParserReturns(new OkResponse<string>("value")))
|
||||
.When(x => x.WhenIAddClaimsToTheRequest())
|
||||
.Then(x => x.ThenTheResultIsSuccess())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void if_claims_exists_should_replace_it()
|
||||
{
|
||||
var context = new DefaultHttpContext
|
||||
{
|
||||
User = new ClaimsPrincipal(new ClaimsIdentity(new List<Claim>
|
||||
{
|
||||
new Claim("existing-key", "data"),
|
||||
new Claim("new-key", "data")
|
||||
})),
|
||||
};
|
||||
|
||||
this.Given(
|
||||
x => x.GivenClaimsToThings(new List<ClaimToThing>
|
||||
{
|
||||
new ClaimToThing("existing-key", "new-key", "", 0)
|
||||
}))
|
||||
.Given(x => x.GivenHttpContext(context))
|
||||
.And(x => x.GivenTheClaimParserReturns(new OkResponse<string>("value")))
|
||||
.When(x => x.WhenIAddClaimsToTheRequest())
|
||||
.Then(x => x.ThenTheResultIsSuccess())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_error()
|
||||
{
|
||||
this.Given(
|
||||
x => x.GivenClaimsToThings(new List<ClaimToThing>
|
||||
{
|
||||
new ClaimToThing("", "", "", 0)
|
||||
}))
|
||||
.Given(x => x.GivenHttpContext(new DefaultHttpContext()))
|
||||
.And(x => x.GivenTheClaimParserReturns(new ErrorResponse<string>(new List<Error>
|
||||
{
|
||||
new AnyError()
|
||||
})))
|
||||
.When(x => x.WhenIAddClaimsToTheRequest())
|
||||
.Then(x => x.ThenTheResultIsError())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
|
||||
private void GivenClaimsToThings(List<ClaimToThing> configuration)
|
||||
{
|
||||
_claimsToThings = configuration;
|
||||
}
|
||||
|
||||
private void GivenHttpContext(HttpContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
private void GivenTheClaimParserReturns(Response<string> claimValue)
|
||||
{
|
||||
_claimValue = claimValue;
|
||||
_parser
|
||||
.Setup(
|
||||
x =>
|
||||
x.GetValue(It.IsAny<IEnumerable<Claim>>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<int>()))
|
||||
.Returns(_claimValue);
|
||||
}
|
||||
|
||||
private void WhenIAddClaimsToTheRequest()
|
||||
{
|
||||
_result = _addClaimsToRequest.SetClaimsOnContext(_claimsToThings, _context);
|
||||
}
|
||||
|
||||
private void ThenTheResultIsSuccess()
|
||||
{
|
||||
_result.IsError.ShouldBe(false);
|
||||
}
|
||||
|
||||
private void ThenTheResultIsError()
|
||||
{
|
||||
|
||||
_result.IsError.ShouldBe(true);
|
||||
}
|
||||
|
||||
class AnyError : Error
|
||||
{
|
||||
public AnyError()
|
||||
: base("blahh", OcelotErrorCode.UnknownError)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,87 +1,87 @@
|
||||
namespace Ocelot.UnitTests.Claims
|
||||
{
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Moq;
|
||||
using Ocelot.Claims;
|
||||
using Ocelot.Claims.Middleware;
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.Configuration.Builder;
|
||||
using Ocelot.DownstreamRouteFinder;
|
||||
using Ocelot.DownstreamRouteFinder.UrlMatcher;
|
||||
using Ocelot.Logging;
|
||||
using Ocelot.Responses;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
public class ClaimsBuilderMiddlewareTests : ServerHostedMiddlewareTest
|
||||
{
|
||||
private readonly Mock<IAddClaimsToRequest> _addHeaders;
|
||||
private Response<DownstreamRoute> _downstreamRoute;
|
||||
|
||||
public ClaimsBuilderMiddlewareTests()
|
||||
{
|
||||
_addHeaders = new Mock<IAddClaimsToRequest>();
|
||||
|
||||
GivenTheTestServerIsConfigured();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_call_claims_to_request_correctly()
|
||||
{
|
||||
var downstreamRoute = new DownstreamRoute(new List<PlaceholderNameAndValue>(),
|
||||
new ReRouteBuilder()
|
||||
.WithDownstreamPathTemplate("any old string")
|
||||
.WithClaimsToClaims(new List<ClaimToThing>
|
||||
{
|
||||
new ClaimToThing("sub", "UserType", "|", 0)
|
||||
})
|
||||
.WithUpstreamHttpMethod(new List<string> { "Get" })
|
||||
.Build());
|
||||
|
||||
this.Given(x => x.GivenTheDownStreamRouteIs(downstreamRoute))
|
||||
.And(x => x.GivenTheAddClaimsToRequestReturns())
|
||||
.When(x => x.WhenICallTheMiddleware())
|
||||
.Then(x => x.ThenTheClaimsToRequestIsCalledCorrectly())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
protected override void GivenTheTestServerServicesAreConfigured(IServiceCollection services)
|
||||
{
|
||||
services.AddSingleton<IOcelotLoggerFactory, AspDotNetLoggerFactory>();
|
||||
services.AddLogging();
|
||||
services.AddSingleton(_addHeaders.Object);
|
||||
services.AddSingleton(ScopedRepository.Object);
|
||||
}
|
||||
|
||||
protected override void GivenTheTestServerPipelineIsConfigured(IApplicationBuilder app)
|
||||
{
|
||||
app.UseClaimsBuilderMiddleware();
|
||||
}
|
||||
|
||||
private void GivenTheDownStreamRouteIs(DownstreamRoute downstreamRoute)
|
||||
{
|
||||
_downstreamRoute = new OkResponse<DownstreamRoute>(downstreamRoute);
|
||||
ScopedRepository
|
||||
.Setup(x => x.Get<DownstreamRoute>(It.IsAny<string>()))
|
||||
.Returns(_downstreamRoute);
|
||||
}
|
||||
|
||||
private void GivenTheAddClaimsToRequestReturns()
|
||||
{
|
||||
_addHeaders
|
||||
.Setup(x => x.SetClaimsOnContext(It.IsAny<List<ClaimToThing>>(),
|
||||
It.IsAny<HttpContext>()))
|
||||
.Returns(new OkResponse());
|
||||
}
|
||||
|
||||
private void ThenTheClaimsToRequestIsCalledCorrectly()
|
||||
{
|
||||
_addHeaders
|
||||
.Verify(x => x.SetClaimsOnContext(It.IsAny<List<ClaimToThing>>(),
|
||||
It.IsAny<HttpContext>()), Times.Once);
|
||||
}
|
||||
}
|
||||
}
|
||||
namespace Ocelot.UnitTests.Claims
|
||||
{
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Moq;
|
||||
using Ocelot.Claims;
|
||||
using Ocelot.Claims.Middleware;
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.Configuration.Builder;
|
||||
using Ocelot.DownstreamRouteFinder;
|
||||
using Ocelot.DownstreamRouteFinder.UrlMatcher;
|
||||
using Ocelot.Logging;
|
||||
using Ocelot.Responses;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
public class ClaimsBuilderMiddlewareTests : ServerHostedMiddlewareTest
|
||||
{
|
||||
private readonly Mock<IAddClaimsToRequest> _addHeaders;
|
||||
private Response<DownstreamRoute> _downstreamRoute;
|
||||
|
||||
public ClaimsBuilderMiddlewareTests()
|
||||
{
|
||||
_addHeaders = new Mock<IAddClaimsToRequest>();
|
||||
|
||||
GivenTheTestServerIsConfigured();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_call_claims_to_request_correctly()
|
||||
{
|
||||
var downstreamRoute = new DownstreamRoute(new List<PlaceholderNameAndValue>(),
|
||||
new ReRouteBuilder()
|
||||
.WithDownstreamPathTemplate("any old string")
|
||||
.WithClaimsToClaims(new List<ClaimToThing>
|
||||
{
|
||||
new ClaimToThing("sub", "UserType", "|", 0)
|
||||
})
|
||||
.WithUpstreamHttpMethod(new List<string> { "Get" })
|
||||
.Build());
|
||||
|
||||
this.Given(x => x.GivenTheDownStreamRouteIs(downstreamRoute))
|
||||
.And(x => x.GivenTheAddClaimsToRequestReturns())
|
||||
.When(x => x.WhenICallTheMiddleware())
|
||||
.Then(x => x.ThenTheClaimsToRequestIsCalledCorrectly())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
protected override void GivenTheTestServerServicesAreConfigured(IServiceCollection services)
|
||||
{
|
||||
services.AddSingleton<IOcelotLoggerFactory, AspDotNetLoggerFactory>();
|
||||
services.AddLogging();
|
||||
services.AddSingleton(_addHeaders.Object);
|
||||
services.AddSingleton(ScopedRepository.Object);
|
||||
}
|
||||
|
||||
protected override void GivenTheTestServerPipelineIsConfigured(IApplicationBuilder app)
|
||||
{
|
||||
app.UseClaimsBuilderMiddleware();
|
||||
}
|
||||
|
||||
private void GivenTheDownStreamRouteIs(DownstreamRoute downstreamRoute)
|
||||
{
|
||||
_downstreamRoute = new OkResponse<DownstreamRoute>(downstreamRoute);
|
||||
ScopedRepository
|
||||
.Setup(x => x.Get<DownstreamRoute>(It.IsAny<string>()))
|
||||
.Returns(_downstreamRoute);
|
||||
}
|
||||
|
||||
private void GivenTheAddClaimsToRequestReturns()
|
||||
{
|
||||
_addHeaders
|
||||
.Setup(x => x.SetClaimsOnContext(It.IsAny<List<ClaimToThing>>(),
|
||||
It.IsAny<HttpContext>()))
|
||||
.Returns(new OkResponse());
|
||||
}
|
||||
|
||||
private void ThenTheClaimsToRequestIsCalledCorrectly()
|
||||
{
|
||||
_addHeaders
|
||||
.Verify(x => x.SetClaimsOnContext(It.IsAny<List<ClaimToThing>>(),
|
||||
It.IsAny<HttpContext>()), Times.Once);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,62 +1,62 @@
|
||||
using System.Collections.Generic;
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.Configuration.Builder;
|
||||
using Ocelot.Configuration.Creator;
|
||||
using Ocelot.Configuration.File;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests.Configuration
|
||||
{
|
||||
public class AuthenticationOptionsCreatorTests
|
||||
{
|
||||
private readonly AuthenticationOptionsCreator _authOptionsCreator;
|
||||
private FileReRoute _fileReRoute;
|
||||
private AuthenticationOptions _result;
|
||||
|
||||
public AuthenticationOptionsCreatorTests()
|
||||
{
|
||||
_authOptionsCreator = new AuthenticationOptionsCreator();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_auth_options()
|
||||
{
|
||||
var fileReRoute = new FileReRoute()
|
||||
{
|
||||
AuthenticationOptions = new FileAuthenticationOptions
|
||||
{
|
||||
AuthenticationProviderKey = "Test",
|
||||
AllowedScopes = new List<string> { "cheese" },
|
||||
}
|
||||
};
|
||||
|
||||
var expected = new AuthenticationOptionsBuilder()
|
||||
.WithAllowedScopes(fileReRoute.AuthenticationOptions?.AllowedScopes)
|
||||
.WithAuthenticationProviderKey("Test")
|
||||
.Build();
|
||||
|
||||
this.Given(x => x.GivenTheFollowing(fileReRoute))
|
||||
.When(x => x.WhenICreateTheAuthenticationOptions())
|
||||
.Then(x => x.ThenTheFollowingConfigIsReturned(expected))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenTheFollowing(FileReRoute fileReRoute)
|
||||
{
|
||||
_fileReRoute = fileReRoute;
|
||||
}
|
||||
|
||||
private void WhenICreateTheAuthenticationOptions()
|
||||
{
|
||||
_result = _authOptionsCreator.Create(_fileReRoute);
|
||||
}
|
||||
|
||||
private void ThenTheFollowingConfigIsReturned(AuthenticationOptions expected)
|
||||
{
|
||||
_result.AllowedScopes.ShouldBe(expected.AllowedScopes);
|
||||
_result.AuthenticationProviderKey.ShouldBe(expected.AuthenticationProviderKey);
|
||||
}
|
||||
}
|
||||
using System.Collections.Generic;
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.Configuration.Builder;
|
||||
using Ocelot.Configuration.Creator;
|
||||
using Ocelot.Configuration.File;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests.Configuration
|
||||
{
|
||||
public class AuthenticationOptionsCreatorTests
|
||||
{
|
||||
private readonly AuthenticationOptionsCreator _authOptionsCreator;
|
||||
private FileReRoute _fileReRoute;
|
||||
private AuthenticationOptions _result;
|
||||
|
||||
public AuthenticationOptionsCreatorTests()
|
||||
{
|
||||
_authOptionsCreator = new AuthenticationOptionsCreator();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_auth_options()
|
||||
{
|
||||
var fileReRoute = new FileReRoute()
|
||||
{
|
||||
AuthenticationOptions = new FileAuthenticationOptions
|
||||
{
|
||||
AuthenticationProviderKey = "Test",
|
||||
AllowedScopes = new List<string> { "cheese" },
|
||||
}
|
||||
};
|
||||
|
||||
var expected = new AuthenticationOptionsBuilder()
|
||||
.WithAllowedScopes(fileReRoute.AuthenticationOptions?.AllowedScopes)
|
||||
.WithAuthenticationProviderKey("Test")
|
||||
.Build();
|
||||
|
||||
this.Given(x => x.GivenTheFollowing(fileReRoute))
|
||||
.When(x => x.WhenICreateTheAuthenticationOptions())
|
||||
.Then(x => x.ThenTheFollowingConfigIsReturned(expected))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenTheFollowing(FileReRoute fileReRoute)
|
||||
{
|
||||
_fileReRoute = fileReRoute;
|
||||
}
|
||||
|
||||
private void WhenICreateTheAuthenticationOptions()
|
||||
{
|
||||
_result = _authOptionsCreator.Create(_fileReRoute);
|
||||
}
|
||||
|
||||
private void ThenTheFollowingConfigIsReturned(AuthenticationOptions expected)
|
||||
{
|
||||
_result.AllowedScopes.ShouldBe(expected.AllowedScopes);
|
||||
_result.AuthenticationProviderKey.ShouldBe(expected.AuthenticationProviderKey);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,117 +1,117 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.Configuration.Parser;
|
||||
using Ocelot.Errors;
|
||||
using Ocelot.Responses;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests.Configuration
|
||||
{
|
||||
public class ClaimToThingConfigurationParserTests
|
||||
{
|
||||
private Dictionary<string, string> _dictionary;
|
||||
private readonly IClaimToThingConfigurationParser _claimToThingConfigurationParser;
|
||||
private Response<ClaimToThing> _result;
|
||||
|
||||
public ClaimToThingConfigurationParserTests()
|
||||
{
|
||||
_claimToThingConfigurationParser = new ClaimToThingConfigurationParser();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void returns_no_instructions_error()
|
||||
{
|
||||
this.Given(x => x.GivenTheDictionaryIs(new Dictionary<string, string>()
|
||||
{
|
||||
{"CustomerId", ""},
|
||||
}))
|
||||
.When(x => x.WhenICallTheExtractor())
|
||||
.Then(
|
||||
x =>
|
||||
x.ThenAnErrorIsReturned(new ErrorResponse<ClaimToThing>(
|
||||
new List<Error>
|
||||
{
|
||||
new NoInstructionsError(">")
|
||||
})))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void returns_no_instructions_not_for_claims_error()
|
||||
{
|
||||
this.Given(x => x.GivenTheDictionaryIs(new Dictionary<string, string>()
|
||||
{
|
||||
{"CustomerId", "Cheese[CustomerId] > value"},
|
||||
}))
|
||||
.When(x => x.WhenICallTheExtractor())
|
||||
.Then(
|
||||
x =>
|
||||
x.ThenAnErrorIsReturned(new ErrorResponse<ClaimToThing>(
|
||||
new List<Error>
|
||||
{
|
||||
new InstructionNotForClaimsError()
|
||||
})))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void can_parse_entry_to_work_out_properties_with_key()
|
||||
{
|
||||
this.Given(x => x.GivenTheDictionaryIs(new Dictionary<string, string>()
|
||||
{
|
||||
{"CustomerId", "Claims[CustomerId] > value"},
|
||||
}))
|
||||
.When(x => x.WhenICallTheExtractor())
|
||||
.Then(
|
||||
x =>
|
||||
x.ThenTheClaimParserPropertiesAreReturned(
|
||||
new OkResponse<ClaimToThing>(
|
||||
new ClaimToThing("CustomerId", "CustomerId", "", 0))))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void can_parse_entry_to_work_out_properties_with_key_delimiter_and_index()
|
||||
{
|
||||
this.Given(x => x.GivenTheDictionaryIs(new Dictionary<string, string>()
|
||||
{
|
||||
{"UserId", "Claims[Subject] > value[0] > |"},
|
||||
}))
|
||||
.When(x => x.WhenICallTheExtractor())
|
||||
.Then(
|
||||
x =>
|
||||
x.ThenTheClaimParserPropertiesAreReturned(
|
||||
new OkResponse<ClaimToThing>(
|
||||
new ClaimToThing("UserId", "Subject", "|", 0))))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void ThenAnErrorIsReturned(Response<ClaimToThing> expected)
|
||||
{
|
||||
_result.IsError.ShouldBe(expected.IsError);
|
||||
_result.Errors[0].ShouldBeOfType(expected.Errors[0].GetType());
|
||||
}
|
||||
|
||||
private void ThenTheClaimParserPropertiesAreReturned(Response<ClaimToThing> expected)
|
||||
{
|
||||
_result.Data.NewKey.ShouldBe(expected.Data.NewKey);
|
||||
_result.Data.Delimiter.ShouldBe(expected.Data.Delimiter);
|
||||
_result.Data.Index.ShouldBe(expected.Data.Index);
|
||||
_result.IsError.ShouldBe(expected.IsError);
|
||||
}
|
||||
|
||||
private void WhenICallTheExtractor()
|
||||
{
|
||||
var first = _dictionary.First();
|
||||
_result = _claimToThingConfigurationParser.Extract(first.Key, first.Value);
|
||||
}
|
||||
|
||||
private void GivenTheDictionaryIs(Dictionary<string, string> dictionary)
|
||||
{
|
||||
_dictionary = dictionary;
|
||||
}
|
||||
}
|
||||
}
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.Configuration.Parser;
|
||||
using Ocelot.Errors;
|
||||
using Ocelot.Responses;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests.Configuration
|
||||
{
|
||||
public class ClaimToThingConfigurationParserTests
|
||||
{
|
||||
private Dictionary<string, string> _dictionary;
|
||||
private readonly IClaimToThingConfigurationParser _claimToThingConfigurationParser;
|
||||
private Response<ClaimToThing> _result;
|
||||
|
||||
public ClaimToThingConfigurationParserTests()
|
||||
{
|
||||
_claimToThingConfigurationParser = new ClaimToThingConfigurationParser();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void returns_no_instructions_error()
|
||||
{
|
||||
this.Given(x => x.GivenTheDictionaryIs(new Dictionary<string, string>()
|
||||
{
|
||||
{"CustomerId", ""},
|
||||
}))
|
||||
.When(x => x.WhenICallTheExtractor())
|
||||
.Then(
|
||||
x =>
|
||||
x.ThenAnErrorIsReturned(new ErrorResponse<ClaimToThing>(
|
||||
new List<Error>
|
||||
{
|
||||
new NoInstructionsError(">")
|
||||
})))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void returns_no_instructions_not_for_claims_error()
|
||||
{
|
||||
this.Given(x => x.GivenTheDictionaryIs(new Dictionary<string, string>()
|
||||
{
|
||||
{"CustomerId", "Cheese[CustomerId] > value"},
|
||||
}))
|
||||
.When(x => x.WhenICallTheExtractor())
|
||||
.Then(
|
||||
x =>
|
||||
x.ThenAnErrorIsReturned(new ErrorResponse<ClaimToThing>(
|
||||
new List<Error>
|
||||
{
|
||||
new InstructionNotForClaimsError()
|
||||
})))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void can_parse_entry_to_work_out_properties_with_key()
|
||||
{
|
||||
this.Given(x => x.GivenTheDictionaryIs(new Dictionary<string, string>()
|
||||
{
|
||||
{"CustomerId", "Claims[CustomerId] > value"},
|
||||
}))
|
||||
.When(x => x.WhenICallTheExtractor())
|
||||
.Then(
|
||||
x =>
|
||||
x.ThenTheClaimParserPropertiesAreReturned(
|
||||
new OkResponse<ClaimToThing>(
|
||||
new ClaimToThing("CustomerId", "CustomerId", "", 0))))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void can_parse_entry_to_work_out_properties_with_key_delimiter_and_index()
|
||||
{
|
||||
this.Given(x => x.GivenTheDictionaryIs(new Dictionary<string, string>()
|
||||
{
|
||||
{"UserId", "Claims[Subject] > value[0] > |"},
|
||||
}))
|
||||
.When(x => x.WhenICallTheExtractor())
|
||||
.Then(
|
||||
x =>
|
||||
x.ThenTheClaimParserPropertiesAreReturned(
|
||||
new OkResponse<ClaimToThing>(
|
||||
new ClaimToThing("UserId", "Subject", "|", 0))))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void ThenAnErrorIsReturned(Response<ClaimToThing> expected)
|
||||
{
|
||||
_result.IsError.ShouldBe(expected.IsError);
|
||||
_result.Errors[0].ShouldBeOfType(expected.Errors[0].GetType());
|
||||
}
|
||||
|
||||
private void ThenTheClaimParserPropertiesAreReturned(Response<ClaimToThing> expected)
|
||||
{
|
||||
_result.Data.NewKey.ShouldBe(expected.Data.NewKey);
|
||||
_result.Data.Delimiter.ShouldBe(expected.Data.Delimiter);
|
||||
_result.Data.Index.ShouldBe(expected.Data.Index);
|
||||
_result.IsError.ShouldBe(expected.IsError);
|
||||
}
|
||||
|
||||
private void WhenICallTheExtractor()
|
||||
{
|
||||
var first = _dictionary.First();
|
||||
_result = _claimToThingConfigurationParser.Extract(first.Key, first.Value);
|
||||
}
|
||||
|
||||
private void GivenTheDictionaryIs(Dictionary<string, string> dictionary)
|
||||
{
|
||||
_dictionary = dictionary;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,110 +1,110 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Moq;
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.Configuration.Creator;
|
||||
using Ocelot.Configuration.Parser;
|
||||
using Ocelot.Errors;
|
||||
using Ocelot.Logging;
|
||||
using Ocelot.Responses;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests.Configuration
|
||||
{
|
||||
public class ClaimsToThingCreatorTests
|
||||
{
|
||||
private readonly Mock<IClaimToThingConfigurationParser> _configParser;
|
||||
private Dictionary<string,string> _claimsToThings;
|
||||
private ClaimsToThingCreator _claimsToThingsCreator;
|
||||
private Mock<IOcelotLoggerFactory> _loggerFactory;
|
||||
private List<ClaimToThing> _result;
|
||||
private Mock<IOcelotLogger> _logger;
|
||||
|
||||
public ClaimsToThingCreatorTests()
|
||||
{
|
||||
_loggerFactory = new Mock<IOcelotLoggerFactory>();
|
||||
_logger = new Mock<IOcelotLogger>();
|
||||
_loggerFactory
|
||||
.Setup(x => x.CreateLogger<ClaimsToThingCreator>())
|
||||
.Returns(_logger.Object);
|
||||
_configParser = new Mock<IClaimToThingConfigurationParser>();
|
||||
_claimsToThingsCreator = new ClaimsToThingCreator(_configParser.Object, _loggerFactory.Object);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_claims_to_things()
|
||||
{
|
||||
var userInput = new Dictionary<string,string>()
|
||||
{
|
||||
{"CustomerId", "Claims[CustomerId] > value"}
|
||||
};
|
||||
|
||||
var claimsToThing = new OkResponse<ClaimToThing>(new ClaimToThing("CustomerId", "CustomerId", "", 0));
|
||||
|
||||
this.Given(x => x.GivenTheFollowingDictionary(userInput))
|
||||
.And(x => x.GivenTheConfigHeaderExtractorReturns(claimsToThing))
|
||||
.When(x => x.WhenIGetTheThings())
|
||||
.Then(x => x.ThenTheConfigParserIsCalledCorrectly())
|
||||
.And(x => x.ThenClaimsToThingsAreReturned())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_log_error_if_cannot_parse_claim_to_thing()
|
||||
{
|
||||
var userInput = new Dictionary<string,string>()
|
||||
{
|
||||
{"CustomerId", "Claims[CustomerId] > value"}
|
||||
};
|
||||
|
||||
var claimsToThing = new ErrorResponse<ClaimToThing>(It.IsAny<Error>());
|
||||
|
||||
this.Given(x => x.GivenTheFollowingDictionary(userInput))
|
||||
.And(x => x.GivenTheConfigHeaderExtractorReturns(claimsToThing))
|
||||
.When(x => x.WhenIGetTheThings())
|
||||
.Then(x => x.ThenTheConfigParserIsCalledCorrectly())
|
||||
.And(x => x.ThenNoClaimsToThingsAreReturned())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void ThenTheLoggerIsCalledCorrectly()
|
||||
{
|
||||
_logger
|
||||
.Verify(x => x.LogDebug(It.IsAny<string>(), It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
|
||||
private void ThenClaimsToThingsAreReturned()
|
||||
{
|
||||
_result.Count.ShouldBeGreaterThan(0);
|
||||
}
|
||||
private void GivenTheFollowingDictionary(Dictionary<string,string> claimsToThings)
|
||||
{
|
||||
_claimsToThings = claimsToThings;
|
||||
}
|
||||
|
||||
private void GivenTheConfigHeaderExtractorReturns(Response<ClaimToThing> expected)
|
||||
{
|
||||
_configParser
|
||||
.Setup(x => x.Extract(It.IsAny<string>(), It.IsAny<string>()))
|
||||
.Returns(expected);
|
||||
}
|
||||
|
||||
private void ThenNoClaimsToThingsAreReturned()
|
||||
{
|
||||
_result.Count.ShouldBe(0);
|
||||
}
|
||||
|
||||
private void WhenIGetTheThings()
|
||||
{
|
||||
_result = _claimsToThingsCreator.Create(_claimsToThings);
|
||||
}
|
||||
|
||||
private void ThenTheConfigParserIsCalledCorrectly()
|
||||
{
|
||||
_configParser
|
||||
.Verify(x => x.Extract(_claimsToThings.First().Key, _claimsToThings.First().Value), Times.Once);
|
||||
}
|
||||
}
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Moq;
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.Configuration.Creator;
|
||||
using Ocelot.Configuration.Parser;
|
||||
using Ocelot.Errors;
|
||||
using Ocelot.Logging;
|
||||
using Ocelot.Responses;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests.Configuration
|
||||
{
|
||||
public class ClaimsToThingCreatorTests
|
||||
{
|
||||
private readonly Mock<IClaimToThingConfigurationParser> _configParser;
|
||||
private Dictionary<string,string> _claimsToThings;
|
||||
private ClaimsToThingCreator _claimsToThingsCreator;
|
||||
private Mock<IOcelotLoggerFactory> _loggerFactory;
|
||||
private List<ClaimToThing> _result;
|
||||
private Mock<IOcelotLogger> _logger;
|
||||
|
||||
public ClaimsToThingCreatorTests()
|
||||
{
|
||||
_loggerFactory = new Mock<IOcelotLoggerFactory>();
|
||||
_logger = new Mock<IOcelotLogger>();
|
||||
_loggerFactory
|
||||
.Setup(x => x.CreateLogger<ClaimsToThingCreator>())
|
||||
.Returns(_logger.Object);
|
||||
_configParser = new Mock<IClaimToThingConfigurationParser>();
|
||||
_claimsToThingsCreator = new ClaimsToThingCreator(_configParser.Object, _loggerFactory.Object);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_claims_to_things()
|
||||
{
|
||||
var userInput = new Dictionary<string,string>()
|
||||
{
|
||||
{"CustomerId", "Claims[CustomerId] > value"}
|
||||
};
|
||||
|
||||
var claimsToThing = new OkResponse<ClaimToThing>(new ClaimToThing("CustomerId", "CustomerId", "", 0));
|
||||
|
||||
this.Given(x => x.GivenTheFollowingDictionary(userInput))
|
||||
.And(x => x.GivenTheConfigHeaderExtractorReturns(claimsToThing))
|
||||
.When(x => x.WhenIGetTheThings())
|
||||
.Then(x => x.ThenTheConfigParserIsCalledCorrectly())
|
||||
.And(x => x.ThenClaimsToThingsAreReturned())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_log_error_if_cannot_parse_claim_to_thing()
|
||||
{
|
||||
var userInput = new Dictionary<string,string>()
|
||||
{
|
||||
{"CustomerId", "Claims[CustomerId] > value"}
|
||||
};
|
||||
|
||||
var claimsToThing = new ErrorResponse<ClaimToThing>(It.IsAny<Error>());
|
||||
|
||||
this.Given(x => x.GivenTheFollowingDictionary(userInput))
|
||||
.And(x => x.GivenTheConfigHeaderExtractorReturns(claimsToThing))
|
||||
.When(x => x.WhenIGetTheThings())
|
||||
.Then(x => x.ThenTheConfigParserIsCalledCorrectly())
|
||||
.And(x => x.ThenNoClaimsToThingsAreReturned())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void ThenTheLoggerIsCalledCorrectly()
|
||||
{
|
||||
_logger
|
||||
.Verify(x => x.LogDebug(It.IsAny<string>(), It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
|
||||
private void ThenClaimsToThingsAreReturned()
|
||||
{
|
||||
_result.Count.ShouldBeGreaterThan(0);
|
||||
}
|
||||
private void GivenTheFollowingDictionary(Dictionary<string,string> claimsToThings)
|
||||
{
|
||||
_claimsToThings = claimsToThings;
|
||||
}
|
||||
|
||||
private void GivenTheConfigHeaderExtractorReturns(Response<ClaimToThing> expected)
|
||||
{
|
||||
_configParser
|
||||
.Setup(x => x.Extract(It.IsAny<string>(), It.IsAny<string>()))
|
||||
.Returns(expected);
|
||||
}
|
||||
|
||||
private void ThenNoClaimsToThingsAreReturned()
|
||||
{
|
||||
_result.Count.ShouldBe(0);
|
||||
}
|
||||
|
||||
private void WhenIGetTheThings()
|
||||
{
|
||||
_result = _claimsToThingsCreator.Create(_claimsToThings);
|
||||
}
|
||||
|
||||
private void ThenTheConfigParserIsCalledCorrectly()
|
||||
{
|
||||
_configParser
|
||||
.Verify(x => x.Extract(_claimsToThings.First().Key, _claimsToThings.First().Value), Times.Once);
|
||||
}
|
||||
}
|
||||
}
|
File diff suppressed because it is too large
Load Diff
@ -1,89 +1,95 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using Moq;
|
||||
using Ocelot.Configuration.File;
|
||||
using Ocelot.Configuration.Repository;
|
||||
using Ocelot.Configuration.Setter;
|
||||
using Ocelot.Logging;
|
||||
using Ocelot.Responses;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
using Shouldly;
|
||||
using static Ocelot.UnitTests.Wait;
|
||||
|
||||
|
||||
namespace Ocelot.UnitTests.Configuration
|
||||
{
|
||||
public class ConsulFileConfigurationPollerTests : IDisposable
|
||||
{
|
||||
private ConsulFileConfigurationPoller _poller;
|
||||
private Mock<IOcelotLoggerFactory> _factory;
|
||||
private Mock<IFileConfigurationRepository> _repo;
|
||||
private Mock<IFileConfigurationSetter> _setter;
|
||||
private FileConfiguration _fileConfig;
|
||||
|
||||
public ConsulFileConfigurationPollerTests()
|
||||
{
|
||||
var logger = new Mock<IOcelotLogger>();
|
||||
_factory = new Mock<IOcelotLoggerFactory>();
|
||||
_factory.Setup(x => x.CreateLogger<ConsulFileConfigurationPoller>()).Returns(logger.Object);
|
||||
_repo = new Mock<IFileConfigurationRepository>();
|
||||
_setter = new Mock<IFileConfigurationSetter>();
|
||||
_fileConfig = new FileConfiguration();
|
||||
_repo.Setup(x => x.Get()).ReturnsAsync(new OkResponse<FileConfiguration>(_fileConfig));
|
||||
_poller = new ConsulFileConfigurationPoller(_factory.Object, _repo.Object, _setter.Object);
|
||||
}
|
||||
public void Dispose()
|
||||
{
|
||||
_poller.Dispose();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_start()
|
||||
{
|
||||
this.Given(x => ThenTheSetterIsCalled(_fileConfig))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_call_setter_when_gets_new_config()
|
||||
{
|
||||
|
||||
var newConfig = new FileConfiguration {
|
||||
ReRoutes = new List<FileReRoute>
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamHost = "test"
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.Given(x => WhenTheConfigIsChangedInConsul(newConfig))
|
||||
.Then(x => ThenTheSetterIsCalled(newConfig))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void WhenTheConfigIsChangedInConsul(FileConfiguration newConfig)
|
||||
{
|
||||
_repo.Setup(x => x.Get()).ReturnsAsync(new OkResponse<FileConfiguration>(newConfig));
|
||||
}
|
||||
|
||||
private void ThenTheSetterIsCalled(FileConfiguration fileConfig)
|
||||
{
|
||||
var result = WaitFor(2000).Until(() => {
|
||||
try
|
||||
{
|
||||
_setter.Verify(x => x.Set(fileConfig), Times.Once);
|
||||
return true;
|
||||
}
|
||||
catch(Exception ex)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
});
|
||||
result.ShouldBeTrue();
|
||||
}
|
||||
}
|
||||
}
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using Moq;
|
||||
using Ocelot.Configuration.File;
|
||||
using Ocelot.Configuration.Repository;
|
||||
using Ocelot.Configuration.Setter;
|
||||
using Ocelot.Logging;
|
||||
using Ocelot.Responses;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
using Shouldly;
|
||||
using static Ocelot.UnitTests.Wait;
|
||||
|
||||
|
||||
namespace Ocelot.UnitTests.Configuration
|
||||
{
|
||||
public class ConsulFileConfigurationPollerTests : IDisposable
|
||||
{
|
||||
private ConsulFileConfigurationPoller _poller;
|
||||
private Mock<IOcelotLoggerFactory> _factory;
|
||||
private Mock<IFileConfigurationRepository> _repo;
|
||||
private Mock<IFileConfigurationSetter> _setter;
|
||||
private FileConfiguration _fileConfig;
|
||||
|
||||
public ConsulFileConfigurationPollerTests()
|
||||
{
|
||||
var logger = new Mock<IOcelotLogger>();
|
||||
_factory = new Mock<IOcelotLoggerFactory>();
|
||||
_factory.Setup(x => x.CreateLogger<ConsulFileConfigurationPoller>()).Returns(logger.Object);
|
||||
_repo = new Mock<IFileConfigurationRepository>();
|
||||
_setter = new Mock<IFileConfigurationSetter>();
|
||||
_fileConfig = new FileConfiguration();
|
||||
_repo.Setup(x => x.Get()).ReturnsAsync(new OkResponse<FileConfiguration>(_fileConfig));
|
||||
_poller = new ConsulFileConfigurationPoller(_factory.Object, _repo.Object, _setter.Object);
|
||||
}
|
||||
public void Dispose()
|
||||
{
|
||||
_poller.Dispose();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_start()
|
||||
{
|
||||
this.Given(x => ThenTheSetterIsCalled(_fileConfig))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_call_setter_when_gets_new_config()
|
||||
{
|
||||
|
||||
var newConfig = new FileConfiguration {
|
||||
ReRoutes = new List<FileReRoute>
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamHostAndPorts = new List<FileHostAndPort>
|
||||
{
|
||||
new FileHostAndPort
|
||||
{
|
||||
Host = "test"
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
this.Given(x => WhenTheConfigIsChangedInConsul(newConfig))
|
||||
.Then(x => ThenTheSetterIsCalled(newConfig))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void WhenTheConfigIsChangedInConsul(FileConfiguration newConfig)
|
||||
{
|
||||
_repo.Setup(x => x.Get()).ReturnsAsync(new OkResponse<FileConfiguration>(newConfig));
|
||||
}
|
||||
|
||||
private void ThenTheSetterIsCalled(FileConfiguration fileConfig)
|
||||
{
|
||||
var result = WaitFor(2000).Until(() => {
|
||||
try
|
||||
{
|
||||
_setter.Verify(x => x.Set(fileConfig), Times.Once);
|
||||
return true;
|
||||
}
|
||||
catch(Exception ex)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
});
|
||||
result.ShouldBeTrue();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,122 @@
|
||||
using System.Collections.Generic;
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.Configuration.Builder;
|
||||
using Ocelot.Configuration.Creator;
|
||||
using Ocelot.Configuration.File;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests.Configuration
|
||||
{
|
||||
public class DownstreamAddressesCreatorTests
|
||||
{
|
||||
public DownstreamAddressesCreator _creator;
|
||||
private FileReRoute _reRoute;
|
||||
private List<DownstreamHostAndPort> _result;
|
||||
|
||||
public DownstreamAddressesCreatorTests()
|
||||
{
|
||||
_creator = new DownstreamAddressesCreator();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_do_nothing()
|
||||
{
|
||||
var reRoute = new FileReRoute
|
||||
{
|
||||
};
|
||||
|
||||
var expected = new List<DownstreamHostAndPort>
|
||||
{
|
||||
};
|
||||
|
||||
this.Given(x => GivenTheFollowingReRoute(reRoute))
|
||||
.When(x => WhenICreate())
|
||||
.Then(x => TheThenFollowingIsReturned(expected))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_create_downstream_addresses_from_old_downstream_path_and_port()
|
||||
{
|
||||
var reRoute = new FileReRoute
|
||||
{
|
||||
DownstreamHostAndPorts = new List<FileHostAndPort>
|
||||
{
|
||||
new FileHostAndPort
|
||||
{
|
||||
Host = "test",
|
||||
Port = 80
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
var expected = new List<DownstreamHostAndPort>
|
||||
{
|
||||
new DownstreamHostAndPort("test", 80),
|
||||
};
|
||||
|
||||
this.Given(x => GivenTheFollowingReRoute(reRoute))
|
||||
.When(x => WhenICreate())
|
||||
.Then(x => TheThenFollowingIsReturned(expected))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_create_downstream_addresses_from_downstream_host_and_ports()
|
||||
{
|
||||
var reRoute = new FileReRoute
|
||||
{
|
||||
DownstreamHostAndPorts = new List<FileHostAndPort>
|
||||
{
|
||||
new FileHostAndPort
|
||||
{
|
||||
Host = "test",
|
||||
Port = 80
|
||||
},
|
||||
new FileHostAndPort
|
||||
{
|
||||
Host = "west",
|
||||
Port = 443
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
var expected = new List<DownstreamHostAndPort>
|
||||
{
|
||||
new DownstreamHostAndPort("test", 80),
|
||||
new DownstreamHostAndPort("west", 443)
|
||||
};
|
||||
|
||||
this.Given(x => GivenTheFollowingReRoute(reRoute))
|
||||
.When(x => WhenICreate())
|
||||
.Then(x => TheThenFollowingIsReturned(expected))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenTheFollowingReRoute(FileReRoute reRoute)
|
||||
{
|
||||
_reRoute = reRoute;
|
||||
}
|
||||
|
||||
private void WhenICreate()
|
||||
{
|
||||
_result = _creator.Create(_reRoute);
|
||||
}
|
||||
|
||||
private void TheThenFollowingIsReturned(List<DownstreamHostAndPort> expecteds)
|
||||
{
|
||||
_result.Count.ShouldBe(expecteds.Count);
|
||||
|
||||
for (int i = 0; i < _result.Count; i++)
|
||||
{
|
||||
var result = _result[i];
|
||||
var expected = expecteds[i];
|
||||
|
||||
result.Host.ShouldBe(expected.Host);
|
||||
result.Port.ShouldBe(expected.Port);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -15,6 +15,7 @@ using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests.Configuration
|
||||
{
|
||||
using System;
|
||||
using Ocelot.DependencyInjection;
|
||||
using Ocelot.Errors;
|
||||
using Ocelot.UnitTests.TestData;
|
||||
@ -40,6 +41,7 @@ namespace Ocelot.UnitTests.Configuration
|
||||
private Mock<IHttpHandlerOptionsCreator> _httpHandlerOptionsCreator;
|
||||
private Mock<IAdministrationPath> _adminPath;
|
||||
private readonly Mock<IHeaderFindAndReplaceCreator> _headerFindAndReplaceCreator;
|
||||
private readonly Mock<IDownstreamAddressesCreator> _downstreamAddressesCreator;
|
||||
|
||||
public FileConfigurationCreatorTests()
|
||||
{
|
||||
@ -58,6 +60,7 @@ namespace Ocelot.UnitTests.Configuration
|
||||
_httpHandlerOptionsCreator = new Mock<IHttpHandlerOptionsCreator>();
|
||||
_adminPath = new Mock<IAdministrationPath>();
|
||||
_headerFindAndReplaceCreator = new Mock<IHeaderFindAndReplaceCreator>();
|
||||
_downstreamAddressesCreator = new Mock<IDownstreamAddressesCreator>();
|
||||
|
||||
_ocelotConfigurationCreator = new FileOcelotConfigurationCreator(
|
||||
_fileConfig.Object,
|
||||
@ -74,7 +77,8 @@ namespace Ocelot.UnitTests.Configuration
|
||||
_regionCreator.Object,
|
||||
_httpHandlerOptionsCreator.Object,
|
||||
_adminPath.Object,
|
||||
_headerFindAndReplaceCreator.Object);
|
||||
_headerFindAndReplaceCreator.Object,
|
||||
_downstreamAddressesCreator.Object);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@ -94,6 +98,7 @@ namespace Ocelot.UnitTests.Configuration
|
||||
}
|
||||
}))
|
||||
.And(x => x.GivenTheFollowingIsReturned(serviceProviderConfig))
|
||||
.And(x => GivenTheDownstreamAddresses())
|
||||
.And(x => GivenTheHeaderFindAndReplaceCreatorReturns())
|
||||
.And(x => x.GivenTheConfigIsValid())
|
||||
.When(x => x.WhenICreateTheConfig())
|
||||
@ -113,7 +118,13 @@ namespace Ocelot.UnitTests.Configuration
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamHost = "127.0.0.1",
|
||||
DownstreamHostAndPorts = new List<FileHostAndPort>
|
||||
{
|
||||
new FileHostAndPort
|
||||
{
|
||||
Host = "127.0.0.1",
|
||||
}
|
||||
},
|
||||
UpstreamPathTemplate = "/api/products/{productId}",
|
||||
DownstreamPathTemplate = "/products/{productId}",
|
||||
UpstreamHttpMethod = new List<string> { "Get" },
|
||||
@ -125,6 +136,7 @@ namespace Ocelot.UnitTests.Configuration
|
||||
},
|
||||
}))
|
||||
.And(x => x.GivenTheFollowingOptionsAreReturned(reRouteOptions))
|
||||
.And(x => GivenTheDownstreamAddresses())
|
||||
.And(x => GivenTheHeaderFindAndReplaceCreatorReturns())
|
||||
.And(x => x.GivenTheConfigIsValid())
|
||||
.And(x => x.GivenTheFollowingRegionIsReturned("region"))
|
||||
@ -146,7 +158,13 @@ namespace Ocelot.UnitTests.Configuration
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamHost = "127.0.0.1",
|
||||
DownstreamHostAndPorts = new List<FileHostAndPort>
|
||||
{
|
||||
new FileHostAndPort
|
||||
{
|
||||
Host = "127.0.0.1",
|
||||
}
|
||||
},
|
||||
UpstreamPathTemplate = "/api/products/{productId}",
|
||||
DownstreamPathTemplate = "/products/{productId}",
|
||||
UpstreamHttpMethod = new List<string> { "Get" },
|
||||
@ -154,6 +172,7 @@ namespace Ocelot.UnitTests.Configuration
|
||||
},
|
||||
}))
|
||||
.And(x => x.GivenTheConfigIsValid())
|
||||
.And(x => GivenTheDownstreamAddresses())
|
||||
.And(x => GivenTheHeaderFindAndReplaceCreatorReturns())
|
||||
.And(x => x.GivenTheFollowingOptionsAreReturned(reRouteOptions))
|
||||
.When(x => x.WhenICreateTheConfig())
|
||||
@ -180,7 +199,13 @@ namespace Ocelot.UnitTests.Configuration
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamHost = "127.0.0.1",
|
||||
DownstreamHostAndPorts = new List<FileHostAndPort>
|
||||
{
|
||||
new FileHostAndPort
|
||||
{
|
||||
Host = "127.0.0.1",
|
||||
}
|
||||
},
|
||||
UpstreamPathTemplate = "/api/products/{productId}",
|
||||
DownstreamPathTemplate = "/products/{productId}",
|
||||
UpstreamHttpMethod = new List<string> { "Get" },
|
||||
@ -194,6 +219,7 @@ namespace Ocelot.UnitTests.Configuration
|
||||
},
|
||||
}))
|
||||
.And(x => x.GivenTheConfigIsValid())
|
||||
.And(x => GivenTheDownstreamAddresses())
|
||||
.And(x => GivenTheHeaderFindAndReplaceCreatorReturns())
|
||||
.And(x => x.GivenTheFollowingOptionsAreReturned(serviceOptions))
|
||||
.And(x => x.GivenTheQosOptionsCreatorReturns(expected))
|
||||
@ -214,7 +240,13 @@ namespace Ocelot.UnitTests.Configuration
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamHost = "127.0.0.1",
|
||||
DownstreamHostAndPorts = new List<FileHostAndPort>
|
||||
{
|
||||
new FileHostAndPort
|
||||
{
|
||||
Host = "127.0.0.1",
|
||||
}
|
||||
},
|
||||
UpstreamPathTemplate = "/api/products/{productId}",
|
||||
DownstreamPathTemplate = "/products/{productId}",
|
||||
UpstreamHttpMethod = new List<string> { "Get" },
|
||||
@ -222,13 +254,14 @@ namespace Ocelot.UnitTests.Configuration
|
||||
},
|
||||
}))
|
||||
.And(x => x.GivenTheConfigIsValid())
|
||||
.And(x => GivenTheDownstreamAddresses())
|
||||
.And(x => GivenTheHeaderFindAndReplaceCreatorReturns())
|
||||
.And(x => x.GivenTheFollowingOptionsAreReturned(reRouteOptions))
|
||||
.When(x => x.WhenICreateTheConfig())
|
||||
.Then(x => x.ThenTheReRoutesAre(new List<ReRoute>
|
||||
{
|
||||
new ReRouteBuilder()
|
||||
.WithDownstreamHost("127.0.0.1")
|
||||
.WithDownstreamAddresses(new List<DownstreamHostAndPort>(){new DownstreamHostAndPort("127.0.0.1", 80) })
|
||||
.WithDownstreamPathTemplate("/products/{productId}")
|
||||
.WithUpstreamPathTemplate("/api/products/{productId}")
|
||||
.WithUpstreamHttpMethod(new List<string> { "Get" })
|
||||
@ -257,6 +290,7 @@ namespace Ocelot.UnitTests.Configuration
|
||||
},
|
||||
}))
|
||||
.And(x => x.GivenTheConfigIsValid())
|
||||
.And(x => GivenTheDownstreamAddresses())
|
||||
.And(x => GivenTheHeaderFindAndReplaceCreatorReturns())
|
||||
.And(x => x.GivenTheFollowingOptionsAreReturned(reRouteOptions))
|
||||
.When(x => x.WhenICreateTheConfig())
|
||||
@ -300,6 +334,7 @@ namespace Ocelot.UnitTests.Configuration
|
||||
}
|
||||
}))
|
||||
.And(x => x.GivenTheConfigIsValid())
|
||||
.And(x => GivenTheDownstreamAddresses())
|
||||
.And(x => GivenTheHeaderFindAndReplaceCreatorReturns())
|
||||
.And(x => x.GivenTheFollowingOptionsAreReturned(reRouteOptions))
|
||||
.When(x => x.WhenICreateTheConfig())
|
||||
@ -336,6 +371,7 @@ namespace Ocelot.UnitTests.Configuration
|
||||
}
|
||||
}))
|
||||
.And(x => x.GivenTheConfigIsValid())
|
||||
.And(x => GivenTheDownstreamAddresses())
|
||||
.And(x => GivenTheHeaderFindAndReplaceCreatorReturns())
|
||||
.And(x => x.GivenTheFollowingOptionsAreReturned(reRouteOptions))
|
||||
.When(x => x.WhenICreateTheConfig())
|
||||
@ -371,6 +407,7 @@ namespace Ocelot.UnitTests.Configuration
|
||||
}
|
||||
}))
|
||||
.And(x => x.GivenTheConfigIsValid())
|
||||
.And(x => GivenTheDownstreamAddresses())
|
||||
.And(x => GivenTheHeaderFindAndReplaceCreatorReturns())
|
||||
.And(x => x.GivenTheFollowingOptionsAreReturned(reRouteOptions))
|
||||
.And(x => x.GivenTheUpstreamTemplatePatternCreatorReturns("(?i)/api/products/.*/$"))
|
||||
@ -411,6 +448,7 @@ namespace Ocelot.UnitTests.Configuration
|
||||
}
|
||||
}))
|
||||
.And(x => x.GivenTheConfigIsValid())
|
||||
.And(x => GivenTheDownstreamAddresses())
|
||||
.And(x => GivenTheHeaderFindAndReplaceCreatorReturns())
|
||||
.And(x => x.GivenTheFollowingOptionsAreReturned(reRouteOptions))
|
||||
.And(x => x.GivenTheRequestIdCreatorReturns("blahhhh"))
|
||||
@ -441,7 +479,13 @@ namespace Ocelot.UnitTests.Configuration
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamHost = "127.0.0.1",
|
||||
DownstreamHostAndPorts = new List<FileHostAndPort>
|
||||
{
|
||||
new FileHostAndPort
|
||||
{
|
||||
Host = "127.0.0.1",
|
||||
}
|
||||
},
|
||||
UpstreamPathTemplate = "/api/products/{productId}",
|
||||
DownstreamPathTemplate = "/products/{productId}",
|
||||
UpstreamHttpMethod = new List<string> { "Get" }
|
||||
@ -449,6 +493,7 @@ namespace Ocelot.UnitTests.Configuration
|
||||
},
|
||||
}))
|
||||
.And(x => x.GivenTheFollowingOptionsAreReturned(reRouteOptions))
|
||||
.And(x => GivenTheDownstreamAddresses())
|
||||
.And(x => GivenTheHeaderFindAndReplaceCreatorReturns())
|
||||
.And(x => x.GivenTheConfigIsValid())
|
||||
.And(x => x.GivenTheFollowingHttpHandlerOptionsAreReturned(httpHandlerOptions))
|
||||
@ -484,6 +529,7 @@ namespace Ocelot.UnitTests.Configuration
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenTheConfigIs(fileConfig))
|
||||
.And(x => GivenTheDownstreamAddresses())
|
||||
.And(x => x.GivenTheConfigIsValid())
|
||||
.And(x => GivenTheHeaderFindAndReplaceCreatorReturns())
|
||||
.And(x => x.GivenTheAuthOptionsCreatorReturns(authenticationOptions))
|
||||
@ -519,6 +565,7 @@ namespace Ocelot.UnitTests.Configuration
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenTheConfigIs(fileConfig))
|
||||
.And(x => GivenTheDownstreamAddresses())
|
||||
.And(x => x.GivenTheConfigIsValid())
|
||||
.And(x => GivenTheHeaderFindAndReplaceCreatorReturns())
|
||||
.And(x => x.GivenTheFollowingOptionsAreReturned(reRouteOptions))
|
||||
@ -536,6 +583,7 @@ namespace Ocelot.UnitTests.Configuration
|
||||
var errors = new List<Error> {new FileValidationFailedError("some message")};
|
||||
|
||||
this.Given(x => x.GivenTheConfigIs(new FileConfiguration()))
|
||||
.And(x => GivenTheDownstreamAddresses())
|
||||
.And(x => x.GivenTheConfigIsInvalid(errors))
|
||||
.When(x => x.WhenICreateTheConfig())
|
||||
.Then(x => x.ThenTheErrorsAreReturned(errors))
|
||||
@ -719,5 +767,10 @@ namespace Ocelot.UnitTests.Configuration
|
||||
{
|
||||
_httpHandlerOptionsCreator.Verify(x => x.Create(_fileConfiguration.ReRoutes[0]), Times.Once());
|
||||
}
|
||||
|
||||
private void GivenTheDownstreamAddresses()
|
||||
{
|
||||
_downstreamAddressesCreator.Setup(x => x.Create(It.IsAny<FileReRoute>())).Returns(new List<DownstreamHostAndPort>());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,62 +1,62 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Moq;
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.Configuration.File;
|
||||
using Ocelot.Responses;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
using Newtonsoft.Json;
|
||||
using System.IO;
|
||||
using Ocelot.Configuration.Provider;
|
||||
using Ocelot.Configuration.Repository;
|
||||
|
||||
namespace Ocelot.UnitTests.Configuration
|
||||
{
|
||||
public class FileConfigurationProviderTests
|
||||
{
|
||||
private readonly IFileConfigurationProvider _provider;
|
||||
private Mock<IFileConfigurationRepository> _repo;
|
||||
private FileConfiguration _result;
|
||||
private FileConfiguration _fileConfiguration;
|
||||
|
||||
public FileConfigurationProviderTests()
|
||||
{
|
||||
_repo = new Mock<IFileConfigurationRepository>();
|
||||
_provider = new FileConfigurationProvider(_repo.Object);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_file_configuration()
|
||||
{
|
||||
var config = new FileConfiguration();
|
||||
|
||||
this.Given(x => x.GivenTheConfigurationIs(config))
|
||||
.When(x => x.WhenIGetTheReRoutes())
|
||||
.Then(x => x.ThenTheRepoIsCalledCorrectly())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
|
||||
|
||||
private void GivenTheConfigurationIs(FileConfiguration fileConfiguration)
|
||||
{
|
||||
_fileConfiguration = fileConfiguration;
|
||||
_repo
|
||||
.Setup(x => x.Get())
|
||||
.ReturnsAsync(new OkResponse<FileConfiguration>(fileConfiguration));
|
||||
}
|
||||
|
||||
private void WhenIGetTheReRoutes()
|
||||
{
|
||||
_result = _provider.Get().Result.Data;
|
||||
}
|
||||
|
||||
private void ThenTheRepoIsCalledCorrectly()
|
||||
{
|
||||
_repo
|
||||
.Verify(x => x.Get(), Times.Once);
|
||||
}
|
||||
}
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Moq;
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.Configuration.File;
|
||||
using Ocelot.Responses;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
using Newtonsoft.Json;
|
||||
using System.IO;
|
||||
using Ocelot.Configuration.Provider;
|
||||
using Ocelot.Configuration.Repository;
|
||||
|
||||
namespace Ocelot.UnitTests.Configuration
|
||||
{
|
||||
public class FileConfigurationProviderTests
|
||||
{
|
||||
private readonly IFileConfigurationProvider _provider;
|
||||
private Mock<IFileConfigurationRepository> _repo;
|
||||
private FileConfiguration _result;
|
||||
private FileConfiguration _fileConfiguration;
|
||||
|
||||
public FileConfigurationProviderTests()
|
||||
{
|
||||
_repo = new Mock<IFileConfigurationRepository>();
|
||||
_provider = new FileConfigurationProvider(_repo.Object);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_file_configuration()
|
||||
{
|
||||
var config = new FileConfiguration();
|
||||
|
||||
this.Given(x => x.GivenTheConfigurationIs(config))
|
||||
.When(x => x.WhenIGetTheReRoutes())
|
||||
.Then(x => x.ThenTheRepoIsCalledCorrectly())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
|
||||
|
||||
private void GivenTheConfigurationIs(FileConfiguration fileConfiguration)
|
||||
{
|
||||
_fileConfiguration = fileConfiguration;
|
||||
_repo
|
||||
.Setup(x => x.Get())
|
||||
.ReturnsAsync(new OkResponse<FileConfiguration>(fileConfiguration));
|
||||
}
|
||||
|
||||
private void WhenIGetTheReRoutes()
|
||||
{
|
||||
_result = _provider.Get().Result.Data;
|
||||
}
|
||||
|
||||
private void ThenTheRepoIsCalledCorrectly()
|
||||
{
|
||||
_repo
|
||||
.Verify(x => x.Get(), Times.Once);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,199 +1,225 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Moq;
|
||||
using Ocelot.Configuration.File;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
using Newtonsoft.Json;
|
||||
using System.IO;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Ocelot.Configuration.Repository;
|
||||
|
||||
namespace Ocelot.UnitTests.Configuration
|
||||
{
|
||||
public class FileConfigurationRepositoryTests
|
||||
{
|
||||
private readonly Mock<IHostingEnvironment> _hostingEnvironment = new Mock<IHostingEnvironment>();
|
||||
private IFileConfigurationRepository _repo;
|
||||
private FileConfiguration _result;
|
||||
private FileConfiguration _fileConfiguration;
|
||||
private string _environmentName = "DEV";
|
||||
|
||||
public FileConfigurationRepositoryTests()
|
||||
{
|
||||
_hostingEnvironment.Setup(he => he.EnvironmentName).Returns(_environmentName);
|
||||
_repo = new FileConfigurationRepository(_hostingEnvironment.Object);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_file_configuration()
|
||||
{
|
||||
var config = FakeFileConfigurationForGet();
|
||||
|
||||
this.Given(x => x.GivenTheConfigurationIs(config))
|
||||
.When(x => x.WhenIGetTheReRoutes())
|
||||
.Then(x => x.ThenTheFollowingIsReturned(config))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_file_configuration_if_environment_name_is_unavailable()
|
||||
{
|
||||
var config = FakeFileConfigurationForGet();
|
||||
|
||||
this.Given(x => x.GivenTheEnvironmentNameIsUnavailable())
|
||||
.And(x => x.GivenTheConfigurationIs(config))
|
||||
.When(x => x.WhenIGetTheReRoutes())
|
||||
.Then(x => x.ThenTheFollowingIsReturned(config))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_set_file_configuration()
|
||||
{
|
||||
var config = FakeFileConfigurationForSet();
|
||||
|
||||
this.Given(x => GivenIHaveAConfiguration(config))
|
||||
.When(x => WhenISetTheConfiguration())
|
||||
.Then(x => ThenTheConfigurationIsStoredAs(config))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_set_file_configuration_if_environment_name_is_unavailable()
|
||||
{
|
||||
var config = FakeFileConfigurationForSet();
|
||||
this.Given(x => GivenIHaveAConfiguration(config))
|
||||
.And(x => GivenTheEnvironmentNameIsUnavailable())
|
||||
.When(x => WhenISetTheConfiguration())
|
||||
.Then(x => ThenTheConfigurationIsStoredAs(config))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenTheEnvironmentNameIsUnavailable()
|
||||
{
|
||||
_environmentName = null;
|
||||
_hostingEnvironment.Setup(he => he.EnvironmentName).Returns(_environmentName);
|
||||
_repo = new FileConfigurationRepository(_hostingEnvironment.Object);
|
||||
}
|
||||
|
||||
private void GivenIHaveAConfiguration(FileConfiguration fileConfiguration)
|
||||
{
|
||||
_fileConfiguration = fileConfiguration;
|
||||
}
|
||||
|
||||
private void WhenISetTheConfiguration()
|
||||
{
|
||||
_repo.Set(_fileConfiguration);
|
||||
_result = _repo.Get().Result.Data;
|
||||
}
|
||||
|
||||
private void ThenTheConfigurationIsStoredAs(FileConfiguration expected)
|
||||
{
|
||||
_result.GlobalConfiguration.RequestIdKey.ShouldBe(expected.GlobalConfiguration.RequestIdKey);
|
||||
_result.GlobalConfiguration.ServiceDiscoveryProvider.Host.ShouldBe(expected.GlobalConfiguration.ServiceDiscoveryProvider.Host);
|
||||
_result.GlobalConfiguration.ServiceDiscoveryProvider.Port.ShouldBe(expected.GlobalConfiguration.ServiceDiscoveryProvider.Port);
|
||||
|
||||
for(var i = 0; i < _result.ReRoutes.Count; i++)
|
||||
{
|
||||
_result.ReRoutes[i].DownstreamHost.ShouldBe(expected.ReRoutes[i].DownstreamHost);
|
||||
_result.ReRoutes[i].DownstreamPathTemplate.ShouldBe(expected.ReRoutes[i].DownstreamPathTemplate);
|
||||
_result.ReRoutes[i].DownstreamPort.ShouldBe(expected.ReRoutes[i].DownstreamPort);
|
||||
_result.ReRoutes[i].DownstreamScheme.ShouldBe(expected.ReRoutes[i].DownstreamScheme);
|
||||
}
|
||||
}
|
||||
|
||||
private void GivenTheConfigurationIs(FileConfiguration fileConfiguration)
|
||||
{
|
||||
var configurationPath = $"{AppContext.BaseDirectory}/configuration{(string.IsNullOrEmpty(_environmentName) ? string.Empty : ".")}{_environmentName}.json";
|
||||
|
||||
var jsonConfiguration = JsonConvert.SerializeObject(fileConfiguration);
|
||||
|
||||
if (File.Exists(configurationPath))
|
||||
{
|
||||
File.Delete(configurationPath);
|
||||
}
|
||||
|
||||
File.WriteAllText(configurationPath, jsonConfiguration);
|
||||
}
|
||||
|
||||
private void WhenIGetTheReRoutes()
|
||||
{
|
||||
_result = _repo.Get().Result.Data;
|
||||
}
|
||||
|
||||
private void ThenTheFollowingIsReturned(FileConfiguration expected)
|
||||
{
|
||||
_result.GlobalConfiguration.RequestIdKey.ShouldBe(expected.GlobalConfiguration.RequestIdKey);
|
||||
_result.GlobalConfiguration.ServiceDiscoveryProvider.Host.ShouldBe(expected.GlobalConfiguration.ServiceDiscoveryProvider.Host);
|
||||
_result.GlobalConfiguration.ServiceDiscoveryProvider.Port.ShouldBe(expected.GlobalConfiguration.ServiceDiscoveryProvider.Port);
|
||||
|
||||
for(var i = 0; i < _result.ReRoutes.Count; i++)
|
||||
{
|
||||
_result.ReRoutes[i].DownstreamHost.ShouldBe(expected.ReRoutes[i].DownstreamHost);
|
||||
_result.ReRoutes[i].DownstreamPathTemplate.ShouldBe(expected.ReRoutes[i].DownstreamPathTemplate);
|
||||
_result.ReRoutes[i].DownstreamPort.ShouldBe(expected.ReRoutes[i].DownstreamPort);
|
||||
_result.ReRoutes[i].DownstreamScheme.ShouldBe(expected.ReRoutes[i].DownstreamScheme);
|
||||
}
|
||||
}
|
||||
|
||||
private FileConfiguration FakeFileConfigurationForSet()
|
||||
{
|
||||
var reRoutes = new List<FileReRoute>
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamHost = "123.12.12.12",
|
||||
DownstreamPort = 80,
|
||||
DownstreamScheme = "https",
|
||||
DownstreamPathTemplate = "/asdfs/test/{test}"
|
||||
}
|
||||
};
|
||||
|
||||
var globalConfiguration = new FileGlobalConfiguration
|
||||
{
|
||||
ServiceDiscoveryProvider = new FileServiceDiscoveryProvider
|
||||
{
|
||||
Port = 198,
|
||||
Host = "blah"
|
||||
}
|
||||
};
|
||||
|
||||
return new FileConfiguration
|
||||
{
|
||||
GlobalConfiguration = globalConfiguration,
|
||||
ReRoutes = reRoutes
|
||||
};
|
||||
}
|
||||
|
||||
private FileConfiguration FakeFileConfigurationForGet()
|
||||
{
|
||||
var reRoutes = new List<FileReRoute>
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamHost = "localhost",
|
||||
DownstreamPort = 80,
|
||||
DownstreamScheme = "https",
|
||||
DownstreamPathTemplate = "/test/test/{test}"
|
||||
}
|
||||
};
|
||||
|
||||
var globalConfiguration = new FileGlobalConfiguration
|
||||
{
|
||||
ServiceDiscoveryProvider = new FileServiceDiscoveryProvider
|
||||
{
|
||||
Port = 198,
|
||||
Host = "blah"
|
||||
}
|
||||
};
|
||||
|
||||
return new FileConfiguration
|
||||
{
|
||||
GlobalConfiguration = globalConfiguration,
|
||||
ReRoutes = reRoutes
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Moq;
|
||||
using Ocelot.Configuration.File;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
using Newtonsoft.Json;
|
||||
using System.IO;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Ocelot.Configuration.Repository;
|
||||
|
||||
namespace Ocelot.UnitTests.Configuration
|
||||
{
|
||||
public class FileConfigurationRepositoryTests
|
||||
{
|
||||
private readonly Mock<IHostingEnvironment> _hostingEnvironment = new Mock<IHostingEnvironment>();
|
||||
private IFileConfigurationRepository _repo;
|
||||
private FileConfiguration _result;
|
||||
private FileConfiguration _fileConfiguration;
|
||||
private string _environmentName = "DEV";
|
||||
|
||||
public FileConfigurationRepositoryTests()
|
||||
{
|
||||
_hostingEnvironment.Setup(he => he.EnvironmentName).Returns(_environmentName);
|
||||
_repo = new FileConfigurationRepository(_hostingEnvironment.Object);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_file_configuration()
|
||||
{
|
||||
var config = FakeFileConfigurationForGet();
|
||||
|
||||
this.Given(x => x.GivenTheConfigurationIs(config))
|
||||
.When(x => x.WhenIGetTheReRoutes())
|
||||
.Then(x => x.ThenTheFollowingIsReturned(config))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_file_configuration_if_environment_name_is_unavailable()
|
||||
{
|
||||
var config = FakeFileConfigurationForGet();
|
||||
|
||||
this.Given(x => x.GivenTheEnvironmentNameIsUnavailable())
|
||||
.And(x => x.GivenTheConfigurationIs(config))
|
||||
.When(x => x.WhenIGetTheReRoutes())
|
||||
.Then(x => x.ThenTheFollowingIsReturned(config))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_set_file_configuration()
|
||||
{
|
||||
var config = FakeFileConfigurationForSet();
|
||||
|
||||
this.Given(x => GivenIHaveAConfiguration(config))
|
||||
.When(x => WhenISetTheConfiguration())
|
||||
.Then(x => ThenTheConfigurationIsStoredAs(config))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_set_file_configuration_if_environment_name_is_unavailable()
|
||||
{
|
||||
var config = FakeFileConfigurationForSet();
|
||||
this.Given(x => GivenIHaveAConfiguration(config))
|
||||
.And(x => GivenTheEnvironmentNameIsUnavailable())
|
||||
.When(x => WhenISetTheConfiguration())
|
||||
.Then(x => ThenTheConfigurationIsStoredAs(config))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenTheEnvironmentNameIsUnavailable()
|
||||
{
|
||||
_environmentName = null;
|
||||
_hostingEnvironment.Setup(he => he.EnvironmentName).Returns(_environmentName);
|
||||
_repo = new FileConfigurationRepository(_hostingEnvironment.Object);
|
||||
}
|
||||
|
||||
private void GivenIHaveAConfiguration(FileConfiguration fileConfiguration)
|
||||
{
|
||||
_fileConfiguration = fileConfiguration;
|
||||
}
|
||||
|
||||
private void WhenISetTheConfiguration()
|
||||
{
|
||||
_repo.Set(_fileConfiguration);
|
||||
_result = _repo.Get().Result.Data;
|
||||
}
|
||||
|
||||
private void ThenTheConfigurationIsStoredAs(FileConfiguration expecteds)
|
||||
{
|
||||
_result.GlobalConfiguration.RequestIdKey.ShouldBe(expecteds.GlobalConfiguration.RequestIdKey);
|
||||
_result.GlobalConfiguration.ServiceDiscoveryProvider.Host.ShouldBe(expecteds.GlobalConfiguration.ServiceDiscoveryProvider.Host);
|
||||
_result.GlobalConfiguration.ServiceDiscoveryProvider.Port.ShouldBe(expecteds.GlobalConfiguration.ServiceDiscoveryProvider.Port);
|
||||
|
||||
for(var i = 0; i < _result.ReRoutes.Count; i++)
|
||||
{
|
||||
for (int j = 0; j < _result.ReRoutes[i].DownstreamHostAndPorts.Count; j++)
|
||||
{
|
||||
var result = _result.ReRoutes[i].DownstreamHostAndPorts[j];
|
||||
var expected = expecteds.ReRoutes[i].DownstreamHostAndPorts[j];
|
||||
|
||||
result.Host.ShouldBe(expected.Host);
|
||||
result.Port.ShouldBe(expected.Port);
|
||||
}
|
||||
|
||||
_result.ReRoutes[i].DownstreamPathTemplate.ShouldBe(expecteds.ReRoutes[i].DownstreamPathTemplate);
|
||||
_result.ReRoutes[i].DownstreamScheme.ShouldBe(expecteds.ReRoutes[i].DownstreamScheme);
|
||||
}
|
||||
}
|
||||
|
||||
private void GivenTheConfigurationIs(FileConfiguration fileConfiguration)
|
||||
{
|
||||
var configurationPath = $"{AppContext.BaseDirectory}/configuration{(string.IsNullOrEmpty(_environmentName) ? string.Empty : ".")}{_environmentName}.json";
|
||||
|
||||
var jsonConfiguration = JsonConvert.SerializeObject(fileConfiguration);
|
||||
|
||||
if (File.Exists(configurationPath))
|
||||
{
|
||||
File.Delete(configurationPath);
|
||||
}
|
||||
|
||||
File.WriteAllText(configurationPath, jsonConfiguration);
|
||||
}
|
||||
|
||||
private void WhenIGetTheReRoutes()
|
||||
{
|
||||
_result = _repo.Get().Result.Data;
|
||||
}
|
||||
|
||||
private void ThenTheFollowingIsReturned(FileConfiguration expecteds)
|
||||
{
|
||||
_result.GlobalConfiguration.RequestIdKey.ShouldBe(expecteds.GlobalConfiguration.RequestIdKey);
|
||||
_result.GlobalConfiguration.ServiceDiscoveryProvider.Host.ShouldBe(expecteds.GlobalConfiguration.ServiceDiscoveryProvider.Host);
|
||||
_result.GlobalConfiguration.ServiceDiscoveryProvider.Port.ShouldBe(expecteds.GlobalConfiguration.ServiceDiscoveryProvider.Port);
|
||||
|
||||
for(var i = 0; i < _result.ReRoutes.Count; i++)
|
||||
{
|
||||
for (int j = 0; j < _result.ReRoutes[i].DownstreamHostAndPorts.Count; j++)
|
||||
{
|
||||
var result = _result.ReRoutes[i].DownstreamHostAndPorts[j];
|
||||
var expected = expecteds.ReRoutes[i].DownstreamHostAndPorts[j];
|
||||
|
||||
result.Host.ShouldBe(expected.Host);
|
||||
result.Port.ShouldBe(expected.Port);
|
||||
}
|
||||
|
||||
_result.ReRoutes[i].DownstreamPathTemplate.ShouldBe(expecteds.ReRoutes[i].DownstreamPathTemplate);
|
||||
_result.ReRoutes[i].DownstreamScheme.ShouldBe(expecteds.ReRoutes[i].DownstreamScheme);
|
||||
}
|
||||
}
|
||||
|
||||
private FileConfiguration FakeFileConfigurationForSet()
|
||||
{
|
||||
var reRoutes = new List<FileReRoute>
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamHostAndPorts = new List<FileHostAndPort>
|
||||
{
|
||||
new FileHostAndPort
|
||||
{
|
||||
Host = "123.12.12.12",
|
||||
Port = 80,
|
||||
}
|
||||
},
|
||||
DownstreamScheme = "https",
|
||||
DownstreamPathTemplate = "/asdfs/test/{test}"
|
||||
}
|
||||
};
|
||||
|
||||
var globalConfiguration = new FileGlobalConfiguration
|
||||
{
|
||||
ServiceDiscoveryProvider = new FileServiceDiscoveryProvider
|
||||
{
|
||||
Port = 198,
|
||||
Host = "blah"
|
||||
}
|
||||
};
|
||||
|
||||
return new FileConfiguration
|
||||
{
|
||||
GlobalConfiguration = globalConfiguration,
|
||||
ReRoutes = reRoutes
|
||||
};
|
||||
}
|
||||
|
||||
private FileConfiguration FakeFileConfigurationForGet()
|
||||
{
|
||||
var reRoutes = new List<FileReRoute>
|
||||
{
|
||||
new FileReRoute
|
||||
{
|
||||
DownstreamHostAndPorts = new List<FileHostAndPort>
|
||||
{
|
||||
new FileHostAndPort
|
||||
{
|
||||
Host = "localhost",
|
||||
Port = 80,
|
||||
}
|
||||
},
|
||||
DownstreamScheme = "https",
|
||||
DownstreamPathTemplate = "/test/test/{test}"
|
||||
}
|
||||
};
|
||||
|
||||
var globalConfiguration = new FileGlobalConfiguration
|
||||
{
|
||||
ServiceDiscoveryProvider = new FileServiceDiscoveryProvider
|
||||
{
|
||||
Port = 198,
|
||||
Host = "blah"
|
||||
}
|
||||
};
|
||||
|
||||
return new FileConfiguration
|
||||
{
|
||||
GlobalConfiguration = globalConfiguration,
|
||||
ReRoutes = reRoutes
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,113 +1,113 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Moq;
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.Configuration.Builder;
|
||||
using Ocelot.Configuration.Creator;
|
||||
using Ocelot.Configuration.File;
|
||||
using Ocelot.Configuration.Repository;
|
||||
using Ocelot.Configuration.Setter;
|
||||
using Ocelot.Errors;
|
||||
using Ocelot.Responses;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests.Configuration
|
||||
{
|
||||
public class FileConfigurationSetterTests
|
||||
{
|
||||
private FileConfiguration _fileConfiguration;
|
||||
private FileConfigurationSetter _configSetter;
|
||||
private Mock<IOcelotConfigurationRepository> _configRepo;
|
||||
private Mock<IOcelotConfigurationCreator> _configCreator;
|
||||
private Response<IOcelotConfiguration> _configuration;
|
||||
private object _result;
|
||||
private Mock<IFileConfigurationRepository> _repo;
|
||||
|
||||
public FileConfigurationSetterTests()
|
||||
{
|
||||
_repo = new Mock<IFileConfigurationRepository>();
|
||||
_configRepo = new Mock<IOcelotConfigurationRepository>();
|
||||
_configCreator = new Mock<IOcelotConfigurationCreator>();
|
||||
_configSetter = new FileConfigurationSetter(_configRepo.Object, _configCreator.Object, _repo.Object);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_set_configuration()
|
||||
{
|
||||
var fileConfig = new FileConfiguration();
|
||||
var serviceProviderConfig = new ServiceProviderConfigurationBuilder().Build();
|
||||
var config = new OcelotConfiguration(new List<ReRoute>(), string.Empty, serviceProviderConfig, "asdf");
|
||||
|
||||
this.Given(x => GivenTheFollowingConfiguration(fileConfig))
|
||||
.And(x => GivenTheRepoReturns(new OkResponse()))
|
||||
.And(x => GivenTheCreatorReturns(new OkResponse<IOcelotConfiguration>(config)))
|
||||
.When(x => WhenISetTheConfiguration())
|
||||
.Then(x => ThenTheConfigurationRepositoryIsCalledCorrectly())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
|
||||
[Fact]
|
||||
public void should_return_error_if_unable_to_set_file_configuration()
|
||||
{
|
||||
var fileConfig = new FileConfiguration();
|
||||
|
||||
this.Given(x => GivenTheFollowingConfiguration(fileConfig))
|
||||
.And(x => GivenTheRepoReturns(new ErrorResponse(It.IsAny<Error>())))
|
||||
.When(x => WhenISetTheConfiguration())
|
||||
.And(x => ThenAnErrorResponseIsReturned())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_error_if_unable_to_set_ocelot_configuration()
|
||||
{
|
||||
var fileConfig = new FileConfiguration();
|
||||
|
||||
this.Given(x => GivenTheFollowingConfiguration(fileConfig))
|
||||
.And(x => GivenTheRepoReturns(new OkResponse()))
|
||||
.And(x => GivenTheCreatorReturns(new ErrorResponse<IOcelotConfiguration>(It.IsAny<Error>())))
|
||||
.When(x => WhenISetTheConfiguration())
|
||||
.And(x => ThenAnErrorResponseIsReturned())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenTheRepoReturns(Response response)
|
||||
{
|
||||
_repo
|
||||
.Setup(x => x.Set(It.IsAny<FileConfiguration>()))
|
||||
.ReturnsAsync(response);
|
||||
}
|
||||
|
||||
private void ThenAnErrorResponseIsReturned()
|
||||
{
|
||||
_result.ShouldBeOfType<ErrorResponse>();
|
||||
}
|
||||
|
||||
private void GivenTheCreatorReturns(Response<IOcelotConfiguration> configuration)
|
||||
{
|
||||
_configuration = configuration;
|
||||
_configCreator
|
||||
.Setup(x => x.Create(_fileConfiguration))
|
||||
.ReturnsAsync(_configuration);
|
||||
}
|
||||
|
||||
private void GivenTheFollowingConfiguration(FileConfiguration fileConfiguration)
|
||||
{
|
||||
_fileConfiguration = fileConfiguration;
|
||||
}
|
||||
|
||||
private void WhenISetTheConfiguration()
|
||||
{
|
||||
_result = _configSetter.Set(_fileConfiguration).Result;
|
||||
}
|
||||
|
||||
private void ThenTheConfigurationRepositoryIsCalledCorrectly()
|
||||
{
|
||||
_configRepo
|
||||
.Verify(x => x.AddOrReplace(_configuration.Data), Times.Once);
|
||||
}
|
||||
}
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Moq;
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.Configuration.Builder;
|
||||
using Ocelot.Configuration.Creator;
|
||||
using Ocelot.Configuration.File;
|
||||
using Ocelot.Configuration.Repository;
|
||||
using Ocelot.Configuration.Setter;
|
||||
using Ocelot.Errors;
|
||||
using Ocelot.Responses;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests.Configuration
|
||||
{
|
||||
public class FileConfigurationSetterTests
|
||||
{
|
||||
private FileConfiguration _fileConfiguration;
|
||||
private FileConfigurationSetter _configSetter;
|
||||
private Mock<IOcelotConfigurationRepository> _configRepo;
|
||||
private Mock<IOcelotConfigurationCreator> _configCreator;
|
||||
private Response<IOcelotConfiguration> _configuration;
|
||||
private object _result;
|
||||
private Mock<IFileConfigurationRepository> _repo;
|
||||
|
||||
public FileConfigurationSetterTests()
|
||||
{
|
||||
_repo = new Mock<IFileConfigurationRepository>();
|
||||
_configRepo = new Mock<IOcelotConfigurationRepository>();
|
||||
_configCreator = new Mock<IOcelotConfigurationCreator>();
|
||||
_configSetter = new FileConfigurationSetter(_configRepo.Object, _configCreator.Object, _repo.Object);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_set_configuration()
|
||||
{
|
||||
var fileConfig = new FileConfiguration();
|
||||
var serviceProviderConfig = new ServiceProviderConfigurationBuilder().Build();
|
||||
var config = new OcelotConfiguration(new List<ReRoute>(), string.Empty, serviceProviderConfig, "asdf");
|
||||
|
||||
this.Given(x => GivenTheFollowingConfiguration(fileConfig))
|
||||
.And(x => GivenTheRepoReturns(new OkResponse()))
|
||||
.And(x => GivenTheCreatorReturns(new OkResponse<IOcelotConfiguration>(config)))
|
||||
.When(x => WhenISetTheConfiguration())
|
||||
.Then(x => ThenTheConfigurationRepositoryIsCalledCorrectly())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
|
||||
[Fact]
|
||||
public void should_return_error_if_unable_to_set_file_configuration()
|
||||
{
|
||||
var fileConfig = new FileConfiguration();
|
||||
|
||||
this.Given(x => GivenTheFollowingConfiguration(fileConfig))
|
||||
.And(x => GivenTheRepoReturns(new ErrorResponse(It.IsAny<Error>())))
|
||||
.When(x => WhenISetTheConfiguration())
|
||||
.And(x => ThenAnErrorResponseIsReturned())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_error_if_unable_to_set_ocelot_configuration()
|
||||
{
|
||||
var fileConfig = new FileConfiguration();
|
||||
|
||||
this.Given(x => GivenTheFollowingConfiguration(fileConfig))
|
||||
.And(x => GivenTheRepoReturns(new OkResponse()))
|
||||
.And(x => GivenTheCreatorReturns(new ErrorResponse<IOcelotConfiguration>(It.IsAny<Error>())))
|
||||
.When(x => WhenISetTheConfiguration())
|
||||
.And(x => ThenAnErrorResponseIsReturned())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenTheRepoReturns(Response response)
|
||||
{
|
||||
_repo
|
||||
.Setup(x => x.Set(It.IsAny<FileConfiguration>()))
|
||||
.ReturnsAsync(response);
|
||||
}
|
||||
|
||||
private void ThenAnErrorResponseIsReturned()
|
||||
{
|
||||
_result.ShouldBeOfType<ErrorResponse>();
|
||||
}
|
||||
|
||||
private void GivenTheCreatorReturns(Response<IOcelotConfiguration> configuration)
|
||||
{
|
||||
_configuration = configuration;
|
||||
_configCreator
|
||||
.Setup(x => x.Create(_fileConfiguration))
|
||||
.ReturnsAsync(_configuration);
|
||||
}
|
||||
|
||||
private void GivenTheFollowingConfiguration(FileConfiguration fileConfiguration)
|
||||
{
|
||||
_fileConfiguration = fileConfiguration;
|
||||
}
|
||||
|
||||
private void WhenISetTheConfiguration()
|
||||
{
|
||||
_result = _configSetter.Set(_fileConfiguration).Result;
|
||||
}
|
||||
|
||||
private void ThenTheConfigurationRepositoryIsCalledCorrectly()
|
||||
{
|
||||
_configRepo
|
||||
.Verify(x => x.AddOrReplace(_configuration.Data), Times.Once);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,33 +1,33 @@
|
||||
using System;
|
||||
using System.Security.Cryptography;
|
||||
using Microsoft.AspNetCore.Cryptography.KeyDerivation;
|
||||
using Shouldly;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests.Configuration
|
||||
{
|
||||
public class HashCreationTests
|
||||
{
|
||||
[Fact]
|
||||
public void should_create_hash_and_salt()
|
||||
{
|
||||
var password = "secret";
|
||||
|
||||
var salt = new byte[128 / 8];
|
||||
|
||||
using (var rng = RandomNumberGenerator.Create())
|
||||
{
|
||||
rng.GetBytes(salt);
|
||||
}
|
||||
|
||||
var storedSalt = Convert.ToBase64String(salt);
|
||||
|
||||
var storedHash = Convert.ToBase64String(KeyDerivation.Pbkdf2(
|
||||
password: password,
|
||||
salt: salt,
|
||||
prf: KeyDerivationPrf.HMACSHA256,
|
||||
iterationCount: 10000,
|
||||
numBytesRequested: 256 / 8));
|
||||
}
|
||||
}
|
||||
using System;
|
||||
using System.Security.Cryptography;
|
||||
using Microsoft.AspNetCore.Cryptography.KeyDerivation;
|
||||
using Shouldly;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests.Configuration
|
||||
{
|
||||
public class HashCreationTests
|
||||
{
|
||||
[Fact]
|
||||
public void should_create_hash_and_salt()
|
||||
{
|
||||
var password = "secret";
|
||||
|
||||
var salt = new byte[128 / 8];
|
||||
|
||||
using (var rng = RandomNumberGenerator.Create())
|
||||
{
|
||||
rng.GetBytes(salt);
|
||||
}
|
||||
|
||||
var storedSalt = Convert.ToBase64String(salt);
|
||||
|
||||
var storedHash = Convert.ToBase64String(KeyDerivation.Pbkdf2(
|
||||
password: password,
|
||||
salt: salt,
|
||||
prf: KeyDerivationPrf.HMACSHA256,
|
||||
iterationCount: 10000,
|
||||
numBytesRequested: 256 / 8));
|
||||
}
|
||||
}
|
||||
}
|
@ -1,76 +1,76 @@
|
||||
using Ocelot.Configuration.Authentication;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests.Configuration
|
||||
{
|
||||
public class HashMatcherTests
|
||||
{
|
||||
private string _password;
|
||||
private string _hash;
|
||||
private string _salt;
|
||||
private bool _result;
|
||||
private HashMatcher _hashMatcher;
|
||||
|
||||
public HashMatcherTests()
|
||||
{
|
||||
_hashMatcher = new HashMatcher();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_match_hash()
|
||||
{
|
||||
var hash = "kE/mxd1hO9h9Sl2VhGhwJUd9xZEv4NP6qXoN39nIqM4=";
|
||||
var salt = "zzWITpnDximUNKYLiUam/w==";
|
||||
var password = "secret";
|
||||
|
||||
this.Given(x => GivenThePassword(password))
|
||||
.And(x => GivenTheHash(hash))
|
||||
.And(x => GivenTheSalt(salt))
|
||||
.When(x => WhenIMatch())
|
||||
.Then(x => ThenTheResultIs(true))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_not_match_hash()
|
||||
{
|
||||
var hash = "kE/mxd1hO9h9Sl2VhGhwJUd9xZEv4NP6qXoN39nIqM4=";
|
||||
var salt = "zzWITpnDximUNKYLiUam/w==";
|
||||
var password = "secret1";
|
||||
|
||||
this.Given(x => GivenThePassword(password))
|
||||
.And(x => GivenTheHash(hash))
|
||||
.And(x => GivenTheSalt(salt))
|
||||
.When(x => WhenIMatch())
|
||||
.Then(x => ThenTheResultIs(false))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenThePassword(string password)
|
||||
{
|
||||
_password = password;
|
||||
}
|
||||
|
||||
private void GivenTheHash(string hash)
|
||||
{
|
||||
_hash = hash;
|
||||
}
|
||||
|
||||
private void GivenTheSalt(string salt)
|
||||
{
|
||||
_salt = salt;
|
||||
}
|
||||
|
||||
private void WhenIMatch()
|
||||
{
|
||||
_result = _hashMatcher.Match(_password, _salt, _hash);
|
||||
}
|
||||
|
||||
private void ThenTheResultIs(bool expected)
|
||||
{
|
||||
_result.ShouldBe(expected);
|
||||
}
|
||||
}
|
||||
using Ocelot.Configuration.Authentication;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests.Configuration
|
||||
{
|
||||
public class HashMatcherTests
|
||||
{
|
||||
private string _password;
|
||||
private string _hash;
|
||||
private string _salt;
|
||||
private bool _result;
|
||||
private HashMatcher _hashMatcher;
|
||||
|
||||
public HashMatcherTests()
|
||||
{
|
||||
_hashMatcher = new HashMatcher();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_match_hash()
|
||||
{
|
||||
var hash = "kE/mxd1hO9h9Sl2VhGhwJUd9xZEv4NP6qXoN39nIqM4=";
|
||||
var salt = "zzWITpnDximUNKYLiUam/w==";
|
||||
var password = "secret";
|
||||
|
||||
this.Given(x => GivenThePassword(password))
|
||||
.And(x => GivenTheHash(hash))
|
||||
.And(x => GivenTheSalt(salt))
|
||||
.When(x => WhenIMatch())
|
||||
.Then(x => ThenTheResultIs(true))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_not_match_hash()
|
||||
{
|
||||
var hash = "kE/mxd1hO9h9Sl2VhGhwJUd9xZEv4NP6qXoN39nIqM4=";
|
||||
var salt = "zzWITpnDximUNKYLiUam/w==";
|
||||
var password = "secret1";
|
||||
|
||||
this.Given(x => GivenThePassword(password))
|
||||
.And(x => GivenTheHash(hash))
|
||||
.And(x => GivenTheSalt(salt))
|
||||
.When(x => WhenIMatch())
|
||||
.Then(x => ThenTheResultIs(false))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenThePassword(string password)
|
||||
{
|
||||
_password = password;
|
||||
}
|
||||
|
||||
private void GivenTheHash(string hash)
|
||||
{
|
||||
_hash = hash;
|
||||
}
|
||||
|
||||
private void GivenTheSalt(string salt)
|
||||
{
|
||||
_salt = salt;
|
||||
}
|
||||
|
||||
private void WhenIMatch()
|
||||
{
|
||||
_result = _hashMatcher.Match(_password, _salt, _hash);
|
||||
}
|
||||
|
||||
private void ThenTheResultIs(bool expected)
|
||||
{
|
||||
_result.ShouldBe(expected);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,158 +1,158 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Moq;
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.Configuration.Builder;
|
||||
using Ocelot.Configuration.Creator;
|
||||
using Ocelot.Configuration.File;
|
||||
using Ocelot.Middleware;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests.Configuration
|
||||
{
|
||||
public class HeaderFindAndReplaceCreatorTests
|
||||
{
|
||||
private HeaderFindAndReplaceCreator _creator;
|
||||
private FileReRoute _reRoute;
|
||||
private HeaderTransformations _result;
|
||||
private Mock<IBaseUrlFinder> _finder;
|
||||
|
||||
public HeaderFindAndReplaceCreatorTests()
|
||||
{
|
||||
_finder = new Mock<IBaseUrlFinder>();
|
||||
_creator = new HeaderFindAndReplaceCreator(_finder.Object);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_create()
|
||||
{
|
||||
var reRoute = new FileReRoute
|
||||
{
|
||||
UpstreamHeaderTransform = new Dictionary<string, string>
|
||||
{
|
||||
{"Test", "Test, Chicken"},
|
||||
|
||||
{"Moop", "o, a"}
|
||||
},
|
||||
DownstreamHeaderTransform = new Dictionary<string, string>
|
||||
{
|
||||
{"Pop", "West, East"},
|
||||
|
||||
{"Bop", "e, r"}
|
||||
}
|
||||
};
|
||||
|
||||
var upstream = new List<HeaderFindAndReplace>
|
||||
{
|
||||
new HeaderFindAndReplace("Test", "Test", "Chicken", 0),
|
||||
new HeaderFindAndReplace("Moop", "o", "a", 0)
|
||||
};
|
||||
|
||||
var downstream = new List<HeaderFindAndReplace>
|
||||
{
|
||||
new HeaderFindAndReplace("Pop", "West", "East", 0),
|
||||
new HeaderFindAndReplace("Bop", "e", "r", 0)
|
||||
};
|
||||
|
||||
this.Given(x => GivenTheReRoute(reRoute))
|
||||
.When(x => WhenICreate())
|
||||
.Then(x => ThenTheFollowingUpstreamIsReturned(upstream))
|
||||
.Then(x => ThenTheFollowingDownstreamIsReturned(downstream))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_use_base_url_placeholder()
|
||||
{
|
||||
var reRoute = new FileReRoute
|
||||
{
|
||||
DownstreamHeaderTransform = new Dictionary<string, string>
|
||||
{
|
||||
{"Location", "http://www.bbc.co.uk/, {BaseUrl}"},
|
||||
}
|
||||
};
|
||||
|
||||
var downstream = new List<HeaderFindAndReplace>
|
||||
{
|
||||
new HeaderFindAndReplace("Location", "http://www.bbc.co.uk/", "http://ocelot.com/", 0),
|
||||
};
|
||||
|
||||
this.Given(x => GivenTheReRoute(reRoute))
|
||||
.And(x => GivenTheBaseUrlIs("http://ocelot.com/"))
|
||||
.When(x => WhenICreate())
|
||||
.Then(x => ThenTheFollowingDownstreamIsReturned(downstream))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
|
||||
[Fact]
|
||||
public void should_use_base_url_partial_placeholder()
|
||||
{
|
||||
var reRoute = new FileReRoute
|
||||
{
|
||||
DownstreamHeaderTransform = new Dictionary<string, string>
|
||||
{
|
||||
{"Location", "http://www.bbc.co.uk/pay, {BaseUrl}pay"},
|
||||
}
|
||||
};
|
||||
|
||||
var downstream = new List<HeaderFindAndReplace>
|
||||
{
|
||||
new HeaderFindAndReplace("Location", "http://www.bbc.co.uk/pay", "http://ocelot.com/pay", 0),
|
||||
};
|
||||
|
||||
this.Given(x => GivenTheReRoute(reRoute))
|
||||
.And(x => GivenTheBaseUrlIs("http://ocelot.com/"))
|
||||
.When(x => WhenICreate())
|
||||
.Then(x => ThenTheFollowingDownstreamIsReturned(downstream))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenTheBaseUrlIs(string baseUrl)
|
||||
{
|
||||
_finder.Setup(x => x.Find()).Returns(baseUrl);
|
||||
}
|
||||
|
||||
private void ThenTheFollowingDownstreamIsReturned(List<HeaderFindAndReplace> downstream)
|
||||
{
|
||||
_result.Downstream.Count.ShouldBe(downstream.Count);
|
||||
|
||||
for (int i = 0; i < _result.Downstream.Count; i++)
|
||||
{
|
||||
var result = _result.Downstream[i];
|
||||
var expected = downstream[i];
|
||||
result.Find.ShouldBe(expected.Find);
|
||||
result.Index.ShouldBe(expected.Index);
|
||||
result.Key.ShouldBe(expected.Key);
|
||||
result.Replace.ShouldBe(expected.Replace);
|
||||
}
|
||||
}
|
||||
|
||||
private void GivenTheReRoute(FileReRoute reRoute)
|
||||
{
|
||||
_reRoute = reRoute;
|
||||
}
|
||||
|
||||
private void WhenICreate()
|
||||
{
|
||||
_result = _creator.Create(_reRoute);
|
||||
}
|
||||
|
||||
private void ThenTheFollowingUpstreamIsReturned(List<HeaderFindAndReplace> expecteds)
|
||||
{
|
||||
_result.Upstream.Count.ShouldBe(expecteds.Count);
|
||||
|
||||
for (int i = 0; i < _result.Upstream.Count; i++)
|
||||
{
|
||||
var result = _result.Upstream[i];
|
||||
var expected = expecteds[i];
|
||||
result.Find.ShouldBe(expected.Find);
|
||||
result.Index.ShouldBe(expected.Index);
|
||||
result.Key.ShouldBe(expected.Key);
|
||||
result.Replace.ShouldBe(expected.Replace);
|
||||
}
|
||||
}
|
||||
}
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Moq;
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.Configuration.Builder;
|
||||
using Ocelot.Configuration.Creator;
|
||||
using Ocelot.Configuration.File;
|
||||
using Ocelot.Middleware;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests.Configuration
|
||||
{
|
||||
public class HeaderFindAndReplaceCreatorTests
|
||||
{
|
||||
private HeaderFindAndReplaceCreator _creator;
|
||||
private FileReRoute _reRoute;
|
||||
private HeaderTransformations _result;
|
||||
private Mock<IBaseUrlFinder> _finder;
|
||||
|
||||
public HeaderFindAndReplaceCreatorTests()
|
||||
{
|
||||
_finder = new Mock<IBaseUrlFinder>();
|
||||
_creator = new HeaderFindAndReplaceCreator(_finder.Object);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_create()
|
||||
{
|
||||
var reRoute = new FileReRoute
|
||||
{
|
||||
UpstreamHeaderTransform = new Dictionary<string, string>
|
||||
{
|
||||
{"Test", "Test, Chicken"},
|
||||
|
||||
{"Moop", "o, a"}
|
||||
},
|
||||
DownstreamHeaderTransform = new Dictionary<string, string>
|
||||
{
|
||||
{"Pop", "West, East"},
|
||||
|
||||
{"Bop", "e, r"}
|
||||
}
|
||||
};
|
||||
|
||||
var upstream = new List<HeaderFindAndReplace>
|
||||
{
|
||||
new HeaderFindAndReplace("Test", "Test", "Chicken", 0),
|
||||
new HeaderFindAndReplace("Moop", "o", "a", 0)
|
||||
};
|
||||
|
||||
var downstream = new List<HeaderFindAndReplace>
|
||||
{
|
||||
new HeaderFindAndReplace("Pop", "West", "East", 0),
|
||||
new HeaderFindAndReplace("Bop", "e", "r", 0)
|
||||
};
|
||||
|
||||
this.Given(x => GivenTheReRoute(reRoute))
|
||||
.When(x => WhenICreate())
|
||||
.Then(x => ThenTheFollowingUpstreamIsReturned(upstream))
|
||||
.Then(x => ThenTheFollowingDownstreamIsReturned(downstream))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_use_base_url_placeholder()
|
||||
{
|
||||
var reRoute = new FileReRoute
|
||||
{
|
||||
DownstreamHeaderTransform = new Dictionary<string, string>
|
||||
{
|
||||
{"Location", "http://www.bbc.co.uk/, {BaseUrl}"},
|
||||
}
|
||||
};
|
||||
|
||||
var downstream = new List<HeaderFindAndReplace>
|
||||
{
|
||||
new HeaderFindAndReplace("Location", "http://www.bbc.co.uk/", "http://ocelot.com/", 0),
|
||||
};
|
||||
|
||||
this.Given(x => GivenTheReRoute(reRoute))
|
||||
.And(x => GivenTheBaseUrlIs("http://ocelot.com/"))
|
||||
.When(x => WhenICreate())
|
||||
.Then(x => ThenTheFollowingDownstreamIsReturned(downstream))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
|
||||
[Fact]
|
||||
public void should_use_base_url_partial_placeholder()
|
||||
{
|
||||
var reRoute = new FileReRoute
|
||||
{
|
||||
DownstreamHeaderTransform = new Dictionary<string, string>
|
||||
{
|
||||
{"Location", "http://www.bbc.co.uk/pay, {BaseUrl}pay"},
|
||||
}
|
||||
};
|
||||
|
||||
var downstream = new List<HeaderFindAndReplace>
|
||||
{
|
||||
new HeaderFindAndReplace("Location", "http://www.bbc.co.uk/pay", "http://ocelot.com/pay", 0),
|
||||
};
|
||||
|
||||
this.Given(x => GivenTheReRoute(reRoute))
|
||||
.And(x => GivenTheBaseUrlIs("http://ocelot.com/"))
|
||||
.When(x => WhenICreate())
|
||||
.Then(x => ThenTheFollowingDownstreamIsReturned(downstream))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenTheBaseUrlIs(string baseUrl)
|
||||
{
|
||||
_finder.Setup(x => x.Find()).Returns(baseUrl);
|
||||
}
|
||||
|
||||
private void ThenTheFollowingDownstreamIsReturned(List<HeaderFindAndReplace> downstream)
|
||||
{
|
||||
_result.Downstream.Count.ShouldBe(downstream.Count);
|
||||
|
||||
for (int i = 0; i < _result.Downstream.Count; i++)
|
||||
{
|
||||
var result = _result.Downstream[i];
|
||||
var expected = downstream[i];
|
||||
result.Find.ShouldBe(expected.Find);
|
||||
result.Index.ShouldBe(expected.Index);
|
||||
result.Key.ShouldBe(expected.Key);
|
||||
result.Replace.ShouldBe(expected.Replace);
|
||||
}
|
||||
}
|
||||
|
||||
private void GivenTheReRoute(FileReRoute reRoute)
|
||||
{
|
||||
_reRoute = reRoute;
|
||||
}
|
||||
|
||||
private void WhenICreate()
|
||||
{
|
||||
_result = _creator.Create(_reRoute);
|
||||
}
|
||||
|
||||
private void ThenTheFollowingUpstreamIsReturned(List<HeaderFindAndReplace> expecteds)
|
||||
{
|
||||
_result.Upstream.Count.ShouldBe(expecteds.Count);
|
||||
|
||||
for (int i = 0; i < _result.Upstream.Count; i++)
|
||||
{
|
||||
var result = _result.Upstream[i];
|
||||
var expected = expecteds[i];
|
||||
result.Find.ShouldBe(expected.Find);
|
||||
result.Index.ShouldBe(expected.Index);
|
||||
result.Key.ShouldBe(expected.Key);
|
||||
result.Replace.ShouldBe(expected.Replace);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,16 +1,16 @@
|
||||
using Ocelot.Configuration.Creator;
|
||||
using Shouldly;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests.Configuration
|
||||
{
|
||||
public class IdentityServerConfigurationCreatorTests
|
||||
{
|
||||
[Fact]
|
||||
public void happy_path_only_exists_for_test_coverage_even_uncle_bob_probably_wouldnt_test_this()
|
||||
{
|
||||
var result = IdentityServerConfigurationCreator.GetIdentityServerConfiguration("secret");
|
||||
result.ApiName.ShouldBe("admin");
|
||||
}
|
||||
}
|
||||
using Ocelot.Configuration.Creator;
|
||||
using Shouldly;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests.Configuration
|
||||
{
|
||||
public class IdentityServerConfigurationCreatorTests
|
||||
{
|
||||
[Fact]
|
||||
public void happy_path_only_exists_for_test_coverage_even_uncle_bob_probably_wouldnt_test_this()
|
||||
{
|
||||
var result = IdentityServerConfigurationCreator.GetIdentityServerConfiguration("secret");
|
||||
result.ApiName.ShouldBe("admin");
|
||||
}
|
||||
}
|
||||
}
|
@ -1,101 +1,101 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.Configuration.Builder;
|
||||
using Ocelot.Configuration.Repository;
|
||||
using Ocelot.Responses;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests.Configuration
|
||||
{
|
||||
public class InMemoryConfigurationRepositoryTests
|
||||
{
|
||||
private readonly InMemoryOcelotConfigurationRepository _repo;
|
||||
private IOcelotConfiguration _config;
|
||||
private Response _result;
|
||||
private Response<IOcelotConfiguration> _getResult;
|
||||
|
||||
public InMemoryConfigurationRepositoryTests()
|
||||
{
|
||||
_repo = new InMemoryOcelotConfigurationRepository();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void can_add_config()
|
||||
{
|
||||
this.Given(x => x.GivenTheConfigurationIs(new FakeConfig("initial", "adminath")))
|
||||
.When(x => x.WhenIAddOrReplaceTheConfig())
|
||||
.Then(x => x.ThenNoErrorsAreReturned())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void can_get_config()
|
||||
{
|
||||
this.Given(x => x.GivenThereIsASavedConfiguration())
|
||||
.When(x => x.WhenIGetTheConfiguration())
|
||||
.Then(x => x.ThenTheConfigurationIsReturned())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void ThenTheConfigurationIsReturned()
|
||||
{
|
||||
_getResult.Data.ReRoutes[0].DownstreamPathTemplate.Value.ShouldBe("initial");
|
||||
}
|
||||
|
||||
private void WhenIGetTheConfiguration()
|
||||
{
|
||||
_getResult = _repo.Get().Result;
|
||||
}
|
||||
|
||||
private void GivenThereIsASavedConfiguration()
|
||||
{
|
||||
GivenTheConfigurationIs(new FakeConfig("initial", "adminath"));
|
||||
WhenIAddOrReplaceTheConfig();
|
||||
}
|
||||
|
||||
private void GivenTheConfigurationIs(IOcelotConfiguration config)
|
||||
{
|
||||
_config = config;
|
||||
}
|
||||
|
||||
private void WhenIAddOrReplaceTheConfig()
|
||||
{
|
||||
_result = _repo.AddOrReplace(_config).Result;
|
||||
}
|
||||
|
||||
private void ThenNoErrorsAreReturned()
|
||||
{
|
||||
_result.IsError.ShouldBeFalse();
|
||||
}
|
||||
|
||||
class FakeConfig : IOcelotConfiguration
|
||||
{
|
||||
private readonly string _downstreamTemplatePath;
|
||||
|
||||
public FakeConfig(string downstreamTemplatePath, string administrationPath)
|
||||
{
|
||||
_downstreamTemplatePath = downstreamTemplatePath;
|
||||
AdministrationPath = administrationPath;
|
||||
}
|
||||
|
||||
public List<ReRoute> ReRoutes => new List<ReRoute>
|
||||
{
|
||||
new ReRouteBuilder()
|
||||
.WithDownstreamPathTemplate(_downstreamTemplatePath)
|
||||
.WithUpstreamHttpMethod(new List<string> { "Get" })
|
||||
.Build()
|
||||
};
|
||||
|
||||
public string AdministrationPath {get;}
|
||||
|
||||
public ServiceProviderConfiguration ServiceProviderConfiguration => throw new NotImplementedException();
|
||||
|
||||
public string RequestId {get;}
|
||||
}
|
||||
}
|
||||
}
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.Configuration.Builder;
|
||||
using Ocelot.Configuration.Repository;
|
||||
using Ocelot.Responses;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests.Configuration
|
||||
{
|
||||
public class InMemoryConfigurationRepositoryTests
|
||||
{
|
||||
private readonly InMemoryOcelotConfigurationRepository _repo;
|
||||
private IOcelotConfiguration _config;
|
||||
private Response _result;
|
||||
private Response<IOcelotConfiguration> _getResult;
|
||||
|
||||
public InMemoryConfigurationRepositoryTests()
|
||||
{
|
||||
_repo = new InMemoryOcelotConfigurationRepository();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void can_add_config()
|
||||
{
|
||||
this.Given(x => x.GivenTheConfigurationIs(new FakeConfig("initial", "adminath")))
|
||||
.When(x => x.WhenIAddOrReplaceTheConfig())
|
||||
.Then(x => x.ThenNoErrorsAreReturned())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void can_get_config()
|
||||
{
|
||||
this.Given(x => x.GivenThereIsASavedConfiguration())
|
||||
.When(x => x.WhenIGetTheConfiguration())
|
||||
.Then(x => x.ThenTheConfigurationIsReturned())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void ThenTheConfigurationIsReturned()
|
||||
{
|
||||
_getResult.Data.ReRoutes[0].DownstreamPathTemplate.Value.ShouldBe("initial");
|
||||
}
|
||||
|
||||
private void WhenIGetTheConfiguration()
|
||||
{
|
||||
_getResult = _repo.Get().Result;
|
||||
}
|
||||
|
||||
private void GivenThereIsASavedConfiguration()
|
||||
{
|
||||
GivenTheConfigurationIs(new FakeConfig("initial", "adminath"));
|
||||
WhenIAddOrReplaceTheConfig();
|
||||
}
|
||||
|
||||
private void GivenTheConfigurationIs(IOcelotConfiguration config)
|
||||
{
|
||||
_config = config;
|
||||
}
|
||||
|
||||
private void WhenIAddOrReplaceTheConfig()
|
||||
{
|
||||
_result = _repo.AddOrReplace(_config).Result;
|
||||
}
|
||||
|
||||
private void ThenNoErrorsAreReturned()
|
||||
{
|
||||
_result.IsError.ShouldBeFalse();
|
||||
}
|
||||
|
||||
class FakeConfig : IOcelotConfiguration
|
||||
{
|
||||
private readonly string _downstreamTemplatePath;
|
||||
|
||||
public FakeConfig(string downstreamTemplatePath, string administrationPath)
|
||||
{
|
||||
_downstreamTemplatePath = downstreamTemplatePath;
|
||||
AdministrationPath = administrationPath;
|
||||
}
|
||||
|
||||
public List<ReRoute> ReRoutes => new List<ReRoute>
|
||||
{
|
||||
new ReRouteBuilder()
|
||||
.WithDownstreamPathTemplate(_downstreamTemplatePath)
|
||||
.WithUpstreamHttpMethod(new List<string> { "Get" })
|
||||
.Build()
|
||||
};
|
||||
|
||||
public string AdministrationPath {get;}
|
||||
|
||||
public ServiceProviderConfiguration ServiceProviderConfiguration => throw new NotImplementedException();
|
||||
|
||||
public string RequestId {get;}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,80 +1,80 @@
|
||||
using System.Collections.Generic;
|
||||
using Moq;
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.Configuration.Builder;
|
||||
using Ocelot.Configuration.Creator;
|
||||
using Ocelot.Configuration.Provider;
|
||||
using Ocelot.Configuration.Repository;
|
||||
using Ocelot.Errors;
|
||||
using Ocelot.Responses;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests.Configuration
|
||||
{
|
||||
public class OcelotConfigurationProviderTests
|
||||
{
|
||||
private readonly IOcelotConfigurationProvider _ocelotConfigurationProvider;
|
||||
private readonly Mock<IOcelotConfigurationRepository> _configurationRepository;
|
||||
private Response<IOcelotConfiguration> _result;
|
||||
|
||||
public OcelotConfigurationProviderTests()
|
||||
{
|
||||
_configurationRepository = new Mock<IOcelotConfigurationRepository>();
|
||||
_ocelotConfigurationProvider = new OcelotConfigurationProvider(_configurationRepository.Object);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_get_config()
|
||||
{
|
||||
var serviceProviderConfig = new ServiceProviderConfigurationBuilder().Build();
|
||||
|
||||
this.Given(x => x.GivenTheRepoReturns(new OkResponse<IOcelotConfiguration>(new OcelotConfiguration(new List<ReRoute>(), string.Empty, serviceProviderConfig, ""))))
|
||||
.When(x => x.WhenIGetTheConfig())
|
||||
.Then(x => x.TheFollowingIsReturned(new OkResponse<IOcelotConfiguration>(new OcelotConfiguration(new List<ReRoute>(), string.Empty, serviceProviderConfig, ""))))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_error()
|
||||
{
|
||||
this.Given(x => x.GivenTheRepoReturns(new ErrorResponse<IOcelotConfiguration>(new List<Error>
|
||||
{
|
||||
new AnyError()
|
||||
})))
|
||||
.When(x => x.WhenIGetTheConfig())
|
||||
.Then(x => x.TheFollowingIsReturned(
|
||||
new ErrorResponse<IOcelotConfiguration>(new List<Error>
|
||||
{
|
||||
new AnyError()
|
||||
})))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenTheRepoReturns(Response<IOcelotConfiguration> config)
|
||||
{
|
||||
_configurationRepository
|
||||
.Setup(x => x.Get())
|
||||
.ReturnsAsync(config);
|
||||
}
|
||||
|
||||
private void WhenIGetTheConfig()
|
||||
{
|
||||
_result = _ocelotConfigurationProvider.Get().Result;
|
||||
}
|
||||
|
||||
private void TheFollowingIsReturned(Response<IOcelotConfiguration> expected)
|
||||
{
|
||||
_result.IsError.ShouldBe(expected.IsError);
|
||||
}
|
||||
|
||||
class AnyError : Error
|
||||
{
|
||||
public AnyError()
|
||||
: base("blamo", OcelotErrorCode.UnknownError)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
using System.Collections.Generic;
|
||||
using Moq;
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.Configuration.Builder;
|
||||
using Ocelot.Configuration.Creator;
|
||||
using Ocelot.Configuration.Provider;
|
||||
using Ocelot.Configuration.Repository;
|
||||
using Ocelot.Errors;
|
||||
using Ocelot.Responses;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests.Configuration
|
||||
{
|
||||
public class OcelotConfigurationProviderTests
|
||||
{
|
||||
private readonly IOcelotConfigurationProvider _ocelotConfigurationProvider;
|
||||
private readonly Mock<IOcelotConfigurationRepository> _configurationRepository;
|
||||
private Response<IOcelotConfiguration> _result;
|
||||
|
||||
public OcelotConfigurationProviderTests()
|
||||
{
|
||||
_configurationRepository = new Mock<IOcelotConfigurationRepository>();
|
||||
_ocelotConfigurationProvider = new OcelotConfigurationProvider(_configurationRepository.Object);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_get_config()
|
||||
{
|
||||
var serviceProviderConfig = new ServiceProviderConfigurationBuilder().Build();
|
||||
|
||||
this.Given(x => x.GivenTheRepoReturns(new OkResponse<IOcelotConfiguration>(new OcelotConfiguration(new List<ReRoute>(), string.Empty, serviceProviderConfig, ""))))
|
||||
.When(x => x.WhenIGetTheConfig())
|
||||
.Then(x => x.TheFollowingIsReturned(new OkResponse<IOcelotConfiguration>(new OcelotConfiguration(new List<ReRoute>(), string.Empty, serviceProviderConfig, ""))))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_error()
|
||||
{
|
||||
this.Given(x => x.GivenTheRepoReturns(new ErrorResponse<IOcelotConfiguration>(new List<Error>
|
||||
{
|
||||
new AnyError()
|
||||
})))
|
||||
.When(x => x.WhenIGetTheConfig())
|
||||
.Then(x => x.TheFollowingIsReturned(
|
||||
new ErrorResponse<IOcelotConfiguration>(new List<Error>
|
||||
{
|
||||
new AnyError()
|
||||
})))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenTheRepoReturns(Response<IOcelotConfiguration> config)
|
||||
{
|
||||
_configurationRepository
|
||||
.Setup(x => x.Get())
|
||||
.ReturnsAsync(config);
|
||||
}
|
||||
|
||||
private void WhenIGetTheConfig()
|
||||
{
|
||||
_result = _ocelotConfigurationProvider.Get().Result;
|
||||
}
|
||||
|
||||
private void TheFollowingIsReturned(Response<IOcelotConfiguration> expected)
|
||||
{
|
||||
_result.IsError.ShouldBe(expected.IsError);
|
||||
}
|
||||
|
||||
class AnyError : Error
|
||||
{
|
||||
public AnyError()
|
||||
: base("blamo", OcelotErrorCode.UnknownError)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,63 +1,63 @@
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.Configuration.Builder;
|
||||
using Ocelot.Configuration.Creator;
|
||||
using Ocelot.Configuration.File;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests.Configuration
|
||||
{
|
||||
public class QoSOptionsCreatorTests
|
||||
{
|
||||
private QoSOptionsCreator _creator;
|
||||
private FileReRoute _fileReRoute;
|
||||
private QoSOptions _result;
|
||||
|
||||
public QoSOptionsCreatorTests()
|
||||
{
|
||||
_creator = new QoSOptionsCreator();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_create_qos_options()
|
||||
{
|
||||
var reRoute = new FileReRoute
|
||||
{
|
||||
QoSOptions = new FileQoSOptions
|
||||
{
|
||||
ExceptionsAllowedBeforeBreaking = 1,
|
||||
DurationOfBreak = 1,
|
||||
TimeoutValue = 1
|
||||
}
|
||||
};
|
||||
var expected = new QoSOptionsBuilder()
|
||||
.WithDurationOfBreak(1)
|
||||
.WithExceptionsAllowedBeforeBreaking(1)
|
||||
.WithTimeoutValue(1)
|
||||
.Build();
|
||||
|
||||
this.Given(x => x.GivenTheFollowingReRoute(reRoute))
|
||||
.When(x => x.WhenICreate())
|
||||
.Then(x => x.ThenTheFollowingIsReturned(expected))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenTheFollowingReRoute(FileReRoute fileReRoute)
|
||||
{
|
||||
_fileReRoute = fileReRoute;
|
||||
}
|
||||
|
||||
private void WhenICreate()
|
||||
{
|
||||
_result = _creator.Create(_fileReRoute);
|
||||
}
|
||||
|
||||
private void ThenTheFollowingIsReturned(QoSOptions expected)
|
||||
{
|
||||
_result.DurationOfBreak.ShouldBe(expected.DurationOfBreak);
|
||||
_result.ExceptionsAllowedBeforeBreaking.ShouldBe(expected.ExceptionsAllowedBeforeBreaking);
|
||||
_result.TimeoutValue.ShouldBe(expected.TimeoutValue);
|
||||
}
|
||||
}
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.Configuration.Builder;
|
||||
using Ocelot.Configuration.Creator;
|
||||
using Ocelot.Configuration.File;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests.Configuration
|
||||
{
|
||||
public class QoSOptionsCreatorTests
|
||||
{
|
||||
private QoSOptionsCreator _creator;
|
||||
private FileReRoute _fileReRoute;
|
||||
private QoSOptions _result;
|
||||
|
||||
public QoSOptionsCreatorTests()
|
||||
{
|
||||
_creator = new QoSOptionsCreator();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_create_qos_options()
|
||||
{
|
||||
var reRoute = new FileReRoute
|
||||
{
|
||||
QoSOptions = new FileQoSOptions
|
||||
{
|
||||
ExceptionsAllowedBeforeBreaking = 1,
|
||||
DurationOfBreak = 1,
|
||||
TimeoutValue = 1
|
||||
}
|
||||
};
|
||||
var expected = new QoSOptionsBuilder()
|
||||
.WithDurationOfBreak(1)
|
||||
.WithExceptionsAllowedBeforeBreaking(1)
|
||||
.WithTimeoutValue(1)
|
||||
.Build();
|
||||
|
||||
this.Given(x => x.GivenTheFollowingReRoute(reRoute))
|
||||
.When(x => x.WhenICreate())
|
||||
.Then(x => x.ThenTheFollowingIsReturned(expected))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenTheFollowingReRoute(FileReRoute fileReRoute)
|
||||
{
|
||||
_fileReRoute = fileReRoute;
|
||||
}
|
||||
|
||||
private void WhenICreate()
|
||||
{
|
||||
_result = _creator.Create(_fileReRoute);
|
||||
}
|
||||
|
||||
private void ThenTheFollowingIsReturned(QoSOptions expected)
|
||||
{
|
||||
_result.DurationOfBreak.ShouldBe(expected.DurationOfBreak);
|
||||
_result.ExceptionsAllowedBeforeBreaking.ShouldBe(expected.ExceptionsAllowedBeforeBreaking);
|
||||
_result.TimeoutValue.ShouldBe(expected.TimeoutValue);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,108 +1,108 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.Configuration.Builder;
|
||||
using Ocelot.Configuration.Creator;
|
||||
using Ocelot.Configuration.File;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests.Configuration
|
||||
{
|
||||
public class RateLimitOptionsCreatorTests
|
||||
{
|
||||
private FileReRoute _fileReRoute;
|
||||
private FileGlobalConfiguration _fileGlobalConfig;
|
||||
private bool _enabled;
|
||||
private RateLimitOptionsCreator _creator;
|
||||
private RateLimitOptions _result;
|
||||
|
||||
public RateLimitOptionsCreatorTests()
|
||||
{
|
||||
_creator = new RateLimitOptionsCreator();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_create_rate_limit_options()
|
||||
{
|
||||
var fileReRoute = new FileReRoute
|
||||
{
|
||||
RateLimitOptions = new FileRateLimitRule
|
||||
{
|
||||
ClientWhitelist = new List<string>(),
|
||||
Period = "Period",
|
||||
Limit = 1,
|
||||
PeriodTimespan = 1,
|
||||
EnableRateLimiting = true
|
||||
}
|
||||
};
|
||||
var fileGlobalConfig = new FileGlobalConfiguration
|
||||
{
|
||||
RateLimitOptions = new FileRateLimitOptions
|
||||
{
|
||||
ClientIdHeader = "ClientIdHeader",
|
||||
DisableRateLimitHeaders = true,
|
||||
QuotaExceededMessage = "QuotaExceededMessage",
|
||||
RateLimitCounterPrefix = "RateLimitCounterPrefix",
|
||||
HttpStatusCode = 200
|
||||
}
|
||||
};
|
||||
var expected = new RateLimitOptionsBuilder()
|
||||
.WithClientIdHeader("ClientIdHeader")
|
||||
.WithClientWhiteList(fileReRoute.RateLimitOptions.ClientWhitelist)
|
||||
.WithDisableRateLimitHeaders(true)
|
||||
.WithEnableRateLimiting(true)
|
||||
.WithHttpStatusCode(200)
|
||||
.WithQuotaExceededMessage("QuotaExceededMessage")
|
||||
.WithRateLimitCounterPrefix("RateLimitCounterPrefix")
|
||||
.WithRateLimitRule(new RateLimitRule(fileReRoute.RateLimitOptions.Period,
|
||||
fileReRoute.RateLimitOptions.PeriodTimespan,
|
||||
fileReRoute.RateLimitOptions.Limit))
|
||||
.Build();
|
||||
|
||||
this.Given(x => x.GivenTheFollowingFileReRoute(fileReRoute))
|
||||
.And(x => x.GivenTheFollowingFileGlobalConfig(fileGlobalConfig))
|
||||
.And(x => x.GivenRateLimitingIsEnabled())
|
||||
.When(x => x.WhenICreate())
|
||||
.Then(x => x.ThenTheFollowingIsReturned(expected))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenTheFollowingFileReRoute(FileReRoute fileReRoute)
|
||||
{
|
||||
_fileReRoute = fileReRoute;
|
||||
}
|
||||
|
||||
private void GivenTheFollowingFileGlobalConfig(FileGlobalConfiguration fileGlobalConfig)
|
||||
{
|
||||
_fileGlobalConfig = fileGlobalConfig;
|
||||
}
|
||||
|
||||
private void GivenRateLimitingIsEnabled()
|
||||
{
|
||||
_enabled = true;
|
||||
}
|
||||
|
||||
private void WhenICreate()
|
||||
{
|
||||
_result = _creator.Create(_fileReRoute, _fileGlobalConfig, _enabled);
|
||||
}
|
||||
|
||||
private void ThenTheFollowingIsReturned(RateLimitOptions expected)
|
||||
{
|
||||
_result.ClientIdHeader.ShouldBe(expected.ClientIdHeader);
|
||||
_result.ClientWhitelist.ShouldBe(expected.ClientWhitelist);
|
||||
_result.DisableRateLimitHeaders.ShouldBe(expected.DisableRateLimitHeaders);
|
||||
_result.EnableRateLimiting.ShouldBe(expected.EnableRateLimiting);
|
||||
_result.HttpStatusCode.ShouldBe(expected.HttpStatusCode);
|
||||
_result.QuotaExceededMessage.ShouldBe(expected.QuotaExceededMessage);
|
||||
_result.RateLimitCounterPrefix.ShouldBe(expected.RateLimitCounterPrefix);
|
||||
_result.RateLimitRule.Limit.ShouldBe(expected.RateLimitRule.Limit);
|
||||
_result.RateLimitRule.Period.ShouldBe(expected.RateLimitRule.Period);
|
||||
TimeSpan.FromSeconds(_result.RateLimitRule.PeriodTimespan).Ticks.ShouldBe(TimeSpan.FromSeconds(expected.RateLimitRule.PeriodTimespan).Ticks);
|
||||
}
|
||||
}
|
||||
}
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.Configuration.Builder;
|
||||
using Ocelot.Configuration.Creator;
|
||||
using Ocelot.Configuration.File;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests.Configuration
|
||||
{
|
||||
public class RateLimitOptionsCreatorTests
|
||||
{
|
||||
private FileReRoute _fileReRoute;
|
||||
private FileGlobalConfiguration _fileGlobalConfig;
|
||||
private bool _enabled;
|
||||
private RateLimitOptionsCreator _creator;
|
||||
private RateLimitOptions _result;
|
||||
|
||||
public RateLimitOptionsCreatorTests()
|
||||
{
|
||||
_creator = new RateLimitOptionsCreator();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_create_rate_limit_options()
|
||||
{
|
||||
var fileReRoute = new FileReRoute
|
||||
{
|
||||
RateLimitOptions = new FileRateLimitRule
|
||||
{
|
||||
ClientWhitelist = new List<string>(),
|
||||
Period = "Period",
|
||||
Limit = 1,
|
||||
PeriodTimespan = 1,
|
||||
EnableRateLimiting = true
|
||||
}
|
||||
};
|
||||
var fileGlobalConfig = new FileGlobalConfiguration
|
||||
{
|
||||
RateLimitOptions = new FileRateLimitOptions
|
||||
{
|
||||
ClientIdHeader = "ClientIdHeader",
|
||||
DisableRateLimitHeaders = true,
|
||||
QuotaExceededMessage = "QuotaExceededMessage",
|
||||
RateLimitCounterPrefix = "RateLimitCounterPrefix",
|
||||
HttpStatusCode = 200
|
||||
}
|
||||
};
|
||||
var expected = new RateLimitOptionsBuilder()
|
||||
.WithClientIdHeader("ClientIdHeader")
|
||||
.WithClientWhiteList(fileReRoute.RateLimitOptions.ClientWhitelist)
|
||||
.WithDisableRateLimitHeaders(true)
|
||||
.WithEnableRateLimiting(true)
|
||||
.WithHttpStatusCode(200)
|
||||
.WithQuotaExceededMessage("QuotaExceededMessage")
|
||||
.WithRateLimitCounterPrefix("RateLimitCounterPrefix")
|
||||
.WithRateLimitRule(new RateLimitRule(fileReRoute.RateLimitOptions.Period,
|
||||
fileReRoute.RateLimitOptions.PeriodTimespan,
|
||||
fileReRoute.RateLimitOptions.Limit))
|
||||
.Build();
|
||||
|
||||
this.Given(x => x.GivenTheFollowingFileReRoute(fileReRoute))
|
||||
.And(x => x.GivenTheFollowingFileGlobalConfig(fileGlobalConfig))
|
||||
.And(x => x.GivenRateLimitingIsEnabled())
|
||||
.When(x => x.WhenICreate())
|
||||
.Then(x => x.ThenTheFollowingIsReturned(expected))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenTheFollowingFileReRoute(FileReRoute fileReRoute)
|
||||
{
|
||||
_fileReRoute = fileReRoute;
|
||||
}
|
||||
|
||||
private void GivenTheFollowingFileGlobalConfig(FileGlobalConfiguration fileGlobalConfig)
|
||||
{
|
||||
_fileGlobalConfig = fileGlobalConfig;
|
||||
}
|
||||
|
||||
private void GivenRateLimitingIsEnabled()
|
||||
{
|
||||
_enabled = true;
|
||||
}
|
||||
|
||||
private void WhenICreate()
|
||||
{
|
||||
_result = _creator.Create(_fileReRoute, _fileGlobalConfig, _enabled);
|
||||
}
|
||||
|
||||
private void ThenTheFollowingIsReturned(RateLimitOptions expected)
|
||||
{
|
||||
_result.ClientIdHeader.ShouldBe(expected.ClientIdHeader);
|
||||
_result.ClientWhitelist.ShouldBe(expected.ClientWhitelist);
|
||||
_result.DisableRateLimitHeaders.ShouldBe(expected.DisableRateLimitHeaders);
|
||||
_result.EnableRateLimiting.ShouldBe(expected.EnableRateLimiting);
|
||||
_result.HttpStatusCode.ShouldBe(expected.HttpStatusCode);
|
||||
_result.QuotaExceededMessage.ShouldBe(expected.QuotaExceededMessage);
|
||||
_result.RateLimitCounterPrefix.ShouldBe(expected.RateLimitCounterPrefix);
|
||||
_result.RateLimitRule.Limit.ShouldBe(expected.RateLimitRule.Limit);
|
||||
_result.RateLimitRule.Period.ShouldBe(expected.RateLimitRule.Period);
|
||||
TimeSpan.FromSeconds(_result.RateLimitRule.PeriodTimespan).Ticks.ShouldBe(TimeSpan.FromSeconds(expected.RateLimitRule.PeriodTimespan).Ticks);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,84 +1,84 @@
|
||||
using System.Collections.Generic;
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.Configuration.Builder;
|
||||
using Ocelot.Configuration.Creator;
|
||||
using Ocelot.Configuration.File;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests.Configuration
|
||||
{
|
||||
public class ReRouteOptionsCreatorTests
|
||||
{
|
||||
private ReRouteOptionsCreator _creator;
|
||||
private FileReRoute _reRoute;
|
||||
private ReRouteOptions _result;
|
||||
|
||||
public ReRouteOptionsCreatorTests()
|
||||
{
|
||||
_creator = new ReRouteOptionsCreator();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_create_re_route_options()
|
||||
{
|
||||
var reRoute = new FileReRoute
|
||||
{
|
||||
RateLimitOptions = new FileRateLimitRule
|
||||
{
|
||||
EnableRateLimiting = true
|
||||
},
|
||||
QoSOptions = new FileQoSOptions
|
||||
{
|
||||
ExceptionsAllowedBeforeBreaking = 1,
|
||||
TimeoutValue = 1
|
||||
},
|
||||
AuthenticationOptions = new FileAuthenticationOptions()
|
||||
{
|
||||
AuthenticationProviderKey = "Test"
|
||||
},
|
||||
RouteClaimsRequirement = new Dictionary<string, string>()
|
||||
{
|
||||
{"",""}
|
||||
},
|
||||
FileCacheOptions = new FileCacheOptions
|
||||
{
|
||||
TtlSeconds = 1
|
||||
}
|
||||
};
|
||||
|
||||
var expected = new ReRouteOptionsBuilder()
|
||||
.WithIsAuthenticated(true)
|
||||
.WithIsAuthorised(true)
|
||||
.WithIsCached(true)
|
||||
.WithIsQos(true)
|
||||
.WithRateLimiting(true)
|
||||
.Build();
|
||||
|
||||
this.Given(x => x.GivenTheFollowing(reRoute))
|
||||
.When(x => x.WhenICreate())
|
||||
.Then(x => x.ThenTheFollowingIsReturned(expected))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenTheFollowing(FileReRoute reRoute)
|
||||
{
|
||||
_reRoute = reRoute;
|
||||
}
|
||||
|
||||
private void WhenICreate()
|
||||
{
|
||||
_result = _creator.Create(_reRoute);
|
||||
}
|
||||
|
||||
private void ThenTheFollowingIsReturned(ReRouteOptions expected)
|
||||
{
|
||||
_result.IsAuthenticated.ShouldBe(expected.IsAuthenticated);
|
||||
_result.IsAuthorised.ShouldBe(expected.IsAuthorised);
|
||||
_result.IsQos.ShouldBe(expected.IsQos);
|
||||
_result.IsCached.ShouldBe(expected.IsCached);
|
||||
_result.EnableRateLimiting.ShouldBe(expected.EnableRateLimiting);
|
||||
}
|
||||
}
|
||||
using System.Collections.Generic;
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.Configuration.Builder;
|
||||
using Ocelot.Configuration.Creator;
|
||||
using Ocelot.Configuration.File;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests.Configuration
|
||||
{
|
||||
public class ReRouteOptionsCreatorTests
|
||||
{
|
||||
private ReRouteOptionsCreator _creator;
|
||||
private FileReRoute _reRoute;
|
||||
private ReRouteOptions _result;
|
||||
|
||||
public ReRouteOptionsCreatorTests()
|
||||
{
|
||||
_creator = new ReRouteOptionsCreator();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_create_re_route_options()
|
||||
{
|
||||
var reRoute = new FileReRoute
|
||||
{
|
||||
RateLimitOptions = new FileRateLimitRule
|
||||
{
|
||||
EnableRateLimiting = true
|
||||
},
|
||||
QoSOptions = new FileQoSOptions
|
||||
{
|
||||
ExceptionsAllowedBeforeBreaking = 1,
|
||||
TimeoutValue = 1
|
||||
},
|
||||
AuthenticationOptions = new FileAuthenticationOptions()
|
||||
{
|
||||
AuthenticationProviderKey = "Test"
|
||||
},
|
||||
RouteClaimsRequirement = new Dictionary<string, string>()
|
||||
{
|
||||
{"",""}
|
||||
},
|
||||
FileCacheOptions = new FileCacheOptions
|
||||
{
|
||||
TtlSeconds = 1
|
||||
}
|
||||
};
|
||||
|
||||
var expected = new ReRouteOptionsBuilder()
|
||||
.WithIsAuthenticated(true)
|
||||
.WithIsAuthorised(true)
|
||||
.WithIsCached(true)
|
||||
.WithIsQos(true)
|
||||
.WithRateLimiting(true)
|
||||
.Build();
|
||||
|
||||
this.Given(x => x.GivenTheFollowing(reRoute))
|
||||
.When(x => x.WhenICreate())
|
||||
.Then(x => x.ThenTheFollowingIsReturned(expected))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenTheFollowing(FileReRoute reRoute)
|
||||
{
|
||||
_reRoute = reRoute;
|
||||
}
|
||||
|
||||
private void WhenICreate()
|
||||
{
|
||||
_result = _creator.Create(_reRoute);
|
||||
}
|
||||
|
||||
private void ThenTheFollowingIsReturned(ReRouteOptions expected)
|
||||
{
|
||||
_result.IsAuthenticated.ShouldBe(expected.IsAuthenticated);
|
||||
_result.IsAuthorised.ShouldBe(expected.IsAuthorised);
|
||||
_result.IsQos.ShouldBe(expected.IsQos);
|
||||
_result.IsCached.ShouldBe(expected.IsCached);
|
||||
_result.EnableRateLimiting.ShouldBe(expected.EnableRateLimiting);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,92 +1,92 @@
|
||||
using Ocelot.Configuration.Creator;
|
||||
using Ocelot.Configuration.File;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests.Configuration
|
||||
{
|
||||
public class RequestIdKeyCreatorTests
|
||||
{
|
||||
private FileReRoute _fileReRoute;
|
||||
private FileGlobalConfiguration _fileGlobalConfig;
|
||||
private string _result;
|
||||
private RequestIdKeyCreator _creator;
|
||||
|
||||
public RequestIdKeyCreatorTests()
|
||||
{
|
||||
_creator = new RequestIdKeyCreator();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_use_global_configuration()
|
||||
{
|
||||
var reRoute = new FileReRoute();
|
||||
var globalConfig = new FileGlobalConfiguration
|
||||
{
|
||||
RequestIdKey = "cheese"
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenTheFollowingReRoute(reRoute))
|
||||
.And(x => x.GivenTheFollowingGlobalConfig(globalConfig))
|
||||
.When(x => x.WhenICreate())
|
||||
.Then(x => x.ThenTheFollowingIsReturned("cheese"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_use_re_route_specific()
|
||||
{
|
||||
var reRoute = new FileReRoute
|
||||
{
|
||||
RequestIdKey = "cheese"
|
||||
};
|
||||
var globalConfig = new FileGlobalConfiguration();
|
||||
|
||||
this.Given(x => x.GivenTheFollowingReRoute(reRoute))
|
||||
.And(x => x.GivenTheFollowingGlobalConfig(globalConfig))
|
||||
.When(x => x.WhenICreate())
|
||||
.Then(x => x.ThenTheFollowingIsReturned("cheese"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_use_re_route_over_global_specific()
|
||||
{
|
||||
var reRoute = new FileReRoute
|
||||
{
|
||||
RequestIdKey = "cheese"
|
||||
};
|
||||
var globalConfig = new FileGlobalConfiguration
|
||||
{
|
||||
RequestIdKey = "test"
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenTheFollowingReRoute(reRoute))
|
||||
.And(x => x.GivenTheFollowingGlobalConfig(globalConfig))
|
||||
.When(x => x.WhenICreate())
|
||||
.Then(x => x.ThenTheFollowingIsReturned("cheese"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenTheFollowingReRoute(FileReRoute fileReRoute)
|
||||
{
|
||||
_fileReRoute = fileReRoute;
|
||||
}
|
||||
|
||||
private void GivenTheFollowingGlobalConfig(FileGlobalConfiguration globalConfig)
|
||||
{
|
||||
_fileGlobalConfig = globalConfig;
|
||||
}
|
||||
|
||||
private void WhenICreate()
|
||||
{
|
||||
_result = _creator.Create(_fileReRoute, _fileGlobalConfig);
|
||||
}
|
||||
|
||||
private void ThenTheFollowingIsReturned(string expected)
|
||||
{
|
||||
_result.ShouldBe(expected);
|
||||
}
|
||||
}
|
||||
using Ocelot.Configuration.Creator;
|
||||
using Ocelot.Configuration.File;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests.Configuration
|
||||
{
|
||||
public class RequestIdKeyCreatorTests
|
||||
{
|
||||
private FileReRoute _fileReRoute;
|
||||
private FileGlobalConfiguration _fileGlobalConfig;
|
||||
private string _result;
|
||||
private RequestIdKeyCreator _creator;
|
||||
|
||||
public RequestIdKeyCreatorTests()
|
||||
{
|
||||
_creator = new RequestIdKeyCreator();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_use_global_configuration()
|
||||
{
|
||||
var reRoute = new FileReRoute();
|
||||
var globalConfig = new FileGlobalConfiguration
|
||||
{
|
||||
RequestIdKey = "cheese"
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenTheFollowingReRoute(reRoute))
|
||||
.And(x => x.GivenTheFollowingGlobalConfig(globalConfig))
|
||||
.When(x => x.WhenICreate())
|
||||
.Then(x => x.ThenTheFollowingIsReturned("cheese"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_use_re_route_specific()
|
||||
{
|
||||
var reRoute = new FileReRoute
|
||||
{
|
||||
RequestIdKey = "cheese"
|
||||
};
|
||||
var globalConfig = new FileGlobalConfiguration();
|
||||
|
||||
this.Given(x => x.GivenTheFollowingReRoute(reRoute))
|
||||
.And(x => x.GivenTheFollowingGlobalConfig(globalConfig))
|
||||
.When(x => x.WhenICreate())
|
||||
.Then(x => x.ThenTheFollowingIsReturned("cheese"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_use_re_route_over_global_specific()
|
||||
{
|
||||
var reRoute = new FileReRoute
|
||||
{
|
||||
RequestIdKey = "cheese"
|
||||
};
|
||||
var globalConfig = new FileGlobalConfiguration
|
||||
{
|
||||
RequestIdKey = "test"
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenTheFollowingReRoute(reRoute))
|
||||
.And(x => x.GivenTheFollowingGlobalConfig(globalConfig))
|
||||
.When(x => x.WhenICreate())
|
||||
.Then(x => x.ThenTheFollowingIsReturned("cheese"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenTheFollowingReRoute(FileReRoute fileReRoute)
|
||||
{
|
||||
_fileReRoute = fileReRoute;
|
||||
}
|
||||
|
||||
private void GivenTheFollowingGlobalConfig(FileGlobalConfiguration globalConfig)
|
||||
{
|
||||
_fileGlobalConfig = globalConfig;
|
||||
}
|
||||
|
||||
private void WhenICreate()
|
||||
{
|
||||
_result = _creator.Create(_fileReRoute, _fileGlobalConfig);
|
||||
}
|
||||
|
||||
private void ThenTheFollowingIsReturned(string expected)
|
||||
{
|
||||
_result.ShouldBe(expected);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,70 +1,70 @@
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.Configuration.Builder;
|
||||
using Ocelot.Configuration.Creator;
|
||||
using Ocelot.Configuration.File;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests.Configuration
|
||||
{
|
||||
public class ServiceProviderCreatorTests
|
||||
{
|
||||
private ServiceProviderConfigurationCreator _creator;
|
||||
private FileReRoute _reRoute;
|
||||
private FileGlobalConfiguration _globalConfig;
|
||||
private ServiceProviderConfiguration _result;
|
||||
|
||||
public ServiceProviderCreatorTests()
|
||||
{
|
||||
_creator = new ServiceProviderConfigurationCreator();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_create_service_provider_config()
|
||||
{
|
||||
var reRoute = new FileReRoute();
|
||||
|
||||
var globalConfig = new FileGlobalConfiguration
|
||||
{
|
||||
ServiceDiscoveryProvider = new FileServiceDiscoveryProvider
|
||||
{
|
||||
Host = "127.0.0.1",
|
||||
Port = 1234
|
||||
}
|
||||
};
|
||||
|
||||
var expected = new ServiceProviderConfigurationBuilder()
|
||||
.WithServiceDiscoveryProviderHost("127.0.0.1")
|
||||
.WithServiceDiscoveryProviderPort(1234)
|
||||
.Build();
|
||||
|
||||
this.Given(x => x.GivenTheFollowingReRoute(reRoute))
|
||||
.And(x => x.GivenTheFollowingGlobalConfig(globalConfig))
|
||||
.When(x => x.WhenICreate())
|
||||
.Then(x => x.ThenTheConfigIs(expected))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenTheFollowingReRoute(FileReRoute fileReRoute)
|
||||
{
|
||||
_reRoute = fileReRoute;
|
||||
}
|
||||
|
||||
private void GivenTheFollowingGlobalConfig(FileGlobalConfiguration fileGlobalConfig)
|
||||
{
|
||||
_globalConfig = fileGlobalConfig;
|
||||
}
|
||||
|
||||
private void WhenICreate()
|
||||
{
|
||||
_result = _creator.Create(_globalConfig);
|
||||
}
|
||||
|
||||
private void ThenTheConfigIs(ServiceProviderConfiguration expected)
|
||||
{
|
||||
_result.ServiceProviderHost.ShouldBe(expected.ServiceProviderHost);
|
||||
_result.ServiceProviderPort.ShouldBe(expected.ServiceProviderPort);
|
||||
}
|
||||
}
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.Configuration.Builder;
|
||||
using Ocelot.Configuration.Creator;
|
||||
using Ocelot.Configuration.File;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests.Configuration
|
||||
{
|
||||
public class ServiceProviderCreatorTests
|
||||
{
|
||||
private ServiceProviderConfigurationCreator _creator;
|
||||
private FileReRoute _reRoute;
|
||||
private FileGlobalConfiguration _globalConfig;
|
||||
private ServiceProviderConfiguration _result;
|
||||
|
||||
public ServiceProviderCreatorTests()
|
||||
{
|
||||
_creator = new ServiceProviderConfigurationCreator();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_create_service_provider_config()
|
||||
{
|
||||
var reRoute = new FileReRoute();
|
||||
|
||||
var globalConfig = new FileGlobalConfiguration
|
||||
{
|
||||
ServiceDiscoveryProvider = new FileServiceDiscoveryProvider
|
||||
{
|
||||
Host = "127.0.0.1",
|
||||
Port = 1234
|
||||
}
|
||||
};
|
||||
|
||||
var expected = new ServiceProviderConfigurationBuilder()
|
||||
.WithServiceDiscoveryProviderHost("127.0.0.1")
|
||||
.WithServiceDiscoveryProviderPort(1234)
|
||||
.Build();
|
||||
|
||||
this.Given(x => x.GivenTheFollowingReRoute(reRoute))
|
||||
.And(x => x.GivenTheFollowingGlobalConfig(globalConfig))
|
||||
.When(x => x.WhenICreate())
|
||||
.Then(x => x.ThenTheConfigIs(expected))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenTheFollowingReRoute(FileReRoute fileReRoute)
|
||||
{
|
||||
_reRoute = fileReRoute;
|
||||
}
|
||||
|
||||
private void GivenTheFollowingGlobalConfig(FileGlobalConfiguration fileGlobalConfig)
|
||||
{
|
||||
_globalConfig = fileGlobalConfig;
|
||||
}
|
||||
|
||||
private void WhenICreate()
|
||||
{
|
||||
_result = _creator.Create(_globalConfig);
|
||||
}
|
||||
|
||||
private void ThenTheConfigIs(ServiceProviderConfiguration expected)
|
||||
{
|
||||
_result.ServiceProviderHost.ShouldBe(expected.ServiceProviderHost);
|
||||
_result.ServiceProviderPort.ShouldBe(expected.ServiceProviderPort);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,184 +1,184 @@
|
||||
using System;
|
||||
using Ocelot.Configuration.Creator;
|
||||
using Ocelot.Configuration.File;
|
||||
using Ocelot.Values;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests.Configuration
|
||||
{
|
||||
public class UpstreamTemplatePatternCreatorTests
|
||||
{
|
||||
private FileReRoute _fileReRoute;
|
||||
private UpstreamTemplatePatternCreator _creator;
|
||||
private UpstreamPathTemplate _result;
|
||||
|
||||
public UpstreamTemplatePatternCreatorTests()
|
||||
{
|
||||
_creator = new UpstreamTemplatePatternCreator();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_set_upstream_template_pattern_to_ignore_case_sensitivity()
|
||||
{
|
||||
var fileReRoute = new FileReRoute
|
||||
{
|
||||
UpstreamPathTemplate = "/PRODUCTS/{productId}",
|
||||
ReRouteIsCaseSensitive = false
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenTheFollowingFileReRoute(fileReRoute))
|
||||
.When(x => x.WhenICreateTheTemplatePattern())
|
||||
.Then(x => x.ThenTheFollowingIsReturned("^(?i)/PRODUCTS/[0-9a-zA-Z].*$"))
|
||||
.And(x => ThenThePriorityIs(1))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
|
||||
[Fact]
|
||||
public void should_match_forward_slash_or_no_forward_slash_if_template_end_with_forward_slash()
|
||||
{
|
||||
var fileReRoute = new FileReRoute
|
||||
{
|
||||
UpstreamPathTemplate = "/PRODUCTS/",
|
||||
ReRouteIsCaseSensitive = false
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenTheFollowingFileReRoute(fileReRoute))
|
||||
.When(x => x.WhenICreateTheTemplatePattern())
|
||||
.Then(x => x.ThenTheFollowingIsReturned("^(?i)/PRODUCTS(/|)$"))
|
||||
.And(x => ThenThePriorityIs(1))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_set_upstream_template_pattern_to_respect_case_sensitivity()
|
||||
{
|
||||
var fileReRoute = new FileReRoute
|
||||
{
|
||||
UpstreamPathTemplate = "/PRODUCTS/{productId}",
|
||||
ReRouteIsCaseSensitive = true
|
||||
};
|
||||
this.Given(x => x.GivenTheFollowingFileReRoute(fileReRoute))
|
||||
.When(x => x.WhenICreateTheTemplatePattern())
|
||||
.Then(x => x.ThenTheFollowingIsReturned("^/PRODUCTS/[0-9a-zA-Z].*$"))
|
||||
.And(x => ThenThePriorityIs(1))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_create_template_pattern_that_matches_anything_to_end_of_string()
|
||||
{
|
||||
var fileReRoute = new FileReRoute
|
||||
{
|
||||
UpstreamPathTemplate = "/api/products/{productId}",
|
||||
ReRouteIsCaseSensitive = true
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenTheFollowingFileReRoute(fileReRoute))
|
||||
.When(x => x.WhenICreateTheTemplatePattern())
|
||||
.Then(x => x.ThenTheFollowingIsReturned("^/api/products/[0-9a-zA-Z].*$"))
|
||||
.And(x => ThenThePriorityIs(1))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_create_template_pattern_that_matches_more_than_one_placeholder()
|
||||
{
|
||||
var fileReRoute = new FileReRoute
|
||||
{
|
||||
UpstreamPathTemplate = "/api/products/{productId}/variants/{variantId}",
|
||||
ReRouteIsCaseSensitive = true
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenTheFollowingFileReRoute(fileReRoute))
|
||||
.When(x => x.WhenICreateTheTemplatePattern())
|
||||
.Then(x => x.ThenTheFollowingIsReturned("^/api/products/[0-9a-zA-Z].*/variants/[0-9a-zA-Z].*$"))
|
||||
.And(x => ThenThePriorityIs(1))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_create_template_pattern_that_matches_more_than_one_placeholder_with_trailing_slash()
|
||||
{
|
||||
var fileReRoute = new FileReRoute
|
||||
{
|
||||
UpstreamPathTemplate = "/api/products/{productId}/variants/{variantId}/",
|
||||
ReRouteIsCaseSensitive = true
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenTheFollowingFileReRoute(fileReRoute))
|
||||
.When(x => x.WhenICreateTheTemplatePattern())
|
||||
.Then(x => x.ThenTheFollowingIsReturned("^/api/products/[0-9a-zA-Z].*/variants/[0-9a-zA-Z].*(/|)$"))
|
||||
.And(x => ThenThePriorityIs(1))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_create_template_pattern_that_matches_to_end_of_string()
|
||||
{
|
||||
var fileReRoute = new FileReRoute
|
||||
{
|
||||
UpstreamPathTemplate = "/"
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenTheFollowingFileReRoute(fileReRoute))
|
||||
.When(x => x.WhenICreateTheTemplatePattern())
|
||||
.Then(x => x.ThenTheFollowingIsReturned("^/$"))
|
||||
.And(x => ThenThePriorityIs(1))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_create_template_pattern_that_matches_to_end_of_string_when_slash_and_placeholder()
|
||||
{
|
||||
var fileReRoute = new FileReRoute
|
||||
{
|
||||
UpstreamPathTemplate = "/{url}"
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenTheFollowingFileReRoute(fileReRoute))
|
||||
.When(x => x.WhenICreateTheTemplatePattern())
|
||||
.Then(x => x.ThenTheFollowingIsReturned("^/.*"))
|
||||
.And(x => ThenThePriorityIs(0))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_create_template_pattern_that_starts_with_placeholder_then_has_another_later()
|
||||
{
|
||||
var fileReRoute = new FileReRoute
|
||||
{
|
||||
UpstreamPathTemplate = "/{productId}/products/variants/{variantId}/",
|
||||
ReRouteIsCaseSensitive = true
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenTheFollowingFileReRoute(fileReRoute))
|
||||
.When(x => x.WhenICreateTheTemplatePattern())
|
||||
.Then(x => x.ThenTheFollowingIsReturned("^/[0-9a-zA-Z].*/products/variants/[0-9a-zA-Z].*(/|)$"))
|
||||
.And(x => ThenThePriorityIs(1))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenTheFollowingFileReRoute(FileReRoute fileReRoute)
|
||||
{
|
||||
_fileReRoute = fileReRoute;
|
||||
}
|
||||
|
||||
private void WhenICreateTheTemplatePattern()
|
||||
{
|
||||
_result = _creator.Create(_fileReRoute);
|
||||
}
|
||||
|
||||
private void ThenTheFollowingIsReturned(string expected)
|
||||
{
|
||||
_result.Template.ShouldBe(expected);
|
||||
}
|
||||
|
||||
private void ThenThePriorityIs(int v)
|
||||
{
|
||||
_result.Priority.ShouldBe(v);
|
||||
}
|
||||
}
|
||||
using System;
|
||||
using Ocelot.Configuration.Creator;
|
||||
using Ocelot.Configuration.File;
|
||||
using Ocelot.Values;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests.Configuration
|
||||
{
|
||||
public class UpstreamTemplatePatternCreatorTests
|
||||
{
|
||||
private FileReRoute _fileReRoute;
|
||||
private UpstreamTemplatePatternCreator _creator;
|
||||
private UpstreamPathTemplate _result;
|
||||
|
||||
public UpstreamTemplatePatternCreatorTests()
|
||||
{
|
||||
_creator = new UpstreamTemplatePatternCreator();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_set_upstream_template_pattern_to_ignore_case_sensitivity()
|
||||
{
|
||||
var fileReRoute = new FileReRoute
|
||||
{
|
||||
UpstreamPathTemplate = "/PRODUCTS/{productId}",
|
||||
ReRouteIsCaseSensitive = false
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenTheFollowingFileReRoute(fileReRoute))
|
||||
.When(x => x.WhenICreateTheTemplatePattern())
|
||||
.Then(x => x.ThenTheFollowingIsReturned("^(?i)/PRODUCTS/[0-9a-zA-Z].*$"))
|
||||
.And(x => ThenThePriorityIs(1))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
|
||||
[Fact]
|
||||
public void should_match_forward_slash_or_no_forward_slash_if_template_end_with_forward_slash()
|
||||
{
|
||||
var fileReRoute = new FileReRoute
|
||||
{
|
||||
UpstreamPathTemplate = "/PRODUCTS/",
|
||||
ReRouteIsCaseSensitive = false
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenTheFollowingFileReRoute(fileReRoute))
|
||||
.When(x => x.WhenICreateTheTemplatePattern())
|
||||
.Then(x => x.ThenTheFollowingIsReturned("^(?i)/PRODUCTS(/|)$"))
|
||||
.And(x => ThenThePriorityIs(1))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_set_upstream_template_pattern_to_respect_case_sensitivity()
|
||||
{
|
||||
var fileReRoute = new FileReRoute
|
||||
{
|
||||
UpstreamPathTemplate = "/PRODUCTS/{productId}",
|
||||
ReRouteIsCaseSensitive = true
|
||||
};
|
||||
this.Given(x => x.GivenTheFollowingFileReRoute(fileReRoute))
|
||||
.When(x => x.WhenICreateTheTemplatePattern())
|
||||
.Then(x => x.ThenTheFollowingIsReturned("^/PRODUCTS/[0-9a-zA-Z].*$"))
|
||||
.And(x => ThenThePriorityIs(1))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_create_template_pattern_that_matches_anything_to_end_of_string()
|
||||
{
|
||||
var fileReRoute = new FileReRoute
|
||||
{
|
||||
UpstreamPathTemplate = "/api/products/{productId}",
|
||||
ReRouteIsCaseSensitive = true
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenTheFollowingFileReRoute(fileReRoute))
|
||||
.When(x => x.WhenICreateTheTemplatePattern())
|
||||
.Then(x => x.ThenTheFollowingIsReturned("^/api/products/[0-9a-zA-Z].*$"))
|
||||
.And(x => ThenThePriorityIs(1))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_create_template_pattern_that_matches_more_than_one_placeholder()
|
||||
{
|
||||
var fileReRoute = new FileReRoute
|
||||
{
|
||||
UpstreamPathTemplate = "/api/products/{productId}/variants/{variantId}",
|
||||
ReRouteIsCaseSensitive = true
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenTheFollowingFileReRoute(fileReRoute))
|
||||
.When(x => x.WhenICreateTheTemplatePattern())
|
||||
.Then(x => x.ThenTheFollowingIsReturned("^/api/products/[0-9a-zA-Z].*/variants/[0-9a-zA-Z].*$"))
|
||||
.And(x => ThenThePriorityIs(1))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_create_template_pattern_that_matches_more_than_one_placeholder_with_trailing_slash()
|
||||
{
|
||||
var fileReRoute = new FileReRoute
|
||||
{
|
||||
UpstreamPathTemplate = "/api/products/{productId}/variants/{variantId}/",
|
||||
ReRouteIsCaseSensitive = true
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenTheFollowingFileReRoute(fileReRoute))
|
||||
.When(x => x.WhenICreateTheTemplatePattern())
|
||||
.Then(x => x.ThenTheFollowingIsReturned("^/api/products/[0-9a-zA-Z].*/variants/[0-9a-zA-Z].*(/|)$"))
|
||||
.And(x => ThenThePriorityIs(1))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_create_template_pattern_that_matches_to_end_of_string()
|
||||
{
|
||||
var fileReRoute = new FileReRoute
|
||||
{
|
||||
UpstreamPathTemplate = "/"
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenTheFollowingFileReRoute(fileReRoute))
|
||||
.When(x => x.WhenICreateTheTemplatePattern())
|
||||
.Then(x => x.ThenTheFollowingIsReturned("^/$"))
|
||||
.And(x => ThenThePriorityIs(1))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_create_template_pattern_that_matches_to_end_of_string_when_slash_and_placeholder()
|
||||
{
|
||||
var fileReRoute = new FileReRoute
|
||||
{
|
||||
UpstreamPathTemplate = "/{url}"
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenTheFollowingFileReRoute(fileReRoute))
|
||||
.When(x => x.WhenICreateTheTemplatePattern())
|
||||
.Then(x => x.ThenTheFollowingIsReturned("^/.*"))
|
||||
.And(x => ThenThePriorityIs(0))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_create_template_pattern_that_starts_with_placeholder_then_has_another_later()
|
||||
{
|
||||
var fileReRoute = new FileReRoute
|
||||
{
|
||||
UpstreamPathTemplate = "/{productId}/products/variants/{variantId}/",
|
||||
ReRouteIsCaseSensitive = true
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenTheFollowingFileReRoute(fileReRoute))
|
||||
.When(x => x.WhenICreateTheTemplatePattern())
|
||||
.Then(x => x.ThenTheFollowingIsReturned("^/[0-9a-zA-Z].*/products/variants/[0-9a-zA-Z].*(/|)$"))
|
||||
.And(x => ThenThePriorityIs(1))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenTheFollowingFileReRoute(FileReRoute fileReRoute)
|
||||
{
|
||||
_fileReRoute = fileReRoute;
|
||||
}
|
||||
|
||||
private void WhenICreateTheTemplatePattern()
|
||||
{
|
||||
_result = _creator.Create(_fileReRoute);
|
||||
}
|
||||
|
||||
private void ThenTheFollowingIsReturned(string expected)
|
||||
{
|
||||
_result.Template.ShouldBe(expected);
|
||||
}
|
||||
|
||||
private void ThenThePriorityIs(int v)
|
||||
{
|
||||
_result.Priority.ShouldBe(v);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,195 +1,195 @@
|
||||
using System;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Moq;
|
||||
using Ocelot.Configuration.File;
|
||||
using Ocelot.Configuration.Setter;
|
||||
using Ocelot.Errors;
|
||||
using Ocelot.Responses;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
using Shouldly;
|
||||
using Ocelot.Configuration.Provider;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Ocelot.Raft;
|
||||
using Rafty.Concensus;
|
||||
using Newtonsoft.Json;
|
||||
using Rafty.FiniteStateMachine;
|
||||
using Ocelot.Configuration;
|
||||
|
||||
namespace Ocelot.UnitTests.Controllers
|
||||
{
|
||||
public class FileConfigurationControllerTests
|
||||
{
|
||||
private FileConfigurationController _controller;
|
||||
private Mock<IFileConfigurationProvider> _configGetter;
|
||||
private Mock<IFileConfigurationSetter> _configSetter;
|
||||
private IActionResult _result;
|
||||
private FileConfiguration _fileConfiguration;
|
||||
private Mock<IServiceProvider> _provider;
|
||||
private Mock<INode> _node;
|
||||
|
||||
public FileConfigurationControllerTests()
|
||||
{
|
||||
_provider = new Mock<IServiceProvider>();
|
||||
_configGetter = new Mock<IFileConfigurationProvider>();
|
||||
_configSetter = new Mock<IFileConfigurationSetter>();
|
||||
_controller = new FileConfigurationController(_configGetter.Object, _configSetter.Object, _provider.Object);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_get_file_configuration()
|
||||
{
|
||||
var expected = new Responses.OkResponse<FileConfiguration>(new FileConfiguration());
|
||||
|
||||
this.Given(x => x.GivenTheGetConfigurationReturns(expected))
|
||||
.When(x => x.WhenIGetTheFileConfiguration())
|
||||
.Then(x => x.TheTheGetFileConfigurationIsCalledCorrectly())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_error_when_cannot_get_config()
|
||||
{
|
||||
var expected = new Responses.ErrorResponse<FileConfiguration>(It.IsAny<Error>());
|
||||
|
||||
this.Given(x => x.GivenTheGetConfigurationReturns(expected))
|
||||
.When(x => x.WhenIGetTheFileConfiguration())
|
||||
.Then(x => x.TheTheGetFileConfigurationIsCalledCorrectly())
|
||||
.And(x => x.ThenTheResponseIs<BadRequestObjectResult>())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_post_file_configuration()
|
||||
{
|
||||
var expected = new FileConfiguration();
|
||||
|
||||
this.Given(x => GivenTheFileConfiguration(expected))
|
||||
.And(x => GivenTheConfigSetterReturns(new OkResponse()))
|
||||
.When(x => WhenIPostTheFileConfiguration())
|
||||
.Then(x => x.ThenTheConfigrationSetterIsCalledCorrectly())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_post_file_configuration_using_raft_node()
|
||||
{
|
||||
var expected = new FileConfiguration();
|
||||
|
||||
this.Given(x => GivenTheFileConfiguration(expected))
|
||||
.And(x => GivenARaftNodeIsRegistered())
|
||||
.And(x => GivenTheNodeReturnsOK())
|
||||
.And(x => GivenTheConfigSetterReturns(new OkResponse()))
|
||||
.When(x => WhenIPostTheFileConfiguration())
|
||||
.Then(x => x.ThenTheNodeIsCalledCorrectly())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_error_when_cannot_set_config_using_raft_node()
|
||||
{
|
||||
var expected = new FileConfiguration();
|
||||
|
||||
this.Given(x => GivenTheFileConfiguration(expected))
|
||||
.And(x => GivenARaftNodeIsRegistered())
|
||||
.And(x => GivenTheNodeReturnsError())
|
||||
.When(x => WhenIPostTheFileConfiguration())
|
||||
.Then(x => ThenTheResponseIs<BadRequestObjectResult>())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_error_when_cannot_set_config()
|
||||
{
|
||||
var expected = new FileConfiguration();
|
||||
|
||||
this.Given(x => GivenTheFileConfiguration(expected))
|
||||
.And(x => GivenTheConfigSetterReturns(new ErrorResponse(new FakeError())))
|
||||
.When(x => WhenIPostTheFileConfiguration())
|
||||
.Then(x => x.ThenTheConfigrationSetterIsCalledCorrectly())
|
||||
.And(x => ThenTheResponseIs<BadRequestObjectResult>())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
|
||||
private void ThenTheNodeIsCalledCorrectly()
|
||||
{
|
||||
_node.Verify(x => x.Accept(It.IsAny<UpdateFileConfiguration>()), Times.Once);
|
||||
}
|
||||
|
||||
private void GivenARaftNodeIsRegistered()
|
||||
{
|
||||
_node = new Mock<INode>();
|
||||
_provider
|
||||
.Setup(x => x.GetService(typeof(INode)))
|
||||
.Returns(_node.Object);
|
||||
}
|
||||
|
||||
private void GivenTheNodeReturnsOK()
|
||||
{
|
||||
_node
|
||||
.Setup(x => x.Accept(It.IsAny<UpdateFileConfiguration>()))
|
||||
.Returns(new Rafty.Concensus.OkResponse<UpdateFileConfiguration>(new UpdateFileConfiguration(new FileConfiguration())));
|
||||
}
|
||||
|
||||
private void GivenTheNodeReturnsError()
|
||||
{
|
||||
_node
|
||||
.Setup(x => x.Accept(It.IsAny<UpdateFileConfiguration>()))
|
||||
.Returns(new Rafty.Concensus.ErrorResponse<UpdateFileConfiguration>("error", new UpdateFileConfiguration(new FileConfiguration())));
|
||||
}
|
||||
|
||||
private void GivenTheConfigSetterReturns(Response response)
|
||||
{
|
||||
_configSetter
|
||||
.Setup(x => x.Set(It.IsAny<FileConfiguration>()))
|
||||
.ReturnsAsync(response);
|
||||
}
|
||||
|
||||
private void ThenTheConfigrationSetterIsCalledCorrectly()
|
||||
{
|
||||
_configSetter
|
||||
.Verify(x => x.Set(_fileConfiguration), Times.Once);
|
||||
}
|
||||
|
||||
private void WhenIPostTheFileConfiguration()
|
||||
{
|
||||
_result = _controller.Post(_fileConfiguration).Result;
|
||||
}
|
||||
|
||||
private void GivenTheFileConfiguration(FileConfiguration fileConfiguration)
|
||||
{
|
||||
_fileConfiguration = fileConfiguration;
|
||||
}
|
||||
|
||||
private void ThenTheResponseIs<T>()
|
||||
{
|
||||
_result.ShouldBeOfType<T>();
|
||||
}
|
||||
|
||||
private void GivenTheGetConfigurationReturns(Ocelot.Responses.Response<FileConfiguration> fileConfiguration)
|
||||
{
|
||||
_configGetter
|
||||
.Setup(x => x.Get())
|
||||
.ReturnsAsync(fileConfiguration);
|
||||
}
|
||||
|
||||
private void WhenIGetTheFileConfiguration()
|
||||
{
|
||||
_result = _controller.Get().Result;
|
||||
}
|
||||
|
||||
private void TheTheGetFileConfigurationIsCalledCorrectly()
|
||||
{
|
||||
_configGetter
|
||||
.Verify(x => x.Get(), Times.Once);
|
||||
}
|
||||
|
||||
class FakeError : Error
|
||||
{
|
||||
public FakeError() : base(string.Empty, OcelotErrorCode.CannotAddDataError)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
using System;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Moq;
|
||||
using Ocelot.Configuration.File;
|
||||
using Ocelot.Configuration.Setter;
|
||||
using Ocelot.Errors;
|
||||
using Ocelot.Responses;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
using Shouldly;
|
||||
using Ocelot.Configuration.Provider;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Ocelot.Raft;
|
||||
using Rafty.Concensus;
|
||||
using Newtonsoft.Json;
|
||||
using Rafty.FiniteStateMachine;
|
||||
using Ocelot.Configuration;
|
||||
|
||||
namespace Ocelot.UnitTests.Controllers
|
||||
{
|
||||
public class FileConfigurationControllerTests
|
||||
{
|
||||
private FileConfigurationController _controller;
|
||||
private Mock<IFileConfigurationProvider> _configGetter;
|
||||
private Mock<IFileConfigurationSetter> _configSetter;
|
||||
private IActionResult _result;
|
||||
private FileConfiguration _fileConfiguration;
|
||||
private Mock<IServiceProvider> _provider;
|
||||
private Mock<INode> _node;
|
||||
|
||||
public FileConfigurationControllerTests()
|
||||
{
|
||||
_provider = new Mock<IServiceProvider>();
|
||||
_configGetter = new Mock<IFileConfigurationProvider>();
|
||||
_configSetter = new Mock<IFileConfigurationSetter>();
|
||||
_controller = new FileConfigurationController(_configGetter.Object, _configSetter.Object, _provider.Object);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_get_file_configuration()
|
||||
{
|
||||
var expected = new Responses.OkResponse<FileConfiguration>(new FileConfiguration());
|
||||
|
||||
this.Given(x => x.GivenTheGetConfigurationReturns(expected))
|
||||
.When(x => x.WhenIGetTheFileConfiguration())
|
||||
.Then(x => x.TheTheGetFileConfigurationIsCalledCorrectly())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_error_when_cannot_get_config()
|
||||
{
|
||||
var expected = new Responses.ErrorResponse<FileConfiguration>(It.IsAny<Error>());
|
||||
|
||||
this.Given(x => x.GivenTheGetConfigurationReturns(expected))
|
||||
.When(x => x.WhenIGetTheFileConfiguration())
|
||||
.Then(x => x.TheTheGetFileConfigurationIsCalledCorrectly())
|
||||
.And(x => x.ThenTheResponseIs<BadRequestObjectResult>())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_post_file_configuration()
|
||||
{
|
||||
var expected = new FileConfiguration();
|
||||
|
||||
this.Given(x => GivenTheFileConfiguration(expected))
|
||||
.And(x => GivenTheConfigSetterReturns(new OkResponse()))
|
||||
.When(x => WhenIPostTheFileConfiguration())
|
||||
.Then(x => x.ThenTheConfigrationSetterIsCalledCorrectly())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_post_file_configuration_using_raft_node()
|
||||
{
|
||||
var expected = new FileConfiguration();
|
||||
|
||||
this.Given(x => GivenTheFileConfiguration(expected))
|
||||
.And(x => GivenARaftNodeIsRegistered())
|
||||
.And(x => GivenTheNodeReturnsOK())
|
||||
.And(x => GivenTheConfigSetterReturns(new OkResponse()))
|
||||
.When(x => WhenIPostTheFileConfiguration())
|
||||
.Then(x => x.ThenTheNodeIsCalledCorrectly())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_error_when_cannot_set_config_using_raft_node()
|
||||
{
|
||||
var expected = new FileConfiguration();
|
||||
|
||||
this.Given(x => GivenTheFileConfiguration(expected))
|
||||
.And(x => GivenARaftNodeIsRegistered())
|
||||
.And(x => GivenTheNodeReturnsError())
|
||||
.When(x => WhenIPostTheFileConfiguration())
|
||||
.Then(x => ThenTheResponseIs<BadRequestObjectResult>())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_error_when_cannot_set_config()
|
||||
{
|
||||
var expected = new FileConfiguration();
|
||||
|
||||
this.Given(x => GivenTheFileConfiguration(expected))
|
||||
.And(x => GivenTheConfigSetterReturns(new ErrorResponse(new FakeError())))
|
||||
.When(x => WhenIPostTheFileConfiguration())
|
||||
.Then(x => x.ThenTheConfigrationSetterIsCalledCorrectly())
|
||||
.And(x => ThenTheResponseIs<BadRequestObjectResult>())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
|
||||
private void ThenTheNodeIsCalledCorrectly()
|
||||
{
|
||||
_node.Verify(x => x.Accept(It.IsAny<UpdateFileConfiguration>()), Times.Once);
|
||||
}
|
||||
|
||||
private void GivenARaftNodeIsRegistered()
|
||||
{
|
||||
_node = new Mock<INode>();
|
||||
_provider
|
||||
.Setup(x => x.GetService(typeof(INode)))
|
||||
.Returns(_node.Object);
|
||||
}
|
||||
|
||||
private void GivenTheNodeReturnsOK()
|
||||
{
|
||||
_node
|
||||
.Setup(x => x.Accept(It.IsAny<UpdateFileConfiguration>()))
|
||||
.Returns(new Rafty.Concensus.OkResponse<UpdateFileConfiguration>(new UpdateFileConfiguration(new FileConfiguration())));
|
||||
}
|
||||
|
||||
private void GivenTheNodeReturnsError()
|
||||
{
|
||||
_node
|
||||
.Setup(x => x.Accept(It.IsAny<UpdateFileConfiguration>()))
|
||||
.Returns(new Rafty.Concensus.ErrorResponse<UpdateFileConfiguration>("error", new UpdateFileConfiguration(new FileConfiguration())));
|
||||
}
|
||||
|
||||
private void GivenTheConfigSetterReturns(Response response)
|
||||
{
|
||||
_configSetter
|
||||
.Setup(x => x.Set(It.IsAny<FileConfiguration>()))
|
||||
.ReturnsAsync(response);
|
||||
}
|
||||
|
||||
private void ThenTheConfigrationSetterIsCalledCorrectly()
|
||||
{
|
||||
_configSetter
|
||||
.Verify(x => x.Set(_fileConfiguration), Times.Once);
|
||||
}
|
||||
|
||||
private void WhenIPostTheFileConfiguration()
|
||||
{
|
||||
_result = _controller.Post(_fileConfiguration).Result;
|
||||
}
|
||||
|
||||
private void GivenTheFileConfiguration(FileConfiguration fileConfiguration)
|
||||
{
|
||||
_fileConfiguration = fileConfiguration;
|
||||
}
|
||||
|
||||
private void ThenTheResponseIs<T>()
|
||||
{
|
||||
_result.ShouldBeOfType<T>();
|
||||
}
|
||||
|
||||
private void GivenTheGetConfigurationReturns(Ocelot.Responses.Response<FileConfiguration> fileConfiguration)
|
||||
{
|
||||
_configGetter
|
||||
.Setup(x => x.Get())
|
||||
.ReturnsAsync(fileConfiguration);
|
||||
}
|
||||
|
||||
private void WhenIGetTheFileConfiguration()
|
||||
{
|
||||
_result = _controller.Get().Result;
|
||||
}
|
||||
|
||||
private void TheTheGetFileConfigurationIsCalledCorrectly()
|
||||
{
|
||||
_configGetter
|
||||
.Verify(x => x.Get(), Times.Once);
|
||||
}
|
||||
|
||||
class FakeError : Error
|
||||
{
|
||||
public FakeError() : base(string.Empty, OcelotErrorCode.CannotAddDataError)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,45 +1,45 @@
|
||||
using Xunit;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Ocelot.Cache;
|
||||
using System;
|
||||
using Moq;
|
||||
using System.Net.Http;
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace Ocelot.UnitTests.Controllers
|
||||
{
|
||||
public class OutputCacheControllerTests
|
||||
{
|
||||
private OutputCacheController _controller;
|
||||
private Mock<IOcelotCache<CachedResponse>> _cache;
|
||||
private IActionResult _result;
|
||||
|
||||
public OutputCacheControllerTests()
|
||||
{
|
||||
_cache = new Mock<IOcelotCache<CachedResponse>>();
|
||||
_controller = new OutputCacheController(_cache.Object);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_delete_key()
|
||||
{
|
||||
this.When(_ => WhenIDeleteTheKey("a"))
|
||||
.Then(_ => ThenTheKeyIsDeleted("a"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void ThenTheKeyIsDeleted(string key)
|
||||
{
|
||||
_result.ShouldBeOfType<NoContentResult>();
|
||||
_cache
|
||||
.Verify(x => x.ClearRegion(key), Times.Once);
|
||||
}
|
||||
|
||||
private void WhenIDeleteTheKey(string key)
|
||||
{
|
||||
_result = _controller.Delete(key);
|
||||
}
|
||||
}
|
||||
using Xunit;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Ocelot.Cache;
|
||||
using System;
|
||||
using Moq;
|
||||
using System.Net.Http;
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace Ocelot.UnitTests.Controllers
|
||||
{
|
||||
public class OutputCacheControllerTests
|
||||
{
|
||||
private OutputCacheController _controller;
|
||||
private Mock<IOcelotCache<CachedResponse>> _cache;
|
||||
private IActionResult _result;
|
||||
|
||||
public OutputCacheControllerTests()
|
||||
{
|
||||
_cache = new Mock<IOcelotCache<CachedResponse>>();
|
||||
_controller = new OutputCacheController(_cache.Object);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_delete_key()
|
||||
{
|
||||
this.When(_ => WhenIDeleteTheKey("a"))
|
||||
.Then(_ => ThenTheKeyIsDeleted("a"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void ThenTheKeyIsDeleted(string key)
|
||||
{
|
||||
_result.ShouldBeOfType<NoContentResult>();
|
||||
_cache
|
||||
.Verify(x => x.ClearRegion(key), Times.Once);
|
||||
}
|
||||
|
||||
private void WhenIDeleteTheKey(string key)
|
||||
{
|
||||
_result = _controller.Delete(key);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,225 +1,225 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Text;
|
||||
using CacheManager.Core;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Hosting.Internal;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Ocelot.Cache;
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.Configuration.File;
|
||||
using Ocelot.Configuration.Setter;
|
||||
using Ocelot.DependencyInjection;
|
||||
using Ocelot.Logging;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests.DependencyInjection
|
||||
{
|
||||
public class OcelotBuilderTests
|
||||
{
|
||||
private IServiceCollection _services;
|
||||
private IServiceProvider _serviceProvider;
|
||||
private IConfiguration _configRoot;
|
||||
private IOcelotBuilder _ocelotBuilder;
|
||||
private int _maxRetries;
|
||||
|
||||
public OcelotBuilderTests()
|
||||
{
|
||||
IWebHostBuilder builder = new WebHostBuilder();
|
||||
_configRoot = new ConfigurationRoot(new List<IConfigurationProvider>());
|
||||
_services = new ServiceCollection();
|
||||
_services.AddSingleton(builder);
|
||||
_services.AddSingleton<IHostingEnvironment, HostingEnvironment>();
|
||||
_services.AddSingleton<IConfiguration>(_configRoot);
|
||||
_maxRetries = 100;
|
||||
}
|
||||
private Exception _ex;
|
||||
|
||||
[Fact]
|
||||
public void should_set_up_services()
|
||||
{
|
||||
this.When(x => WhenISetUpOcelotServices())
|
||||
.Then(x => ThenAnExceptionIsntThrown())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_ocelot_builder()
|
||||
{
|
||||
this.When(x => WhenISetUpOcelotServices())
|
||||
.Then(x => ThenAnOcelotBuilderIsReturned())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
|
||||
[Fact]
|
||||
public void should_set_up_cache_manager()
|
||||
{
|
||||
this.Given(x => WhenISetUpOcelotServices())
|
||||
.When(x => WhenISetUpCacheManager())
|
||||
.Then(x => ThenAnExceptionIsntThrown())
|
||||
.And(x => OnlyOneVersionOfEachCacheIsRegistered())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_set_up_consul()
|
||||
{
|
||||
this.Given(x => WhenISetUpOcelotServices())
|
||||
.When(x => WhenISetUpConsul())
|
||||
.Then(x => ThenAnExceptionIsntThrown())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_set_up_rafty()
|
||||
{
|
||||
this.Given(x => WhenISetUpOcelotServices())
|
||||
.When(x => WhenISetUpRafty())
|
||||
.Then(x => ThenAnExceptionIsntThrown())
|
||||
.Then(x => ThenTheCorrectAdminPathIsRegitered())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_use_logger_factory()
|
||||
{
|
||||
this.Given(x => WhenISetUpOcelotServices())
|
||||
.When(x => WhenIValidateScopes())
|
||||
.When(x => WhenIAccessLoggerFactory())
|
||||
.Then(x => ThenAnExceptionIsntThrown())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_set_up_without_passing_in_config()
|
||||
{
|
||||
this.When(x => WhenISetUpOcelotServicesWithoutConfig())
|
||||
.Then(x => ThenAnExceptionIsntThrown())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void ThenTheCorrectAdminPathIsRegitered()
|
||||
{
|
||||
_serviceProvider = _services.BuildServiceProvider();
|
||||
var path = _serviceProvider.GetService<IAdministrationPath>();
|
||||
path.Path.ShouldBe("/administration");
|
||||
}
|
||||
|
||||
private void OnlyOneVersionOfEachCacheIsRegistered()
|
||||
{
|
||||
var outputCache = _services.Single(x => x.ServiceType == typeof(IOcelotCache<CachedResponse>));
|
||||
var outputCacheManager = _services.Single(x => x.ServiceType == typeof(ICacheManager<CachedResponse>));
|
||||
var thing = (CacheManager.Core.ICacheManager<CachedResponse>)outputCacheManager.ImplementationInstance;
|
||||
thing.Configuration.MaxRetries.ShouldBe(_maxRetries);
|
||||
|
||||
var ocelotConfigCache = _services.Single(x => x.ServiceType == typeof(IOcelotCache<IOcelotConfiguration>));
|
||||
var ocelotConfigCacheManager = _services.Single(x => x.ServiceType == typeof(ICacheManager<IOcelotConfiguration>));
|
||||
|
||||
var fileConfigCache = _services.Single(x => x.ServiceType == typeof(IOcelotCache<FileConfiguration>));
|
||||
var fileConfigCacheManager = _services.Single(x => x.ServiceType == typeof(ICacheManager<FileConfiguration>));
|
||||
}
|
||||
|
||||
private void WhenISetUpConsul()
|
||||
{
|
||||
try
|
||||
{
|
||||
_ocelotBuilder.AddStoreOcelotConfigurationInConsul();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_ex = e;
|
||||
}
|
||||
}
|
||||
|
||||
private void WhenISetUpRafty()
|
||||
{
|
||||
try
|
||||
{
|
||||
_ocelotBuilder.AddAdministration("/administration", "secret").AddRafty();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_ex = e;
|
||||
}
|
||||
}
|
||||
|
||||
private void ThenAnOcelotBuilderIsReturned()
|
||||
{
|
||||
_ocelotBuilder.ShouldBeOfType<OcelotBuilder>();
|
||||
}
|
||||
|
||||
private void WhenISetUpOcelotServices()
|
||||
{
|
||||
try
|
||||
{
|
||||
_ocelotBuilder = _services.AddOcelot(_configRoot);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_ex = e;
|
||||
}
|
||||
}
|
||||
|
||||
private void WhenISetUpOcelotServicesWithoutConfig()
|
||||
{
|
||||
try
|
||||
{
|
||||
_ocelotBuilder = _services.AddOcelot();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_ex = e;
|
||||
}
|
||||
}
|
||||
|
||||
private void WhenISetUpCacheManager()
|
||||
{
|
||||
try
|
||||
{
|
||||
_ocelotBuilder.AddCacheManager(x => {
|
||||
x.WithMaxRetries(_maxRetries);
|
||||
x.WithDictionaryHandle();
|
||||
});
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_ex = e;
|
||||
}
|
||||
}
|
||||
|
||||
private void WhenIAccessLoggerFactory()
|
||||
{
|
||||
try
|
||||
{
|
||||
var logger = _serviceProvider.GetService<IFileConfigurationSetter>();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_ex = e;
|
||||
}
|
||||
}
|
||||
|
||||
private void WhenIValidateScopes()
|
||||
{
|
||||
try
|
||||
{
|
||||
_serviceProvider = _services.BuildServiceProvider(new ServiceProviderOptions { ValidateScopes = true });
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_ex = e;
|
||||
}
|
||||
}
|
||||
|
||||
private void ThenAnExceptionIsntThrown()
|
||||
{
|
||||
_ex.ShouldBeNull();
|
||||
}
|
||||
}
|
||||
}
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Text;
|
||||
using CacheManager.Core;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Hosting.Internal;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Ocelot.Cache;
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.Configuration.File;
|
||||
using Ocelot.Configuration.Setter;
|
||||
using Ocelot.DependencyInjection;
|
||||
using Ocelot.Logging;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests.DependencyInjection
|
||||
{
|
||||
public class OcelotBuilderTests
|
||||
{
|
||||
private IServiceCollection _services;
|
||||
private IServiceProvider _serviceProvider;
|
||||
private IConfiguration _configRoot;
|
||||
private IOcelotBuilder _ocelotBuilder;
|
||||
private int _maxRetries;
|
||||
|
||||
public OcelotBuilderTests()
|
||||
{
|
||||
IWebHostBuilder builder = new WebHostBuilder();
|
||||
_configRoot = new ConfigurationRoot(new List<IConfigurationProvider>());
|
||||
_services = new ServiceCollection();
|
||||
_services.AddSingleton(builder);
|
||||
_services.AddSingleton<IHostingEnvironment, HostingEnvironment>();
|
||||
_services.AddSingleton<IConfiguration>(_configRoot);
|
||||
_maxRetries = 100;
|
||||
}
|
||||
private Exception _ex;
|
||||
|
||||
[Fact]
|
||||
public void should_set_up_services()
|
||||
{
|
||||
this.When(x => WhenISetUpOcelotServices())
|
||||
.Then(x => ThenAnExceptionIsntThrown())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_ocelot_builder()
|
||||
{
|
||||
this.When(x => WhenISetUpOcelotServices())
|
||||
.Then(x => ThenAnOcelotBuilderIsReturned())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
|
||||
[Fact]
|
||||
public void should_set_up_cache_manager()
|
||||
{
|
||||
this.Given(x => WhenISetUpOcelotServices())
|
||||
.When(x => WhenISetUpCacheManager())
|
||||
.Then(x => ThenAnExceptionIsntThrown())
|
||||
.And(x => OnlyOneVersionOfEachCacheIsRegistered())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_set_up_consul()
|
||||
{
|
||||
this.Given(x => WhenISetUpOcelotServices())
|
||||
.When(x => WhenISetUpConsul())
|
||||
.Then(x => ThenAnExceptionIsntThrown())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_set_up_rafty()
|
||||
{
|
||||
this.Given(x => WhenISetUpOcelotServices())
|
||||
.When(x => WhenISetUpRafty())
|
||||
.Then(x => ThenAnExceptionIsntThrown())
|
||||
.Then(x => ThenTheCorrectAdminPathIsRegitered())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_use_logger_factory()
|
||||
{
|
||||
this.Given(x => WhenISetUpOcelotServices())
|
||||
.When(x => WhenIValidateScopes())
|
||||
.When(x => WhenIAccessLoggerFactory())
|
||||
.Then(x => ThenAnExceptionIsntThrown())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_set_up_without_passing_in_config()
|
||||
{
|
||||
this.When(x => WhenISetUpOcelotServicesWithoutConfig())
|
||||
.Then(x => ThenAnExceptionIsntThrown())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void ThenTheCorrectAdminPathIsRegitered()
|
||||
{
|
||||
_serviceProvider = _services.BuildServiceProvider();
|
||||
var path = _serviceProvider.GetService<IAdministrationPath>();
|
||||
path.Path.ShouldBe("/administration");
|
||||
}
|
||||
|
||||
private void OnlyOneVersionOfEachCacheIsRegistered()
|
||||
{
|
||||
var outputCache = _services.Single(x => x.ServiceType == typeof(IOcelotCache<CachedResponse>));
|
||||
var outputCacheManager = _services.Single(x => x.ServiceType == typeof(ICacheManager<CachedResponse>));
|
||||
var thing = (CacheManager.Core.ICacheManager<CachedResponse>)outputCacheManager.ImplementationInstance;
|
||||
thing.Configuration.MaxRetries.ShouldBe(_maxRetries);
|
||||
|
||||
var ocelotConfigCache = _services.Single(x => x.ServiceType == typeof(IOcelotCache<IOcelotConfiguration>));
|
||||
var ocelotConfigCacheManager = _services.Single(x => x.ServiceType == typeof(ICacheManager<IOcelotConfiguration>));
|
||||
|
||||
var fileConfigCache = _services.Single(x => x.ServiceType == typeof(IOcelotCache<FileConfiguration>));
|
||||
var fileConfigCacheManager = _services.Single(x => x.ServiceType == typeof(ICacheManager<FileConfiguration>));
|
||||
}
|
||||
|
||||
private void WhenISetUpConsul()
|
||||
{
|
||||
try
|
||||
{
|
||||
_ocelotBuilder.AddStoreOcelotConfigurationInConsul();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_ex = e;
|
||||
}
|
||||
}
|
||||
|
||||
private void WhenISetUpRafty()
|
||||
{
|
||||
try
|
||||
{
|
||||
_ocelotBuilder.AddAdministration("/administration", "secret").AddRafty();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_ex = e;
|
||||
}
|
||||
}
|
||||
|
||||
private void ThenAnOcelotBuilderIsReturned()
|
||||
{
|
||||
_ocelotBuilder.ShouldBeOfType<OcelotBuilder>();
|
||||
}
|
||||
|
||||
private void WhenISetUpOcelotServices()
|
||||
{
|
||||
try
|
||||
{
|
||||
_ocelotBuilder = _services.AddOcelot(_configRoot);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_ex = e;
|
||||
}
|
||||
}
|
||||
|
||||
private void WhenISetUpOcelotServicesWithoutConfig()
|
||||
{
|
||||
try
|
||||
{
|
||||
_ocelotBuilder = _services.AddOcelot();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_ex = e;
|
||||
}
|
||||
}
|
||||
|
||||
private void WhenISetUpCacheManager()
|
||||
{
|
||||
try
|
||||
{
|
||||
_ocelotBuilder.AddCacheManager(x => {
|
||||
x.WithMaxRetries(_maxRetries);
|
||||
x.WithDictionaryHandle();
|
||||
});
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_ex = e;
|
||||
}
|
||||
}
|
||||
|
||||
private void WhenIAccessLoggerFactory()
|
||||
{
|
||||
try
|
||||
{
|
||||
var logger = _serviceProvider.GetService<IFileConfigurationSetter>();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_ex = e;
|
||||
}
|
||||
}
|
||||
|
||||
private void WhenIValidateScopes()
|
||||
{
|
||||
try
|
||||
{
|
||||
_serviceProvider = _services.BuildServiceProvider(new ServiceProviderOptions { ValidateScopes = true });
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_ex = e;
|
||||
}
|
||||
}
|
||||
|
||||
private void ThenAnExceptionIsntThrown()
|
||||
{
|
||||
_ex.ShouldBeNull();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,91 +1,91 @@
|
||||
namespace Ocelot.UnitTests.DownstreamRouteFinder
|
||||
{
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Moq;
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.Configuration.Builder;
|
||||
using Ocelot.Configuration.Provider;
|
||||
using Ocelot.DownstreamRouteFinder;
|
||||
using Ocelot.DownstreamRouteFinder.Finder;
|
||||
using Ocelot.DownstreamRouteFinder.Middleware;
|
||||
using Ocelot.DownstreamRouteFinder.UrlMatcher;
|
||||
using Ocelot.Logging;
|
||||
using Ocelot.Responses;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
public class DownstreamRouteFinderMiddlewareTests : ServerHostedMiddlewareTest
|
||||
{
|
||||
private readonly Mock<IDownstreamRouteFinder> _downstreamRouteFinder;
|
||||
private readonly Mock<IOcelotConfigurationProvider> _provider;
|
||||
private Response<DownstreamRoute> _downstreamRoute;
|
||||
private IOcelotConfiguration _config;
|
||||
|
||||
public DownstreamRouteFinderMiddlewareTests()
|
||||
{
|
||||
_provider = new Mock<IOcelotConfigurationProvider>();
|
||||
_downstreamRouteFinder = new Mock<IDownstreamRouteFinder>();
|
||||
|
||||
GivenTheTestServerIsConfigured();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_call_scoped_data_repository_correctly()
|
||||
{
|
||||
var config = new OcelotConfiguration(null, null, new ServiceProviderConfigurationBuilder().Build(), "");
|
||||
|
||||
this.Given(x => x.GivenTheDownStreamRouteFinderReturns(
|
||||
new DownstreamRoute(
|
||||
new List<PlaceholderNameAndValue>(),
|
||||
new ReRouteBuilder()
|
||||
.WithDownstreamPathTemplate("any old string")
|
||||
.WithUpstreamHttpMethod(new List<string> { "Get" })
|
||||
.Build())))
|
||||
.And(x => GivenTheFollowingConfig(config))
|
||||
.When(x => x.WhenICallTheMiddleware())
|
||||
.Then(x => x.ThenTheScopedDataRepositoryIsCalledCorrectly())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenTheFollowingConfig(IOcelotConfiguration config)
|
||||
{
|
||||
_config = config;
|
||||
_provider
|
||||
.Setup(x => x.Get())
|
||||
.ReturnsAsync(new OkResponse<IOcelotConfiguration>(_config));
|
||||
}
|
||||
|
||||
protected override void GivenTheTestServerServicesAreConfigured(IServiceCollection services)
|
||||
{
|
||||
services.AddSingleton<IOcelotLoggerFactory, AspDotNetLoggerFactory>();
|
||||
services.AddLogging();
|
||||
services.AddSingleton(_downstreamRouteFinder.Object);
|
||||
services.AddSingleton(_provider.Object);
|
||||
services.AddSingleton(ScopedRepository.Object);
|
||||
}
|
||||
|
||||
protected override void GivenTheTestServerPipelineIsConfigured(IApplicationBuilder app)
|
||||
{
|
||||
app.UseDownstreamRouteFinderMiddleware();
|
||||
}
|
||||
|
||||
private void GivenTheDownStreamRouteFinderReturns(DownstreamRoute downstreamRoute)
|
||||
{
|
||||
_downstreamRoute = new OkResponse<DownstreamRoute>(downstreamRoute);
|
||||
_downstreamRouteFinder
|
||||
.Setup(x => x.FindDownstreamRoute(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<IOcelotConfiguration>()))
|
||||
.Returns(_downstreamRoute);
|
||||
}
|
||||
|
||||
private void ThenTheScopedDataRepositoryIsCalledCorrectly()
|
||||
{
|
||||
ScopedRepository
|
||||
.Verify(x => x.Add("DownstreamRoute", _downstreamRoute.Data), Times.Once());
|
||||
|
||||
ScopedRepository
|
||||
.Verify(x => x.Add("ServiceProviderConfiguration", _config.ServiceProviderConfiguration), Times.Once());
|
||||
}
|
||||
}
|
||||
}
|
||||
namespace Ocelot.UnitTests.DownstreamRouteFinder
|
||||
{
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Moq;
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.Configuration.Builder;
|
||||
using Ocelot.Configuration.Provider;
|
||||
using Ocelot.DownstreamRouteFinder;
|
||||
using Ocelot.DownstreamRouteFinder.Finder;
|
||||
using Ocelot.DownstreamRouteFinder.Middleware;
|
||||
using Ocelot.DownstreamRouteFinder.UrlMatcher;
|
||||
using Ocelot.Logging;
|
||||
using Ocelot.Responses;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
public class DownstreamRouteFinderMiddlewareTests : ServerHostedMiddlewareTest
|
||||
{
|
||||
private readonly Mock<IDownstreamRouteFinder> _downstreamRouteFinder;
|
||||
private readonly Mock<IOcelotConfigurationProvider> _provider;
|
||||
private Response<DownstreamRoute> _downstreamRoute;
|
||||
private IOcelotConfiguration _config;
|
||||
|
||||
public DownstreamRouteFinderMiddlewareTests()
|
||||
{
|
||||
_provider = new Mock<IOcelotConfigurationProvider>();
|
||||
_downstreamRouteFinder = new Mock<IDownstreamRouteFinder>();
|
||||
|
||||
GivenTheTestServerIsConfigured();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_call_scoped_data_repository_correctly()
|
||||
{
|
||||
var config = new OcelotConfiguration(null, null, new ServiceProviderConfigurationBuilder().Build(), "");
|
||||
|
||||
this.Given(x => x.GivenTheDownStreamRouteFinderReturns(
|
||||
new DownstreamRoute(
|
||||
new List<PlaceholderNameAndValue>(),
|
||||
new ReRouteBuilder()
|
||||
.WithDownstreamPathTemplate("any old string")
|
||||
.WithUpstreamHttpMethod(new List<string> { "Get" })
|
||||
.Build())))
|
||||
.And(x => GivenTheFollowingConfig(config))
|
||||
.When(x => x.WhenICallTheMiddleware())
|
||||
.Then(x => x.ThenTheScopedDataRepositoryIsCalledCorrectly())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenTheFollowingConfig(IOcelotConfiguration config)
|
||||
{
|
||||
_config = config;
|
||||
_provider
|
||||
.Setup(x => x.Get())
|
||||
.ReturnsAsync(new OkResponse<IOcelotConfiguration>(_config));
|
||||
}
|
||||
|
||||
protected override void GivenTheTestServerServicesAreConfigured(IServiceCollection services)
|
||||
{
|
||||
services.AddSingleton<IOcelotLoggerFactory, AspDotNetLoggerFactory>();
|
||||
services.AddLogging();
|
||||
services.AddSingleton(_downstreamRouteFinder.Object);
|
||||
services.AddSingleton(_provider.Object);
|
||||
services.AddSingleton(ScopedRepository.Object);
|
||||
}
|
||||
|
||||
protected override void GivenTheTestServerPipelineIsConfigured(IApplicationBuilder app)
|
||||
{
|
||||
app.UseDownstreamRouteFinderMiddleware();
|
||||
}
|
||||
|
||||
private void GivenTheDownStreamRouteFinderReturns(DownstreamRoute downstreamRoute)
|
||||
{
|
||||
_downstreamRoute = new OkResponse<DownstreamRoute>(downstreamRoute);
|
||||
_downstreamRouteFinder
|
||||
.Setup(x => x.FindDownstreamRoute(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<IOcelotConfiguration>()))
|
||||
.Returns(_downstreamRoute);
|
||||
}
|
||||
|
||||
private void ThenTheScopedDataRepositoryIsCalledCorrectly()
|
||||
{
|
||||
ScopedRepository
|
||||
.Verify(x => x.Add("DownstreamRoute", _downstreamRoute.Data), Times.Once());
|
||||
|
||||
ScopedRepository
|
||||
.Verify(x => x.Add("ServiceProviderConfiguration", _config.ServiceProviderConfiguration), Times.Once());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,452 +1,452 @@
|
||||
using System.Collections.Generic;
|
||||
using Moq;
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.Configuration.Builder;
|
||||
using Ocelot.Configuration.Creator;
|
||||
using Ocelot.Configuration.Provider;
|
||||
using Ocelot.DownstreamRouteFinder;
|
||||
using Ocelot.DownstreamRouteFinder.Finder;
|
||||
using Ocelot.DownstreamRouteFinder.UrlMatcher;
|
||||
using Ocelot.Responses;
|
||||
using Ocelot.Values;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests.DownstreamRouteFinder
|
||||
{
|
||||
public class DownstreamRouteFinderTests
|
||||
{
|
||||
private readonly IDownstreamRouteFinder _downstreamRouteFinder;
|
||||
private readonly Mock<IUrlPathToUrlTemplateMatcher> _mockMatcher;
|
||||
private readonly Mock<IPlaceholderNameAndValueFinder> _finder;
|
||||
private string _upstreamUrlPath;
|
||||
private Response<DownstreamRoute> _result;
|
||||
private List<ReRoute> _reRoutesConfig;
|
||||
private OcelotConfiguration _config;
|
||||
private Response<UrlMatch> _match;
|
||||
private string _upstreamHttpMethod;
|
||||
|
||||
public DownstreamRouteFinderTests()
|
||||
{
|
||||
_mockMatcher = new Mock<IUrlPathToUrlTemplateMatcher>();
|
||||
_finder = new Mock<IPlaceholderNameAndValueFinder>();
|
||||
_downstreamRouteFinder = new Ocelot.DownstreamRouteFinder.Finder.DownstreamRouteFinder(_mockMatcher.Object, _finder.Object);
|
||||
}
|
||||
|
||||
|
||||
[Fact]
|
||||
public void should_return_highest_priority_when_first()
|
||||
{
|
||||
var serviceProviderConfig = new ServiceProviderConfigurationBuilder().Build();
|
||||
|
||||
this.Given(x => x.GivenThereIsAnUpstreamUrlPath("someUpstreamPath"))
|
||||
.And(x => x.GivenTheTemplateVariableAndNameFinderReturns(
|
||||
new OkResponse<List<PlaceholderNameAndValue>>(new List<PlaceholderNameAndValue>())))
|
||||
.And(x => x.GivenTheConfigurationIs(new List<ReRoute>
|
||||
{
|
||||
new ReRouteBuilder()
|
||||
.WithDownstreamPathTemplate("someDownstreamPath")
|
||||
.WithUpstreamPathTemplate("someUpstreamPath")
|
||||
.WithUpstreamHttpMethod(new List<string> { "Post" })
|
||||
.WithUpstreamTemplatePattern(new UpstreamPathTemplate("test", 1))
|
||||
.Build(),
|
||||
new ReRouteBuilder()
|
||||
.WithDownstreamPathTemplate("someDownstreamPath")
|
||||
.WithUpstreamPathTemplate("someUpstreamPath")
|
||||
.WithUpstreamHttpMethod(new List<string> { "Post" })
|
||||
.WithUpstreamTemplatePattern(new UpstreamPathTemplate("test", 0))
|
||||
.Build()
|
||||
}, string.Empty, serviceProviderConfig))
|
||||
.And(x => x.GivenTheUrlMatcherReturns(new OkResponse<UrlMatch>(new UrlMatch(true))))
|
||||
.And(x => x.GivenTheUpstreamHttpMethodIs("Post"))
|
||||
.When(x => x.WhenICallTheFinder())
|
||||
.Then(x => x.ThenTheFollowingIsReturned(new DownstreamRoute(new List<PlaceholderNameAndValue>(),
|
||||
new ReRouteBuilder()
|
||||
.WithDownstreamPathTemplate("someDownstreamPath")
|
||||
.WithUpstreamTemplatePattern(new UpstreamPathTemplate("test", 1))
|
||||
.WithUpstreamHttpMethod(new List<string> { "Post" })
|
||||
.WithUpstreamTemplatePattern(new UpstreamPathTemplate("test", 1))
|
||||
.Build()
|
||||
)))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_highest_priority_when_lowest()
|
||||
{
|
||||
var serviceProviderConfig = new ServiceProviderConfigurationBuilder().Build();
|
||||
|
||||
this.Given(x => x.GivenThereIsAnUpstreamUrlPath("someUpstreamPath"))
|
||||
.And(x => x.GivenTheTemplateVariableAndNameFinderReturns(
|
||||
new OkResponse<List<PlaceholderNameAndValue>>(new List<PlaceholderNameAndValue>())))
|
||||
.And(x => x.GivenTheConfigurationIs(new List<ReRoute>
|
||||
{
|
||||
new ReRouteBuilder()
|
||||
.WithDownstreamPathTemplate("someDownstreamPath")
|
||||
.WithUpstreamPathTemplate("someUpstreamPath")
|
||||
.WithUpstreamHttpMethod(new List<string> { "Post" })
|
||||
.WithUpstreamTemplatePattern(new UpstreamPathTemplate("test", 0))
|
||||
.Build(),
|
||||
new ReRouteBuilder()
|
||||
.WithDownstreamPathTemplate("someDownstreamPath")
|
||||
.WithUpstreamPathTemplate("someUpstreamPath")
|
||||
.WithUpstreamHttpMethod(new List<string> { "Post" })
|
||||
.WithUpstreamTemplatePattern(new UpstreamPathTemplate("test", 1))
|
||||
.Build()
|
||||
}, string.Empty, serviceProviderConfig))
|
||||
.And(x => x.GivenTheUrlMatcherReturns(new OkResponse<UrlMatch>(new UrlMatch(true))))
|
||||
.And(x => x.GivenTheUpstreamHttpMethodIs("Post"))
|
||||
.When(x => x.WhenICallTheFinder())
|
||||
.Then(x => x.ThenTheFollowingIsReturned(new DownstreamRoute(new List<PlaceholderNameAndValue>(),
|
||||
new ReRouteBuilder()
|
||||
.WithDownstreamPathTemplate("someDownstreamPath")
|
||||
.WithUpstreamTemplatePattern(new UpstreamPathTemplate("test", 1))
|
||||
.WithUpstreamHttpMethod(new List<string> { "Post" })
|
||||
.Build()
|
||||
)))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_route()
|
||||
{
|
||||
var serviceProviderConfig = new ServiceProviderConfigurationBuilder().Build();
|
||||
|
||||
this.Given(x => x.GivenThereIsAnUpstreamUrlPath("matchInUrlMatcher/"))
|
||||
.And(x =>x.GivenTheTemplateVariableAndNameFinderReturns(
|
||||
new OkResponse<List<PlaceholderNameAndValue>>(
|
||||
new List<PlaceholderNameAndValue>())))
|
||||
.And(x => x.GivenTheConfigurationIs(new List<ReRoute>
|
||||
{
|
||||
new ReRouteBuilder()
|
||||
.WithDownstreamPathTemplate("someDownstreamPath")
|
||||
.WithUpstreamPathTemplate("someUpstreamPath")
|
||||
.WithUpstreamHttpMethod(new List<string> { "Get" })
|
||||
.WithUpstreamTemplatePattern(new UpstreamPathTemplate("someUpstreamPath", 1))
|
||||
.Build()
|
||||
}, string.Empty, serviceProviderConfig
|
||||
))
|
||||
.And(x => x.GivenTheUrlMatcherReturns(new OkResponse<UrlMatch>(new UrlMatch(true))))
|
||||
.And(x => x.GivenTheUpstreamHttpMethodIs("Get"))
|
||||
.When(x => x.WhenICallTheFinder())
|
||||
.Then(
|
||||
x => x.ThenTheFollowingIsReturned(new DownstreamRoute(
|
||||
new List<PlaceholderNameAndValue>(),
|
||||
new ReRouteBuilder()
|
||||
.WithDownstreamPathTemplate("someDownstreamPath")
|
||||
.WithUpstreamHttpMethod(new List<string> { "Get" })
|
||||
.WithUpstreamTemplatePattern(new UpstreamPathTemplate("someUpstreamPath", 1))
|
||||
.Build()
|
||||
)))
|
||||
.And(x => x.ThenTheUrlMatcherIsCalledCorrectly())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
|
||||
[Fact]
|
||||
public void should_not_append_slash_to_upstream_url_path()
|
||||
{
|
||||
var serviceProviderConfig = new ServiceProviderConfigurationBuilder().Build();
|
||||
|
||||
this.Given(x => x.GivenThereIsAnUpstreamUrlPath("matchInUrlMatcher"))
|
||||
.And(x =>x.GivenTheTemplateVariableAndNameFinderReturns(
|
||||
new OkResponse<List<PlaceholderNameAndValue>>(
|
||||
new List<PlaceholderNameAndValue>())))
|
||||
.And(x => x.GivenTheConfigurationIs(new List<ReRoute>
|
||||
{
|
||||
new ReRouteBuilder()
|
||||
.WithDownstreamPathTemplate("someDownstreamPath")
|
||||
.WithUpstreamPathTemplate("someUpstreamPath")
|
||||
.WithUpstreamHttpMethod(new List<string> { "Get" })
|
||||
.WithUpstreamTemplatePattern(new UpstreamPathTemplate("someUpstreamPath", 1))
|
||||
.Build()
|
||||
}, string.Empty, serviceProviderConfig
|
||||
))
|
||||
.And(x => x.GivenTheUrlMatcherReturns(new OkResponse<UrlMatch>(new UrlMatch(true))))
|
||||
.And(x => x.GivenTheUpstreamHttpMethodIs("Get"))
|
||||
.When(x => x.WhenICallTheFinder())
|
||||
.Then(
|
||||
x => x.ThenTheFollowingIsReturned(new DownstreamRoute(
|
||||
new List<PlaceholderNameAndValue>(),
|
||||
new ReRouteBuilder()
|
||||
.WithDownstreamPathTemplate("someDownstreamPath")
|
||||
.WithUpstreamHttpMethod(new List<string> { "Get" })
|
||||
.WithUpstreamTemplatePattern(new UpstreamPathTemplate("someUpstreamPath", 1))
|
||||
.Build()
|
||||
)))
|
||||
.And(x => x.ThenTheUrlMatcherIsCalledCorrectly("matchInUrlMatcher"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_route_if_upstream_path_and_upstream_template_are_the_same()
|
||||
{
|
||||
var serviceProviderConfig = new ServiceProviderConfigurationBuilder().Build();
|
||||
|
||||
this.Given(x => x.GivenThereIsAnUpstreamUrlPath("someUpstreamPath"))
|
||||
.And(
|
||||
x =>
|
||||
x.GivenTheTemplateVariableAndNameFinderReturns(
|
||||
new OkResponse<List<PlaceholderNameAndValue>>(new List<PlaceholderNameAndValue>())))
|
||||
.And(x => x.GivenTheConfigurationIs(new List<ReRoute>
|
||||
{
|
||||
new ReRouteBuilder()
|
||||
.WithDownstreamPathTemplate("someDownstreamPath")
|
||||
.WithUpstreamPathTemplate("someUpstreamPath")
|
||||
.WithUpstreamHttpMethod(new List<string> { "Get" })
|
||||
.WithUpstreamTemplatePattern(new UpstreamPathTemplate("someUpstreamPath", 1))
|
||||
.Build()
|
||||
}, string.Empty, serviceProviderConfig
|
||||
))
|
||||
.And(x => x.GivenTheUrlMatcherReturns(new OkResponse<UrlMatch>(new UrlMatch(true))))
|
||||
.And(x => x.GivenTheUpstreamHttpMethodIs("Get"))
|
||||
.When(x => x.WhenICallTheFinder())
|
||||
.Then(
|
||||
x => x.ThenTheFollowingIsReturned(new DownstreamRoute(new List<PlaceholderNameAndValue>(),
|
||||
new ReRouteBuilder()
|
||||
.WithDownstreamPathTemplate("someDownstreamPath")
|
||||
.WithUpstreamHttpMethod(new List<string> { "Get" })
|
||||
.WithUpstreamTemplatePattern(new UpstreamPathTemplate("someUpstreamPath", 1))
|
||||
.Build()
|
||||
)))
|
||||
.And(x => x.ThenTheUrlMatcherIsNotCalled())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_correct_route_for_http_verb()
|
||||
{
|
||||
var serviceProviderConfig = new ServiceProviderConfigurationBuilder().Build();
|
||||
|
||||
this.Given(x => x.GivenThereIsAnUpstreamUrlPath("someUpstreamPath"))
|
||||
.And(
|
||||
x =>
|
||||
x.GivenTheTemplateVariableAndNameFinderReturns(
|
||||
new OkResponse<List<PlaceholderNameAndValue>>(new List<PlaceholderNameAndValue>())))
|
||||
.And(x => x.GivenTheConfigurationIs(new List<ReRoute>
|
||||
{
|
||||
new ReRouteBuilder()
|
||||
.WithDownstreamPathTemplate("someDownstreamPath")
|
||||
.WithUpstreamPathTemplate("someUpstreamPath")
|
||||
.WithUpstreamHttpMethod(new List<string> { "Get" })
|
||||
.WithUpstreamTemplatePattern(new UpstreamPathTemplate("", 1))
|
||||
.Build(),
|
||||
new ReRouteBuilder()
|
||||
.WithDownstreamPathTemplate("someDownstreamPathForAPost")
|
||||
.WithUpstreamPathTemplate("someUpstreamPath")
|
||||
.WithUpstreamHttpMethod(new List<string> { "Post" })
|
||||
.WithUpstreamTemplatePattern(new UpstreamPathTemplate("", 1))
|
||||
.Build()
|
||||
}, string.Empty, serviceProviderConfig
|
||||
))
|
||||
.And(x => x.GivenTheUrlMatcherReturns(new OkResponse<UrlMatch>(new UrlMatch(true))))
|
||||
.And(x => x.GivenTheUpstreamHttpMethodIs("Post"))
|
||||
.When(x => x.WhenICallTheFinder())
|
||||
.Then(
|
||||
x => x.ThenTheFollowingIsReturned(new DownstreamRoute(new List<PlaceholderNameAndValue>(),
|
||||
new ReRouteBuilder()
|
||||
.WithDownstreamPathTemplate("someDownstreamPathForAPost")
|
||||
.WithUpstreamHttpMethod(new List<string> { "Post" })
|
||||
.WithUpstreamTemplatePattern(new UpstreamPathTemplate("", 1))
|
||||
.Build()
|
||||
)))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_not_return_route()
|
||||
{
|
||||
var serviceProviderConfig = new ServiceProviderConfigurationBuilder().Build();
|
||||
|
||||
this.Given(x => x.GivenThereIsAnUpstreamUrlPath("dontMatchPath/"))
|
||||
.And(x => x.GivenTheConfigurationIs(new List<ReRoute>
|
||||
{
|
||||
new ReRouteBuilder()
|
||||
.WithDownstreamPathTemplate("somPath")
|
||||
.WithUpstreamPathTemplate("somePath")
|
||||
.WithUpstreamHttpMethod(new List<string> { "Get" })
|
||||
.WithUpstreamTemplatePattern(new UpstreamPathTemplate("somePath", 1))
|
||||
.Build(),
|
||||
}, string.Empty, serviceProviderConfig
|
||||
))
|
||||
.And(x => x.GivenTheUrlMatcherReturns(new OkResponse<UrlMatch>(new UrlMatch(false))))
|
||||
.And(x => x.GivenTheUpstreamHttpMethodIs("Get"))
|
||||
.When(x => x.WhenICallTheFinder())
|
||||
.Then(
|
||||
x => x.ThenAnErrorResponseIsReturned())
|
||||
.And(x => x.ThenTheUrlMatcherIsCalledCorrectly())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_correct_route_for_http_verb_setting_multiple_upstream_http_method()
|
||||
{
|
||||
var serviceProviderConfig = new ServiceProviderConfigurationBuilder().Build();
|
||||
|
||||
this.Given(x => x.GivenThereIsAnUpstreamUrlPath("someUpstreamPath"))
|
||||
.And(
|
||||
x =>
|
||||
x.GivenTheTemplateVariableAndNameFinderReturns(
|
||||
new OkResponse<List<PlaceholderNameAndValue>>(new List<PlaceholderNameAndValue>())))
|
||||
.And(x => x.GivenTheConfigurationIs(new List<ReRoute>
|
||||
{
|
||||
new ReRouteBuilder()
|
||||
.WithDownstreamPathTemplate("someDownstreamPath")
|
||||
.WithUpstreamPathTemplate("someUpstreamPath")
|
||||
.WithUpstreamHttpMethod(new List<string> { "Get", "Post" })
|
||||
.WithUpstreamTemplatePattern(new UpstreamPathTemplate("", 1))
|
||||
.Build()
|
||||
}, string.Empty, serviceProviderConfig
|
||||
))
|
||||
.And(x => x.GivenTheUrlMatcherReturns(new OkResponse<UrlMatch>(new UrlMatch(true))))
|
||||
.And(x => x.GivenTheUpstreamHttpMethodIs("Post"))
|
||||
.When(x => x.WhenICallTheFinder())
|
||||
.Then(
|
||||
x => x.ThenTheFollowingIsReturned(new DownstreamRoute(new List<PlaceholderNameAndValue>(),
|
||||
new ReRouteBuilder()
|
||||
.WithDownstreamPathTemplate("someDownstreamPath")
|
||||
.WithUpstreamHttpMethod(new List<string> { "Post" })
|
||||
.WithUpstreamTemplatePattern(new UpstreamPathTemplate("", 1))
|
||||
.Build()
|
||||
)))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_correct_route_for_http_verb_setting_all_upstream_http_method()
|
||||
{
|
||||
var serviceProviderConfig = new ServiceProviderConfigurationBuilder().Build();
|
||||
|
||||
this.Given(x => x.GivenThereIsAnUpstreamUrlPath("someUpstreamPath"))
|
||||
.And(
|
||||
x =>
|
||||
x.GivenTheTemplateVariableAndNameFinderReturns(
|
||||
new OkResponse<List<PlaceholderNameAndValue>>(new List<PlaceholderNameAndValue>())))
|
||||
.And(x => x.GivenTheConfigurationIs(new List<ReRoute>
|
||||
{
|
||||
new ReRouteBuilder()
|
||||
.WithDownstreamPathTemplate("someDownstreamPath")
|
||||
.WithUpstreamPathTemplate("someUpstreamPath")
|
||||
.WithUpstreamHttpMethod(new List<string>())
|
||||
.WithUpstreamTemplatePattern(new UpstreamPathTemplate("", 1))
|
||||
.Build()
|
||||
}, string.Empty, serviceProviderConfig
|
||||
))
|
||||
.And(x => x.GivenTheUrlMatcherReturns(new OkResponse<UrlMatch>(new UrlMatch(true))))
|
||||
.And(x => x.GivenTheUpstreamHttpMethodIs("Post"))
|
||||
.When(x => x.WhenICallTheFinder())
|
||||
.Then(
|
||||
x => x.ThenTheFollowingIsReturned(new DownstreamRoute(new List<PlaceholderNameAndValue>(),
|
||||
new ReRouteBuilder()
|
||||
.WithDownstreamPathTemplate("someDownstreamPath")
|
||||
.WithUpstreamHttpMethod(new List<string> { "Post" })
|
||||
.WithUpstreamTemplatePattern(new UpstreamPathTemplate("", 1))
|
||||
.Build()
|
||||
)))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_not_return_route_for_http_verb_not_setting_in_upstream_http_method()
|
||||
{
|
||||
var serviceProviderConfig = new ServiceProviderConfigurationBuilder().Build();
|
||||
|
||||
this.Given(x => x.GivenThereIsAnUpstreamUrlPath("someUpstreamPath"))
|
||||
.And(
|
||||
x =>
|
||||
x.GivenTheTemplateVariableAndNameFinderReturns(
|
||||
new OkResponse<List<PlaceholderNameAndValue>>(new List<PlaceholderNameAndValue>())))
|
||||
.And(x => x.GivenTheConfigurationIs(new List<ReRoute>
|
||||
{
|
||||
new ReRouteBuilder()
|
||||
.WithDownstreamPathTemplate("someDownstreamPath")
|
||||
.WithUpstreamPathTemplate("someUpstreamPath")
|
||||
.WithUpstreamHttpMethod(new List<string> { "Get", "Patch", "Delete" })
|
||||
.WithUpstreamTemplatePattern(new UpstreamPathTemplate("", 1))
|
||||
.Build()
|
||||
}, string.Empty, serviceProviderConfig
|
||||
))
|
||||
.And(x => x.GivenTheUrlMatcherReturns(new OkResponse<UrlMatch>(new UrlMatch(true))))
|
||||
.And(x => x.GivenTheUpstreamHttpMethodIs("Post"))
|
||||
.When(x => x.WhenICallTheFinder())
|
||||
.Then(
|
||||
x => x.ThenAnErrorResponseIsReturned())
|
||||
.And(x => x.ThenTheUrlMatcherIsNotCalled())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenTheTemplateVariableAndNameFinderReturns(Response<List<PlaceholderNameAndValue>> response)
|
||||
{
|
||||
_finder
|
||||
.Setup(x => x.Find(It.IsAny<string>(), It.IsAny<string>()))
|
||||
.Returns(response);
|
||||
}
|
||||
|
||||
private void GivenTheUpstreamHttpMethodIs(string upstreamHttpMethod)
|
||||
{
|
||||
_upstreamHttpMethod = upstreamHttpMethod;
|
||||
}
|
||||
|
||||
private void ThenAnErrorResponseIsReturned()
|
||||
{
|
||||
_result.IsError.ShouldBeTrue();
|
||||
}
|
||||
|
||||
private void ThenTheUrlMatcherIsCalledCorrectly()
|
||||
{
|
||||
_mockMatcher
|
||||
.Verify(x => x.Match(_upstreamUrlPath, _reRoutesConfig[0].UpstreamPathTemplate.Value), Times.Once);
|
||||
}
|
||||
|
||||
private void ThenTheUrlMatcherIsCalledCorrectly(string expectedUpstreamUrlPath)
|
||||
{
|
||||
_mockMatcher
|
||||
.Verify(x => x.Match(expectedUpstreamUrlPath, _reRoutesConfig[0].UpstreamPathTemplate.Value), Times.Once);
|
||||
}
|
||||
|
||||
private void ThenTheUrlMatcherIsNotCalled()
|
||||
{
|
||||
_mockMatcher
|
||||
.Verify(x => x.Match(_upstreamUrlPath, _reRoutesConfig[0].UpstreamPathTemplate.Value), Times.Never);
|
||||
}
|
||||
|
||||
private void GivenTheUrlMatcherReturns(Response<UrlMatch> match)
|
||||
{
|
||||
_match = match;
|
||||
_mockMatcher
|
||||
.Setup(x => x.Match(It.IsAny<string>(), It.IsAny<string>()))
|
||||
.Returns(_match);
|
||||
}
|
||||
|
||||
private void GivenTheConfigurationIs(List<ReRoute> reRoutesConfig, string adminPath, ServiceProviderConfiguration serviceProviderConfig)
|
||||
{
|
||||
_reRoutesConfig = reRoutesConfig;
|
||||
_config = new OcelotConfiguration(_reRoutesConfig, adminPath, serviceProviderConfig, "");
|
||||
}
|
||||
|
||||
private void GivenThereIsAnUpstreamUrlPath(string upstreamUrlPath)
|
||||
{
|
||||
_upstreamUrlPath = upstreamUrlPath;
|
||||
}
|
||||
|
||||
private void WhenICallTheFinder()
|
||||
{
|
||||
_result = _downstreamRouteFinder.FindDownstreamRoute(_upstreamUrlPath, _upstreamHttpMethod, _config);
|
||||
}
|
||||
|
||||
private void ThenTheFollowingIsReturned(DownstreamRoute expected)
|
||||
{
|
||||
_result.Data.ReRoute.DownstreamPathTemplate.Value.ShouldBe(expected.ReRoute.DownstreamPathTemplate.Value);
|
||||
_result.Data.ReRoute.UpstreamTemplatePattern.Priority.ShouldBe(expected.ReRoute.UpstreamTemplatePattern.Priority);
|
||||
|
||||
for (int i = 0; i < _result.Data.TemplatePlaceholderNameAndValues.Count; i++)
|
||||
{
|
||||
_result.Data.TemplatePlaceholderNameAndValues[i].Name.ShouldBe(expected.TemplatePlaceholderNameAndValues[i].Name);
|
||||
_result.Data.TemplatePlaceholderNameAndValues[i].Value.ShouldBe(expected.TemplatePlaceholderNameAndValues[i].Value);
|
||||
}
|
||||
|
||||
_result.IsError.ShouldBeFalse();
|
||||
}
|
||||
}
|
||||
}
|
||||
using System.Collections.Generic;
|
||||
using Moq;
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.Configuration.Builder;
|
||||
using Ocelot.Configuration.Creator;
|
||||
using Ocelot.Configuration.Provider;
|
||||
using Ocelot.DownstreamRouteFinder;
|
||||
using Ocelot.DownstreamRouteFinder.Finder;
|
||||
using Ocelot.DownstreamRouteFinder.UrlMatcher;
|
||||
using Ocelot.Responses;
|
||||
using Ocelot.Values;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests.DownstreamRouteFinder
|
||||
{
|
||||
public class DownstreamRouteFinderTests
|
||||
{
|
||||
private readonly IDownstreamRouteFinder _downstreamRouteFinder;
|
||||
private readonly Mock<IUrlPathToUrlTemplateMatcher> _mockMatcher;
|
||||
private readonly Mock<IPlaceholderNameAndValueFinder> _finder;
|
||||
private string _upstreamUrlPath;
|
||||
private Response<DownstreamRoute> _result;
|
||||
private List<ReRoute> _reRoutesConfig;
|
||||
private OcelotConfiguration _config;
|
||||
private Response<UrlMatch> _match;
|
||||
private string _upstreamHttpMethod;
|
||||
|
||||
public DownstreamRouteFinderTests()
|
||||
{
|
||||
_mockMatcher = new Mock<IUrlPathToUrlTemplateMatcher>();
|
||||
_finder = new Mock<IPlaceholderNameAndValueFinder>();
|
||||
_downstreamRouteFinder = new Ocelot.DownstreamRouteFinder.Finder.DownstreamRouteFinder(_mockMatcher.Object, _finder.Object);
|
||||
}
|
||||
|
||||
|
||||
[Fact]
|
||||
public void should_return_highest_priority_when_first()
|
||||
{
|
||||
var serviceProviderConfig = new ServiceProviderConfigurationBuilder().Build();
|
||||
|
||||
this.Given(x => x.GivenThereIsAnUpstreamUrlPath("someUpstreamPath"))
|
||||
.And(x => x.GivenTheTemplateVariableAndNameFinderReturns(
|
||||
new OkResponse<List<PlaceholderNameAndValue>>(new List<PlaceholderNameAndValue>())))
|
||||
.And(x => x.GivenTheConfigurationIs(new List<ReRoute>
|
||||
{
|
||||
new ReRouteBuilder()
|
||||
.WithDownstreamPathTemplate("someDownstreamPath")
|
||||
.WithUpstreamPathTemplate("someUpstreamPath")
|
||||
.WithUpstreamHttpMethod(new List<string> { "Post" })
|
||||
.WithUpstreamTemplatePattern(new UpstreamPathTemplate("test", 1))
|
||||
.Build(),
|
||||
new ReRouteBuilder()
|
||||
.WithDownstreamPathTemplate("someDownstreamPath")
|
||||
.WithUpstreamPathTemplate("someUpstreamPath")
|
||||
.WithUpstreamHttpMethod(new List<string> { "Post" })
|
||||
.WithUpstreamTemplatePattern(new UpstreamPathTemplate("test", 0))
|
||||
.Build()
|
||||
}, string.Empty, serviceProviderConfig))
|
||||
.And(x => x.GivenTheUrlMatcherReturns(new OkResponse<UrlMatch>(new UrlMatch(true))))
|
||||
.And(x => x.GivenTheUpstreamHttpMethodIs("Post"))
|
||||
.When(x => x.WhenICallTheFinder())
|
||||
.Then(x => x.ThenTheFollowingIsReturned(new DownstreamRoute(new List<PlaceholderNameAndValue>(),
|
||||
new ReRouteBuilder()
|
||||
.WithDownstreamPathTemplate("someDownstreamPath")
|
||||
.WithUpstreamTemplatePattern(new UpstreamPathTemplate("test", 1))
|
||||
.WithUpstreamHttpMethod(new List<string> { "Post" })
|
||||
.WithUpstreamTemplatePattern(new UpstreamPathTemplate("test", 1))
|
||||
.Build()
|
||||
)))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_highest_priority_when_lowest()
|
||||
{
|
||||
var serviceProviderConfig = new ServiceProviderConfigurationBuilder().Build();
|
||||
|
||||
this.Given(x => x.GivenThereIsAnUpstreamUrlPath("someUpstreamPath"))
|
||||
.And(x => x.GivenTheTemplateVariableAndNameFinderReturns(
|
||||
new OkResponse<List<PlaceholderNameAndValue>>(new List<PlaceholderNameAndValue>())))
|
||||
.And(x => x.GivenTheConfigurationIs(new List<ReRoute>
|
||||
{
|
||||
new ReRouteBuilder()
|
||||
.WithDownstreamPathTemplate("someDownstreamPath")
|
||||
.WithUpstreamPathTemplate("someUpstreamPath")
|
||||
.WithUpstreamHttpMethod(new List<string> { "Post" })
|
||||
.WithUpstreamTemplatePattern(new UpstreamPathTemplate("test", 0))
|
||||
.Build(),
|
||||
new ReRouteBuilder()
|
||||
.WithDownstreamPathTemplate("someDownstreamPath")
|
||||
.WithUpstreamPathTemplate("someUpstreamPath")
|
||||
.WithUpstreamHttpMethod(new List<string> { "Post" })
|
||||
.WithUpstreamTemplatePattern(new UpstreamPathTemplate("test", 1))
|
||||
.Build()
|
||||
}, string.Empty, serviceProviderConfig))
|
||||
.And(x => x.GivenTheUrlMatcherReturns(new OkResponse<UrlMatch>(new UrlMatch(true))))
|
||||
.And(x => x.GivenTheUpstreamHttpMethodIs("Post"))
|
||||
.When(x => x.WhenICallTheFinder())
|
||||
.Then(x => x.ThenTheFollowingIsReturned(new DownstreamRoute(new List<PlaceholderNameAndValue>(),
|
||||
new ReRouteBuilder()
|
||||
.WithDownstreamPathTemplate("someDownstreamPath")
|
||||
.WithUpstreamTemplatePattern(new UpstreamPathTemplate("test", 1))
|
||||
.WithUpstreamHttpMethod(new List<string> { "Post" })
|
||||
.Build()
|
||||
)))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_route()
|
||||
{
|
||||
var serviceProviderConfig = new ServiceProviderConfigurationBuilder().Build();
|
||||
|
||||
this.Given(x => x.GivenThereIsAnUpstreamUrlPath("matchInUrlMatcher/"))
|
||||
.And(x =>x.GivenTheTemplateVariableAndNameFinderReturns(
|
||||
new OkResponse<List<PlaceholderNameAndValue>>(
|
||||
new List<PlaceholderNameAndValue>())))
|
||||
.And(x => x.GivenTheConfigurationIs(new List<ReRoute>
|
||||
{
|
||||
new ReRouteBuilder()
|
||||
.WithDownstreamPathTemplate("someDownstreamPath")
|
||||
.WithUpstreamPathTemplate("someUpstreamPath")
|
||||
.WithUpstreamHttpMethod(new List<string> { "Get" })
|
||||
.WithUpstreamTemplatePattern(new UpstreamPathTemplate("someUpstreamPath", 1))
|
||||
.Build()
|
||||
}, string.Empty, serviceProviderConfig
|
||||
))
|
||||
.And(x => x.GivenTheUrlMatcherReturns(new OkResponse<UrlMatch>(new UrlMatch(true))))
|
||||
.And(x => x.GivenTheUpstreamHttpMethodIs("Get"))
|
||||
.When(x => x.WhenICallTheFinder())
|
||||
.Then(
|
||||
x => x.ThenTheFollowingIsReturned(new DownstreamRoute(
|
||||
new List<PlaceholderNameAndValue>(),
|
||||
new ReRouteBuilder()
|
||||
.WithDownstreamPathTemplate("someDownstreamPath")
|
||||
.WithUpstreamHttpMethod(new List<string> { "Get" })
|
||||
.WithUpstreamTemplatePattern(new UpstreamPathTemplate("someUpstreamPath", 1))
|
||||
.Build()
|
||||
)))
|
||||
.And(x => x.ThenTheUrlMatcherIsCalledCorrectly())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
|
||||
[Fact]
|
||||
public void should_not_append_slash_to_upstream_url_path()
|
||||
{
|
||||
var serviceProviderConfig = new ServiceProviderConfigurationBuilder().Build();
|
||||
|
||||
this.Given(x => x.GivenThereIsAnUpstreamUrlPath("matchInUrlMatcher"))
|
||||
.And(x =>x.GivenTheTemplateVariableAndNameFinderReturns(
|
||||
new OkResponse<List<PlaceholderNameAndValue>>(
|
||||
new List<PlaceholderNameAndValue>())))
|
||||
.And(x => x.GivenTheConfigurationIs(new List<ReRoute>
|
||||
{
|
||||
new ReRouteBuilder()
|
||||
.WithDownstreamPathTemplate("someDownstreamPath")
|
||||
.WithUpstreamPathTemplate("someUpstreamPath")
|
||||
.WithUpstreamHttpMethod(new List<string> { "Get" })
|
||||
.WithUpstreamTemplatePattern(new UpstreamPathTemplate("someUpstreamPath", 1))
|
||||
.Build()
|
||||
}, string.Empty, serviceProviderConfig
|
||||
))
|
||||
.And(x => x.GivenTheUrlMatcherReturns(new OkResponse<UrlMatch>(new UrlMatch(true))))
|
||||
.And(x => x.GivenTheUpstreamHttpMethodIs("Get"))
|
||||
.When(x => x.WhenICallTheFinder())
|
||||
.Then(
|
||||
x => x.ThenTheFollowingIsReturned(new DownstreamRoute(
|
||||
new List<PlaceholderNameAndValue>(),
|
||||
new ReRouteBuilder()
|
||||
.WithDownstreamPathTemplate("someDownstreamPath")
|
||||
.WithUpstreamHttpMethod(new List<string> { "Get" })
|
||||
.WithUpstreamTemplatePattern(new UpstreamPathTemplate("someUpstreamPath", 1))
|
||||
.Build()
|
||||
)))
|
||||
.And(x => x.ThenTheUrlMatcherIsCalledCorrectly("matchInUrlMatcher"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_route_if_upstream_path_and_upstream_template_are_the_same()
|
||||
{
|
||||
var serviceProviderConfig = new ServiceProviderConfigurationBuilder().Build();
|
||||
|
||||
this.Given(x => x.GivenThereIsAnUpstreamUrlPath("someUpstreamPath"))
|
||||
.And(
|
||||
x =>
|
||||
x.GivenTheTemplateVariableAndNameFinderReturns(
|
||||
new OkResponse<List<PlaceholderNameAndValue>>(new List<PlaceholderNameAndValue>())))
|
||||
.And(x => x.GivenTheConfigurationIs(new List<ReRoute>
|
||||
{
|
||||
new ReRouteBuilder()
|
||||
.WithDownstreamPathTemplate("someDownstreamPath")
|
||||
.WithUpstreamPathTemplate("someUpstreamPath")
|
||||
.WithUpstreamHttpMethod(new List<string> { "Get" })
|
||||
.WithUpstreamTemplatePattern(new UpstreamPathTemplate("someUpstreamPath", 1))
|
||||
.Build()
|
||||
}, string.Empty, serviceProviderConfig
|
||||
))
|
||||
.And(x => x.GivenTheUrlMatcherReturns(new OkResponse<UrlMatch>(new UrlMatch(true))))
|
||||
.And(x => x.GivenTheUpstreamHttpMethodIs("Get"))
|
||||
.When(x => x.WhenICallTheFinder())
|
||||
.Then(
|
||||
x => x.ThenTheFollowingIsReturned(new DownstreamRoute(new List<PlaceholderNameAndValue>(),
|
||||
new ReRouteBuilder()
|
||||
.WithDownstreamPathTemplate("someDownstreamPath")
|
||||
.WithUpstreamHttpMethod(new List<string> { "Get" })
|
||||
.WithUpstreamTemplatePattern(new UpstreamPathTemplate("someUpstreamPath", 1))
|
||||
.Build()
|
||||
)))
|
||||
.And(x => x.ThenTheUrlMatcherIsNotCalled())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_correct_route_for_http_verb()
|
||||
{
|
||||
var serviceProviderConfig = new ServiceProviderConfigurationBuilder().Build();
|
||||
|
||||
this.Given(x => x.GivenThereIsAnUpstreamUrlPath("someUpstreamPath"))
|
||||
.And(
|
||||
x =>
|
||||
x.GivenTheTemplateVariableAndNameFinderReturns(
|
||||
new OkResponse<List<PlaceholderNameAndValue>>(new List<PlaceholderNameAndValue>())))
|
||||
.And(x => x.GivenTheConfigurationIs(new List<ReRoute>
|
||||
{
|
||||
new ReRouteBuilder()
|
||||
.WithDownstreamPathTemplate("someDownstreamPath")
|
||||
.WithUpstreamPathTemplate("someUpstreamPath")
|
||||
.WithUpstreamHttpMethod(new List<string> { "Get" })
|
||||
.WithUpstreamTemplatePattern(new UpstreamPathTemplate("", 1))
|
||||
.Build(),
|
||||
new ReRouteBuilder()
|
||||
.WithDownstreamPathTemplate("someDownstreamPathForAPost")
|
||||
.WithUpstreamPathTemplate("someUpstreamPath")
|
||||
.WithUpstreamHttpMethod(new List<string> { "Post" })
|
||||
.WithUpstreamTemplatePattern(new UpstreamPathTemplate("", 1))
|
||||
.Build()
|
||||
}, string.Empty, serviceProviderConfig
|
||||
))
|
||||
.And(x => x.GivenTheUrlMatcherReturns(new OkResponse<UrlMatch>(new UrlMatch(true))))
|
||||
.And(x => x.GivenTheUpstreamHttpMethodIs("Post"))
|
||||
.When(x => x.WhenICallTheFinder())
|
||||
.Then(
|
||||
x => x.ThenTheFollowingIsReturned(new DownstreamRoute(new List<PlaceholderNameAndValue>(),
|
||||
new ReRouteBuilder()
|
||||
.WithDownstreamPathTemplate("someDownstreamPathForAPost")
|
||||
.WithUpstreamHttpMethod(new List<string> { "Post" })
|
||||
.WithUpstreamTemplatePattern(new UpstreamPathTemplate("", 1))
|
||||
.Build()
|
||||
)))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_not_return_route()
|
||||
{
|
||||
var serviceProviderConfig = new ServiceProviderConfigurationBuilder().Build();
|
||||
|
||||
this.Given(x => x.GivenThereIsAnUpstreamUrlPath("dontMatchPath/"))
|
||||
.And(x => x.GivenTheConfigurationIs(new List<ReRoute>
|
||||
{
|
||||
new ReRouteBuilder()
|
||||
.WithDownstreamPathTemplate("somPath")
|
||||
.WithUpstreamPathTemplate("somePath")
|
||||
.WithUpstreamHttpMethod(new List<string> { "Get" })
|
||||
.WithUpstreamTemplatePattern(new UpstreamPathTemplate("somePath", 1))
|
||||
.Build(),
|
||||
}, string.Empty, serviceProviderConfig
|
||||
))
|
||||
.And(x => x.GivenTheUrlMatcherReturns(new OkResponse<UrlMatch>(new UrlMatch(false))))
|
||||
.And(x => x.GivenTheUpstreamHttpMethodIs("Get"))
|
||||
.When(x => x.WhenICallTheFinder())
|
||||
.Then(
|
||||
x => x.ThenAnErrorResponseIsReturned())
|
||||
.And(x => x.ThenTheUrlMatcherIsCalledCorrectly())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_correct_route_for_http_verb_setting_multiple_upstream_http_method()
|
||||
{
|
||||
var serviceProviderConfig = new ServiceProviderConfigurationBuilder().Build();
|
||||
|
||||
this.Given(x => x.GivenThereIsAnUpstreamUrlPath("someUpstreamPath"))
|
||||
.And(
|
||||
x =>
|
||||
x.GivenTheTemplateVariableAndNameFinderReturns(
|
||||
new OkResponse<List<PlaceholderNameAndValue>>(new List<PlaceholderNameAndValue>())))
|
||||
.And(x => x.GivenTheConfigurationIs(new List<ReRoute>
|
||||
{
|
||||
new ReRouteBuilder()
|
||||
.WithDownstreamPathTemplate("someDownstreamPath")
|
||||
.WithUpstreamPathTemplate("someUpstreamPath")
|
||||
.WithUpstreamHttpMethod(new List<string> { "Get", "Post" })
|
||||
.WithUpstreamTemplatePattern(new UpstreamPathTemplate("", 1))
|
||||
.Build()
|
||||
}, string.Empty, serviceProviderConfig
|
||||
))
|
||||
.And(x => x.GivenTheUrlMatcherReturns(new OkResponse<UrlMatch>(new UrlMatch(true))))
|
||||
.And(x => x.GivenTheUpstreamHttpMethodIs("Post"))
|
||||
.When(x => x.WhenICallTheFinder())
|
||||
.Then(
|
||||
x => x.ThenTheFollowingIsReturned(new DownstreamRoute(new List<PlaceholderNameAndValue>(),
|
||||
new ReRouteBuilder()
|
||||
.WithDownstreamPathTemplate("someDownstreamPath")
|
||||
.WithUpstreamHttpMethod(new List<string> { "Post" })
|
||||
.WithUpstreamTemplatePattern(new UpstreamPathTemplate("", 1))
|
||||
.Build()
|
||||
)))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_correct_route_for_http_verb_setting_all_upstream_http_method()
|
||||
{
|
||||
var serviceProviderConfig = new ServiceProviderConfigurationBuilder().Build();
|
||||
|
||||
this.Given(x => x.GivenThereIsAnUpstreamUrlPath("someUpstreamPath"))
|
||||
.And(
|
||||
x =>
|
||||
x.GivenTheTemplateVariableAndNameFinderReturns(
|
||||
new OkResponse<List<PlaceholderNameAndValue>>(new List<PlaceholderNameAndValue>())))
|
||||
.And(x => x.GivenTheConfigurationIs(new List<ReRoute>
|
||||
{
|
||||
new ReRouteBuilder()
|
||||
.WithDownstreamPathTemplate("someDownstreamPath")
|
||||
.WithUpstreamPathTemplate("someUpstreamPath")
|
||||
.WithUpstreamHttpMethod(new List<string>())
|
||||
.WithUpstreamTemplatePattern(new UpstreamPathTemplate("", 1))
|
||||
.Build()
|
||||
}, string.Empty, serviceProviderConfig
|
||||
))
|
||||
.And(x => x.GivenTheUrlMatcherReturns(new OkResponse<UrlMatch>(new UrlMatch(true))))
|
||||
.And(x => x.GivenTheUpstreamHttpMethodIs("Post"))
|
||||
.When(x => x.WhenICallTheFinder())
|
||||
.Then(
|
||||
x => x.ThenTheFollowingIsReturned(new DownstreamRoute(new List<PlaceholderNameAndValue>(),
|
||||
new ReRouteBuilder()
|
||||
.WithDownstreamPathTemplate("someDownstreamPath")
|
||||
.WithUpstreamHttpMethod(new List<string> { "Post" })
|
||||
.WithUpstreamTemplatePattern(new UpstreamPathTemplate("", 1))
|
||||
.Build()
|
||||
)))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_not_return_route_for_http_verb_not_setting_in_upstream_http_method()
|
||||
{
|
||||
var serviceProviderConfig = new ServiceProviderConfigurationBuilder().Build();
|
||||
|
||||
this.Given(x => x.GivenThereIsAnUpstreamUrlPath("someUpstreamPath"))
|
||||
.And(
|
||||
x =>
|
||||
x.GivenTheTemplateVariableAndNameFinderReturns(
|
||||
new OkResponse<List<PlaceholderNameAndValue>>(new List<PlaceholderNameAndValue>())))
|
||||
.And(x => x.GivenTheConfigurationIs(new List<ReRoute>
|
||||
{
|
||||
new ReRouteBuilder()
|
||||
.WithDownstreamPathTemplate("someDownstreamPath")
|
||||
.WithUpstreamPathTemplate("someUpstreamPath")
|
||||
.WithUpstreamHttpMethod(new List<string> { "Get", "Patch", "Delete" })
|
||||
.WithUpstreamTemplatePattern(new UpstreamPathTemplate("", 1))
|
||||
.Build()
|
||||
}, string.Empty, serviceProviderConfig
|
||||
))
|
||||
.And(x => x.GivenTheUrlMatcherReturns(new OkResponse<UrlMatch>(new UrlMatch(true))))
|
||||
.And(x => x.GivenTheUpstreamHttpMethodIs("Post"))
|
||||
.When(x => x.WhenICallTheFinder())
|
||||
.Then(
|
||||
x => x.ThenAnErrorResponseIsReturned())
|
||||
.And(x => x.ThenTheUrlMatcherIsNotCalled())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenTheTemplateVariableAndNameFinderReturns(Response<List<PlaceholderNameAndValue>> response)
|
||||
{
|
||||
_finder
|
||||
.Setup(x => x.Find(It.IsAny<string>(), It.IsAny<string>()))
|
||||
.Returns(response);
|
||||
}
|
||||
|
||||
private void GivenTheUpstreamHttpMethodIs(string upstreamHttpMethod)
|
||||
{
|
||||
_upstreamHttpMethod = upstreamHttpMethod;
|
||||
}
|
||||
|
||||
private void ThenAnErrorResponseIsReturned()
|
||||
{
|
||||
_result.IsError.ShouldBeTrue();
|
||||
}
|
||||
|
||||
private void ThenTheUrlMatcherIsCalledCorrectly()
|
||||
{
|
||||
_mockMatcher
|
||||
.Verify(x => x.Match(_upstreamUrlPath, _reRoutesConfig[0].UpstreamPathTemplate.Value), Times.Once);
|
||||
}
|
||||
|
||||
private void ThenTheUrlMatcherIsCalledCorrectly(string expectedUpstreamUrlPath)
|
||||
{
|
||||
_mockMatcher
|
||||
.Verify(x => x.Match(expectedUpstreamUrlPath, _reRoutesConfig[0].UpstreamPathTemplate.Value), Times.Once);
|
||||
}
|
||||
|
||||
private void ThenTheUrlMatcherIsNotCalled()
|
||||
{
|
||||
_mockMatcher
|
||||
.Verify(x => x.Match(_upstreamUrlPath, _reRoutesConfig[0].UpstreamPathTemplate.Value), Times.Never);
|
||||
}
|
||||
|
||||
private void GivenTheUrlMatcherReturns(Response<UrlMatch> match)
|
||||
{
|
||||
_match = match;
|
||||
_mockMatcher
|
||||
.Setup(x => x.Match(It.IsAny<string>(), It.IsAny<string>()))
|
||||
.Returns(_match);
|
||||
}
|
||||
|
||||
private void GivenTheConfigurationIs(List<ReRoute> reRoutesConfig, string adminPath, ServiceProviderConfiguration serviceProviderConfig)
|
||||
{
|
||||
_reRoutesConfig = reRoutesConfig;
|
||||
_config = new OcelotConfiguration(_reRoutesConfig, adminPath, serviceProviderConfig, "");
|
||||
}
|
||||
|
||||
private void GivenThereIsAnUpstreamUrlPath(string upstreamUrlPath)
|
||||
{
|
||||
_upstreamUrlPath = upstreamUrlPath;
|
||||
}
|
||||
|
||||
private void WhenICallTheFinder()
|
||||
{
|
||||
_result = _downstreamRouteFinder.FindDownstreamRoute(_upstreamUrlPath, _upstreamHttpMethod, _config);
|
||||
}
|
||||
|
||||
private void ThenTheFollowingIsReturned(DownstreamRoute expected)
|
||||
{
|
||||
_result.Data.ReRoute.DownstreamPathTemplate.Value.ShouldBe(expected.ReRoute.DownstreamPathTemplate.Value);
|
||||
_result.Data.ReRoute.UpstreamTemplatePattern.Priority.ShouldBe(expected.ReRoute.UpstreamTemplatePattern.Priority);
|
||||
|
||||
for (int i = 0; i < _result.Data.TemplatePlaceholderNameAndValues.Count; i++)
|
||||
{
|
||||
_result.Data.TemplatePlaceholderNameAndValues[i].Name.ShouldBe(expected.TemplatePlaceholderNameAndValues[i].Name);
|
||||
_result.Data.TemplatePlaceholderNameAndValues[i].Value.ShouldBe(expected.TemplatePlaceholderNameAndValues[i].Value);
|
||||
}
|
||||
|
||||
_result.IsError.ShouldBeFalse();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,218 +1,218 @@
|
||||
using Ocelot.DownstreamRouteFinder.UrlMatcher;
|
||||
using Ocelot.Responses;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests.DownstreamRouteFinder.UrlMatcher
|
||||
{
|
||||
public class RegExUrlMatcherTests
|
||||
{
|
||||
private readonly IUrlPathToUrlTemplateMatcher _urlMatcher;
|
||||
private string _downstreamUrlPath;
|
||||
private string _downstreamPathTemplate;
|
||||
private Response<UrlMatch> _result;
|
||||
|
||||
public RegExUrlMatcherTests()
|
||||
{
|
||||
_urlMatcher = new RegExUrlMatcher();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_not_match_slash_becaue_we_need_to_match_something_after_it()
|
||||
{
|
||||
const string RegExForwardSlashAndOnePlaceHolder = "^/[0-9a-zA-Z].*";
|
||||
|
||||
this.Given(x => x.GivenIHaveAUpstreamPath("/"))
|
||||
.And(x => x.GivenIHaveAnUpstreamUrlTemplatePattern(RegExForwardSlashAndOnePlaceHolder))
|
||||
.When(x => x.WhenIMatchThePaths())
|
||||
.And(x => x.ThenTheResultIsFalse())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_not_match_forward_slash_only_regex()
|
||||
{
|
||||
this.Given(x => x.GivenIHaveAUpstreamPath("/working/"))
|
||||
.And(x => x.GivenIHaveAnUpstreamUrlTemplatePattern("^/$"))
|
||||
.When(x => x.WhenIMatchThePaths())
|
||||
.And(x => x.ThenTheResultIsFalse())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_not_match_issue_134()
|
||||
{
|
||||
this.Given(x => x.GivenIHaveAUpstreamPath("/api/vacancy/1/"))
|
||||
.And(x => x.GivenIHaveAnUpstreamUrlTemplatePattern("^(?i)/vacancy/.*/$"))
|
||||
.When(x => x.WhenIMatchThePaths())
|
||||
.And(x => x.ThenTheResultIsFalse())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_match_forward_slash_only_regex()
|
||||
{
|
||||
this.Given(x => x.GivenIHaveAUpstreamPath("/"))
|
||||
.And(x => x.GivenIHaveAnUpstreamUrlTemplatePattern("^/$"))
|
||||
.When(x => x.WhenIMatchThePaths())
|
||||
.And(x => x.ThenTheResultIsTrue())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_find_match_when_template_smaller_than_valid_path()
|
||||
{
|
||||
this.Given(x => x.GivenIHaveAUpstreamPath("/api/products/2354325435624623464235"))
|
||||
.And(x => x.GivenIHaveAnUpstreamUrlTemplatePattern("^/api/products/.*$"))
|
||||
.When(x => x.WhenIMatchThePaths())
|
||||
.And(x => x.ThenTheResultIsTrue())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_not_find_match()
|
||||
{
|
||||
this.Given(x => x.GivenIHaveAUpstreamPath("/api/values"))
|
||||
.And(x => x.GivenIHaveAnUpstreamUrlTemplatePattern("^/$"))
|
||||
.When(x => x.WhenIMatchThePaths())
|
||||
.And(x => x.ThenTheResultIsFalse())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void can_match_down_stream_url()
|
||||
{
|
||||
this.Given(x => x.GivenIHaveAUpstreamPath(""))
|
||||
.And(x => x.GivenIHaveAnUpstreamUrlTemplatePattern("^$"))
|
||||
.When(x => x.WhenIMatchThePaths())
|
||||
.And(x => x.ThenTheResultIsTrue())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void can_match_down_stream_url_with_no_slash()
|
||||
{
|
||||
this.Given(x => x.GivenIHaveAUpstreamPath("api"))
|
||||
.Given(x => x.GivenIHaveAnUpstreamUrlTemplatePattern("^api$"))
|
||||
.When(x => x.WhenIMatchThePaths())
|
||||
.Then(x => x.ThenTheResultIsTrue())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void can_match_down_stream_url_with_one_slash()
|
||||
{
|
||||
this.Given(x => x.GivenIHaveAUpstreamPath("api/"))
|
||||
.Given(x => x.GivenIHaveAnUpstreamUrlTemplatePattern("^api/$"))
|
||||
.When(x => x.WhenIMatchThePaths())
|
||||
.Then(x => x.ThenTheResultIsTrue())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void can_match_down_stream_url_with_downstream_template()
|
||||
{
|
||||
this.Given(x => x.GivenIHaveAUpstreamPath("api/product/products/"))
|
||||
.Given(x => x.GivenIHaveAnUpstreamUrlTemplatePattern("^api/product/products/$"))
|
||||
.When(x => x.WhenIMatchThePaths())
|
||||
.Then(x => x.ThenTheResultIsTrue())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void can_match_down_stream_url_with_downstream_template_with_one_place_holder()
|
||||
{
|
||||
this.Given(x => x.GivenIHaveAUpstreamPath("api/product/products/1"))
|
||||
.Given(x => x.GivenIHaveAnUpstreamUrlTemplatePattern("^api/product/products/.*$"))
|
||||
.When(x => x.WhenIMatchThePaths())
|
||||
.Then(x => x.ThenTheResultIsTrue())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void can_match_down_stream_url_with_downstream_template_with_two_place_holders()
|
||||
{
|
||||
this.Given(x => x.GivenIHaveAUpstreamPath("api/product/products/1/2"))
|
||||
.Given(x => x.GivenIHaveAnUpstreamUrlTemplatePattern("^api/product/products/.*/.*$"))
|
||||
.When(x => x.WhenIMatchThePaths())
|
||||
.Then(x => x.ThenTheResultIsTrue())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void can_match_down_stream_url_with_downstream_template_with_two_place_holders_seperated_by_something()
|
||||
{
|
||||
this.Given(x => x.GivenIHaveAUpstreamPath("api/product/products/1/categories/2"))
|
||||
.And(x => x.GivenIHaveAnUpstreamUrlTemplatePattern("^api/product/products/.*/categories/.*$"))
|
||||
.When(x => x.WhenIMatchThePaths())
|
||||
.Then(x => x.ThenTheResultIsTrue())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void can_match_down_stream_url_with_downstream_template_with_three_place_holders_seperated_by_something()
|
||||
{
|
||||
this.Given(x => x.GivenIHaveAUpstreamPath("api/product/products/1/categories/2/variant/123"))
|
||||
.And(x => x.GivenIHaveAnUpstreamUrlTemplatePattern("^api/product/products/.*/categories/.*/variant/.*$"))
|
||||
.When(x => x.WhenIMatchThePaths())
|
||||
.Then(x => x.ThenTheResultIsTrue())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void can_match_down_stream_url_with_downstream_template_with_three_place_holders()
|
||||
{
|
||||
this.Given(x => x.GivenIHaveAUpstreamPath("api/product/products/1/categories/2/variant/"))
|
||||
.And(x => x.GivenIHaveAnUpstreamUrlTemplatePattern("^api/product/products/.*/categories/.*/variant/$"))
|
||||
.When(x => x.WhenIMatchThePaths())
|
||||
.Then(x => x.ThenTheResultIsTrue())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_ignore_case_sensitivity()
|
||||
{
|
||||
this.Given(x => x.GivenIHaveAUpstreamPath("API/product/products/1/categories/2/variant/"))
|
||||
.And(x => x.GivenIHaveAnUpstreamUrlTemplatePattern("^(?i)api/product/products/.*/categories/.*/variant/$"))
|
||||
.When(x => x.WhenIMatchThePaths())
|
||||
.Then(x => x.ThenTheResultIsTrue())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_respect_case_sensitivity()
|
||||
{
|
||||
this.Given(x => x.GivenIHaveAUpstreamPath("API/product/products/1/categories/2/variant/"))
|
||||
.And(x => x.GivenIHaveAnUpstreamUrlTemplatePattern("^api/product/products/.*/categories/.*/variant/$"))
|
||||
.When(x => x.WhenIMatchThePaths())
|
||||
.Then(x => x.ThenTheResultIsFalse())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenIHaveAUpstreamPath(string downstreamPath)
|
||||
{
|
||||
_downstreamUrlPath = downstreamPath;
|
||||
}
|
||||
|
||||
private void GivenIHaveAnUpstreamUrlTemplatePattern(string downstreamUrlTemplate)
|
||||
{
|
||||
_downstreamPathTemplate = downstreamUrlTemplate;
|
||||
}
|
||||
|
||||
private void WhenIMatchThePaths()
|
||||
{
|
||||
_result = _urlMatcher.Match(_downstreamUrlPath, _downstreamPathTemplate);
|
||||
}
|
||||
|
||||
private void ThenTheResultIsTrue()
|
||||
{
|
||||
_result.Data.Match.ShouldBeTrue();
|
||||
}
|
||||
|
||||
private void ThenTheResultIsFalse()
|
||||
{
|
||||
_result.Data.Match.ShouldBeFalse();
|
||||
}
|
||||
}
|
||||
using Ocelot.DownstreamRouteFinder.UrlMatcher;
|
||||
using Ocelot.Responses;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests.DownstreamRouteFinder.UrlMatcher
|
||||
{
|
||||
public class RegExUrlMatcherTests
|
||||
{
|
||||
private readonly IUrlPathToUrlTemplateMatcher _urlMatcher;
|
||||
private string _downstreamUrlPath;
|
||||
private string _downstreamPathTemplate;
|
||||
private Response<UrlMatch> _result;
|
||||
|
||||
public RegExUrlMatcherTests()
|
||||
{
|
||||
_urlMatcher = new RegExUrlMatcher();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_not_match_slash_becaue_we_need_to_match_something_after_it()
|
||||
{
|
||||
const string RegExForwardSlashAndOnePlaceHolder = "^/[0-9a-zA-Z].*";
|
||||
|
||||
this.Given(x => x.GivenIHaveAUpstreamPath("/"))
|
||||
.And(x => x.GivenIHaveAnUpstreamUrlTemplatePattern(RegExForwardSlashAndOnePlaceHolder))
|
||||
.When(x => x.WhenIMatchThePaths())
|
||||
.And(x => x.ThenTheResultIsFalse())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_not_match_forward_slash_only_regex()
|
||||
{
|
||||
this.Given(x => x.GivenIHaveAUpstreamPath("/working/"))
|
||||
.And(x => x.GivenIHaveAnUpstreamUrlTemplatePattern("^/$"))
|
||||
.When(x => x.WhenIMatchThePaths())
|
||||
.And(x => x.ThenTheResultIsFalse())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_not_match_issue_134()
|
||||
{
|
||||
this.Given(x => x.GivenIHaveAUpstreamPath("/api/vacancy/1/"))
|
||||
.And(x => x.GivenIHaveAnUpstreamUrlTemplatePattern("^(?i)/vacancy/.*/$"))
|
||||
.When(x => x.WhenIMatchThePaths())
|
||||
.And(x => x.ThenTheResultIsFalse())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_match_forward_slash_only_regex()
|
||||
{
|
||||
this.Given(x => x.GivenIHaveAUpstreamPath("/"))
|
||||
.And(x => x.GivenIHaveAnUpstreamUrlTemplatePattern("^/$"))
|
||||
.When(x => x.WhenIMatchThePaths())
|
||||
.And(x => x.ThenTheResultIsTrue())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_find_match_when_template_smaller_than_valid_path()
|
||||
{
|
||||
this.Given(x => x.GivenIHaveAUpstreamPath("/api/products/2354325435624623464235"))
|
||||
.And(x => x.GivenIHaveAnUpstreamUrlTemplatePattern("^/api/products/.*$"))
|
||||
.When(x => x.WhenIMatchThePaths())
|
||||
.And(x => x.ThenTheResultIsTrue())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_not_find_match()
|
||||
{
|
||||
this.Given(x => x.GivenIHaveAUpstreamPath("/api/values"))
|
||||
.And(x => x.GivenIHaveAnUpstreamUrlTemplatePattern("^/$"))
|
||||
.When(x => x.WhenIMatchThePaths())
|
||||
.And(x => x.ThenTheResultIsFalse())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void can_match_down_stream_url()
|
||||
{
|
||||
this.Given(x => x.GivenIHaveAUpstreamPath(""))
|
||||
.And(x => x.GivenIHaveAnUpstreamUrlTemplatePattern("^$"))
|
||||
.When(x => x.WhenIMatchThePaths())
|
||||
.And(x => x.ThenTheResultIsTrue())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void can_match_down_stream_url_with_no_slash()
|
||||
{
|
||||
this.Given(x => x.GivenIHaveAUpstreamPath("api"))
|
||||
.Given(x => x.GivenIHaveAnUpstreamUrlTemplatePattern("^api$"))
|
||||
.When(x => x.WhenIMatchThePaths())
|
||||
.Then(x => x.ThenTheResultIsTrue())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void can_match_down_stream_url_with_one_slash()
|
||||
{
|
||||
this.Given(x => x.GivenIHaveAUpstreamPath("api/"))
|
||||
.Given(x => x.GivenIHaveAnUpstreamUrlTemplatePattern("^api/$"))
|
||||
.When(x => x.WhenIMatchThePaths())
|
||||
.Then(x => x.ThenTheResultIsTrue())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void can_match_down_stream_url_with_downstream_template()
|
||||
{
|
||||
this.Given(x => x.GivenIHaveAUpstreamPath("api/product/products/"))
|
||||
.Given(x => x.GivenIHaveAnUpstreamUrlTemplatePattern("^api/product/products/$"))
|
||||
.When(x => x.WhenIMatchThePaths())
|
||||
.Then(x => x.ThenTheResultIsTrue())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void can_match_down_stream_url_with_downstream_template_with_one_place_holder()
|
||||
{
|
||||
this.Given(x => x.GivenIHaveAUpstreamPath("api/product/products/1"))
|
||||
.Given(x => x.GivenIHaveAnUpstreamUrlTemplatePattern("^api/product/products/.*$"))
|
||||
.When(x => x.WhenIMatchThePaths())
|
||||
.Then(x => x.ThenTheResultIsTrue())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void can_match_down_stream_url_with_downstream_template_with_two_place_holders()
|
||||
{
|
||||
this.Given(x => x.GivenIHaveAUpstreamPath("api/product/products/1/2"))
|
||||
.Given(x => x.GivenIHaveAnUpstreamUrlTemplatePattern("^api/product/products/.*/.*$"))
|
||||
.When(x => x.WhenIMatchThePaths())
|
||||
.Then(x => x.ThenTheResultIsTrue())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void can_match_down_stream_url_with_downstream_template_with_two_place_holders_seperated_by_something()
|
||||
{
|
||||
this.Given(x => x.GivenIHaveAUpstreamPath("api/product/products/1/categories/2"))
|
||||
.And(x => x.GivenIHaveAnUpstreamUrlTemplatePattern("^api/product/products/.*/categories/.*$"))
|
||||
.When(x => x.WhenIMatchThePaths())
|
||||
.Then(x => x.ThenTheResultIsTrue())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void can_match_down_stream_url_with_downstream_template_with_three_place_holders_seperated_by_something()
|
||||
{
|
||||
this.Given(x => x.GivenIHaveAUpstreamPath("api/product/products/1/categories/2/variant/123"))
|
||||
.And(x => x.GivenIHaveAnUpstreamUrlTemplatePattern("^api/product/products/.*/categories/.*/variant/.*$"))
|
||||
.When(x => x.WhenIMatchThePaths())
|
||||
.Then(x => x.ThenTheResultIsTrue())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void can_match_down_stream_url_with_downstream_template_with_three_place_holders()
|
||||
{
|
||||
this.Given(x => x.GivenIHaveAUpstreamPath("api/product/products/1/categories/2/variant/"))
|
||||
.And(x => x.GivenIHaveAnUpstreamUrlTemplatePattern("^api/product/products/.*/categories/.*/variant/$"))
|
||||
.When(x => x.WhenIMatchThePaths())
|
||||
.Then(x => x.ThenTheResultIsTrue())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_ignore_case_sensitivity()
|
||||
{
|
||||
this.Given(x => x.GivenIHaveAUpstreamPath("API/product/products/1/categories/2/variant/"))
|
||||
.And(x => x.GivenIHaveAnUpstreamUrlTemplatePattern("^(?i)api/product/products/.*/categories/.*/variant/$"))
|
||||
.When(x => x.WhenIMatchThePaths())
|
||||
.Then(x => x.ThenTheResultIsTrue())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_respect_case_sensitivity()
|
||||
{
|
||||
this.Given(x => x.GivenIHaveAUpstreamPath("API/product/products/1/categories/2/variant/"))
|
||||
.And(x => x.GivenIHaveAnUpstreamUrlTemplatePattern("^api/product/products/.*/categories/.*/variant/$"))
|
||||
.When(x => x.WhenIMatchThePaths())
|
||||
.Then(x => x.ThenTheResultIsFalse())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenIHaveAUpstreamPath(string downstreamPath)
|
||||
{
|
||||
_downstreamUrlPath = downstreamPath;
|
||||
}
|
||||
|
||||
private void GivenIHaveAnUpstreamUrlTemplatePattern(string downstreamUrlTemplate)
|
||||
{
|
||||
_downstreamPathTemplate = downstreamUrlTemplate;
|
||||
}
|
||||
|
||||
private void WhenIMatchThePaths()
|
||||
{
|
||||
_result = _urlMatcher.Match(_downstreamUrlPath, _downstreamPathTemplate);
|
||||
}
|
||||
|
||||
private void ThenTheResultIsTrue()
|
||||
{
|
||||
_result.Data.Match.ShouldBeTrue();
|
||||
}
|
||||
|
||||
private void ThenTheResultIsFalse()
|
||||
{
|
||||
_result.Data.Match.ShouldBeFalse();
|
||||
}
|
||||
}
|
||||
}
|
@ -1,267 +1,267 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Ocelot.DownstreamRouteFinder.UrlMatcher;
|
||||
using Ocelot.Responses;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests.DownstreamRouteFinder.UrlMatcher
|
||||
{
|
||||
public class UrlPathPlaceholderNameAndValueFinderTests
|
||||
{
|
||||
private readonly IPlaceholderNameAndValueFinder _finder;
|
||||
private string _downstreamUrlPath;
|
||||
private string _downstreamPathTemplate;
|
||||
private Response<List<PlaceholderNameAndValue>> _result;
|
||||
|
||||
public UrlPathPlaceholderNameAndValueFinderTests()
|
||||
{
|
||||
_finder = new UrlPathPlaceholderNameAndValueFinder();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void can_match_down_stream_url()
|
||||
{
|
||||
this.Given(x => x.GivenIHaveAUpstreamPath(""))
|
||||
.And(x => x.GivenIHaveAnUpstreamUrlTemplate(""))
|
||||
.When(x => x.WhenIFindTheUrlVariableNamesAndValues())
|
||||
.And(x => x.ThenTheTemplatesVariablesAre(new List<PlaceholderNameAndValue>()))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
|
||||
[Fact]
|
||||
public void can_match_down_stream_url_with_nothing_then_placeholder_no_value_is_blank()
|
||||
{
|
||||
var expectedTemplates = new List<PlaceholderNameAndValue>
|
||||
{
|
||||
new PlaceholderNameAndValue("{url}", "")
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenIHaveAUpstreamPath(""))
|
||||
.And(x => x.GivenIHaveAnUpstreamUrlTemplate("/{url}"))
|
||||
.When(x => x.WhenIFindTheUrlVariableNamesAndValues())
|
||||
.And(x => x.ThenTheTemplatesVariablesAre(expectedTemplates))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void can_match_down_stream_url_with_nothing_then_placeholder_value_is_test()
|
||||
{
|
||||
var expectedTemplates = new List<PlaceholderNameAndValue>
|
||||
{
|
||||
new PlaceholderNameAndValue("{url}", "test")
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenIHaveAUpstreamPath("/test"))
|
||||
.And(x => x.GivenIHaveAnUpstreamUrlTemplate("/{url}"))
|
||||
.When(x => x.WhenIFindTheUrlVariableNamesAndValues())
|
||||
.And(x => x.ThenTheTemplatesVariablesAre(expectedTemplates))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void can_match_down_stream_url_with_forward_slash_then_placeholder_no_value_is_blank()
|
||||
{
|
||||
var expectedTemplates = new List<PlaceholderNameAndValue>
|
||||
{
|
||||
new PlaceholderNameAndValue("{url}", "")
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenIHaveAUpstreamPath("/"))
|
||||
.And(x => x.GivenIHaveAnUpstreamUrlTemplate("/{url}"))
|
||||
.When(x => x.WhenIFindTheUrlVariableNamesAndValues())
|
||||
.And(x => x.ThenTheTemplatesVariablesAre(expectedTemplates))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void can_match_down_stream_url_with_forward_slash()
|
||||
{
|
||||
var expectedTemplates = new List<PlaceholderNameAndValue>
|
||||
{
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenIHaveAUpstreamPath("/"))
|
||||
.And(x => x.GivenIHaveAnUpstreamUrlTemplate("/"))
|
||||
.When(x => x.WhenIFindTheUrlVariableNamesAndValues())
|
||||
.And(x => x.ThenTheTemplatesVariablesAre(expectedTemplates))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void can_match_down_stream_url_with_forward_slash_then_placeholder_then_another_value()
|
||||
{
|
||||
var expectedTemplates = new List<PlaceholderNameAndValue>
|
||||
{
|
||||
new PlaceholderNameAndValue("{url}", "1")
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenIHaveAUpstreamPath("/1/products"))
|
||||
.And(x => x.GivenIHaveAnUpstreamUrlTemplate("/{url}/products"))
|
||||
.When(x => x.WhenIFindTheUrlVariableNamesAndValues())
|
||||
.And(x => x.ThenTheTemplatesVariablesAre(expectedTemplates))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_not_find_anything()
|
||||
{
|
||||
this.Given(x => x.GivenIHaveAUpstreamPath("/products"))
|
||||
.And(x => x.GivenIHaveAnUpstreamUrlTemplate("/products/"))
|
||||
.When(x => x.WhenIFindTheUrlVariableNamesAndValues())
|
||||
.And(x => x.ThenTheTemplatesVariablesAre(new List<PlaceholderNameAndValue>()))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void can_match_down_stream_url_with_no_slash()
|
||||
{
|
||||
this.Given(x => x.GivenIHaveAUpstreamPath("api"))
|
||||
.Given(x => x.GivenIHaveAnUpstreamUrlTemplate("api"))
|
||||
.When(x => x.WhenIFindTheUrlVariableNamesAndValues())
|
||||
.And(x => x.ThenTheTemplatesVariablesAre(new List<PlaceholderNameAndValue>()))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void can_match_down_stream_url_with_one_slash()
|
||||
{
|
||||
this.Given(x => x.GivenIHaveAUpstreamPath("api/"))
|
||||
.Given(x => x.GivenIHaveAnUpstreamUrlTemplate("api/"))
|
||||
.When(x => x.WhenIFindTheUrlVariableNamesAndValues())
|
||||
.And(x => x.ThenTheTemplatesVariablesAre(new List<PlaceholderNameAndValue>()))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void can_match_down_stream_url_with_downstream_template()
|
||||
{
|
||||
this.Given(x => x.GivenIHaveAUpstreamPath("api/product/products/"))
|
||||
.Given(x => x.GivenIHaveAnUpstreamUrlTemplate("api/product/products/"))
|
||||
.When(x => x.WhenIFindTheUrlVariableNamesAndValues())
|
||||
.And(x => x.ThenTheTemplatesVariablesAre(new List<PlaceholderNameAndValue>()))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void can_match_down_stream_url_with_downstream_template_with_one_place_holder()
|
||||
{
|
||||
var expectedTemplates = new List<PlaceholderNameAndValue>
|
||||
{
|
||||
new PlaceholderNameAndValue("{productId}", "1")
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenIHaveAUpstreamPath("api/product/products/1"))
|
||||
.Given(x => x.GivenIHaveAnUpstreamUrlTemplate("api/product/products/{productId}"))
|
||||
.When(x => x.WhenIFindTheUrlVariableNamesAndValues())
|
||||
.And(x => x.ThenTheTemplatesVariablesAre(expectedTemplates))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void can_match_down_stream_url_with_downstream_template_with_two_place_holders()
|
||||
{
|
||||
var expectedTemplates = new List<PlaceholderNameAndValue>
|
||||
{
|
||||
new PlaceholderNameAndValue("{productId}", "1"),
|
||||
new PlaceholderNameAndValue("{categoryId}", "2")
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenIHaveAUpstreamPath("api/product/products/1/2"))
|
||||
.Given(x => x.GivenIHaveAnUpstreamUrlTemplate("api/product/products/{productId}/{categoryId}"))
|
||||
.When(x => x.WhenIFindTheUrlVariableNamesAndValues())
|
||||
.And(x => x.ThenTheTemplatesVariablesAre(expectedTemplates))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void can_match_down_stream_url_with_downstream_template_with_two_place_holders_seperated_by_something()
|
||||
{
|
||||
var expectedTemplates = new List<PlaceholderNameAndValue>
|
||||
{
|
||||
new PlaceholderNameAndValue("{productId}", "1"),
|
||||
new PlaceholderNameAndValue("{categoryId}", "2")
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenIHaveAUpstreamPath("api/product/products/1/categories/2"))
|
||||
.And(x => x.GivenIHaveAnUpstreamUrlTemplate("api/product/products/{productId}/categories/{categoryId}"))
|
||||
.When(x => x.WhenIFindTheUrlVariableNamesAndValues())
|
||||
.And(x => x.ThenTheTemplatesVariablesAre(expectedTemplates))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void can_match_down_stream_url_with_downstream_template_with_three_place_holders_seperated_by_something()
|
||||
{
|
||||
var expectedTemplates = new List<PlaceholderNameAndValue>
|
||||
{
|
||||
new PlaceholderNameAndValue("{productId}", "1"),
|
||||
new PlaceholderNameAndValue("{categoryId}", "2"),
|
||||
new PlaceholderNameAndValue("{variantId}", "123")
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenIHaveAUpstreamPath("api/product/products/1/categories/2/variant/123"))
|
||||
.And(x => x.GivenIHaveAnUpstreamUrlTemplate("api/product/products/{productId}/categories/{categoryId}/variant/{variantId}"))
|
||||
.When(x => x.WhenIFindTheUrlVariableNamesAndValues())
|
||||
.And(x => x.ThenTheTemplatesVariablesAre(expectedTemplates))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void can_match_down_stream_url_with_downstream_template_with_three_place_holders()
|
||||
{
|
||||
var expectedTemplates = new List<PlaceholderNameAndValue>
|
||||
{
|
||||
new PlaceholderNameAndValue("{productId}", "1"),
|
||||
new PlaceholderNameAndValue("{categoryId}", "2")
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenIHaveAUpstreamPath("api/product/products/1/categories/2/variant/"))
|
||||
.And(x => x.GivenIHaveAnUpstreamUrlTemplate("api/product/products/{productId}/categories/{categoryId}/variant/"))
|
||||
.When(x => x.WhenIFindTheUrlVariableNamesAndValues())
|
||||
.And(x => x.ThenTheTemplatesVariablesAre(expectedTemplates))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void can_match_down_stream_url_with_downstream_template_with_place_holder_to_final_url_path()
|
||||
{
|
||||
var expectedTemplates = new List<PlaceholderNameAndValue>
|
||||
{
|
||||
new PlaceholderNameAndValue("{finalUrlPath}", "product/products/categories/"),
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenIHaveAUpstreamPath("api/product/products/categories/"))
|
||||
.And(x => x.GivenIHaveAnUpstreamUrlTemplate("api/{finalUrlPath}/"))
|
||||
.When(x => x.WhenIFindTheUrlVariableNamesAndValues())
|
||||
.And(x => x.ThenTheTemplatesVariablesAre(expectedTemplates))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void ThenTheTemplatesVariablesAre(List<PlaceholderNameAndValue> expectedResults)
|
||||
{
|
||||
foreach (var expectedResult in expectedResults)
|
||||
{
|
||||
var result = _result.Data.First(t => t.Name == expectedResult.Name);
|
||||
result.Value.ShouldBe(expectedResult.Value);
|
||||
}
|
||||
}
|
||||
|
||||
private void GivenIHaveAUpstreamPath(string downstreamPath)
|
||||
{
|
||||
_downstreamUrlPath = downstreamPath;
|
||||
}
|
||||
|
||||
private void GivenIHaveAnUpstreamUrlTemplate(string downstreamUrlTemplate)
|
||||
{
|
||||
_downstreamPathTemplate = downstreamUrlTemplate;
|
||||
}
|
||||
|
||||
private void WhenIFindTheUrlVariableNamesAndValues()
|
||||
{
|
||||
_result = _finder.Find(_downstreamUrlPath, _downstreamPathTemplate);
|
||||
}
|
||||
}
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Ocelot.DownstreamRouteFinder.UrlMatcher;
|
||||
using Ocelot.Responses;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests.DownstreamRouteFinder.UrlMatcher
|
||||
{
|
||||
public class UrlPathPlaceholderNameAndValueFinderTests
|
||||
{
|
||||
private readonly IPlaceholderNameAndValueFinder _finder;
|
||||
private string _downstreamUrlPath;
|
||||
private string _downstreamPathTemplate;
|
||||
private Response<List<PlaceholderNameAndValue>> _result;
|
||||
|
||||
public UrlPathPlaceholderNameAndValueFinderTests()
|
||||
{
|
||||
_finder = new UrlPathPlaceholderNameAndValueFinder();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void can_match_down_stream_url()
|
||||
{
|
||||
this.Given(x => x.GivenIHaveAUpstreamPath(""))
|
||||
.And(x => x.GivenIHaveAnUpstreamUrlTemplate(""))
|
||||
.When(x => x.WhenIFindTheUrlVariableNamesAndValues())
|
||||
.And(x => x.ThenTheTemplatesVariablesAre(new List<PlaceholderNameAndValue>()))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
|
||||
[Fact]
|
||||
public void can_match_down_stream_url_with_nothing_then_placeholder_no_value_is_blank()
|
||||
{
|
||||
var expectedTemplates = new List<PlaceholderNameAndValue>
|
||||
{
|
||||
new PlaceholderNameAndValue("{url}", "")
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenIHaveAUpstreamPath(""))
|
||||
.And(x => x.GivenIHaveAnUpstreamUrlTemplate("/{url}"))
|
||||
.When(x => x.WhenIFindTheUrlVariableNamesAndValues())
|
||||
.And(x => x.ThenTheTemplatesVariablesAre(expectedTemplates))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void can_match_down_stream_url_with_nothing_then_placeholder_value_is_test()
|
||||
{
|
||||
var expectedTemplates = new List<PlaceholderNameAndValue>
|
||||
{
|
||||
new PlaceholderNameAndValue("{url}", "test")
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenIHaveAUpstreamPath("/test"))
|
||||
.And(x => x.GivenIHaveAnUpstreamUrlTemplate("/{url}"))
|
||||
.When(x => x.WhenIFindTheUrlVariableNamesAndValues())
|
||||
.And(x => x.ThenTheTemplatesVariablesAre(expectedTemplates))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void can_match_down_stream_url_with_forward_slash_then_placeholder_no_value_is_blank()
|
||||
{
|
||||
var expectedTemplates = new List<PlaceholderNameAndValue>
|
||||
{
|
||||
new PlaceholderNameAndValue("{url}", "")
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenIHaveAUpstreamPath("/"))
|
||||
.And(x => x.GivenIHaveAnUpstreamUrlTemplate("/{url}"))
|
||||
.When(x => x.WhenIFindTheUrlVariableNamesAndValues())
|
||||
.And(x => x.ThenTheTemplatesVariablesAre(expectedTemplates))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void can_match_down_stream_url_with_forward_slash()
|
||||
{
|
||||
var expectedTemplates = new List<PlaceholderNameAndValue>
|
||||
{
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenIHaveAUpstreamPath("/"))
|
||||
.And(x => x.GivenIHaveAnUpstreamUrlTemplate("/"))
|
||||
.When(x => x.WhenIFindTheUrlVariableNamesAndValues())
|
||||
.And(x => x.ThenTheTemplatesVariablesAre(expectedTemplates))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void can_match_down_stream_url_with_forward_slash_then_placeholder_then_another_value()
|
||||
{
|
||||
var expectedTemplates = new List<PlaceholderNameAndValue>
|
||||
{
|
||||
new PlaceholderNameAndValue("{url}", "1")
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenIHaveAUpstreamPath("/1/products"))
|
||||
.And(x => x.GivenIHaveAnUpstreamUrlTemplate("/{url}/products"))
|
||||
.When(x => x.WhenIFindTheUrlVariableNamesAndValues())
|
||||
.And(x => x.ThenTheTemplatesVariablesAre(expectedTemplates))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_not_find_anything()
|
||||
{
|
||||
this.Given(x => x.GivenIHaveAUpstreamPath("/products"))
|
||||
.And(x => x.GivenIHaveAnUpstreamUrlTemplate("/products/"))
|
||||
.When(x => x.WhenIFindTheUrlVariableNamesAndValues())
|
||||
.And(x => x.ThenTheTemplatesVariablesAre(new List<PlaceholderNameAndValue>()))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void can_match_down_stream_url_with_no_slash()
|
||||
{
|
||||
this.Given(x => x.GivenIHaveAUpstreamPath("api"))
|
||||
.Given(x => x.GivenIHaveAnUpstreamUrlTemplate("api"))
|
||||
.When(x => x.WhenIFindTheUrlVariableNamesAndValues())
|
||||
.And(x => x.ThenTheTemplatesVariablesAre(new List<PlaceholderNameAndValue>()))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void can_match_down_stream_url_with_one_slash()
|
||||
{
|
||||
this.Given(x => x.GivenIHaveAUpstreamPath("api/"))
|
||||
.Given(x => x.GivenIHaveAnUpstreamUrlTemplate("api/"))
|
||||
.When(x => x.WhenIFindTheUrlVariableNamesAndValues())
|
||||
.And(x => x.ThenTheTemplatesVariablesAre(new List<PlaceholderNameAndValue>()))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void can_match_down_stream_url_with_downstream_template()
|
||||
{
|
||||
this.Given(x => x.GivenIHaveAUpstreamPath("api/product/products/"))
|
||||
.Given(x => x.GivenIHaveAnUpstreamUrlTemplate("api/product/products/"))
|
||||
.When(x => x.WhenIFindTheUrlVariableNamesAndValues())
|
||||
.And(x => x.ThenTheTemplatesVariablesAre(new List<PlaceholderNameAndValue>()))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void can_match_down_stream_url_with_downstream_template_with_one_place_holder()
|
||||
{
|
||||
var expectedTemplates = new List<PlaceholderNameAndValue>
|
||||
{
|
||||
new PlaceholderNameAndValue("{productId}", "1")
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenIHaveAUpstreamPath("api/product/products/1"))
|
||||
.Given(x => x.GivenIHaveAnUpstreamUrlTemplate("api/product/products/{productId}"))
|
||||
.When(x => x.WhenIFindTheUrlVariableNamesAndValues())
|
||||
.And(x => x.ThenTheTemplatesVariablesAre(expectedTemplates))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void can_match_down_stream_url_with_downstream_template_with_two_place_holders()
|
||||
{
|
||||
var expectedTemplates = new List<PlaceholderNameAndValue>
|
||||
{
|
||||
new PlaceholderNameAndValue("{productId}", "1"),
|
||||
new PlaceholderNameAndValue("{categoryId}", "2")
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenIHaveAUpstreamPath("api/product/products/1/2"))
|
||||
.Given(x => x.GivenIHaveAnUpstreamUrlTemplate("api/product/products/{productId}/{categoryId}"))
|
||||
.When(x => x.WhenIFindTheUrlVariableNamesAndValues())
|
||||
.And(x => x.ThenTheTemplatesVariablesAre(expectedTemplates))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void can_match_down_stream_url_with_downstream_template_with_two_place_holders_seperated_by_something()
|
||||
{
|
||||
var expectedTemplates = new List<PlaceholderNameAndValue>
|
||||
{
|
||||
new PlaceholderNameAndValue("{productId}", "1"),
|
||||
new PlaceholderNameAndValue("{categoryId}", "2")
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenIHaveAUpstreamPath("api/product/products/1/categories/2"))
|
||||
.And(x => x.GivenIHaveAnUpstreamUrlTemplate("api/product/products/{productId}/categories/{categoryId}"))
|
||||
.When(x => x.WhenIFindTheUrlVariableNamesAndValues())
|
||||
.And(x => x.ThenTheTemplatesVariablesAre(expectedTemplates))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void can_match_down_stream_url_with_downstream_template_with_three_place_holders_seperated_by_something()
|
||||
{
|
||||
var expectedTemplates = new List<PlaceholderNameAndValue>
|
||||
{
|
||||
new PlaceholderNameAndValue("{productId}", "1"),
|
||||
new PlaceholderNameAndValue("{categoryId}", "2"),
|
||||
new PlaceholderNameAndValue("{variantId}", "123")
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenIHaveAUpstreamPath("api/product/products/1/categories/2/variant/123"))
|
||||
.And(x => x.GivenIHaveAnUpstreamUrlTemplate("api/product/products/{productId}/categories/{categoryId}/variant/{variantId}"))
|
||||
.When(x => x.WhenIFindTheUrlVariableNamesAndValues())
|
||||
.And(x => x.ThenTheTemplatesVariablesAre(expectedTemplates))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void can_match_down_stream_url_with_downstream_template_with_three_place_holders()
|
||||
{
|
||||
var expectedTemplates = new List<PlaceholderNameAndValue>
|
||||
{
|
||||
new PlaceholderNameAndValue("{productId}", "1"),
|
||||
new PlaceholderNameAndValue("{categoryId}", "2")
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenIHaveAUpstreamPath("api/product/products/1/categories/2/variant/"))
|
||||
.And(x => x.GivenIHaveAnUpstreamUrlTemplate("api/product/products/{productId}/categories/{categoryId}/variant/"))
|
||||
.When(x => x.WhenIFindTheUrlVariableNamesAndValues())
|
||||
.And(x => x.ThenTheTemplatesVariablesAre(expectedTemplates))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void can_match_down_stream_url_with_downstream_template_with_place_holder_to_final_url_path()
|
||||
{
|
||||
var expectedTemplates = new List<PlaceholderNameAndValue>
|
||||
{
|
||||
new PlaceholderNameAndValue("{finalUrlPath}", "product/products/categories/"),
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenIHaveAUpstreamPath("api/product/products/categories/"))
|
||||
.And(x => x.GivenIHaveAnUpstreamUrlTemplate("api/{finalUrlPath}/"))
|
||||
.When(x => x.WhenIFindTheUrlVariableNamesAndValues())
|
||||
.And(x => x.ThenTheTemplatesVariablesAre(expectedTemplates))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void ThenTheTemplatesVariablesAre(List<PlaceholderNameAndValue> expectedResults)
|
||||
{
|
||||
foreach (var expectedResult in expectedResults)
|
||||
{
|
||||
var result = _result.Data.First(t => t.Name == expectedResult.Name);
|
||||
result.Value.ShouldBe(expectedResult.Value);
|
||||
}
|
||||
}
|
||||
|
||||
private void GivenIHaveAUpstreamPath(string downstreamPath)
|
||||
{
|
||||
_downstreamUrlPath = downstreamPath;
|
||||
}
|
||||
|
||||
private void GivenIHaveAnUpstreamUrlTemplate(string downstreamUrlTemplate)
|
||||
{
|
||||
_downstreamPathTemplate = downstreamUrlTemplate;
|
||||
}
|
||||
|
||||
private void WhenIFindTheUrlVariableNamesAndValues()
|
||||
{
|
||||
_result = _finder.Find(_downstreamUrlPath, _downstreamPathTemplate);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,103 +1,103 @@
|
||||
namespace Ocelot.UnitTests.DownstreamUrlCreator
|
||||
{
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Net.Http;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Moq;
|
||||
using Ocelot.Configuration.Builder;
|
||||
using Ocelot.DownstreamRouteFinder;
|
||||
using Ocelot.DownstreamRouteFinder.UrlMatcher;
|
||||
using Ocelot.DownstreamUrlCreator;
|
||||
using Ocelot.DownstreamUrlCreator.Middleware;
|
||||
using Ocelot.DownstreamUrlCreator.UrlTemplateReplacer;
|
||||
using Ocelot.Infrastructure.RequestData;
|
||||
using Ocelot.Logging;
|
||||
using Ocelot.Responses;
|
||||
using Ocelot.Values;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
using Shouldly;
|
||||
|
||||
public class DownstreamUrlCreatorMiddlewareTests : ServerHostedMiddlewareTest
|
||||
{
|
||||
private readonly Mock<IDownstreamPathPlaceholderReplacer> _downstreamUrlTemplateVariableReplacer;
|
||||
private readonly Mock<IUrlBuilder> _urlBuilder;
|
||||
private Response<DownstreamRoute> _downstreamRoute;
|
||||
private OkResponse<DownstreamPath> _downstreamPath;
|
||||
private HttpRequestMessage _downstreamRequest;
|
||||
|
||||
public DownstreamUrlCreatorMiddlewareTests()
|
||||
{
|
||||
_downstreamUrlTemplateVariableReplacer = new Mock<IDownstreamPathPlaceholderReplacer>();
|
||||
_urlBuilder = new Mock<IUrlBuilder>();
|
||||
|
||||
_downstreamRequest = new HttpRequestMessage(HttpMethod.Get, "https://my.url/abc/?q=123");
|
||||
|
||||
ScopedRepository
|
||||
.Setup(sr => sr.Get<HttpRequestMessage>("DownstreamRequest"))
|
||||
.Returns(new OkResponse<HttpRequestMessage>(_downstreamRequest));
|
||||
|
||||
GivenTheTestServerIsConfigured();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_replace_scheme_and_path()
|
||||
{
|
||||
this.Given(x => x.GivenTheDownStreamRouteIs(
|
||||
new DownstreamRoute(
|
||||
new List<PlaceholderNameAndValue>(),
|
||||
new ReRouteBuilder()
|
||||
.WithDownstreamPathTemplate("any old string")
|
||||
.WithUpstreamHttpMethod(new List<string> { "Get" })
|
||||
.WithDownstreamScheme("https")
|
||||
.Build())))
|
||||
.And(x => x.GivenTheDownstreamRequestUriIs("http://my.url/abc?q=123"))
|
||||
.And(x => x.GivenTheUrlReplacerWillReturn("/api/products/1"))
|
||||
.When(x => x.WhenICallTheMiddleware())
|
||||
.Then(x => x.ThenTheDownstreamRequestUriIs("https://my.url:80/api/products/1?q=123"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
protected override void GivenTheTestServerServicesAreConfigured(IServiceCollection services)
|
||||
{
|
||||
services.AddSingleton<IOcelotLoggerFactory, AspDotNetLoggerFactory>();
|
||||
services.AddLogging();
|
||||
services.AddSingleton(_downstreamUrlTemplateVariableReplacer.Object);
|
||||
services.AddSingleton(ScopedRepository.Object);
|
||||
services.AddSingleton(_urlBuilder.Object);
|
||||
}
|
||||
|
||||
protected override void GivenTheTestServerPipelineIsConfigured(IApplicationBuilder app)
|
||||
{
|
||||
app.UseDownstreamUrlCreatorMiddleware();
|
||||
}
|
||||
|
||||
private void GivenTheDownStreamRouteIs(DownstreamRoute downstreamRoute)
|
||||
{
|
||||
_downstreamRoute = new OkResponse<DownstreamRoute>(downstreamRoute);
|
||||
ScopedRepository
|
||||
.Setup(x => x.Get<DownstreamRoute>(It.IsAny<string>()))
|
||||
.Returns(_downstreamRoute);
|
||||
}
|
||||
|
||||
private void GivenTheDownstreamRequestUriIs(string uri)
|
||||
{
|
||||
_downstreamRequest.RequestUri = new Uri(uri);
|
||||
}
|
||||
|
||||
private void GivenTheUrlReplacerWillReturn(string path)
|
||||
{
|
||||
_downstreamPath = new OkResponse<DownstreamPath>(new DownstreamPath(path));
|
||||
_downstreamUrlTemplateVariableReplacer
|
||||
.Setup(x => x.Replace(It.IsAny<PathTemplate>(), It.IsAny<List<PlaceholderNameAndValue>>()))
|
||||
.Returns(_downstreamPath);
|
||||
}
|
||||
|
||||
private void ThenTheDownstreamRequestUriIs(string expectedUri)
|
||||
{
|
||||
_downstreamRequest.RequestUri.OriginalString.ShouldBe(expectedUri);
|
||||
}
|
||||
}
|
||||
}
|
||||
namespace Ocelot.UnitTests.DownstreamUrlCreator
|
||||
{
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Net.Http;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Moq;
|
||||
using Ocelot.Configuration.Builder;
|
||||
using Ocelot.DownstreamRouteFinder;
|
||||
using Ocelot.DownstreamRouteFinder.UrlMatcher;
|
||||
using Ocelot.DownstreamUrlCreator;
|
||||
using Ocelot.DownstreamUrlCreator.Middleware;
|
||||
using Ocelot.DownstreamUrlCreator.UrlTemplateReplacer;
|
||||
using Ocelot.Infrastructure.RequestData;
|
||||
using Ocelot.Logging;
|
||||
using Ocelot.Responses;
|
||||
using Ocelot.Values;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
using Shouldly;
|
||||
|
||||
public class DownstreamUrlCreatorMiddlewareTests : ServerHostedMiddlewareTest
|
||||
{
|
||||
private readonly Mock<IDownstreamPathPlaceholderReplacer> _downstreamUrlTemplateVariableReplacer;
|
||||
private readonly Mock<IUrlBuilder> _urlBuilder;
|
||||
private Response<DownstreamRoute> _downstreamRoute;
|
||||
private OkResponse<DownstreamPath> _downstreamPath;
|
||||
private HttpRequestMessage _downstreamRequest;
|
||||
|
||||
public DownstreamUrlCreatorMiddlewareTests()
|
||||
{
|
||||
_downstreamUrlTemplateVariableReplacer = new Mock<IDownstreamPathPlaceholderReplacer>();
|
||||
_urlBuilder = new Mock<IUrlBuilder>();
|
||||
|
||||
_downstreamRequest = new HttpRequestMessage(HttpMethod.Get, "https://my.url/abc/?q=123");
|
||||
|
||||
ScopedRepository
|
||||
.Setup(sr => sr.Get<HttpRequestMessage>("DownstreamRequest"))
|
||||
.Returns(new OkResponse<HttpRequestMessage>(_downstreamRequest));
|
||||
|
||||
GivenTheTestServerIsConfigured();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_replace_scheme_and_path()
|
||||
{
|
||||
this.Given(x => x.GivenTheDownStreamRouteIs(
|
||||
new DownstreamRoute(
|
||||
new List<PlaceholderNameAndValue>(),
|
||||
new ReRouteBuilder()
|
||||
.WithDownstreamPathTemplate("any old string")
|
||||
.WithUpstreamHttpMethod(new List<string> { "Get" })
|
||||
.WithDownstreamScheme("https")
|
||||
.Build())))
|
||||
.And(x => x.GivenTheDownstreamRequestUriIs("http://my.url/abc?q=123"))
|
||||
.And(x => x.GivenTheUrlReplacerWillReturn("/api/products/1"))
|
||||
.When(x => x.WhenICallTheMiddleware())
|
||||
.Then(x => x.ThenTheDownstreamRequestUriIs("https://my.url:80/api/products/1?q=123"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
protected override void GivenTheTestServerServicesAreConfigured(IServiceCollection services)
|
||||
{
|
||||
services.AddSingleton<IOcelotLoggerFactory, AspDotNetLoggerFactory>();
|
||||
services.AddLogging();
|
||||
services.AddSingleton(_downstreamUrlTemplateVariableReplacer.Object);
|
||||
services.AddSingleton(ScopedRepository.Object);
|
||||
services.AddSingleton(_urlBuilder.Object);
|
||||
}
|
||||
|
||||
protected override void GivenTheTestServerPipelineIsConfigured(IApplicationBuilder app)
|
||||
{
|
||||
app.UseDownstreamUrlCreatorMiddleware();
|
||||
}
|
||||
|
||||
private void GivenTheDownStreamRouteIs(DownstreamRoute downstreamRoute)
|
||||
{
|
||||
_downstreamRoute = new OkResponse<DownstreamRoute>(downstreamRoute);
|
||||
ScopedRepository
|
||||
.Setup(x => x.Get<DownstreamRoute>(It.IsAny<string>()))
|
||||
.Returns(_downstreamRoute);
|
||||
}
|
||||
|
||||
private void GivenTheDownstreamRequestUriIs(string uri)
|
||||
{
|
||||
_downstreamRequest.RequestUri = new Uri(uri);
|
||||
}
|
||||
|
||||
private void GivenTheUrlReplacerWillReturn(string path)
|
||||
{
|
||||
_downstreamPath = new OkResponse<DownstreamPath>(new DownstreamPath(path));
|
||||
_downstreamUrlTemplateVariableReplacer
|
||||
.Setup(x => x.Replace(It.IsAny<PathTemplate>(), It.IsAny<List<PlaceholderNameAndValue>>()))
|
||||
.Returns(_downstreamPath);
|
||||
}
|
||||
|
||||
private void ThenTheDownstreamRequestUriIs(string expectedUri)
|
||||
{
|
||||
_downstreamRequest.RequestUri.OriginalString.ShouldBe(expectedUri);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,122 +1,122 @@
|
||||
using System;
|
||||
using Ocelot.DownstreamUrlCreator;
|
||||
using Ocelot.Responses;
|
||||
using Ocelot.Values;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests.DownstreamUrlCreator
|
||||
{
|
||||
public class UrlBuilderTests
|
||||
{
|
||||
private readonly IUrlBuilder _urlBuilder;
|
||||
private string _dsPath;
|
||||
private string _dsScheme;
|
||||
private string _dsHost;
|
||||
private int _dsPort;
|
||||
|
||||
private Response<DownstreamUrl> _result;
|
||||
|
||||
public UrlBuilderTests()
|
||||
{
|
||||
_urlBuilder = new UrlBuilder();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_error_when_downstream_path_is_null()
|
||||
{
|
||||
this.Given(x => x.GivenADownstreamPath(null))
|
||||
.When(x => x.WhenIBuildTheUrl())
|
||||
.Then(x => x.ThenThereIsAnErrorOfType<DownstreamPathNullOrEmptyError>())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_error_when_downstream_scheme_is_null()
|
||||
{
|
||||
this.Given(x => x.GivenADownstreamScheme(null))
|
||||
.And(x => x.GivenADownstreamPath("test"))
|
||||
.When(x => x.WhenIBuildTheUrl())
|
||||
.Then(x => x.ThenThereIsAnErrorOfType<DownstreamSchemeNullOrEmptyError>())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_error_when_downstream_host_is_null()
|
||||
{
|
||||
this.Given(x => x.GivenADownstreamScheme(null))
|
||||
.And(x => x.GivenADownstreamPath("test"))
|
||||
.And(x => x.GivenADownstreamScheme("test"))
|
||||
.When(x => x.WhenIBuildTheUrl())
|
||||
.Then(x => x.ThenThereIsAnErrorOfType<DownstreamHostNullOrEmptyError>())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_not_use_port_if_zero()
|
||||
{
|
||||
this.Given(x => x.GivenADownstreamPath("/api/products/1"))
|
||||
.And(x => x.GivenADownstreamScheme("http"))
|
||||
.And(x => x.GivenADownstreamHost("127.0.0.1"))
|
||||
.And(x => x.GivenADownstreamPort(0))
|
||||
.When(x => x.WhenIBuildTheUrl())
|
||||
.Then(x => x.ThenTheUrlIsReturned("http://127.0.0.1/api/products/1"))
|
||||
.And(x => x.ThenTheUrlIsWellFormed())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_build_well_formed_uri()
|
||||
{
|
||||
this.Given(x => x.GivenADownstreamPath("/api/products/1"))
|
||||
.And(x => x.GivenADownstreamScheme("http"))
|
||||
.And(x => x.GivenADownstreamHost("127.0.0.1"))
|
||||
.And(x => x.GivenADownstreamPort(5000))
|
||||
.When(x => x.WhenIBuildTheUrl())
|
||||
.Then(x => x.ThenTheUrlIsReturned("http://127.0.0.1:5000/api/products/1"))
|
||||
.And(x => x.ThenTheUrlIsWellFormed())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void ThenThereIsAnErrorOfType<T>()
|
||||
{
|
||||
_result.Errors[0].ShouldBeOfType<T>();
|
||||
}
|
||||
|
||||
private void GivenADownstreamPath(string dsPath)
|
||||
{
|
||||
_dsPath = dsPath;
|
||||
}
|
||||
|
||||
private void GivenADownstreamScheme(string dsScheme)
|
||||
{
|
||||
_dsScheme = dsScheme;
|
||||
}
|
||||
|
||||
private void GivenADownstreamHost(string dsHost)
|
||||
{
|
||||
_dsHost = dsHost;
|
||||
}
|
||||
|
||||
private void GivenADownstreamPort(int dsPort)
|
||||
{
|
||||
_dsPort = dsPort;
|
||||
}
|
||||
|
||||
private void WhenIBuildTheUrl()
|
||||
{
|
||||
_result = _urlBuilder.Build(_dsPath, _dsScheme, new HostAndPort(_dsHost, _dsPort));
|
||||
}
|
||||
|
||||
private void ThenTheUrlIsReturned(string expected)
|
||||
{
|
||||
_result.Data.Value.ShouldBe(expected);
|
||||
}
|
||||
|
||||
private void ThenTheUrlIsWellFormed()
|
||||
{
|
||||
Uri.IsWellFormedUriString(_result.Data.Value, UriKind.Absolute).ShouldBeTrue();
|
||||
}
|
||||
}
|
||||
}
|
||||
using System;
|
||||
using Ocelot.DownstreamUrlCreator;
|
||||
using Ocelot.Responses;
|
||||
using Ocelot.Values;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests.DownstreamUrlCreator
|
||||
{
|
||||
public class UrlBuilderTests
|
||||
{
|
||||
private readonly IUrlBuilder _urlBuilder;
|
||||
private string _dsPath;
|
||||
private string _dsScheme;
|
||||
private string _dsHost;
|
||||
private int _dsPort;
|
||||
|
||||
private Response<DownstreamUrl> _result;
|
||||
|
||||
public UrlBuilderTests()
|
||||
{
|
||||
_urlBuilder = new UrlBuilder();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_error_when_downstream_path_is_null()
|
||||
{
|
||||
this.Given(x => x.GivenADownstreamPath(null))
|
||||
.When(x => x.WhenIBuildTheUrl())
|
||||
.Then(x => x.ThenThereIsAnErrorOfType<DownstreamPathNullOrEmptyError>())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_error_when_downstream_scheme_is_null()
|
||||
{
|
||||
this.Given(x => x.GivenADownstreamScheme(null))
|
||||
.And(x => x.GivenADownstreamPath("test"))
|
||||
.When(x => x.WhenIBuildTheUrl())
|
||||
.Then(x => x.ThenThereIsAnErrorOfType<DownstreamSchemeNullOrEmptyError>())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_error_when_downstream_host_is_null()
|
||||
{
|
||||
this.Given(x => x.GivenADownstreamScheme(null))
|
||||
.And(x => x.GivenADownstreamPath("test"))
|
||||
.And(x => x.GivenADownstreamScheme("test"))
|
||||
.When(x => x.WhenIBuildTheUrl())
|
||||
.Then(x => x.ThenThereIsAnErrorOfType<DownstreamHostNullOrEmptyError>())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_not_use_port_if_zero()
|
||||
{
|
||||
this.Given(x => x.GivenADownstreamPath("/api/products/1"))
|
||||
.And(x => x.GivenADownstreamScheme("http"))
|
||||
.And(x => x.GivenADownstreamHost("127.0.0.1"))
|
||||
.And(x => x.GivenADownstreamPort(0))
|
||||
.When(x => x.WhenIBuildTheUrl())
|
||||
.Then(x => x.ThenTheUrlIsReturned("http://127.0.0.1/api/products/1"))
|
||||
.And(x => x.ThenTheUrlIsWellFormed())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_build_well_formed_uri()
|
||||
{
|
||||
this.Given(x => x.GivenADownstreamPath("/api/products/1"))
|
||||
.And(x => x.GivenADownstreamScheme("http"))
|
||||
.And(x => x.GivenADownstreamHost("127.0.0.1"))
|
||||
.And(x => x.GivenADownstreamPort(5000))
|
||||
.When(x => x.WhenIBuildTheUrl())
|
||||
.Then(x => x.ThenTheUrlIsReturned("http://127.0.0.1:5000/api/products/1"))
|
||||
.And(x => x.ThenTheUrlIsWellFormed())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void ThenThereIsAnErrorOfType<T>()
|
||||
{
|
||||
_result.Errors[0].ShouldBeOfType<T>();
|
||||
}
|
||||
|
||||
private void GivenADownstreamPath(string dsPath)
|
||||
{
|
||||
_dsPath = dsPath;
|
||||
}
|
||||
|
||||
private void GivenADownstreamScheme(string dsScheme)
|
||||
{
|
||||
_dsScheme = dsScheme;
|
||||
}
|
||||
|
||||
private void GivenADownstreamHost(string dsHost)
|
||||
{
|
||||
_dsHost = dsHost;
|
||||
}
|
||||
|
||||
private void GivenADownstreamPort(int dsPort)
|
||||
{
|
||||
_dsPort = dsPort;
|
||||
}
|
||||
|
||||
private void WhenIBuildTheUrl()
|
||||
{
|
||||
_result = _urlBuilder.Build(_dsPath, _dsScheme, new ServiceHostAndPort(_dsHost, _dsPort));
|
||||
}
|
||||
|
||||
private void ThenTheUrlIsReturned(string expected)
|
||||
{
|
||||
_result.Data.Value.ShouldBe(expected);
|
||||
}
|
||||
|
||||
private void ThenTheUrlIsWellFormed()
|
||||
{
|
||||
Uri.IsWellFormedUriString(_result.Data.Value, UriKind.Absolute).ShouldBeTrue();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,183 +1,183 @@
|
||||
using System.Collections.Generic;
|
||||
using Ocelot.Configuration.Builder;
|
||||
using Ocelot.DownstreamRouteFinder;
|
||||
using Ocelot.DownstreamRouteFinder.UrlMatcher;
|
||||
using Ocelot.DownstreamUrlCreator.UrlTemplateReplacer;
|
||||
using Ocelot.Responses;
|
||||
using Ocelot.Values;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests.DownstreamUrlCreator.UrlTemplateReplacer
|
||||
{
|
||||
public class UpstreamUrlPathTemplateVariableReplacerTests
|
||||
{
|
||||
private DownstreamRoute _downstreamRoute;
|
||||
private Response<DownstreamPath> _result;
|
||||
private readonly IDownstreamPathPlaceholderReplacer _downstreamPathReplacer;
|
||||
|
||||
public UpstreamUrlPathTemplateVariableReplacerTests()
|
||||
{
|
||||
_downstreamPathReplacer = new DownstreamTemplatePathPlaceholderReplacer();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void can_replace_no_template_variables()
|
||||
{
|
||||
this.Given(x => x.GivenThereIsAUrlMatch(
|
||||
new DownstreamRoute(
|
||||
new List<PlaceholderNameAndValue>(),
|
||||
new ReRouteBuilder()
|
||||
.WithUpstreamHttpMethod(new List<string> { "Get" })
|
||||
.Build())))
|
||||
.When(x => x.WhenIReplaceTheTemplateVariables())
|
||||
.Then(x => x.ThenTheDownstreamUrlPathIsReturned(""))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void can_replace_no_template_variables_with_slash()
|
||||
{
|
||||
this.Given(x => x.GivenThereIsAUrlMatch(
|
||||
new DownstreamRoute(
|
||||
new List<PlaceholderNameAndValue>(),
|
||||
new ReRouteBuilder()
|
||||
.WithDownstreamPathTemplate("/")
|
||||
.WithUpstreamHttpMethod(new List<string> { "Get" })
|
||||
.Build())))
|
||||
.When(x => x.WhenIReplaceTheTemplateVariables())
|
||||
.Then(x => x.ThenTheDownstreamUrlPathIsReturned("/"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void can_replace_url_no_slash()
|
||||
{
|
||||
this.Given(x => x.GivenThereIsAUrlMatch(new DownstreamRoute(new List<PlaceholderNameAndValue>(),
|
||||
new ReRouteBuilder()
|
||||
.WithDownstreamPathTemplate("api")
|
||||
.WithUpstreamHttpMethod(new List<string> { "Get" })
|
||||
.Build())))
|
||||
.When(x => x.WhenIReplaceTheTemplateVariables())
|
||||
.Then(x => x.ThenTheDownstreamUrlPathIsReturned("api"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void can_replace_url_one_slash()
|
||||
{
|
||||
this.Given(x => x.GivenThereIsAUrlMatch(new DownstreamRoute(new List<PlaceholderNameAndValue>(),
|
||||
new ReRouteBuilder()
|
||||
.WithDownstreamPathTemplate("api/")
|
||||
.WithUpstreamHttpMethod(new List<string> { "Get" })
|
||||
.Build())))
|
||||
.When(x => x.WhenIReplaceTheTemplateVariables())
|
||||
.Then(x => x.ThenTheDownstreamUrlPathIsReturned("api/"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void can_replace_url_multiple_slash()
|
||||
{
|
||||
this.Given(x => x.GivenThereIsAUrlMatch(new DownstreamRoute(new List<PlaceholderNameAndValue>(),
|
||||
new ReRouteBuilder()
|
||||
.WithDownstreamPathTemplate("api/product/products/")
|
||||
.WithUpstreamHttpMethod(new List<string> { "Get" })
|
||||
.Build())))
|
||||
.When(x => x.WhenIReplaceTheTemplateVariables())
|
||||
.Then(x => x.ThenTheDownstreamUrlPathIsReturned("api/product/products/"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void can_replace_url_one_template_variable()
|
||||
{
|
||||
var templateVariables = new List<PlaceholderNameAndValue>()
|
||||
{
|
||||
new PlaceholderNameAndValue("{productId}", "1")
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenThereIsAUrlMatch(new DownstreamRoute(templateVariables,
|
||||
new ReRouteBuilder()
|
||||
.WithDownstreamPathTemplate("productservice/products/{productId}/")
|
||||
.WithUpstreamHttpMethod(new List<string> { "Get" })
|
||||
.Build())))
|
||||
.When(x => x.WhenIReplaceTheTemplateVariables())
|
||||
.Then(x => x.ThenTheDownstreamUrlPathIsReturned("productservice/products/1/"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void can_replace_url_one_template_variable_with_path_after()
|
||||
{
|
||||
var templateVariables = new List<PlaceholderNameAndValue>()
|
||||
{
|
||||
new PlaceholderNameAndValue("{productId}", "1")
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenThereIsAUrlMatch(new DownstreamRoute(templateVariables,
|
||||
new ReRouteBuilder()
|
||||
.WithDownstreamPathTemplate("productservice/products/{productId}/variants")
|
||||
.WithUpstreamHttpMethod(new List<string> { "Get" })
|
||||
.Build())))
|
||||
.When(x => x.WhenIReplaceTheTemplateVariables())
|
||||
.Then(x => x.ThenTheDownstreamUrlPathIsReturned("productservice/products/1/variants"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void can_replace_url_two_template_variable()
|
||||
{
|
||||
var templateVariables = new List<PlaceholderNameAndValue>()
|
||||
{
|
||||
new PlaceholderNameAndValue("{productId}", "1"),
|
||||
new PlaceholderNameAndValue("{variantId}", "12")
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenThereIsAUrlMatch(new DownstreamRoute(templateVariables,
|
||||
new ReRouteBuilder()
|
||||
.WithDownstreamPathTemplate("productservice/products/{productId}/variants/{variantId}")
|
||||
.WithUpstreamHttpMethod(new List<string> { "Get" })
|
||||
.Build())))
|
||||
.When(x => x.WhenIReplaceTheTemplateVariables())
|
||||
.Then(x => x.ThenTheDownstreamUrlPathIsReturned("productservice/products/1/variants/12"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void can_replace_url_three_template_variable()
|
||||
{
|
||||
var templateVariables = new List<PlaceholderNameAndValue>()
|
||||
{
|
||||
new PlaceholderNameAndValue("{productId}", "1"),
|
||||
new PlaceholderNameAndValue("{variantId}", "12"),
|
||||
new PlaceholderNameAndValue("{categoryId}", "34")
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenThereIsAUrlMatch(new DownstreamRoute(templateVariables,
|
||||
new ReRouteBuilder()
|
||||
.WithDownstreamPathTemplate("productservice/category/{categoryId}/products/{productId}/variants/{variantId}")
|
||||
.WithUpstreamHttpMethod(new List<string> { "Get" })
|
||||
.Build())))
|
||||
.When(x => x.WhenIReplaceTheTemplateVariables())
|
||||
.Then(x => x.ThenTheDownstreamUrlPathIsReturned("productservice/category/34/products/1/variants/12"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenThereIsAUrlMatch(DownstreamRoute downstreamRoute)
|
||||
{
|
||||
_downstreamRoute = downstreamRoute;
|
||||
}
|
||||
|
||||
private void WhenIReplaceTheTemplateVariables()
|
||||
{
|
||||
_result = _downstreamPathReplacer.Replace(_downstreamRoute.ReRoute.DownstreamPathTemplate, _downstreamRoute.TemplatePlaceholderNameAndValues);
|
||||
}
|
||||
|
||||
private void ThenTheDownstreamUrlPathIsReturned(string expected)
|
||||
{
|
||||
_result.Data.Value.ShouldBe(expected);
|
||||
}
|
||||
}
|
||||
}
|
||||
using System.Collections.Generic;
|
||||
using Ocelot.Configuration.Builder;
|
||||
using Ocelot.DownstreamRouteFinder;
|
||||
using Ocelot.DownstreamRouteFinder.UrlMatcher;
|
||||
using Ocelot.DownstreamUrlCreator.UrlTemplateReplacer;
|
||||
using Ocelot.Responses;
|
||||
using Ocelot.Values;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests.DownstreamUrlCreator.UrlTemplateReplacer
|
||||
{
|
||||
public class UpstreamUrlPathTemplateVariableReplacerTests
|
||||
{
|
||||
private DownstreamRoute _downstreamRoute;
|
||||
private Response<DownstreamPath> _result;
|
||||
private readonly IDownstreamPathPlaceholderReplacer _downstreamPathReplacer;
|
||||
|
||||
public UpstreamUrlPathTemplateVariableReplacerTests()
|
||||
{
|
||||
_downstreamPathReplacer = new DownstreamTemplatePathPlaceholderReplacer();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void can_replace_no_template_variables()
|
||||
{
|
||||
this.Given(x => x.GivenThereIsAUrlMatch(
|
||||
new DownstreamRoute(
|
||||
new List<PlaceholderNameAndValue>(),
|
||||
new ReRouteBuilder()
|
||||
.WithUpstreamHttpMethod(new List<string> { "Get" })
|
||||
.Build())))
|
||||
.When(x => x.WhenIReplaceTheTemplateVariables())
|
||||
.Then(x => x.ThenTheDownstreamUrlPathIsReturned(""))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void can_replace_no_template_variables_with_slash()
|
||||
{
|
||||
this.Given(x => x.GivenThereIsAUrlMatch(
|
||||
new DownstreamRoute(
|
||||
new List<PlaceholderNameAndValue>(),
|
||||
new ReRouteBuilder()
|
||||
.WithDownstreamPathTemplate("/")
|
||||
.WithUpstreamHttpMethod(new List<string> { "Get" })
|
||||
.Build())))
|
||||
.When(x => x.WhenIReplaceTheTemplateVariables())
|
||||
.Then(x => x.ThenTheDownstreamUrlPathIsReturned("/"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void can_replace_url_no_slash()
|
||||
{
|
||||
this.Given(x => x.GivenThereIsAUrlMatch(new DownstreamRoute(new List<PlaceholderNameAndValue>(),
|
||||
new ReRouteBuilder()
|
||||
.WithDownstreamPathTemplate("api")
|
||||
.WithUpstreamHttpMethod(new List<string> { "Get" })
|
||||
.Build())))
|
||||
.When(x => x.WhenIReplaceTheTemplateVariables())
|
||||
.Then(x => x.ThenTheDownstreamUrlPathIsReturned("api"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void can_replace_url_one_slash()
|
||||
{
|
||||
this.Given(x => x.GivenThereIsAUrlMatch(new DownstreamRoute(new List<PlaceholderNameAndValue>(),
|
||||
new ReRouteBuilder()
|
||||
.WithDownstreamPathTemplate("api/")
|
||||
.WithUpstreamHttpMethod(new List<string> { "Get" })
|
||||
.Build())))
|
||||
.When(x => x.WhenIReplaceTheTemplateVariables())
|
||||
.Then(x => x.ThenTheDownstreamUrlPathIsReturned("api/"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void can_replace_url_multiple_slash()
|
||||
{
|
||||
this.Given(x => x.GivenThereIsAUrlMatch(new DownstreamRoute(new List<PlaceholderNameAndValue>(),
|
||||
new ReRouteBuilder()
|
||||
.WithDownstreamPathTemplate("api/product/products/")
|
||||
.WithUpstreamHttpMethod(new List<string> { "Get" })
|
||||
.Build())))
|
||||
.When(x => x.WhenIReplaceTheTemplateVariables())
|
||||
.Then(x => x.ThenTheDownstreamUrlPathIsReturned("api/product/products/"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void can_replace_url_one_template_variable()
|
||||
{
|
||||
var templateVariables = new List<PlaceholderNameAndValue>()
|
||||
{
|
||||
new PlaceholderNameAndValue("{productId}", "1")
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenThereIsAUrlMatch(new DownstreamRoute(templateVariables,
|
||||
new ReRouteBuilder()
|
||||
.WithDownstreamPathTemplate("productservice/products/{productId}/")
|
||||
.WithUpstreamHttpMethod(new List<string> { "Get" })
|
||||
.Build())))
|
||||
.When(x => x.WhenIReplaceTheTemplateVariables())
|
||||
.Then(x => x.ThenTheDownstreamUrlPathIsReturned("productservice/products/1/"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void can_replace_url_one_template_variable_with_path_after()
|
||||
{
|
||||
var templateVariables = new List<PlaceholderNameAndValue>()
|
||||
{
|
||||
new PlaceholderNameAndValue("{productId}", "1")
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenThereIsAUrlMatch(new DownstreamRoute(templateVariables,
|
||||
new ReRouteBuilder()
|
||||
.WithDownstreamPathTemplate("productservice/products/{productId}/variants")
|
||||
.WithUpstreamHttpMethod(new List<string> { "Get" })
|
||||
.Build())))
|
||||
.When(x => x.WhenIReplaceTheTemplateVariables())
|
||||
.Then(x => x.ThenTheDownstreamUrlPathIsReturned("productservice/products/1/variants"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void can_replace_url_two_template_variable()
|
||||
{
|
||||
var templateVariables = new List<PlaceholderNameAndValue>()
|
||||
{
|
||||
new PlaceholderNameAndValue("{productId}", "1"),
|
||||
new PlaceholderNameAndValue("{variantId}", "12")
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenThereIsAUrlMatch(new DownstreamRoute(templateVariables,
|
||||
new ReRouteBuilder()
|
||||
.WithDownstreamPathTemplate("productservice/products/{productId}/variants/{variantId}")
|
||||
.WithUpstreamHttpMethod(new List<string> { "Get" })
|
||||
.Build())))
|
||||
.When(x => x.WhenIReplaceTheTemplateVariables())
|
||||
.Then(x => x.ThenTheDownstreamUrlPathIsReturned("productservice/products/1/variants/12"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void can_replace_url_three_template_variable()
|
||||
{
|
||||
var templateVariables = new List<PlaceholderNameAndValue>()
|
||||
{
|
||||
new PlaceholderNameAndValue("{productId}", "1"),
|
||||
new PlaceholderNameAndValue("{variantId}", "12"),
|
||||
new PlaceholderNameAndValue("{categoryId}", "34")
|
||||
};
|
||||
|
||||
this.Given(x => x.GivenThereIsAUrlMatch(new DownstreamRoute(templateVariables,
|
||||
new ReRouteBuilder()
|
||||
.WithDownstreamPathTemplate("productservice/category/{categoryId}/products/{productId}/variants/{variantId}")
|
||||
.WithUpstreamHttpMethod(new List<string> { "Get" })
|
||||
.Build())))
|
||||
.When(x => x.WhenIReplaceTheTemplateVariables())
|
||||
.Then(x => x.ThenTheDownstreamUrlPathIsReturned("productservice/category/34/products/1/variants/12"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenThereIsAUrlMatch(DownstreamRoute downstreamRoute)
|
||||
{
|
||||
_downstreamRoute = downstreamRoute;
|
||||
}
|
||||
|
||||
private void WhenIReplaceTheTemplateVariables()
|
||||
{
|
||||
_result = _downstreamPathReplacer.Replace(_downstreamRoute.ReRoute.DownstreamPathTemplate, _downstreamRoute.TemplatePlaceholderNameAndValues);
|
||||
}
|
||||
|
||||
private void ThenTheDownstreamUrlPathIsReturned(string expected)
|
||||
{
|
||||
_result.Data.Value.ShouldBe(expected);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,18 +1,18 @@
|
||||
using Ocelot.Errors;
|
||||
using Ocelot.Infrastructure.RequestData;
|
||||
using Shouldly;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests.Errors
|
||||
{
|
||||
public class ErrorTests
|
||||
{
|
||||
[Fact]
|
||||
public void should_return_message()
|
||||
{
|
||||
var error = new CannotAddDataError("message");
|
||||
var result = error.ToString();
|
||||
result.ShouldBe("message");
|
||||
}
|
||||
}
|
||||
using Ocelot.Errors;
|
||||
using Ocelot.Infrastructure.RequestData;
|
||||
using Shouldly;
|
||||
using Xunit;
|
||||
|
||||
namespace Ocelot.UnitTests.Errors
|
||||
{
|
||||
public class ErrorTests
|
||||
{
|
||||
[Fact]
|
||||
public void should_return_message()
|
||||
{
|
||||
var error = new CannotAddDataError("message");
|
||||
var result = error.ToString();
|
||||
result.ShouldBe("message");
|
||||
}
|
||||
}
|
||||
}
|
@ -1,144 +1,144 @@
|
||||
namespace Ocelot.UnitTests.Errors
|
||||
{
|
||||
using System;
|
||||
using System.Net;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Ocelot.Errors.Middleware;
|
||||
using Ocelot.Logging;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Ocelot.Configuration.Provider;
|
||||
using Moq;
|
||||
using Ocelot.Configuration;
|
||||
using Rafty.Concensus;
|
||||
|
||||
public class ExceptionHandlerMiddlewareTests : ServerHostedMiddlewareTest
|
||||
{
|
||||
bool _shouldThrowAnException = false;
|
||||
private Mock<IOcelotConfigurationProvider> _provider;
|
||||
|
||||
public ExceptionHandlerMiddlewareTests()
|
||||
{
|
||||
_provider = new Mock<IOcelotConfigurationProvider>();
|
||||
GivenTheTestServerIsConfigured();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void NoDownstreamException()
|
||||
{
|
||||
var config = new OcelotConfiguration(null, null, null, null);
|
||||
|
||||
this.Given(_ => GivenAnExceptionWillNotBeThrownDownstream())
|
||||
.And(_ => GivenTheConfigurationIs(config))
|
||||
.When(_ => WhenICallTheMiddleware())
|
||||
.Then(_ => ThenTheResponseIsOk())
|
||||
.And(_ => TheRequestIdIsNotSet())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void TheRequestIdIsNotSet()
|
||||
{
|
||||
ScopedRepository.Verify(x => x.Add<string>(It.IsAny<string>(), It.IsAny<string>()), Times.Never);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DownstreamException()
|
||||
{
|
||||
var config = new OcelotConfiguration(null, null, null, null);
|
||||
|
||||
this.Given(_ => GivenAnExceptionWillBeThrownDownstream())
|
||||
.And(_ => GivenTheConfigurationIs(config))
|
||||
.When(_ => WhenICallTheMiddleware())
|
||||
.Then(_ => ThenTheResponseIsError())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ShouldSetRequestId()
|
||||
{
|
||||
var config = new OcelotConfiguration(null, null, null, "requestidkey");
|
||||
|
||||
this.Given(_ => GivenAnExceptionWillNotBeThrownDownstream())
|
||||
.And(_ => GivenTheConfigurationIs(config))
|
||||
.When(_ => WhenICallTheMiddlewareWithTheRequestIdKey("requestidkey", "1234"))
|
||||
.Then(_ => ThenTheResponseIsOk())
|
||||
.And(_ => TheRequestIdIsSet("RequestId", "1234"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ShouldNotSetRequestId()
|
||||
{
|
||||
var config = new OcelotConfiguration(null, null, null, null);
|
||||
|
||||
this.Given(_ => GivenAnExceptionWillNotBeThrownDownstream())
|
||||
.And(_ => GivenTheConfigurationIs(config))
|
||||
.When(_ => WhenICallTheMiddlewareWithTheRequestIdKey("requestidkey", "1234"))
|
||||
.Then(_ => ThenTheResponseIsOk())
|
||||
.And(_ => TheRequestIdIsNotSet())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void TheRequestIdIsSet(string key, string value)
|
||||
{
|
||||
ScopedRepository.Verify(x => x.Add<string>(key, value), Times.Once);
|
||||
}
|
||||
|
||||
private void GivenTheConfigurationIs(IOcelotConfiguration config)
|
||||
{
|
||||
var response = new Ocelot.Responses.OkResponse<IOcelotConfiguration>(config);
|
||||
_provider
|
||||
.Setup(x => x.Get()).ReturnsAsync(response);
|
||||
}
|
||||
|
||||
protected override void GivenTheTestServerServicesAreConfigured(IServiceCollection services)
|
||||
{
|
||||
services.AddSingleton<IOcelotLoggerFactory, AspDotNetLoggerFactory>();
|
||||
services.AddLogging();
|
||||
services.AddSingleton(ScopedRepository.Object);
|
||||
services.AddSingleton<IOcelotConfigurationProvider>(_provider.Object);
|
||||
}
|
||||
|
||||
protected override void GivenTheTestServerPipelineIsConfigured(IApplicationBuilder app)
|
||||
{
|
||||
app.UseExceptionHandlerMiddleware();
|
||||
app.Run(DownstreamExceptionSimulator);
|
||||
}
|
||||
|
||||
private async Task DownstreamExceptionSimulator(HttpContext context)
|
||||
{
|
||||
await Task.CompletedTask;
|
||||
|
||||
if (_shouldThrowAnException)
|
||||
{
|
||||
throw new Exception("BOOM");
|
||||
}
|
||||
|
||||
context.Response.StatusCode = (int)HttpStatusCode.OK;
|
||||
}
|
||||
|
||||
private void GivenAnExceptionWillNotBeThrownDownstream()
|
||||
{
|
||||
_shouldThrowAnException = false;
|
||||
}
|
||||
|
||||
private void GivenAnExceptionWillBeThrownDownstream()
|
||||
{
|
||||
_shouldThrowAnException = true;
|
||||
}
|
||||
|
||||
private void ThenTheResponseIsOk()
|
||||
{
|
||||
ResponseMessage.StatusCode.ShouldBe(HttpStatusCode.OK);
|
||||
}
|
||||
|
||||
private void ThenTheResponseIsError()
|
||||
{
|
||||
ResponseMessage.StatusCode.ShouldBe(HttpStatusCode.InternalServerError);
|
||||
}
|
||||
}
|
||||
namespace Ocelot.UnitTests.Errors
|
||||
{
|
||||
using System;
|
||||
using System.Net;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Ocelot.Errors.Middleware;
|
||||
using Ocelot.Logging;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Ocelot.Configuration.Provider;
|
||||
using Moq;
|
||||
using Ocelot.Configuration;
|
||||
using Rafty.Concensus;
|
||||
|
||||
public class ExceptionHandlerMiddlewareTests : ServerHostedMiddlewareTest
|
||||
{
|
||||
bool _shouldThrowAnException = false;
|
||||
private Mock<IOcelotConfigurationProvider> _provider;
|
||||
|
||||
public ExceptionHandlerMiddlewareTests()
|
||||
{
|
||||
_provider = new Mock<IOcelotConfigurationProvider>();
|
||||
GivenTheTestServerIsConfigured();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void NoDownstreamException()
|
||||
{
|
||||
var config = new OcelotConfiguration(null, null, null, null);
|
||||
|
||||
this.Given(_ => GivenAnExceptionWillNotBeThrownDownstream())
|
||||
.And(_ => GivenTheConfigurationIs(config))
|
||||
.When(_ => WhenICallTheMiddleware())
|
||||
.Then(_ => ThenTheResponseIsOk())
|
||||
.And(_ => TheRequestIdIsNotSet())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void TheRequestIdIsNotSet()
|
||||
{
|
||||
ScopedRepository.Verify(x => x.Add<string>(It.IsAny<string>(), It.IsAny<string>()), Times.Never);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DownstreamException()
|
||||
{
|
||||
var config = new OcelotConfiguration(null, null, null, null);
|
||||
|
||||
this.Given(_ => GivenAnExceptionWillBeThrownDownstream())
|
||||
.And(_ => GivenTheConfigurationIs(config))
|
||||
.When(_ => WhenICallTheMiddleware())
|
||||
.Then(_ => ThenTheResponseIsError())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ShouldSetRequestId()
|
||||
{
|
||||
var config = new OcelotConfiguration(null, null, null, "requestidkey");
|
||||
|
||||
this.Given(_ => GivenAnExceptionWillNotBeThrownDownstream())
|
||||
.And(_ => GivenTheConfigurationIs(config))
|
||||
.When(_ => WhenICallTheMiddlewareWithTheRequestIdKey("requestidkey", "1234"))
|
||||
.Then(_ => ThenTheResponseIsOk())
|
||||
.And(_ => TheRequestIdIsSet("RequestId", "1234"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ShouldNotSetRequestId()
|
||||
{
|
||||
var config = new OcelotConfiguration(null, null, null, null);
|
||||
|
||||
this.Given(_ => GivenAnExceptionWillNotBeThrownDownstream())
|
||||
.And(_ => GivenTheConfigurationIs(config))
|
||||
.When(_ => WhenICallTheMiddlewareWithTheRequestIdKey("requestidkey", "1234"))
|
||||
.Then(_ => ThenTheResponseIsOk())
|
||||
.And(_ => TheRequestIdIsNotSet())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void TheRequestIdIsSet(string key, string value)
|
||||
{
|
||||
ScopedRepository.Verify(x => x.Add<string>(key, value), Times.Once);
|
||||
}
|
||||
|
||||
private void GivenTheConfigurationIs(IOcelotConfiguration config)
|
||||
{
|
||||
var response = new Ocelot.Responses.OkResponse<IOcelotConfiguration>(config);
|
||||
_provider
|
||||
.Setup(x => x.Get()).ReturnsAsync(response);
|
||||
}
|
||||
|
||||
protected override void GivenTheTestServerServicesAreConfigured(IServiceCollection services)
|
||||
{
|
||||
services.AddSingleton<IOcelotLoggerFactory, AspDotNetLoggerFactory>();
|
||||
services.AddLogging();
|
||||
services.AddSingleton(ScopedRepository.Object);
|
||||
services.AddSingleton<IOcelotConfigurationProvider>(_provider.Object);
|
||||
}
|
||||
|
||||
protected override void GivenTheTestServerPipelineIsConfigured(IApplicationBuilder app)
|
||||
{
|
||||
app.UseExceptionHandlerMiddleware();
|
||||
app.Run(DownstreamExceptionSimulator);
|
||||
}
|
||||
|
||||
private async Task DownstreamExceptionSimulator(HttpContext context)
|
||||
{
|
||||
await Task.CompletedTask;
|
||||
|
||||
if (_shouldThrowAnException)
|
||||
{
|
||||
throw new Exception("BOOM");
|
||||
}
|
||||
|
||||
context.Response.StatusCode = (int)HttpStatusCode.OK;
|
||||
}
|
||||
|
||||
private void GivenAnExceptionWillNotBeThrownDownstream()
|
||||
{
|
||||
_shouldThrowAnException = false;
|
||||
}
|
||||
|
||||
private void GivenAnExceptionWillBeThrownDownstream()
|
||||
{
|
||||
_shouldThrowAnException = true;
|
||||
}
|
||||
|
||||
private void ThenTheResponseIsOk()
|
||||
{
|
||||
ResponseMessage.StatusCode.ShouldBe(HttpStatusCode.OK);
|
||||
}
|
||||
|
||||
private void ThenTheResponseIsError()
|
||||
{
|
||||
ResponseMessage.StatusCode.ShouldBe(HttpStatusCode.InternalServerError);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,151 +1,151 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Security.Claims;
|
||||
using Moq;
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.Errors;
|
||||
using Ocelot.Headers;
|
||||
using Ocelot.Infrastructure.Claims.Parser;
|
||||
using Ocelot.Responses;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
using System.Net.Http;
|
||||
|
||||
namespace Ocelot.UnitTests.Headers
|
||||
{
|
||||
public class AddHeadersToRequestTests
|
||||
{
|
||||
private readonly AddHeadersToRequest _addHeadersToRequest;
|
||||
private readonly Mock<IClaimsParser> _parser;
|
||||
private readonly HttpRequestMessage _downstreamRequest;
|
||||
private List<Claim> _claims;
|
||||
private List<ClaimToThing> _configuration;
|
||||
private Response _result;
|
||||
private Response<string> _claimValue;
|
||||
|
||||
public AddHeadersToRequestTests()
|
||||
{
|
||||
_parser = new Mock<IClaimsParser>();
|
||||
_addHeadersToRequest = new AddHeadersToRequest(_parser.Object);
|
||||
_downstreamRequest = new HttpRequestMessage();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_add_headers_to_downstreamRequest()
|
||||
{
|
||||
var claims = new List<Claim>
|
||||
{
|
||||
new Claim("test", "data")
|
||||
};
|
||||
|
||||
this.Given(
|
||||
x => x.GivenConfigurationHeaderExtractorProperties(new List<ClaimToThing>
|
||||
{
|
||||
new ClaimToThing("header-key", "", "", 0)
|
||||
}))
|
||||
.Given(x => x.GivenClaims(claims))
|
||||
.And(x => x.GivenTheClaimParserReturns(new OkResponse<string>("value")))
|
||||
.When(x => x.WhenIAddHeadersToTheRequest())
|
||||
.Then(x => x.ThenTheResultIsSuccess())
|
||||
.And(x => x.ThenTheHeaderIsAdded())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_replace_existing_headers_on_request()
|
||||
{
|
||||
this.Given(
|
||||
x => x.GivenConfigurationHeaderExtractorProperties(new List<ClaimToThing>
|
||||
{
|
||||
new ClaimToThing("header-key", "", "", 0)
|
||||
}))
|
||||
.Given(x => x.GivenClaims(new List<Claim>
|
||||
{
|
||||
new Claim("test", "data")
|
||||
}))
|
||||
.And(x => x.GivenTheClaimParserReturns(new OkResponse<string>("value")))
|
||||
.And(x => x.GivenThatTheRequestContainsHeader("header-key", "initial"))
|
||||
.When(x => x.WhenIAddHeadersToTheRequest())
|
||||
.Then(x => x.ThenTheResultIsSuccess())
|
||||
.And(x => x.ThenTheHeaderIsAdded())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_error()
|
||||
{
|
||||
this.Given(
|
||||
x => x.GivenConfigurationHeaderExtractorProperties(new List<ClaimToThing>
|
||||
{
|
||||
new ClaimToThing("", "", "", 0)
|
||||
}))
|
||||
.Given(x => x.GivenClaims(new List<Claim>()))
|
||||
.And(x => x.GivenTheClaimParserReturns(new ErrorResponse<string>(new List<Error>
|
||||
{
|
||||
new AnyError()
|
||||
})))
|
||||
.When(x => x.WhenIAddHeadersToTheRequest())
|
||||
.Then(x => x.ThenTheResultIsError())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenClaims(List<Claim> claims)
|
||||
{
|
||||
_claims = claims;
|
||||
}
|
||||
|
||||
private void GivenConfigurationHeaderExtractorProperties(List<ClaimToThing> configuration)
|
||||
{
|
||||
_configuration = configuration;
|
||||
}
|
||||
|
||||
private void GivenThatTheRequestContainsHeader(string key, string value)
|
||||
{
|
||||
_downstreamRequest.Headers.Add(key, value);
|
||||
}
|
||||
|
||||
private void GivenTheClaimParserReturns(Response<string> claimValue)
|
||||
{
|
||||
_claimValue = claimValue;
|
||||
_parser
|
||||
.Setup(
|
||||
x =>
|
||||
x.GetValue(It.IsAny<IEnumerable<Claim>>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<int>()))
|
||||
.Returns(_claimValue);
|
||||
}
|
||||
|
||||
private void WhenIAddHeadersToTheRequest()
|
||||
{
|
||||
_result = _addHeadersToRequest.SetHeadersOnDownstreamRequest(_configuration, _claims, _downstreamRequest);
|
||||
}
|
||||
|
||||
private void ThenTheResultIsSuccess()
|
||||
{
|
||||
_result.IsError.ShouldBe(false);
|
||||
}
|
||||
|
||||
private void ThenTheResultIsError()
|
||||
{
|
||||
|
||||
_result.IsError.ShouldBe(true);
|
||||
}
|
||||
|
||||
private void ThenTheHeaderIsAdded()
|
||||
{
|
||||
var header = _downstreamRequest.Headers.First(x => x.Key == "header-key");
|
||||
header.Value.First().ShouldBe(_claimValue.Data);
|
||||
}
|
||||
|
||||
class AnyError : Error
|
||||
{
|
||||
public AnyError()
|
||||
: base("blahh", OcelotErrorCode.UnknownError)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Security.Claims;
|
||||
using Moq;
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.Errors;
|
||||
using Ocelot.Headers;
|
||||
using Ocelot.Infrastructure.Claims.Parser;
|
||||
using Ocelot.Responses;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
using System.Net.Http;
|
||||
|
||||
namespace Ocelot.UnitTests.Headers
|
||||
{
|
||||
public class AddHeadersToRequestTests
|
||||
{
|
||||
private readonly AddHeadersToRequest _addHeadersToRequest;
|
||||
private readonly Mock<IClaimsParser> _parser;
|
||||
private readonly HttpRequestMessage _downstreamRequest;
|
||||
private List<Claim> _claims;
|
||||
private List<ClaimToThing> _configuration;
|
||||
private Response _result;
|
||||
private Response<string> _claimValue;
|
||||
|
||||
public AddHeadersToRequestTests()
|
||||
{
|
||||
_parser = new Mock<IClaimsParser>();
|
||||
_addHeadersToRequest = new AddHeadersToRequest(_parser.Object);
|
||||
_downstreamRequest = new HttpRequestMessage();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_add_headers_to_downstreamRequest()
|
||||
{
|
||||
var claims = new List<Claim>
|
||||
{
|
||||
new Claim("test", "data")
|
||||
};
|
||||
|
||||
this.Given(
|
||||
x => x.GivenConfigurationHeaderExtractorProperties(new List<ClaimToThing>
|
||||
{
|
||||
new ClaimToThing("header-key", "", "", 0)
|
||||
}))
|
||||
.Given(x => x.GivenClaims(claims))
|
||||
.And(x => x.GivenTheClaimParserReturns(new OkResponse<string>("value")))
|
||||
.When(x => x.WhenIAddHeadersToTheRequest())
|
||||
.Then(x => x.ThenTheResultIsSuccess())
|
||||
.And(x => x.ThenTheHeaderIsAdded())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_replace_existing_headers_on_request()
|
||||
{
|
||||
this.Given(
|
||||
x => x.GivenConfigurationHeaderExtractorProperties(new List<ClaimToThing>
|
||||
{
|
||||
new ClaimToThing("header-key", "", "", 0)
|
||||
}))
|
||||
.Given(x => x.GivenClaims(new List<Claim>
|
||||
{
|
||||
new Claim("test", "data")
|
||||
}))
|
||||
.And(x => x.GivenTheClaimParserReturns(new OkResponse<string>("value")))
|
||||
.And(x => x.GivenThatTheRequestContainsHeader("header-key", "initial"))
|
||||
.When(x => x.WhenIAddHeadersToTheRequest())
|
||||
.Then(x => x.ThenTheResultIsSuccess())
|
||||
.And(x => x.ThenTheHeaderIsAdded())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_return_error()
|
||||
{
|
||||
this.Given(
|
||||
x => x.GivenConfigurationHeaderExtractorProperties(new List<ClaimToThing>
|
||||
{
|
||||
new ClaimToThing("", "", "", 0)
|
||||
}))
|
||||
.Given(x => x.GivenClaims(new List<Claim>()))
|
||||
.And(x => x.GivenTheClaimParserReturns(new ErrorResponse<string>(new List<Error>
|
||||
{
|
||||
new AnyError()
|
||||
})))
|
||||
.When(x => x.WhenIAddHeadersToTheRequest())
|
||||
.Then(x => x.ThenTheResultIsError())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenClaims(List<Claim> claims)
|
||||
{
|
||||
_claims = claims;
|
||||
}
|
||||
|
||||
private void GivenConfigurationHeaderExtractorProperties(List<ClaimToThing> configuration)
|
||||
{
|
||||
_configuration = configuration;
|
||||
}
|
||||
|
||||
private void GivenThatTheRequestContainsHeader(string key, string value)
|
||||
{
|
||||
_downstreamRequest.Headers.Add(key, value);
|
||||
}
|
||||
|
||||
private void GivenTheClaimParserReturns(Response<string> claimValue)
|
||||
{
|
||||
_claimValue = claimValue;
|
||||
_parser
|
||||
.Setup(
|
||||
x =>
|
||||
x.GetValue(It.IsAny<IEnumerable<Claim>>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<int>()))
|
||||
.Returns(_claimValue);
|
||||
}
|
||||
|
||||
private void WhenIAddHeadersToTheRequest()
|
||||
{
|
||||
_result = _addHeadersToRequest.SetHeadersOnDownstreamRequest(_configuration, _claims, _downstreamRequest);
|
||||
}
|
||||
|
||||
private void ThenTheResultIsSuccess()
|
||||
{
|
||||
_result.IsError.ShouldBe(false);
|
||||
}
|
||||
|
||||
private void ThenTheResultIsError()
|
||||
{
|
||||
|
||||
_result.IsError.ShouldBe(true);
|
||||
}
|
||||
|
||||
private void ThenTheHeaderIsAdded()
|
||||
{
|
||||
var header = _downstreamRequest.Headers.First(x => x.Key == "header-key");
|
||||
header.Value.First().ShouldBe(_claimValue.Data);
|
||||
}
|
||||
|
||||
class AnyError : Error
|
||||
{
|
||||
public AnyError()
|
||||
: base("blahh", OcelotErrorCode.UnknownError)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,91 +1,91 @@
|
||||
using Xunit;
|
||||
using Shouldly;
|
||||
using Ocelot.Headers.Middleware;
|
||||
using TestStack.BDDfy;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using System.Collections.Generic;
|
||||
using Ocelot.Responses;
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.Headers;
|
||||
|
||||
namespace Ocelot.UnitTests.Headers
|
||||
{
|
||||
public class HttpContextRequestHeaderReplacerTests
|
||||
{
|
||||
private HttpContext _context;
|
||||
private List<HeaderFindAndReplace> _fAndRs;
|
||||
private HttpContextRequestHeaderReplacer _replacer;
|
||||
private Response _result;
|
||||
|
||||
public HttpContextRequestHeaderReplacerTests()
|
||||
{
|
||||
_replacer = new HttpContextRequestHeaderReplacer();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_replace_headers()
|
||||
{
|
||||
var context = new DefaultHttpContext();
|
||||
context.Request.Headers.Add("test", "test");
|
||||
|
||||
var fAndRs = new List<HeaderFindAndReplace>();
|
||||
fAndRs.Add(new HeaderFindAndReplace("test", "test", "chiken", 0));
|
||||
|
||||
this.Given(x => GivenTheFollowingHttpRequest(context))
|
||||
.And(x => GivenTheFollowingHeaderReplacements(fAndRs))
|
||||
.When(x => WhenICallTheReplacer())
|
||||
.Then(x => ThenTheHeadersAreReplaced())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_not_replace_headers()
|
||||
{
|
||||
var context = new DefaultHttpContext();
|
||||
context.Request.Headers.Add("test", "test");
|
||||
|
||||
var fAndRs = new List<HeaderFindAndReplace>();
|
||||
|
||||
this.Given(x => GivenTheFollowingHttpRequest(context))
|
||||
.And(x => GivenTheFollowingHeaderReplacements(fAndRs))
|
||||
.When(x => WhenICallTheReplacer())
|
||||
.Then(x => ThenTheHeadersAreNotReplaced())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void ThenTheHeadersAreNotReplaced()
|
||||
{
|
||||
_result.ShouldBeOfType<OkResponse>();
|
||||
foreach (var f in _fAndRs)
|
||||
{
|
||||
_context.Request.Headers.TryGetValue(f.Key, out var values);
|
||||
values[f.Index].ShouldBe("test");
|
||||
}
|
||||
}
|
||||
|
||||
private void GivenTheFollowingHttpRequest(HttpContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
private void GivenTheFollowingHeaderReplacements(List<HeaderFindAndReplace> fAndRs)
|
||||
{
|
||||
_fAndRs = fAndRs;
|
||||
}
|
||||
|
||||
private void WhenICallTheReplacer()
|
||||
{
|
||||
_result = _replacer.Replace(_context, _fAndRs);
|
||||
}
|
||||
|
||||
private void ThenTheHeadersAreReplaced()
|
||||
{
|
||||
_result.ShouldBeOfType<OkResponse>();
|
||||
foreach (var f in _fAndRs)
|
||||
{
|
||||
_context.Request.Headers.TryGetValue(f.Key, out var values);
|
||||
values[f.Index].ShouldBe(f.Replace);
|
||||
}
|
||||
}
|
||||
}
|
||||
using Xunit;
|
||||
using Shouldly;
|
||||
using Ocelot.Headers.Middleware;
|
||||
using TestStack.BDDfy;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using System.Collections.Generic;
|
||||
using Ocelot.Responses;
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.Headers;
|
||||
|
||||
namespace Ocelot.UnitTests.Headers
|
||||
{
|
||||
public class HttpContextRequestHeaderReplacerTests
|
||||
{
|
||||
private HttpContext _context;
|
||||
private List<HeaderFindAndReplace> _fAndRs;
|
||||
private HttpContextRequestHeaderReplacer _replacer;
|
||||
private Response _result;
|
||||
|
||||
public HttpContextRequestHeaderReplacerTests()
|
||||
{
|
||||
_replacer = new HttpContextRequestHeaderReplacer();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_replace_headers()
|
||||
{
|
||||
var context = new DefaultHttpContext();
|
||||
context.Request.Headers.Add("test", "test");
|
||||
|
||||
var fAndRs = new List<HeaderFindAndReplace>();
|
||||
fAndRs.Add(new HeaderFindAndReplace("test", "test", "chiken", 0));
|
||||
|
||||
this.Given(x => GivenTheFollowingHttpRequest(context))
|
||||
.And(x => GivenTheFollowingHeaderReplacements(fAndRs))
|
||||
.When(x => WhenICallTheReplacer())
|
||||
.Then(x => ThenTheHeadersAreReplaced())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_not_replace_headers()
|
||||
{
|
||||
var context = new DefaultHttpContext();
|
||||
context.Request.Headers.Add("test", "test");
|
||||
|
||||
var fAndRs = new List<HeaderFindAndReplace>();
|
||||
|
||||
this.Given(x => GivenTheFollowingHttpRequest(context))
|
||||
.And(x => GivenTheFollowingHeaderReplacements(fAndRs))
|
||||
.When(x => WhenICallTheReplacer())
|
||||
.Then(x => ThenTheHeadersAreNotReplaced())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void ThenTheHeadersAreNotReplaced()
|
||||
{
|
||||
_result.ShouldBeOfType<OkResponse>();
|
||||
foreach (var f in _fAndRs)
|
||||
{
|
||||
_context.Request.Headers.TryGetValue(f.Key, out var values);
|
||||
values[f.Index].ShouldBe("test");
|
||||
}
|
||||
}
|
||||
|
||||
private void GivenTheFollowingHttpRequest(HttpContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
private void GivenTheFollowingHeaderReplacements(List<HeaderFindAndReplace> fAndRs)
|
||||
{
|
||||
_fAndRs = fAndRs;
|
||||
}
|
||||
|
||||
private void WhenICallTheReplacer()
|
||||
{
|
||||
_result = _replacer.Replace(_context, _fAndRs);
|
||||
}
|
||||
|
||||
private void ThenTheHeadersAreReplaced()
|
||||
{
|
||||
_result.ShouldBeOfType<OkResponse>();
|
||||
foreach (var f in _fAndRs)
|
||||
{
|
||||
_context.Request.Headers.TryGetValue(f.Key, out var values);
|
||||
values[f.Index].ShouldBe(f.Replace);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,101 +1,101 @@
|
||||
using Xunit;
|
||||
using Shouldly;
|
||||
using Ocelot.Logging;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Ocelot.Headers.Middleware;
|
||||
using TestStack.BDDfy;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using System;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using System.Collections.Generic;
|
||||
using Moq;
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.DownstreamRouteFinder;
|
||||
using Ocelot.Responses;
|
||||
using Ocelot.Configuration.Builder;
|
||||
using Ocelot.Headers;
|
||||
using System.Net.Http;
|
||||
|
||||
namespace Ocelot.UnitTests.Headers
|
||||
{
|
||||
public class HttpHeadersTransformationMiddlewareTests : ServerHostedMiddlewareTest
|
||||
{
|
||||
private Mock<IHttpContextRequestHeaderReplacer> _preReplacer;
|
||||
private Mock<IHttpResponseHeaderReplacer> _postReplacer;
|
||||
|
||||
public HttpHeadersTransformationMiddlewareTests()
|
||||
{
|
||||
_preReplacer = new Mock<IHttpContextRequestHeaderReplacer>();
|
||||
_postReplacer = new Mock<IHttpResponseHeaderReplacer>();
|
||||
|
||||
GivenTheTestServerIsConfigured();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_call_pre_and_post_header_transforms()
|
||||
{
|
||||
this.Given(x => GivenTheFollowingRequest())
|
||||
.And(x => GivenTheDownstreamRequestIs())
|
||||
.And(x => GivenTheReRouteHasPreFindAndReplaceSetUp())
|
||||
.And(x => GivenTheHttpResponseMessageIs())
|
||||
.When(x => WhenICallTheMiddleware())
|
||||
.Then(x => ThenTheIHttpContextRequestHeaderReplacerIsCalledCorrectly())
|
||||
.And(x => ThenTheIHttpResponseHeaderReplacerIsCalledCorrectly())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenTheDownstreamRequestIs()
|
||||
{
|
||||
var request = new HttpRequestMessage();
|
||||
var response = new OkResponse<HttpRequestMessage>(request);
|
||||
ScopedRepository.Setup(x => x.Get<HttpRequestMessage>("DownstreamRequest")).Returns(response);
|
||||
}
|
||||
|
||||
private void GivenTheHttpResponseMessageIs()
|
||||
{
|
||||
var httpResponseMessage = new HttpResponseMessage();
|
||||
var response = new OkResponse<HttpResponseMessage>(httpResponseMessage);
|
||||
ScopedRepository.Setup(x => x.Get<HttpResponseMessage>("HttpResponseMessage")).Returns(response);
|
||||
}
|
||||
|
||||
private void GivenTheReRouteHasPreFindAndReplaceSetUp()
|
||||
{
|
||||
var fAndRs = new List<HeaderFindAndReplace>();
|
||||
var reRoute = new ReRouteBuilder().WithUpstreamHeaderFindAndReplace(fAndRs).WithDownstreamHeaderFindAndReplace(fAndRs).Build();
|
||||
var dR = new DownstreamRoute(null, reRoute);
|
||||
var response = new OkResponse<DownstreamRoute>(dR);
|
||||
ScopedRepository.Setup(x => x.Get<DownstreamRoute>("DownstreamRoute")).Returns(response);
|
||||
}
|
||||
|
||||
private void ThenTheIHttpContextRequestHeaderReplacerIsCalledCorrectly()
|
||||
{
|
||||
_preReplacer.Verify(x => x.Replace(It.IsAny<HttpContext>(), It.IsAny<List<HeaderFindAndReplace>>()), Times.Once);
|
||||
}
|
||||
|
||||
private void ThenTheIHttpResponseHeaderReplacerIsCalledCorrectly()
|
||||
{
|
||||
_postReplacer.Verify(x => x.Replace(It.IsAny<HttpResponseMessage>(), It.IsAny<List<HeaderFindAndReplace>>(), It.IsAny<HttpRequestMessage>()), Times.Once);
|
||||
}
|
||||
|
||||
private void GivenTheFollowingRequest()
|
||||
{
|
||||
Client.DefaultRequestHeaders.Add("test", "test");
|
||||
}
|
||||
|
||||
protected override void GivenTheTestServerServicesAreConfigured(IServiceCollection services)
|
||||
{
|
||||
services.AddSingleton<IOcelotLoggerFactory, AspDotNetLoggerFactory>();
|
||||
services.AddLogging();
|
||||
services.AddSingleton(ScopedRepository.Object);
|
||||
services.AddSingleton(_preReplacer.Object);
|
||||
services.AddSingleton(_postReplacer.Object);
|
||||
}
|
||||
|
||||
protected override void GivenTheTestServerPipelineIsConfigured(IApplicationBuilder app)
|
||||
{
|
||||
app.UseHttpHeadersTransformationMiddleware();
|
||||
}
|
||||
}
|
||||
using Xunit;
|
||||
using Shouldly;
|
||||
using Ocelot.Logging;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Ocelot.Headers.Middleware;
|
||||
using TestStack.BDDfy;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using System;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using System.Collections.Generic;
|
||||
using Moq;
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.DownstreamRouteFinder;
|
||||
using Ocelot.Responses;
|
||||
using Ocelot.Configuration.Builder;
|
||||
using Ocelot.Headers;
|
||||
using System.Net.Http;
|
||||
|
||||
namespace Ocelot.UnitTests.Headers
|
||||
{
|
||||
public class HttpHeadersTransformationMiddlewareTests : ServerHostedMiddlewareTest
|
||||
{
|
||||
private Mock<IHttpContextRequestHeaderReplacer> _preReplacer;
|
||||
private Mock<IHttpResponseHeaderReplacer> _postReplacer;
|
||||
|
||||
public HttpHeadersTransformationMiddlewareTests()
|
||||
{
|
||||
_preReplacer = new Mock<IHttpContextRequestHeaderReplacer>();
|
||||
_postReplacer = new Mock<IHttpResponseHeaderReplacer>();
|
||||
|
||||
GivenTheTestServerIsConfigured();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_call_pre_and_post_header_transforms()
|
||||
{
|
||||
this.Given(x => GivenTheFollowingRequest())
|
||||
.And(x => GivenTheDownstreamRequestIs())
|
||||
.And(x => GivenTheReRouteHasPreFindAndReplaceSetUp())
|
||||
.And(x => GivenTheHttpResponseMessageIs())
|
||||
.When(x => WhenICallTheMiddleware())
|
||||
.Then(x => ThenTheIHttpContextRequestHeaderReplacerIsCalledCorrectly())
|
||||
.And(x => ThenTheIHttpResponseHeaderReplacerIsCalledCorrectly())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenTheDownstreamRequestIs()
|
||||
{
|
||||
var request = new HttpRequestMessage();
|
||||
var response = new OkResponse<HttpRequestMessage>(request);
|
||||
ScopedRepository.Setup(x => x.Get<HttpRequestMessage>("DownstreamRequest")).Returns(response);
|
||||
}
|
||||
|
||||
private void GivenTheHttpResponseMessageIs()
|
||||
{
|
||||
var httpResponseMessage = new HttpResponseMessage();
|
||||
var response = new OkResponse<HttpResponseMessage>(httpResponseMessage);
|
||||
ScopedRepository.Setup(x => x.Get<HttpResponseMessage>("HttpResponseMessage")).Returns(response);
|
||||
}
|
||||
|
||||
private void GivenTheReRouteHasPreFindAndReplaceSetUp()
|
||||
{
|
||||
var fAndRs = new List<HeaderFindAndReplace>();
|
||||
var reRoute = new ReRouteBuilder().WithUpstreamHeaderFindAndReplace(fAndRs).WithDownstreamHeaderFindAndReplace(fAndRs).Build();
|
||||
var dR = new DownstreamRoute(null, reRoute);
|
||||
var response = new OkResponse<DownstreamRoute>(dR);
|
||||
ScopedRepository.Setup(x => x.Get<DownstreamRoute>("DownstreamRoute")).Returns(response);
|
||||
}
|
||||
|
||||
private void ThenTheIHttpContextRequestHeaderReplacerIsCalledCorrectly()
|
||||
{
|
||||
_preReplacer.Verify(x => x.Replace(It.IsAny<HttpContext>(), It.IsAny<List<HeaderFindAndReplace>>()), Times.Once);
|
||||
}
|
||||
|
||||
private void ThenTheIHttpResponseHeaderReplacerIsCalledCorrectly()
|
||||
{
|
||||
_postReplacer.Verify(x => x.Replace(It.IsAny<HttpResponseMessage>(), It.IsAny<List<HeaderFindAndReplace>>(), It.IsAny<HttpRequestMessage>()), Times.Once);
|
||||
}
|
||||
|
||||
private void GivenTheFollowingRequest()
|
||||
{
|
||||
Client.DefaultRequestHeaders.Add("test", "test");
|
||||
}
|
||||
|
||||
protected override void GivenTheTestServerServicesAreConfigured(IServiceCollection services)
|
||||
{
|
||||
services.AddSingleton<IOcelotLoggerFactory, AspDotNetLoggerFactory>();
|
||||
services.AddLogging();
|
||||
services.AddSingleton(ScopedRepository.Object);
|
||||
services.AddSingleton(_preReplacer.Object);
|
||||
services.AddSingleton(_postReplacer.Object);
|
||||
}
|
||||
|
||||
protected override void GivenTheTestServerPipelineIsConfigured(IApplicationBuilder app)
|
||||
{
|
||||
app.UseHttpHeadersTransformationMiddleware();
|
||||
}
|
||||
}
|
||||
}
|
@ -1,97 +1,97 @@
|
||||
namespace Ocelot.UnitTests.Headers
|
||||
{
|
||||
using System.Collections.Generic;
|
||||
using System.Net.Http;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Moq;
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.Configuration.Builder;
|
||||
using Ocelot.DownstreamRouteFinder;
|
||||
using Ocelot.DownstreamRouteFinder.UrlMatcher;
|
||||
using Ocelot.Headers;
|
||||
using Ocelot.Headers.Middleware;
|
||||
using Ocelot.Logging;
|
||||
using Ocelot.Responses;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
public class HttpRequestHeadersBuilderMiddlewareTests : ServerHostedMiddlewareTest
|
||||
{
|
||||
private readonly Mock<IAddHeadersToRequest> _addHeaders;
|
||||
private readonly HttpRequestMessage _downstreamRequest;
|
||||
private Response<DownstreamRoute> _downstreamRoute;
|
||||
|
||||
public HttpRequestHeadersBuilderMiddlewareTests()
|
||||
{
|
||||
_addHeaders = new Mock<IAddHeadersToRequest>();
|
||||
|
||||
_downstreamRequest = new HttpRequestMessage();
|
||||
ScopedRepository
|
||||
.Setup(sr => sr.Get<HttpRequestMessage>("DownstreamRequest"))
|
||||
.Returns(new OkResponse<HttpRequestMessage>(_downstreamRequest));
|
||||
|
||||
GivenTheTestServerIsConfigured();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_call_add_headers_to_request_correctly()
|
||||
{
|
||||
var downstreamRoute = new DownstreamRoute(new List<PlaceholderNameAndValue>(),
|
||||
new ReRouteBuilder()
|
||||
.WithDownstreamPathTemplate("any old string")
|
||||
.WithClaimsToHeaders(new List<ClaimToThing>
|
||||
{
|
||||
new ClaimToThing("UserId", "Subject", "", 0)
|
||||
})
|
||||
.WithUpstreamHttpMethod(new List<string> { "Get" })
|
||||
.Build());
|
||||
|
||||
this.Given(x => x.GivenTheDownStreamRouteIs(downstreamRoute))
|
||||
.And(x => x.GivenTheAddHeadersToDownstreamRequestReturnsOk())
|
||||
.When(x => x.WhenICallTheMiddleware())
|
||||
.Then(x => x.ThenTheAddHeadersToRequestIsCalledCorrectly())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
protected override void GivenTheTestServerServicesAreConfigured(IServiceCollection services)
|
||||
{
|
||||
services.AddSingleton<IOcelotLoggerFactory, AspDotNetLoggerFactory>();
|
||||
services.AddLogging();
|
||||
services.AddSingleton(_addHeaders.Object);
|
||||
services.AddSingleton(ScopedRepository.Object);
|
||||
}
|
||||
|
||||
protected override void GivenTheTestServerPipelineIsConfigured(IApplicationBuilder app)
|
||||
{
|
||||
app.UseHttpRequestHeadersBuilderMiddleware();
|
||||
}
|
||||
|
||||
private void GivenTheDownStreamRouteIs(DownstreamRoute downstreamRoute)
|
||||
{
|
||||
_downstreamRoute = new OkResponse<DownstreamRoute>(downstreamRoute);
|
||||
ScopedRepository
|
||||
.Setup(x => x.Get<DownstreamRoute>(It.IsAny<string>()))
|
||||
.Returns(_downstreamRoute);
|
||||
}
|
||||
|
||||
private void GivenTheAddHeadersToDownstreamRequestReturnsOk()
|
||||
{
|
||||
_addHeaders
|
||||
.Setup(x => x.SetHeadersOnDownstreamRequest(
|
||||
It.IsAny<List<ClaimToThing>>(),
|
||||
It.IsAny<IEnumerable<System.Security.Claims.Claim>>(),
|
||||
It.IsAny<HttpRequestMessage>()))
|
||||
.Returns(new OkResponse());
|
||||
}
|
||||
|
||||
private void ThenTheAddHeadersToRequestIsCalledCorrectly()
|
||||
{
|
||||
_addHeaders
|
||||
.Verify(x => x.SetHeadersOnDownstreamRequest(
|
||||
It.IsAny<List<ClaimToThing>>(),
|
||||
It.IsAny<IEnumerable<System.Security.Claims.Claim>>(),
|
||||
_downstreamRequest), Times.Once);
|
||||
}
|
||||
}
|
||||
}
|
||||
namespace Ocelot.UnitTests.Headers
|
||||
{
|
||||
using System.Collections.Generic;
|
||||
using System.Net.Http;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Moq;
|
||||
using Ocelot.Configuration;
|
||||
using Ocelot.Configuration.Builder;
|
||||
using Ocelot.DownstreamRouteFinder;
|
||||
using Ocelot.DownstreamRouteFinder.UrlMatcher;
|
||||
using Ocelot.Headers;
|
||||
using Ocelot.Headers.Middleware;
|
||||
using Ocelot.Logging;
|
||||
using Ocelot.Responses;
|
||||
using TestStack.BDDfy;
|
||||
using Xunit;
|
||||
|
||||
public class HttpRequestHeadersBuilderMiddlewareTests : ServerHostedMiddlewareTest
|
||||
{
|
||||
private readonly Mock<IAddHeadersToRequest> _addHeaders;
|
||||
private readonly HttpRequestMessage _downstreamRequest;
|
||||
private Response<DownstreamRoute> _downstreamRoute;
|
||||
|
||||
public HttpRequestHeadersBuilderMiddlewareTests()
|
||||
{
|
||||
_addHeaders = new Mock<IAddHeadersToRequest>();
|
||||
|
||||
_downstreamRequest = new HttpRequestMessage();
|
||||
ScopedRepository
|
||||
.Setup(sr => sr.Get<HttpRequestMessage>("DownstreamRequest"))
|
||||
.Returns(new OkResponse<HttpRequestMessage>(_downstreamRequest));
|
||||
|
||||
GivenTheTestServerIsConfigured();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_call_add_headers_to_request_correctly()
|
||||
{
|
||||
var downstreamRoute = new DownstreamRoute(new List<PlaceholderNameAndValue>(),
|
||||
new ReRouteBuilder()
|
||||
.WithDownstreamPathTemplate("any old string")
|
||||
.WithClaimsToHeaders(new List<ClaimToThing>
|
||||
{
|
||||
new ClaimToThing("UserId", "Subject", "", 0)
|
||||
})
|
||||
.WithUpstreamHttpMethod(new List<string> { "Get" })
|
||||
.Build());
|
||||
|
||||
this.Given(x => x.GivenTheDownStreamRouteIs(downstreamRoute))
|
||||
.And(x => x.GivenTheAddHeadersToDownstreamRequestReturnsOk())
|
||||
.When(x => x.WhenICallTheMiddleware())
|
||||
.Then(x => x.ThenTheAddHeadersToRequestIsCalledCorrectly())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
protected override void GivenTheTestServerServicesAreConfigured(IServiceCollection services)
|
||||
{
|
||||
services.AddSingleton<IOcelotLoggerFactory, AspDotNetLoggerFactory>();
|
||||
services.AddLogging();
|
||||
services.AddSingleton(_addHeaders.Object);
|
||||
services.AddSingleton(ScopedRepository.Object);
|
||||
}
|
||||
|
||||
protected override void GivenTheTestServerPipelineIsConfigured(IApplicationBuilder app)
|
||||
{
|
||||
app.UseHttpRequestHeadersBuilderMiddleware();
|
||||
}
|
||||
|
||||
private void GivenTheDownStreamRouteIs(DownstreamRoute downstreamRoute)
|
||||
{
|
||||
_downstreamRoute = new OkResponse<DownstreamRoute>(downstreamRoute);
|
||||
ScopedRepository
|
||||
.Setup(x => x.Get<DownstreamRoute>(It.IsAny<string>()))
|
||||
.Returns(_downstreamRoute);
|
||||
}
|
||||
|
||||
private void GivenTheAddHeadersToDownstreamRequestReturnsOk()
|
||||
{
|
||||
_addHeaders
|
||||
.Setup(x => x.SetHeadersOnDownstreamRequest(
|
||||
It.IsAny<List<ClaimToThing>>(),
|
||||
It.IsAny<IEnumerable<System.Security.Claims.Claim>>(),
|
||||
It.IsAny<HttpRequestMessage>()))
|
||||
.Returns(new OkResponse());
|
||||
}
|
||||
|
||||
private void ThenTheAddHeadersToRequestIsCalledCorrectly()
|
||||
{
|
||||
_addHeaders
|
||||
.Verify(x => x.SetHeadersOnDownstreamRequest(
|
||||
It.IsAny<List<ClaimToThing>>(),
|
||||
It.IsAny<IEnumerable<System.Security.Claims.Claim>>(),
|
||||
_downstreamRequest), Times.Once);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,235 +1,235 @@
|
||||
using Xunit;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using System.Net.Http;
|
||||
using Ocelot.Headers;
|
||||
using Ocelot.Configuration;
|
||||
using System.Collections.Generic;
|
||||
using Ocelot.Responses;
|
||||
using System.Linq;
|
||||
|
||||
namespace Ocelot.UnitTests.Headers
|
||||
{
|
||||
public class HttpResponseHeaderReplacerTests
|
||||
{
|
||||
private HttpResponseMessage _response;
|
||||
private HttpResponseHeaderReplacer _replacer;
|
||||
private List<HeaderFindAndReplace> _headerFindAndReplaces;
|
||||
private Response _result;
|
||||
private HttpRequestMessage _request;
|
||||
|
||||
public HttpResponseHeaderReplacerTests()
|
||||
{
|
||||
_replacer = new HttpResponseHeaderReplacer();
|
||||
}
|
||||
[Fact]
|
||||
public void should_replace_headers()
|
||||
{
|
||||
var response = new HttpResponseMessage();
|
||||
response.Headers.Add("test", "test");
|
||||
|
||||
var fAndRs = new List<HeaderFindAndReplace>();
|
||||
fAndRs.Add(new HeaderFindAndReplace("test", "test", "chiken", 0));
|
||||
|
||||
this.Given(x => GivenTheHttpResponse(response))
|
||||
.And(x => GivenTheFollowingHeaderReplacements(fAndRs))
|
||||
.When(x => WhenICallTheReplacer())
|
||||
.Then(x => ThenTheHeadersAreReplaced())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_not_replace_headers()
|
||||
{
|
||||
var response = new HttpResponseMessage();
|
||||
response.Headers.Add("test", "test");
|
||||
|
||||
var fAndRs = new List<HeaderFindAndReplace>();
|
||||
|
||||
this.Given(x => GivenTheHttpResponse(response))
|
||||
.And(x => GivenTheFollowingHeaderReplacements(fAndRs))
|
||||
.When(x => WhenICallTheReplacer())
|
||||
.Then(x => ThenTheHeadersAreNotReplaced())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_replace_downstream_base_url_with_ocelot_base_url()
|
||||
{
|
||||
var downstreamUrl = "http://downstream.com/";
|
||||
|
||||
var request = new HttpRequestMessage();
|
||||
request.RequestUri = new System.Uri(downstreamUrl);
|
||||
|
||||
var response = new HttpResponseMessage();
|
||||
response.Headers.Add("Location", downstreamUrl);
|
||||
|
||||
var fAndRs = new List<HeaderFindAndReplace>();
|
||||
fAndRs.Add(new HeaderFindAndReplace("Location", "{DownstreamBaseUrl}", "http://ocelot.com/", 0));
|
||||
|
||||
this.Given(x => GivenTheHttpResponse(response))
|
||||
.And(x => GivenTheRequestIs(request))
|
||||
.And(x => GivenTheFollowingHeaderReplacements(fAndRs))
|
||||
.When(x => WhenICallTheReplacer())
|
||||
.Then(x => ThenTheHeaderShouldBe("Location", "http://ocelot.com/"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_replace_downstream_base_url_with_ocelot_base_url_with_port()
|
||||
{
|
||||
var downstreamUrl = "http://downstream.com/";
|
||||
|
||||
var request = new HttpRequestMessage();
|
||||
request.RequestUri = new System.Uri(downstreamUrl);
|
||||
|
||||
var response = new HttpResponseMessage();
|
||||
response.Headers.Add("Location", downstreamUrl);
|
||||
|
||||
var fAndRs = new List<HeaderFindAndReplace>();
|
||||
fAndRs.Add(new HeaderFindAndReplace("Location", "{DownstreamBaseUrl}", "http://ocelot.com:123/", 0));
|
||||
|
||||
this.Given(x => GivenTheHttpResponse(response))
|
||||
.And(x => GivenTheRequestIs(request))
|
||||
.And(x => GivenTheFollowingHeaderReplacements(fAndRs))
|
||||
.When(x => WhenICallTheReplacer())
|
||||
.Then(x => ThenTheHeaderShouldBe("Location", "http://ocelot.com:123/"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
|
||||
[Fact]
|
||||
public void should_replace_downstream_base_url_with_ocelot_base_url_and_path()
|
||||
{
|
||||
var downstreamUrl = "http://downstream.com/test/product";
|
||||
|
||||
var request = new HttpRequestMessage();
|
||||
request.RequestUri = new System.Uri(downstreamUrl);
|
||||
|
||||
var response = new HttpResponseMessage();
|
||||
response.Headers.Add("Location", downstreamUrl);
|
||||
|
||||
var fAndRs = new List<HeaderFindAndReplace>();
|
||||
fAndRs.Add(new HeaderFindAndReplace("Location", "{DownstreamBaseUrl}", "http://ocelot.com/", 0));
|
||||
|
||||
this.Given(x => GivenTheHttpResponse(response))
|
||||
.And(x => GivenTheRequestIs(request))
|
||||
.And(x => GivenTheFollowingHeaderReplacements(fAndRs))
|
||||
.When(x => WhenICallTheReplacer())
|
||||
.Then(x => ThenTheHeaderShouldBe("Location", "http://ocelot.com/test/product"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_replace_downstream_base_url_with_ocelot_base_url_with_path_and_port()
|
||||
{
|
||||
var downstreamUrl = "http://downstream.com/test/product";
|
||||
|
||||
var request = new HttpRequestMessage();
|
||||
request.RequestUri = new System.Uri(downstreamUrl);
|
||||
|
||||
var response = new HttpResponseMessage();
|
||||
response.Headers.Add("Location", downstreamUrl);
|
||||
|
||||
var fAndRs = new List<HeaderFindAndReplace>();
|
||||
fAndRs.Add(new HeaderFindAndReplace("Location", "{DownstreamBaseUrl}", "http://ocelot.com:123/", 0));
|
||||
|
||||
this.Given(x => GivenTheHttpResponse(response))
|
||||
.And(x => GivenTheRequestIs(request))
|
||||
.And(x => GivenTheFollowingHeaderReplacements(fAndRs))
|
||||
.When(x => WhenICallTheReplacer())
|
||||
.Then(x => ThenTheHeaderShouldBe("Location", "http://ocelot.com:123/test/product"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_replace_downstream_base_url_and_port_with_ocelot_base_url()
|
||||
{
|
||||
var downstreamUrl = "http://downstream.com:123/test/product";
|
||||
|
||||
var request = new HttpRequestMessage();
|
||||
request.RequestUri = new System.Uri(downstreamUrl);
|
||||
|
||||
var response = new HttpResponseMessage();
|
||||
response.Headers.Add("Location", downstreamUrl);
|
||||
|
||||
var fAndRs = new List<HeaderFindAndReplace>();
|
||||
fAndRs.Add(new HeaderFindAndReplace("Location", "{DownstreamBaseUrl}", "http://ocelot.com/", 0));
|
||||
|
||||
this.Given(x => GivenTheHttpResponse(response))
|
||||
.And(x => GivenTheRequestIs(request))
|
||||
.And(x => GivenTheFollowingHeaderReplacements(fAndRs))
|
||||
.When(x => WhenICallTheReplacer())
|
||||
.Then(x => ThenTheHeaderShouldBe("Location", "http://ocelot.com/test/product"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_replace_downstream_base_url_and_port_with_ocelot_base_url_and_port()
|
||||
{
|
||||
var downstreamUrl = "http://downstream.com:123/test/product";
|
||||
|
||||
var request = new HttpRequestMessage();
|
||||
request.RequestUri = new System.Uri(downstreamUrl);
|
||||
|
||||
var response = new HttpResponseMessage();
|
||||
response.Headers.Add("Location", downstreamUrl);
|
||||
|
||||
var fAndRs = new List<HeaderFindAndReplace>();
|
||||
fAndRs.Add(new HeaderFindAndReplace("Location", "{DownstreamBaseUrl}", "http://ocelot.com:321/", 0));
|
||||
|
||||
this.Given(x => GivenTheHttpResponse(response))
|
||||
.And(x => GivenTheRequestIs(request))
|
||||
.And(x => GivenTheFollowingHeaderReplacements(fAndRs))
|
||||
.When(x => WhenICallTheReplacer())
|
||||
.Then(x => ThenTheHeaderShouldBe("Location", "http://ocelot.com:321/test/product"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenTheRequestIs(HttpRequestMessage request)
|
||||
{
|
||||
_request = request;
|
||||
}
|
||||
|
||||
private void ThenTheHeadersAreNotReplaced()
|
||||
{
|
||||
_result.ShouldBeOfType<OkResponse>();
|
||||
foreach (var f in _headerFindAndReplaces)
|
||||
{
|
||||
_response.Headers.TryGetValues(f.Key, out var values);
|
||||
values.ToList()[f.Index].ShouldBe("test");
|
||||
}
|
||||
}
|
||||
|
||||
private void GivenTheFollowingHeaderReplacements(List<HeaderFindAndReplace> fAndRs)
|
||||
{
|
||||
_headerFindAndReplaces = fAndRs;
|
||||
}
|
||||
|
||||
private void GivenTheHttpResponse(HttpResponseMessage response)
|
||||
{
|
||||
_response = response;
|
||||
}
|
||||
|
||||
private void WhenICallTheReplacer()
|
||||
{
|
||||
_result = _replacer.Replace(_response, _headerFindAndReplaces, _request);
|
||||
}
|
||||
|
||||
private void ThenTheHeaderShouldBe(string key, string value)
|
||||
{
|
||||
var test = _response.Headers.GetValues(key);
|
||||
test.First().ShouldBe(value);
|
||||
}
|
||||
|
||||
private void ThenTheHeadersAreReplaced()
|
||||
{
|
||||
_result.ShouldBeOfType<OkResponse>();
|
||||
foreach (var f in _headerFindAndReplaces)
|
||||
{
|
||||
_response.Headers.TryGetValues(f.Key, out var values);
|
||||
values.ToList()[f.Index].ShouldBe(f.Replace);
|
||||
}
|
||||
}
|
||||
}
|
||||
using Xunit;
|
||||
using Shouldly;
|
||||
using TestStack.BDDfy;
|
||||
using System.Net.Http;
|
||||
using Ocelot.Headers;
|
||||
using Ocelot.Configuration;
|
||||
using System.Collections.Generic;
|
||||
using Ocelot.Responses;
|
||||
using System.Linq;
|
||||
|
||||
namespace Ocelot.UnitTests.Headers
|
||||
{
|
||||
public class HttpResponseHeaderReplacerTests
|
||||
{
|
||||
private HttpResponseMessage _response;
|
||||
private HttpResponseHeaderReplacer _replacer;
|
||||
private List<HeaderFindAndReplace> _headerFindAndReplaces;
|
||||
private Response _result;
|
||||
private HttpRequestMessage _request;
|
||||
|
||||
public HttpResponseHeaderReplacerTests()
|
||||
{
|
||||
_replacer = new HttpResponseHeaderReplacer();
|
||||
}
|
||||
[Fact]
|
||||
public void should_replace_headers()
|
||||
{
|
||||
var response = new HttpResponseMessage();
|
||||
response.Headers.Add("test", "test");
|
||||
|
||||
var fAndRs = new List<HeaderFindAndReplace>();
|
||||
fAndRs.Add(new HeaderFindAndReplace("test", "test", "chiken", 0));
|
||||
|
||||
this.Given(x => GivenTheHttpResponse(response))
|
||||
.And(x => GivenTheFollowingHeaderReplacements(fAndRs))
|
||||
.When(x => WhenICallTheReplacer())
|
||||
.Then(x => ThenTheHeadersAreReplaced())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_not_replace_headers()
|
||||
{
|
||||
var response = new HttpResponseMessage();
|
||||
response.Headers.Add("test", "test");
|
||||
|
||||
var fAndRs = new List<HeaderFindAndReplace>();
|
||||
|
||||
this.Given(x => GivenTheHttpResponse(response))
|
||||
.And(x => GivenTheFollowingHeaderReplacements(fAndRs))
|
||||
.When(x => WhenICallTheReplacer())
|
||||
.Then(x => ThenTheHeadersAreNotReplaced())
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_replace_downstream_base_url_with_ocelot_base_url()
|
||||
{
|
||||
var downstreamUrl = "http://downstream.com/";
|
||||
|
||||
var request = new HttpRequestMessage();
|
||||
request.RequestUri = new System.Uri(downstreamUrl);
|
||||
|
||||
var response = new HttpResponseMessage();
|
||||
response.Headers.Add("Location", downstreamUrl);
|
||||
|
||||
var fAndRs = new List<HeaderFindAndReplace>();
|
||||
fAndRs.Add(new HeaderFindAndReplace("Location", "{DownstreamBaseUrl}", "http://ocelot.com/", 0));
|
||||
|
||||
this.Given(x => GivenTheHttpResponse(response))
|
||||
.And(x => GivenTheRequestIs(request))
|
||||
.And(x => GivenTheFollowingHeaderReplacements(fAndRs))
|
||||
.When(x => WhenICallTheReplacer())
|
||||
.Then(x => ThenTheHeaderShouldBe("Location", "http://ocelot.com/"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_replace_downstream_base_url_with_ocelot_base_url_with_port()
|
||||
{
|
||||
var downstreamUrl = "http://downstream.com/";
|
||||
|
||||
var request = new HttpRequestMessage();
|
||||
request.RequestUri = new System.Uri(downstreamUrl);
|
||||
|
||||
var response = new HttpResponseMessage();
|
||||
response.Headers.Add("Location", downstreamUrl);
|
||||
|
||||
var fAndRs = new List<HeaderFindAndReplace>();
|
||||
fAndRs.Add(new HeaderFindAndReplace("Location", "{DownstreamBaseUrl}", "http://ocelot.com:123/", 0));
|
||||
|
||||
this.Given(x => GivenTheHttpResponse(response))
|
||||
.And(x => GivenTheRequestIs(request))
|
||||
.And(x => GivenTheFollowingHeaderReplacements(fAndRs))
|
||||
.When(x => WhenICallTheReplacer())
|
||||
.Then(x => ThenTheHeaderShouldBe("Location", "http://ocelot.com:123/"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
|
||||
[Fact]
|
||||
public void should_replace_downstream_base_url_with_ocelot_base_url_and_path()
|
||||
{
|
||||
var downstreamUrl = "http://downstream.com/test/product";
|
||||
|
||||
var request = new HttpRequestMessage();
|
||||
request.RequestUri = new System.Uri(downstreamUrl);
|
||||
|
||||
var response = new HttpResponseMessage();
|
||||
response.Headers.Add("Location", downstreamUrl);
|
||||
|
||||
var fAndRs = new List<HeaderFindAndReplace>();
|
||||
fAndRs.Add(new HeaderFindAndReplace("Location", "{DownstreamBaseUrl}", "http://ocelot.com/", 0));
|
||||
|
||||
this.Given(x => GivenTheHttpResponse(response))
|
||||
.And(x => GivenTheRequestIs(request))
|
||||
.And(x => GivenTheFollowingHeaderReplacements(fAndRs))
|
||||
.When(x => WhenICallTheReplacer())
|
||||
.Then(x => ThenTheHeaderShouldBe("Location", "http://ocelot.com/test/product"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_replace_downstream_base_url_with_ocelot_base_url_with_path_and_port()
|
||||
{
|
||||
var downstreamUrl = "http://downstream.com/test/product";
|
||||
|
||||
var request = new HttpRequestMessage();
|
||||
request.RequestUri = new System.Uri(downstreamUrl);
|
||||
|
||||
var response = new HttpResponseMessage();
|
||||
response.Headers.Add("Location", downstreamUrl);
|
||||
|
||||
var fAndRs = new List<HeaderFindAndReplace>();
|
||||
fAndRs.Add(new HeaderFindAndReplace("Location", "{DownstreamBaseUrl}", "http://ocelot.com:123/", 0));
|
||||
|
||||
this.Given(x => GivenTheHttpResponse(response))
|
||||
.And(x => GivenTheRequestIs(request))
|
||||
.And(x => GivenTheFollowingHeaderReplacements(fAndRs))
|
||||
.When(x => WhenICallTheReplacer())
|
||||
.Then(x => ThenTheHeaderShouldBe("Location", "http://ocelot.com:123/test/product"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_replace_downstream_base_url_and_port_with_ocelot_base_url()
|
||||
{
|
||||
var downstreamUrl = "http://downstream.com:123/test/product";
|
||||
|
||||
var request = new HttpRequestMessage();
|
||||
request.RequestUri = new System.Uri(downstreamUrl);
|
||||
|
||||
var response = new HttpResponseMessage();
|
||||
response.Headers.Add("Location", downstreamUrl);
|
||||
|
||||
var fAndRs = new List<HeaderFindAndReplace>();
|
||||
fAndRs.Add(new HeaderFindAndReplace("Location", "{DownstreamBaseUrl}", "http://ocelot.com/", 0));
|
||||
|
||||
this.Given(x => GivenTheHttpResponse(response))
|
||||
.And(x => GivenTheRequestIs(request))
|
||||
.And(x => GivenTheFollowingHeaderReplacements(fAndRs))
|
||||
.When(x => WhenICallTheReplacer())
|
||||
.Then(x => ThenTheHeaderShouldBe("Location", "http://ocelot.com/test/product"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void should_replace_downstream_base_url_and_port_with_ocelot_base_url_and_port()
|
||||
{
|
||||
var downstreamUrl = "http://downstream.com:123/test/product";
|
||||
|
||||
var request = new HttpRequestMessage();
|
||||
request.RequestUri = new System.Uri(downstreamUrl);
|
||||
|
||||
var response = new HttpResponseMessage();
|
||||
response.Headers.Add("Location", downstreamUrl);
|
||||
|
||||
var fAndRs = new List<HeaderFindAndReplace>();
|
||||
fAndRs.Add(new HeaderFindAndReplace("Location", "{DownstreamBaseUrl}", "http://ocelot.com:321/", 0));
|
||||
|
||||
this.Given(x => GivenTheHttpResponse(response))
|
||||
.And(x => GivenTheRequestIs(request))
|
||||
.And(x => GivenTheFollowingHeaderReplacements(fAndRs))
|
||||
.When(x => WhenICallTheReplacer())
|
||||
.Then(x => ThenTheHeaderShouldBe("Location", "http://ocelot.com:321/test/product"))
|
||||
.BDDfy();
|
||||
}
|
||||
|
||||
private void GivenTheRequestIs(HttpRequestMessage request)
|
||||
{
|
||||
_request = request;
|
||||
}
|
||||
|
||||
private void ThenTheHeadersAreNotReplaced()
|
||||
{
|
||||
_result.ShouldBeOfType<OkResponse>();
|
||||
foreach (var f in _headerFindAndReplaces)
|
||||
{
|
||||
_response.Headers.TryGetValues(f.Key, out var values);
|
||||
values.ToList()[f.Index].ShouldBe("test");
|
||||
}
|
||||
}
|
||||
|
||||
private void GivenTheFollowingHeaderReplacements(List<HeaderFindAndReplace> fAndRs)
|
||||
{
|
||||
_headerFindAndReplaces = fAndRs;
|
||||
}
|
||||
|
||||
private void GivenTheHttpResponse(HttpResponseMessage response)
|
||||
{
|
||||
_response = response;
|
||||
}
|
||||
|
||||
private void WhenICallTheReplacer()
|
||||
{
|
||||
_result = _replacer.Replace(_response, _headerFindAndReplaces, _request);
|
||||
}
|
||||
|
||||
private void ThenTheHeaderShouldBe(string key, string value)
|
||||
{
|
||||
var test = _response.Headers.GetValues(key);
|
||||
test.First().ShouldBe(value);
|
||||
}
|
||||
|
||||
private void ThenTheHeadersAreReplaced()
|
||||
{
|
||||
_result.ShouldBeOfType<OkResponse>();
|
||||
foreach (var f in _headerFindAndReplaces)
|
||||
{
|
||||
_response.Headers.TryGetValues(f.Key, out var values);
|
||||
values.ToList()[f.Index].ShouldBe(f.Replace);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user