Feature/fix admin api caching wrong re routes (#421)

* #383 added failing test for this issue

* #383 identified issue was with cached load balancer for a given upstream path template based on the key we use, have modified this to include more data, I guess this might be an issue again for other things so I will have a think about it

* #383 fixed failing tests after key change

* Seems to be an issue with coveralls new package not being on nuget...try same version as their nuget package

* bash the old manual tests json back in
This commit is contained in:
Tom Pallister 2018-06-21 22:45:24 +01:00 committed by GitHub
parent fffc4c8d3c
commit 3eb9b4da89
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 1903 additions and 1731 deletions

1028
build.cake

File diff suppressed because it is too large Load Diff

View File

@ -252,7 +252,7 @@ namespace Ocelot.Configuration.Creator
return $"{nameof(CookieStickySessions)}:{fileReRoute.LoadBalancerOptions.Key}"; return $"{nameof(CookieStickySessions)}:{fileReRoute.LoadBalancerOptions.Key}";
} }
return $"{fileReRoute.UpstreamPathTemplate}|{string.Join(",", fileReRoute.UpstreamHttpMethod)}"; return $"{fileReRoute.UpstreamPathTemplate}|{string.Join(",", fileReRoute.UpstreamHttpMethod)}|{string.Join(",", fileReRoute.DownstreamHostAndPorts.Select(x => $"{x.Host}:{x.Port}"))}";
} }
} }
} }

View File

@ -1,11 +1,11 @@
using System.Threading.Tasks; using System.Threading.Tasks;
using Ocelot.Configuration.File; using Ocelot.Configuration.File;
using Ocelot.Responses; using Ocelot.Responses;
namespace Ocelot.Configuration.Setter namespace Ocelot.Configuration.Setter
{ {
public interface IFileConfigurationSetter public interface IFileConfigurationSetter
{ {
Task<Response> Set(FileConfiguration config); Task<Response> Set(FileConfiguration config);
} }
} }

View File

@ -1,55 +1,55 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using Ocelot.Configuration; using Ocelot.Configuration;
using Ocelot.Infrastructure; using Ocelot.Infrastructure;
using Ocelot.Infrastructure.Extensions; using Ocelot.Infrastructure.Extensions;
using Ocelot.Middleware; using Ocelot.Middleware;
using Ocelot.Middleware.Multiplexer; using Ocelot.Middleware.Multiplexer;
using Ocelot.Request.Middleware; using Ocelot.Request.Middleware;
using Ocelot.Responses; using Ocelot.Responses;
namespace Ocelot.Headers namespace Ocelot.Headers
{ {
public class HttpResponseHeaderReplacer : IHttpResponseHeaderReplacer public class HttpResponseHeaderReplacer : IHttpResponseHeaderReplacer
{ {
private readonly IPlaceholders _placeholders; private readonly IPlaceholders _placeholders;
public HttpResponseHeaderReplacer(IPlaceholders placeholders) public HttpResponseHeaderReplacer(IPlaceholders placeholders)
{ {
_placeholders = placeholders; _placeholders = placeholders;
} }
public Response Replace(DownstreamResponse response, List<HeaderFindAndReplace> fAndRs, DownstreamRequest request) public Response Replace(DownstreamResponse response, List<HeaderFindAndReplace> fAndRs, DownstreamRequest request)
{ {
foreach (var f in fAndRs) foreach (var f in fAndRs)
{ {
var dict = response.Headers.ToDictionary(x => x.Key); var dict = response.Headers.ToDictionary(x => x.Key);
//if the response headers contain a matching find and replace //if the response headers contain a matching find and replace
if(dict.TryGetValue(f.Key, out var values)) if(dict.TryGetValue(f.Key, out var values))
{ {
//check to see if it is a placeholder in the find... //check to see if it is a placeholder in the find...
var placeholderValue = _placeholders.Get(f.Find, request); var placeholderValue = _placeholders.Get(f.Find, request);
if(!placeholderValue.IsError) if(!placeholderValue.IsError)
{ {
//if it is we need to get the value of the placeholder //if it is we need to get the value of the placeholder
var replaced = values.Values.ToList()[f.Index].Replace(placeholderValue.Data, f.Replace.LastCharAsForwardSlash()); var replaced = values.Values.ToList()[f.Index].Replace(placeholderValue.Data, f.Replace.LastCharAsForwardSlash());
response.Headers.Remove(response.Headers.First(item => item.Key == f.Key)); response.Headers.Remove(response.Headers.First(item => item.Key == f.Key));
response.Headers.Add(new Header(f.Key, new List<string> { replaced })); response.Headers.Add(new Header(f.Key, new List<string> { replaced }));
} }
else else
{ {
var replaced = values.Values.ToList()[f.Index].Replace(f.Find, f.Replace); var replaced = values.Values.ToList()[f.Index].Replace(f.Find, f.Replace);
response.Headers.Remove(response.Headers.First(item => item.Key == f.Key)); response.Headers.Remove(response.Headers.First(item => item.Key == f.Key));
response.Headers.Add(new Header(f.Key, new List<string> { replaced })); response.Headers.Add(new Header(f.Key, new List<string> { replaced }));
} }
} }
} }
return new OkResponse(); return new OkResponse();
} }
} }
} }

View File

@ -1,48 +1,48 @@
using System.Threading.Tasks; using System.Threading.Tasks;
using Ocelot.Logging; using Ocelot.Logging;
using Ocelot.Middleware; using Ocelot.Middleware;
namespace Ocelot.Headers.Middleware namespace Ocelot.Headers.Middleware
{ {
public class HttpHeadersTransformationMiddleware : OcelotMiddleware public class HttpHeadersTransformationMiddleware : OcelotMiddleware
{ {
private readonly OcelotRequestDelegate _next; private readonly OcelotRequestDelegate _next;
private readonly IHttpContextRequestHeaderReplacer _preReplacer; private readonly IHttpContextRequestHeaderReplacer _preReplacer;
private readonly IHttpResponseHeaderReplacer _postReplacer; private readonly IHttpResponseHeaderReplacer _postReplacer;
private readonly IAddHeadersToResponse _addHeadersToResponse; private readonly IAddHeadersToResponse _addHeadersToResponse;
private readonly IAddHeadersToRequest _addHeadersToRequest; private readonly IAddHeadersToRequest _addHeadersToRequest;
public HttpHeadersTransformationMiddleware(OcelotRequestDelegate next, public HttpHeadersTransformationMiddleware(OcelotRequestDelegate next,
IOcelotLoggerFactory loggerFactory, IOcelotLoggerFactory loggerFactory,
IHttpContextRequestHeaderReplacer preReplacer, IHttpContextRequestHeaderReplacer preReplacer,
IHttpResponseHeaderReplacer postReplacer, IHttpResponseHeaderReplacer postReplacer,
IAddHeadersToResponse addHeadersToResponse, IAddHeadersToResponse addHeadersToResponse,
IAddHeadersToRequest addHeadersToRequest) IAddHeadersToRequest addHeadersToRequest)
:base(loggerFactory.CreateLogger<HttpHeadersTransformationMiddleware>()) :base(loggerFactory.CreateLogger<HttpHeadersTransformationMiddleware>())
{ {
_addHeadersToResponse = addHeadersToResponse; _addHeadersToResponse = addHeadersToResponse;
_addHeadersToRequest = addHeadersToRequest; _addHeadersToRequest = addHeadersToRequest;
_next = next; _next = next;
_postReplacer = postReplacer; _postReplacer = postReplacer;
_preReplacer = preReplacer; _preReplacer = preReplacer;
} }
public async Task Invoke(DownstreamContext context) public async Task Invoke(DownstreamContext context)
{ {
var preFAndRs = context.DownstreamReRoute.UpstreamHeadersFindAndReplace; var preFAndRs = context.DownstreamReRoute.UpstreamHeadersFindAndReplace;
//todo - this should be on httprequestmessage not httpcontext? //todo - this should be on httprequestmessage not httpcontext?
_preReplacer.Replace(context.HttpContext, preFAndRs); _preReplacer.Replace(context.HttpContext, preFAndRs);
_addHeadersToRequest.SetHeadersOnDownstreamRequest(context.DownstreamReRoute.AddHeadersToUpstream, context.HttpContext); _addHeadersToRequest.SetHeadersOnDownstreamRequest(context.DownstreamReRoute.AddHeadersToUpstream, context.HttpContext);
await _next.Invoke(context); await _next.Invoke(context);
var postFAndRs = context.DownstreamReRoute.DownstreamHeadersFindAndReplace; var postFAndRs = context.DownstreamReRoute.DownstreamHeadersFindAndReplace;
_postReplacer.Replace(context.DownstreamResponse, postFAndRs, context.DownstreamRequest); _postReplacer.Replace(context.DownstreamResponse, postFAndRs, context.DownstreamRequest);
_addHeadersToResponse.Add(context.DownstreamReRoute.AddHeadersToDownstream, context.DownstreamResponse); _addHeadersToResponse.Add(context.DownstreamReRoute.AddHeadersToDownstream, context.DownstreamResponse);
} }
} }
} }

View File

@ -1,365 +1,412 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Net; using System.Net;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Ocelot.Configuration.File; using Ocelot.Configuration.File;
using TestStack.BDDfy; using TestStack.BDDfy;
using Xunit; using Xunit;
namespace Ocelot.AcceptanceTests namespace Ocelot.AcceptanceTests
{ {
public class HeaderTests : IDisposable public class HeaderTests : IDisposable
{ {
private IWebHost _builder; private IWebHost _builder;
private int _count; private int _count;
private readonly Steps _steps; private readonly Steps _steps;
public HeaderTests() public HeaderTests()
{ {
_steps = new Steps(); _steps = new Steps();
} }
[Fact] [Fact]
public void should_transform_upstream_header() public void should_transform_upstream_header()
{ {
var configuration = new FileConfiguration var configuration = new FileConfiguration
{ {
ReRoutes = new List<FileReRoute> ReRoutes = new List<FileReRoute>
{ {
new FileReRoute new FileReRoute
{ {
DownstreamPathTemplate = "/", DownstreamPathTemplate = "/",
DownstreamScheme = "http", DownstreamScheme = "http",
DownstreamHostAndPorts = new List<FileHostAndPort> DownstreamHostAndPorts = new List<FileHostAndPort>
{ {
new FileHostAndPort new FileHostAndPort
{ {
Host = "localhost", Host = "localhost",
Port = 51871, Port = 51871,
} }
}, },
UpstreamPathTemplate = "/", UpstreamPathTemplate = "/",
UpstreamHttpMethod = new List<string> { "Get" }, UpstreamHttpMethod = new List<string> { "Get" },
UpstreamHeaderTransform = new Dictionary<string,string> UpstreamHeaderTransform = new Dictionary<string,string>
{ {
{"Laz", "D, GP"} {"Laz", "D, GP"}
} }
} }
} }
}; };
this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:51871", "/", 200, "Laz")) this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:51871", "/", 200, "Laz"))
.And(x => _steps.GivenThereIsAConfiguration(configuration)) .And(x => _steps.GivenThereIsAConfiguration(configuration))
.And(x => _steps.GivenOcelotIsRunning()) .And(x => _steps.GivenOcelotIsRunning())
.And(x => _steps.GivenIAddAHeader("Laz", "D")) .And(x => _steps.GivenIAddAHeader("Laz", "D"))
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/")) .When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK)) .Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
.And(x => _steps.ThenTheResponseBodyShouldBe("GP")) .And(x => _steps.ThenTheResponseBodyShouldBe("GP"))
.BDDfy(); .BDDfy();
} }
[Fact] [Fact]
public void should_transform_downstream_header() public void should_transform_downstream_header()
{ {
var configuration = new FileConfiguration var configuration = new FileConfiguration
{ {
ReRoutes = new List<FileReRoute> ReRoutes = new List<FileReRoute>
{ {
new FileReRoute new FileReRoute
{ {
DownstreamPathTemplate = "/", DownstreamPathTemplate = "/",
DownstreamScheme = "http", DownstreamScheme = "http",
DownstreamHostAndPorts = new List<FileHostAndPort> DownstreamHostAndPorts = new List<FileHostAndPort>
{ {
new FileHostAndPort new FileHostAndPort
{ {
Host = "localhost", Host = "localhost",
Port = 51871, Port = 51871,
} }
}, },
UpstreamPathTemplate = "/", UpstreamPathTemplate = "/",
UpstreamHttpMethod = new List<string> { "Get" }, UpstreamHttpMethod = new List<string> { "Get" },
DownstreamHeaderTransform = new Dictionary<string,string> DownstreamHeaderTransform = new Dictionary<string,string>
{ {
{"Location", "http://www.bbc.co.uk/, http://ocelot.com/"} {"Location", "http://www.bbc.co.uk/, http://ocelot.com/"}
} }
} }
} }
}; };
this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:51871", "/", 200, "Location", "http://www.bbc.co.uk/")) this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:51871", "/", 200, "Location", "http://www.bbc.co.uk/"))
.And(x => _steps.GivenThereIsAConfiguration(configuration)) .And(x => _steps.GivenThereIsAConfiguration(configuration))
.And(x => _steps.GivenOcelotIsRunning()) .And(x => _steps.GivenOcelotIsRunning())
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/")) .When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK)) .Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
.And(x => _steps.ThenTheResponseHeaderIs("Location", "http://ocelot.com/")) .And(x => _steps.ThenTheResponseHeaderIs("Location", "http://ocelot.com/"))
.BDDfy(); .BDDfy();
} }
[Fact] [Fact]
public void should_fix_issue_190() public void should_fix_issue_190()
{ {
var configuration = new FileConfiguration var configuration = new FileConfiguration
{ {
ReRoutes = new List<FileReRoute> ReRoutes = new List<FileReRoute>
{ {
new FileReRoute new FileReRoute
{ {
DownstreamPathTemplate = "/", DownstreamPathTemplate = "/",
DownstreamScheme = "http", DownstreamScheme = "http",
DownstreamHostAndPorts = new List<FileHostAndPort> DownstreamHostAndPorts = new List<FileHostAndPort>
{ {
new FileHostAndPort new FileHostAndPort
{ {
Host = "localhost", Host = "localhost",
Port = 6773, Port = 6773,
} }
}, },
UpstreamPathTemplate = "/", UpstreamPathTemplate = "/",
UpstreamHttpMethod = new List<string> { "Get" }, UpstreamHttpMethod = new List<string> { "Get" },
DownstreamHeaderTransform = new Dictionary<string,string> DownstreamHeaderTransform = new Dictionary<string,string>
{ {
{"Location", "http://localhost:6773, {BaseUrl}"} {"Location", "http://localhost:6773, {BaseUrl}"}
}, },
HttpHandlerOptions = new FileHttpHandlerOptions HttpHandlerOptions = new FileHttpHandlerOptions
{ {
AllowAutoRedirect = false AllowAutoRedirect = false
} }
} }
} }
}; };
this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:6773", "/", 302, "Location", "http://localhost:6773/pay/Receive")) this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:6773", "/", 302, "Location", "http://localhost:6773/pay/Receive"))
.And(x => _steps.GivenThereIsAConfiguration(configuration)) .And(x => _steps.GivenThereIsAConfiguration(configuration))
.And(x => _steps.GivenOcelotIsRunning()) .And(x => _steps.GivenOcelotIsRunning())
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/")) .When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.Redirect)) .Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.Redirect))
.And(x => _steps.ThenTheResponseHeaderIs("Location", "http://localhost:5000/pay/Receive")) .And(x => _steps.ThenTheResponseHeaderIs("Location", "http://localhost:5000/pay/Receive"))
.BDDfy(); .BDDfy();
} }
[Fact] [Fact]
public void should_fix_issue_205() public void should_fix_issue_205()
{ {
var configuration = new FileConfiguration var configuration = new FileConfiguration
{ {
ReRoutes = new List<FileReRoute> ReRoutes = new List<FileReRoute>
{ {
new FileReRoute new FileReRoute
{ {
DownstreamPathTemplate = "/", DownstreamPathTemplate = "/",
DownstreamScheme = "http", DownstreamScheme = "http",
DownstreamHostAndPorts = new List<FileHostAndPort> DownstreamHostAndPorts = new List<FileHostAndPort>
{ {
new FileHostAndPort new FileHostAndPort
{ {
Host = "localhost", Host = "localhost",
Port = 6773, Port = 6773,
} }
}, },
UpstreamPathTemplate = "/", UpstreamPathTemplate = "/",
UpstreamHttpMethod = new List<string> { "Get" }, UpstreamHttpMethod = new List<string> { "Get" },
DownstreamHeaderTransform = new Dictionary<string,string> DownstreamHeaderTransform = new Dictionary<string,string>
{ {
{"Location", "{DownstreamBaseUrl}, {BaseUrl}"} {"Location", "{DownstreamBaseUrl}, {BaseUrl}"}
}, },
HttpHandlerOptions = new FileHttpHandlerOptions HttpHandlerOptions = new FileHttpHandlerOptions
{ {
AllowAutoRedirect = false AllowAutoRedirect = false
} }
} }
} }
}; };
this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:6773", "/", 302, "Location", "http://localhost:6773/pay/Receive")) this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:6773", "/", 302, "Location", "http://localhost:6773/pay/Receive"))
.And(x => _steps.GivenThereIsAConfiguration(configuration)) .And(x => _steps.GivenThereIsAConfiguration(configuration))
.And(x => _steps.GivenOcelotIsRunning()) .And(x => _steps.GivenOcelotIsRunning())
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/")) .When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.Redirect)) .Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.Redirect))
.And(x => _steps.ThenTheResponseHeaderIs("Location", "http://localhost:5000/pay/Receive")) .And(x => _steps.ThenTheResponseHeaderIs("Location", "http://localhost:5000/pay/Receive"))
.BDDfy(); .BDDfy();
} }
[Fact] [Fact]
public void request_should_reuse_cookies_with_cookie_container() public void should_fix_issue_417()
{ {
var configuration = new FileConfiguration var configuration = new FileConfiguration
{ {
ReRoutes = new List<FileReRoute> ReRoutes = new List<FileReRoute>
{ {
new FileReRoute new FileReRoute
{ {
DownstreamPathTemplate = "/sso/{everything}", DownstreamPathTemplate = "/",
DownstreamScheme = "http", DownstreamScheme = "http",
DownstreamHostAndPorts = new List<FileHostAndPort> DownstreamHostAndPorts = new List<FileHostAndPort>
{ {
new FileHostAndPort new FileHostAndPort
{ {
Host = "localhost", Host = "localhost",
Port = 6774, Port = 6773,
} }
}, },
UpstreamPathTemplate = "/sso/{everything}", UpstreamPathTemplate = "/",
UpstreamHttpMethod = new List<string> { "Get", "Post", "Options" }, UpstreamHttpMethod = new List<string> { "Get" },
HttpHandlerOptions = new FileHttpHandlerOptions DownstreamHeaderTransform = new Dictionary<string,string>
{ {
UseCookieContainer = true {"Location", "{DownstreamBaseUrl}, {BaseUrl}"}
} },
} HttpHandlerOptions = new FileHttpHandlerOptions
} {
}; AllowAutoRedirect = false
}
this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:6774", "/sso/test", 200)) }
.And(x => _steps.GivenThereIsAConfiguration(configuration)) },
.And(x => _steps.GivenOcelotIsRunning()) GlobalConfiguration = new FileGlobalConfiguration
.And(x => _steps.WhenIGetUrlOnTheApiGateway("/sso/test")) {
.And(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK)) BaseUrl = "http://anotherapp.azurewebsites.net"
.And(x => _steps.ThenTheResponseHeaderIs("Set-Cookie", "test=0; path=/")) }
.And(x => _steps.GivenIAddCookieToMyRequest("test=1; path=/")) };
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/sso/test"))
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK)) this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:6773", "/", 302, "Location", "http://localhost:6773/pay/Receive"))
.BDDfy(); .And(x => _steps.GivenThereIsAConfiguration(configuration))
} .And(x => _steps.GivenOcelotIsRunning())
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
[Fact] .Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.Redirect))
public void request_should_have_own_cookies_no_cookie_container() .And(x => _steps.ThenTheResponseHeaderIs("Location", "http://anotherapp.azurewebsites.net/pay/Receive"))
{ .BDDfy();
var configuration = new FileConfiguration }
{
ReRoutes = new List<FileReRoute> [Fact]
{ public void request_should_reuse_cookies_with_cookie_container()
new FileReRoute {
{ var configuration = new FileConfiguration
DownstreamPathTemplate = "/sso/{everything}", {
DownstreamScheme = "http", ReRoutes = new List<FileReRoute>
DownstreamHostAndPorts = new List<FileHostAndPort> {
{ new FileReRoute
new FileHostAndPort {
{ DownstreamPathTemplate = "/sso/{everything}",
Host = "localhost", DownstreamScheme = "http",
Port = 6775, DownstreamHostAndPorts = new List<FileHostAndPort>
} {
}, new FileHostAndPort
UpstreamPathTemplate = "/sso/{everything}", {
UpstreamHttpMethod = new List<string> { "Get", "Post", "Options" }, Host = "localhost",
HttpHandlerOptions = new FileHttpHandlerOptions Port = 6774,
{ }
UseCookieContainer = false },
} UpstreamPathTemplate = "/sso/{everything}",
} UpstreamHttpMethod = new List<string> { "Get", "Post", "Options" },
} HttpHandlerOptions = new FileHttpHandlerOptions
}; {
UseCookieContainer = true
this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:6775", "/sso/test", 200)) }
.And(x => _steps.GivenThereIsAConfiguration(configuration)) }
.And(x => _steps.GivenOcelotIsRunning()) }
.And(x => _steps.WhenIGetUrlOnTheApiGateway("/sso/test")) };
.And(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
.And(x => _steps.ThenTheResponseHeaderIs("Set-Cookie", "test=0; path=/")) this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:6774", "/sso/test", 200))
.And(x => _steps.GivenIAddCookieToMyRequest("test=1; path=/")) .And(x => _steps.GivenThereIsAConfiguration(configuration))
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/sso/test")) .And(x => _steps.GivenOcelotIsRunning())
.Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK)) .And(x => _steps.WhenIGetUrlOnTheApiGateway("/sso/test"))
.BDDfy(); .And(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
} .And(x => _steps.ThenTheResponseHeaderIs("Set-Cookie", "test=0; path=/"))
.And(x => _steps.GivenIAddCookieToMyRequest("test=1; path=/"))
private void GivenThereIsAServiceRunningOn(string baseUrl, string basePath, int statusCode) .When(x => _steps.WhenIGetUrlOnTheApiGateway("/sso/test"))
{ .Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
_builder = new WebHostBuilder() .BDDfy();
.UseUrls(baseUrl) }
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory()) [Fact]
.UseIISIntegration() public void request_should_have_own_cookies_no_cookie_container()
.Configure(app => {
{ var configuration = new FileConfiguration
app.UsePathBase(basePath); {
app.Run(context => ReRoutes = new List<FileReRoute>
{ {
if (_count == 0) new FileReRoute
{ {
context.Response.Cookies.Append("test", "0"); DownstreamPathTemplate = "/sso/{everything}",
_count++; DownstreamScheme = "http",
context.Response.StatusCode = statusCode; DownstreamHostAndPorts = new List<FileHostAndPort>
return Task.CompletedTask; {
} new FileHostAndPort
{
if (context.Request.Cookies.TryGetValue("test", out var cookieValue) || context.Request.Headers.TryGetValue("Set-Cookie", out var headerValue)) Host = "localhost",
{ Port = 6775,
if (cookieValue == "0" || headerValue == "test=1; path=/") }
{ },
context.Response.StatusCode = statusCode; UpstreamPathTemplate = "/sso/{everything}",
return Task.CompletedTask; UpstreamHttpMethod = new List<string> { "Get", "Post", "Options" },
} HttpHandlerOptions = new FileHttpHandlerOptions
} {
UseCookieContainer = false
context.Response.StatusCode = 500; }
return Task.CompletedTask; }
}); }
}) };
.Build();
this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:6775", "/sso/test", 200))
_builder.Start(); .And(x => _steps.GivenThereIsAConfiguration(configuration))
} .And(x => _steps.GivenOcelotIsRunning())
.And(x => _steps.WhenIGetUrlOnTheApiGateway("/sso/test"))
private void GivenThereIsAServiceRunningOn(string baseUrl, string basePath, int statusCode, string headerKey) .And(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
{ .And(x => _steps.ThenTheResponseHeaderIs("Set-Cookie", "test=0; path=/"))
_builder = new WebHostBuilder() .And(x => _steps.GivenIAddCookieToMyRequest("test=1; path=/"))
.UseUrls(baseUrl) .When(x => _steps.WhenIGetUrlOnTheApiGateway("/sso/test"))
.UseKestrel() .Then(x => _steps.ThenTheStatusCodeShouldBe(HttpStatusCode.OK))
.UseContentRoot(Directory.GetCurrentDirectory()) .BDDfy();
.UseIISIntegration() }
.Configure(app =>
{ private void GivenThereIsAServiceRunningOn(string baseUrl, string basePath, int statusCode)
app.UsePathBase(basePath); {
app.Run(async context => _builder = new WebHostBuilder()
{ .UseUrls(baseUrl)
if(context.Request.Headers.TryGetValue(headerKey, out var values)) .UseKestrel()
{ .UseContentRoot(Directory.GetCurrentDirectory())
var result = values.First(); .UseIISIntegration()
context.Response.StatusCode = statusCode; .Configure(app =>
await context.Response.WriteAsync(result); {
} app.UsePathBase(basePath);
}); app.Run(context =>
}) {
.Build(); if (_count == 0)
{
_builder.Start(); context.Response.Cookies.Append("test", "0");
} _count++;
context.Response.StatusCode = statusCode;
private void GivenThereIsAServiceRunningOn(string baseUrl, string basePath, int statusCode, string headerKey, string headerValue) return Task.CompletedTask;
{ }
_builder = new WebHostBuilder()
.UseUrls(baseUrl) if (context.Request.Cookies.TryGetValue("test", out var cookieValue) || context.Request.Headers.TryGetValue("Set-Cookie", out var headerValue))
.UseKestrel() {
.UseContentRoot(Directory.GetCurrentDirectory()) if (cookieValue == "0" || headerValue == "test=1; path=/")
.UseIISIntegration() {
.Configure(app => context.Response.StatusCode = statusCode;
{ return Task.CompletedTask;
app.UsePathBase(basePath); }
app.Run(context => }
{
context.Response.OnStarting(() => { context.Response.StatusCode = 500;
context.Response.Headers.Add(headerKey, headerValue); return Task.CompletedTask;
context.Response.StatusCode = statusCode; });
return Task.CompletedTask; })
}); .Build();
return Task.CompletedTask; _builder.Start();
}); }
})
.Build(); private void GivenThereIsAServiceRunningOn(string baseUrl, string basePath, int statusCode, string headerKey)
{
_builder.Start(); _builder = new WebHostBuilder()
} .UseUrls(baseUrl)
.UseKestrel()
public void Dispose() .UseContentRoot(Directory.GetCurrentDirectory())
{ .UseIISIntegration()
_builder?.Dispose(); .Configure(app =>
_steps.Dispose(); {
} 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(context =>
{
context.Response.OnStarting(() =>
{
context.Response.Headers.Add(headerKey, headerValue);
context.Response.StatusCode = statusCode;
return Task.CompletedTask;
});
return Task.CompletedTask;
});
})
.Build();
_builder.Start();
}
public void Dispose()
{
_builder?.Dispose();
_steps.Dispose();
}
}
}

File diff suppressed because it is too large Load Diff

View File

@ -199,7 +199,7 @@
.WithDownstreamScheme("http") .WithDownstreamScheme("http")
.WithUpstreamHttpMethod(new List<string>() {"Get"}) .WithUpstreamHttpMethod(new List<string>() {"Get"})
.WithDownstreamAddresses(new List<DownstreamHostAndPort>() {new DownstreamHostAndPort("localhost", 51878)}) .WithDownstreamAddresses(new List<DownstreamHostAndPort>() {new DownstreamHostAndPort("localhost", 51878)})
.WithLoadBalancerKey("/laura|Get") .WithLoadBalancerKey("/laura|Get|localhost:51878")
.Build(); .Build();
var lauraReRoute = new ReRouteBuilder() var lauraReRoute = new ReRouteBuilder()
@ -218,7 +218,7 @@
.WithDownstreamScheme("http") .WithDownstreamScheme("http")
.WithUpstreamHttpMethod(new List<string>() { "Get" }) .WithUpstreamHttpMethod(new List<string>() { "Get" })
.WithDownstreamAddresses(new List<DownstreamHostAndPort>() { new DownstreamHostAndPort("localhost", 51878) }) .WithDownstreamAddresses(new List<DownstreamHostAndPort>() { new DownstreamHostAndPort("localhost", 51878) })
.WithLoadBalancerKey("/tom|Get") .WithLoadBalancerKey("/tom|Get|localhost:51880")
.Build(); .Build();
var tomReRoute = new ReRouteBuilder() var tomReRoute = new ReRouteBuilder()
@ -409,7 +409,7 @@
.WithDownstreamPathTemplate("/products/{productId}") .WithDownstreamPathTemplate("/products/{productId}")
.WithUpstreamPathTemplate("/api/products/{productId}") .WithUpstreamPathTemplate("/api/products/{productId}")
.WithUpstreamHttpMethod(new List<string> {"Get"}) .WithUpstreamHttpMethod(new List<string> {"Get"})
.WithLoadBalancerKey("/api/products/{productId}|Get") .WithLoadBalancerKey("/api/products/{productId}|Get|127.0.0.1:0")
.Build(); .Build();
this.Given(x => x.GivenTheConfigIs(new FileConfiguration this.Given(x => x.GivenTheConfigIs(new FileConfiguration
@ -461,7 +461,7 @@
.WithUpstreamPathTemplate("/api/products/{productId}") .WithUpstreamPathTemplate("/api/products/{productId}")
.WithUpstreamHttpMethod(new List<string> {"Get"}) .WithUpstreamHttpMethod(new List<string> {"Get"})
.WithDelegatingHandlers(handlers) .WithDelegatingHandlers(handlers)
.WithLoadBalancerKey("/api/products/{productId}|Get") .WithLoadBalancerKey("/api/products/{productId}|Get|")
.Build(); .Build();
this.Given(x => x.GivenTheConfigIs(new FileConfiguration this.Given(x => x.GivenTheConfigIs(new FileConfiguration
@ -506,7 +506,7 @@
.WithUpstreamHttpMethod(new List<string> {"Get"}) .WithUpstreamHttpMethod(new List<string> {"Get"})
.WithUseServiceDiscovery(true) .WithUseServiceDiscovery(true)
.WithServiceName("ProductService") .WithServiceName("ProductService")
.WithLoadBalancerKey("/api/products/{productId}|Get") .WithLoadBalancerKey("/api/products/{productId}|Get|")
.Build(); .Build();
this.Given(x => x.GivenTheConfigIs(new FileConfiguration this.Given(x => x.GivenTheConfigIs(new FileConfiguration
@ -557,7 +557,7 @@
.WithUpstreamPathTemplate("/api/products/{productId}") .WithUpstreamPathTemplate("/api/products/{productId}")
.WithUpstreamHttpMethod(new List<string> {"Get"}) .WithUpstreamHttpMethod(new List<string> {"Get"})
.WithUseServiceDiscovery(false) .WithUseServiceDiscovery(false)
.WithLoadBalancerKey("/api/products/{productId}|Get") .WithLoadBalancerKey("/api/products/{productId}|Get|")
.Build(); .Build();
this.Given(x => x.GivenTheConfigIs(new FileConfiguration this.Given(x => x.GivenTheConfigIs(new FileConfiguration
@ -600,7 +600,7 @@
.WithUpstreamPathTemplate("/api/products/{productId}") .WithUpstreamPathTemplate("/api/products/{productId}")
.WithUpstreamHttpMethod(new List<string> {"Get"}) .WithUpstreamHttpMethod(new List<string> {"Get"})
.WithUpstreamTemplatePattern(new UpstreamPathTemplate("(?i)/api/products/.*/$", 1)) .WithUpstreamTemplatePattern(new UpstreamPathTemplate("(?i)/api/products/.*/$", 1))
.WithLoadBalancerKey("/api/products/{productId}|Get") .WithLoadBalancerKey("/api/products/{productId}|Get|")
.Build(); .Build();
this.Given(x => x.GivenTheConfigIs(new FileConfiguration this.Given(x => x.GivenTheConfigIs(new FileConfiguration
@ -645,7 +645,7 @@
.WithUpstreamPathTemplate("/api/products/{productId}") .WithUpstreamPathTemplate("/api/products/{productId}")
.WithUpstreamHttpMethod(new List<string> {"Get"}) .WithUpstreamHttpMethod(new List<string> {"Get"})
.WithRequestIdKey("blahhhh") .WithRequestIdKey("blahhhh")
.WithLoadBalancerKey("/api/products/{productId}|Get") .WithLoadBalancerKey("/api/products/{productId}|Get|")
.Build(); .Build();
this.Given(x => x.GivenTheConfigIs(new FileConfiguration this.Given(x => x.GivenTheConfigIs(new FileConfiguration
@ -740,7 +740,7 @@
{ {
new ClaimToThing("CustomerId", "CustomerId", "", 0), new ClaimToThing("CustomerId", "CustomerId", "", 0),
}) })
.WithLoadBalancerKey("/api/products/{productId}|Get") .WithLoadBalancerKey("/api/products/{productId}|Get|")
.Build(); .Build();
var expected = new List<ReRoute> var expected = new List<ReRoute>
@ -783,7 +783,7 @@
.WithUpstreamPathTemplate("/api/products/{productId}") .WithUpstreamPathTemplate("/api/products/{productId}")
.WithUpstreamHttpMethod(new List<string> {"Get"}) .WithUpstreamHttpMethod(new List<string> {"Get"})
.WithAuthenticationOptions(authenticationOptions) .WithAuthenticationOptions(authenticationOptions)
.WithLoadBalancerKey("/api/products/{productId}|Get") .WithLoadBalancerKey("/api/products/{productId}|Get|")
.Build(); .Build();
var expected = new List<ReRoute> var expected = new List<ReRoute>