Ive made the acceptance tests share the same builder in an effort to duplicate the linux port in use issue I have when running acceptance tests...seems to have been some use...now to test in CI (#486)

This commit is contained in:
Tom Pallister 2018-07-21 11:24:05 +01:00 committed by GitHub
parent daa0491992
commit 600732651b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
27 changed files with 1033 additions and 1505 deletions

View File

@ -20,14 +20,14 @@ namespace Ocelot.AcceptanceTests
{ {
public class AggregateTests : IDisposable public class AggregateTests : IDisposable
{ {
private IWebHost _serviceOneBuilder;
private IWebHost _serviceTwoBuilder;
private readonly Steps _steps; private readonly Steps _steps;
private string _downstreamPathOne; private string _downstreamPathOne;
private string _downstreamPathTwo; private string _downstreamPathTwo;
private readonly ServiceHandler _serviceHandler;
public AggregateTests() public AggregateTests()
{ {
_serviceHandler = new ServiceHandler();
_steps = new Steps(); _steps = new Steps();
} }
@ -116,7 +116,7 @@ namespace Ocelot.AcceptanceTests
new FileHostAndPort new FileHostAndPort
{ {
Host = "localhost", Host = "localhost",
Port = 51885, Port = 51875,
} }
}, },
UpstreamPathTemplate = "/laura", UpstreamPathTemplate = "/laura",
@ -157,7 +157,7 @@ namespace Ocelot.AcceptanceTests
var expected = "{\"Laura\":{Hello from Laura},\"Tom\":{Hello from Tom}}"; var expected = "{\"Laura\":{Hello from Laura},\"Tom\":{Hello from Tom}}";
this.Given(x => x.GivenServiceOneIsRunning("http://localhost:51885", "/", 200, "{Hello from Laura}")) this.Given(x => x.GivenServiceOneIsRunning("http://localhost:51875", "/", 200, "{Hello from Laura}"))
.Given(x => x.GivenServiceTwoIsRunning("http://localhost:51886", "/", 200, "{Hello from Tom}")) .Given(x => x.GivenServiceTwoIsRunning("http://localhost:51886", "/", 200, "{Hello from Tom}"))
.And(x => _steps.GivenThereIsAConfiguration(configuration)) .And(x => _steps.GivenThereIsAConfiguration(configuration))
.And(x => _steps.GivenOcelotIsRunning()) .And(x => _steps.GivenOcelotIsRunning())
@ -370,15 +370,7 @@ namespace Ocelot.AcceptanceTests
private void GivenServiceOneIsRunning(string baseUrl, string basePath, int statusCode, string responseBody) private void GivenServiceOneIsRunning(string baseUrl, string basePath, int statusCode, string responseBody)
{ {
_serviceOneBuilder = new WebHostBuilder() _serviceHandler.GivenThereIsAServiceRunningOn(baseUrl, basePath, async context =>
.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; _downstreamPathOne = !string.IsNullOrEmpty(context.Request.PathBase.Value) ? context.Request.PathBase.Value : context.Request.Path.Value;
@ -393,23 +385,11 @@ namespace Ocelot.AcceptanceTests
await context.Response.WriteAsync(responseBody); await context.Response.WriteAsync(responseBody);
} }
}); });
})
.Build();
_serviceOneBuilder.Start();
} }
private void GivenServiceTwoIsRunning(string baseUrl, string basePath, int statusCode, string responseBody) private void GivenServiceTwoIsRunning(string baseUrl, string basePath, int statusCode, string responseBody)
{ {
_serviceTwoBuilder = new WebHostBuilder() _serviceHandler.GivenThereIsAServiceRunningOn(baseUrl, basePath, async context =>
.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; _downstreamPathTwo = !string.IsNullOrEmpty(context.Request.PathBase.Value) ? context.Request.PathBase.Value : context.Request.Path.Value;
@ -424,10 +404,6 @@ namespace Ocelot.AcceptanceTests
await context.Response.WriteAsync(responseBody); await context.Response.WriteAsync(responseBody);
} }
}); });
})
.Build();
_serviceTwoBuilder.Start();
} }
internal void ThenTheDownstreamUrlPathShouldBe(string expectedDownstreamPathOne, string expectedDownstreamPath) internal void ThenTheDownstreamUrlPathShouldBe(string expectedDownstreamPathOne, string expectedDownstreamPath)
@ -438,8 +414,7 @@ namespace Ocelot.AcceptanceTests
public void Dispose() public void Dispose()
{ {
_serviceOneBuilder?.Dispose(); _serviceHandler.Dispose();
_serviceTwoBuilder?.Dispose();
_steps.Dispose(); _steps.Dispose();
} }
} }

View File

@ -1,3 +1,6 @@
namespace Ocelot.AcceptanceTests
{
using IdentityServer4.Test;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
@ -13,25 +16,21 @@ using Ocelot.Configuration.File;
using TestStack.BDDfy; using TestStack.BDDfy;
using Xunit; using Xunit;
namespace Ocelot.AcceptanceTests
{
using IdentityServer4.Test;
public class AuthenticationTests : IDisposable public class AuthenticationTests : IDisposable
{ {
private IWebHost _servicebuilder;
private readonly Steps _steps; private readonly Steps _steps;
private IWebHost _identityServerBuilder; private IWebHost _identityServerBuilder;
private string _identityServerRootUrl = "http://localhost:51888"; private string _identityServerRootUrl = "http://localhost:51888";
private string _downstreamServicePath = "/"; private string _downstreamServicePath = "/";
private string _downstreamServiceHost = "localhost"; private string _downstreamServiceHost = "localhost";
private int _downstreamServicePort = 51876;
private string _downstreamServiceScheme = "http"; private string _downstreamServiceScheme = "http";
private string _downstreamServiceUrl = "http://localhost:51876"; private string _downstreamServiceUrl = "http://localhost:";
private readonly Action<IdentityServerAuthenticationOptions> _options; private readonly Action<IdentityServerAuthenticationOptions> _options;
private readonly ServiceHandler _serviceHandler;
public AuthenticationTests() public AuthenticationTests()
{ {
_serviceHandler = new ServiceHandler();
_steps = new Steps(); _steps = new Steps();
_options = o => _options = o =>
{ {
@ -46,6 +45,8 @@ namespace Ocelot.AcceptanceTests
[Fact] [Fact]
public void should_return_401_using_identity_server_access_token() public void should_return_401_using_identity_server_access_token()
{ {
int port = 54329;
var configuration = new FileConfiguration var configuration = new FileConfiguration
{ {
ReRoutes = new List<FileReRoute> ReRoutes = new List<FileReRoute>
@ -58,7 +59,7 @@ namespace Ocelot.AcceptanceTests
new FileHostAndPort new FileHostAndPort
{ {
Host =_downstreamServiceHost, Host =_downstreamServiceHost,
Port = _downstreamServicePort, Port = port,
} }
}, },
DownstreamScheme = _downstreamServiceScheme, DownstreamScheme = _downstreamServiceScheme,
@ -73,7 +74,7 @@ namespace Ocelot.AcceptanceTests
}; };
this.Given(x => x.GivenThereIsAnIdentityServerOn(_identityServerRootUrl, "api", "api2", AccessTokenType.Jwt)) this.Given(x => x.GivenThereIsAnIdentityServerOn(_identityServerRootUrl, "api", "api2", AccessTokenType.Jwt))
.And(x => x.GivenThereIsAServiceRunningOn(_downstreamServiceUrl, 201, string.Empty)) .And(x => x.GivenThereIsAServiceRunningOn($"{_downstreamServiceUrl}{port}", 201, string.Empty))
.And(x => _steps.GivenThereIsAConfiguration(configuration)) .And(x => _steps.GivenThereIsAConfiguration(configuration))
.And(x => _steps.GivenOcelotIsRunning(_options, "Test")) .And(x => _steps.GivenOcelotIsRunning(_options, "Test"))
.And(x => _steps.GivenThePostHasContent("postContent")) .And(x => _steps.GivenThePostHasContent("postContent"))
@ -85,6 +86,8 @@ namespace Ocelot.AcceptanceTests
[Fact] [Fact]
public void should_return_response_200_using_identity_server() public void should_return_response_200_using_identity_server()
{ {
int port = 54099;
var configuration = new FileConfiguration var configuration = new FileConfiguration
{ {
ReRoutes = new List<FileReRoute> ReRoutes = new List<FileReRoute>
@ -97,7 +100,7 @@ namespace Ocelot.AcceptanceTests
new FileHostAndPort new FileHostAndPort
{ {
Host =_downstreamServiceHost, Host =_downstreamServiceHost,
Port = _downstreamServicePort, Port = port,
} }
}, },
DownstreamScheme = _downstreamServiceScheme, DownstreamScheme = _downstreamServiceScheme,
@ -112,7 +115,7 @@ namespace Ocelot.AcceptanceTests
}; };
this.Given(x => x.GivenThereIsAnIdentityServerOn(_identityServerRootUrl, "api", "api2", AccessTokenType.Jwt)) this.Given(x => x.GivenThereIsAnIdentityServerOn(_identityServerRootUrl, "api", "api2", AccessTokenType.Jwt))
.And(x => x.GivenThereIsAServiceRunningOn(_downstreamServiceUrl, 200, "Hello from Laura")) .And(x => x.GivenThereIsAServiceRunningOn($"{_downstreamServiceUrl}{port}", 200, "Hello from Laura"))
.And(x => _steps.GivenIHaveAToken(_identityServerRootUrl)) .And(x => _steps.GivenIHaveAToken(_identityServerRootUrl))
.And(x => _steps.GivenThereIsAConfiguration(configuration)) .And(x => _steps.GivenThereIsAConfiguration(configuration))
.And(x => _steps.GivenOcelotIsRunning(_options, "Test")) .And(x => _steps.GivenOcelotIsRunning(_options, "Test"))
@ -126,6 +129,8 @@ namespace Ocelot.AcceptanceTests
[Fact] [Fact]
public void should_return_response_401_using_identity_server_with_token_requested_for_other_api() public void should_return_response_401_using_identity_server_with_token_requested_for_other_api()
{ {
int port = 54196;
var configuration = new FileConfiguration var configuration = new FileConfiguration
{ {
ReRoutes = new List<FileReRoute> ReRoutes = new List<FileReRoute>
@ -138,7 +143,7 @@ namespace Ocelot.AcceptanceTests
new FileHostAndPort new FileHostAndPort
{ {
Host =_downstreamServiceHost, Host =_downstreamServiceHost,
Port = _downstreamServicePort, Port = port,
} }
}, },
DownstreamScheme = _downstreamServiceScheme, DownstreamScheme = _downstreamServiceScheme,
@ -153,7 +158,7 @@ namespace Ocelot.AcceptanceTests
}; };
this.Given(x => x.GivenThereIsAnIdentityServerOn(_identityServerRootUrl, "api", "api2", AccessTokenType.Jwt)) this.Given(x => x.GivenThereIsAnIdentityServerOn(_identityServerRootUrl, "api", "api2", AccessTokenType.Jwt))
.And(x => x.GivenThereIsAServiceRunningOn(_downstreamServiceUrl, 200, "Hello from Laura")) .And(x => x.GivenThereIsAServiceRunningOn($"{_downstreamServiceUrl}{port}", 200, "Hello from Laura"))
.And(x => _steps.GivenIHaveATokenForApi2(_identityServerRootUrl)) .And(x => _steps.GivenIHaveATokenForApi2(_identityServerRootUrl))
.And(x => _steps.GivenThereIsAConfiguration(configuration)) .And(x => _steps.GivenThereIsAConfiguration(configuration))
.And(x => _steps.GivenOcelotIsRunning(_options, "Test")) .And(x => _steps.GivenOcelotIsRunning(_options, "Test"))
@ -166,6 +171,8 @@ namespace Ocelot.AcceptanceTests
[Fact] [Fact]
public void should_return_201_using_identity_server_access_token() public void should_return_201_using_identity_server_access_token()
{ {
int port = 52226;
var configuration = new FileConfiguration var configuration = new FileConfiguration
{ {
ReRoutes = new List<FileReRoute> ReRoutes = new List<FileReRoute>
@ -178,7 +185,7 @@ namespace Ocelot.AcceptanceTests
new FileHostAndPort new FileHostAndPort
{ {
Host =_downstreamServiceHost, Host =_downstreamServiceHost,
Port = _downstreamServicePort, Port = port,
} }
}, },
DownstreamScheme = _downstreamServiceScheme, DownstreamScheme = _downstreamServiceScheme,
@ -193,7 +200,7 @@ namespace Ocelot.AcceptanceTests
}; };
this.Given(x => x.GivenThereIsAnIdentityServerOn(_identityServerRootUrl, "api", "api2", AccessTokenType.Jwt)) this.Given(x => x.GivenThereIsAnIdentityServerOn(_identityServerRootUrl, "api", "api2", AccessTokenType.Jwt))
.And(x => x.GivenThereIsAServiceRunningOn(_downstreamServiceUrl, 201, string.Empty)) .And(x => x.GivenThereIsAServiceRunningOn($"{_downstreamServiceUrl}{port}", 201, string.Empty))
.And(x => _steps.GivenIHaveAToken(_identityServerRootUrl)) .And(x => _steps.GivenIHaveAToken(_identityServerRootUrl))
.And(x => _steps.GivenThereIsAConfiguration(configuration)) .And(x => _steps.GivenThereIsAConfiguration(configuration))
.And(x => _steps.GivenOcelotIsRunning(_options, "Test")) .And(x => _steps.GivenOcelotIsRunning(_options, "Test"))
@ -207,6 +214,8 @@ namespace Ocelot.AcceptanceTests
[Fact] [Fact]
public void should_return_201_using_identity_server_reference_token() public void should_return_201_using_identity_server_reference_token()
{ {
int port = 52222;
var configuration = new FileConfiguration var configuration = new FileConfiguration
{ {
ReRoutes = new List<FileReRoute> ReRoutes = new List<FileReRoute>
@ -219,7 +228,7 @@ namespace Ocelot.AcceptanceTests
new FileHostAndPort new FileHostAndPort
{ {
Host =_downstreamServiceHost, Host =_downstreamServiceHost,
Port = _downstreamServicePort, Port = port,
} }
}, },
DownstreamScheme = _downstreamServiceScheme, DownstreamScheme = _downstreamServiceScheme,
@ -234,7 +243,7 @@ namespace Ocelot.AcceptanceTests
}; };
this.Given(x => x.GivenThereIsAnIdentityServerOn(_identityServerRootUrl, "api", "api2", AccessTokenType.Reference)) this.Given(x => x.GivenThereIsAnIdentityServerOn(_identityServerRootUrl, "api", "api2", AccessTokenType.Reference))
.And(x => x.GivenThereIsAServiceRunningOn(_downstreamServiceUrl, 201, string.Empty)) .And(x => x.GivenThereIsAServiceRunningOn($"{_downstreamServiceUrl}{port}", 201, string.Empty))
.And(x => _steps.GivenIHaveAToken(_identityServerRootUrl)) .And(x => _steps.GivenIHaveAToken(_identityServerRootUrl))
.And(x => _steps.GivenThereIsAConfiguration(configuration)) .And(x => _steps.GivenThereIsAConfiguration(configuration))
.And(x => _steps.GivenOcelotIsRunning(_options, "Test")) .And(x => _steps.GivenOcelotIsRunning(_options, "Test"))
@ -247,23 +256,11 @@ namespace Ocelot.AcceptanceTests
private void GivenThereIsAServiceRunningOn(string url, int statusCode, string responseBody) private void GivenThereIsAServiceRunningOn(string url, int statusCode, string responseBody)
{ {
_servicebuilder = new WebHostBuilder() _serviceHandler.GivenThereIsAServiceRunningOn(url, async context =>
.UseUrls(url)
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseUrls(url)
.Configure(app =>
{
app.Run(async context =>
{ {
context.Response.StatusCode = statusCode; context.Response.StatusCode = statusCode;
await context.Response.WriteAsync(responseBody); await context.Response.WriteAsync(responseBody);
}); });
})
.Build();
_servicebuilder.Start();
} }
private void GivenThereIsAnIdentityServerOn(string url, string apiName, string api2Name, AccessTokenType tokenType) private void GivenThereIsAnIdentityServerOn(string url, string apiName, string api2Name, AccessTokenType tokenType)
@ -371,7 +368,7 @@ namespace Ocelot.AcceptanceTests
public void Dispose() public void Dispose()
{ {
_servicebuilder?.Dispose(); _serviceHandler.Dispose();
_steps.Dispose(); _steps.Dispose();
_identityServerBuilder?.Dispose(); _identityServerBuilder?.Dispose();
} }

View File

@ -1,3 +1,5 @@
namespace Ocelot.AcceptanceTests
{
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
@ -12,22 +14,19 @@ using Microsoft.Extensions.DependencyInjection;
using Ocelot.Configuration.File; using Ocelot.Configuration.File;
using TestStack.BDDfy; using TestStack.BDDfy;
using Xunit; using Xunit;
namespace Ocelot.AcceptanceTests
{
using IdentityServer4;
using IdentityServer4.Test; using IdentityServer4.Test;
public class AuthorisationTests : IDisposable public class AuthorisationTests : IDisposable
{ {
private IWebHost _servicebuilder;
private IWebHost _identityServerBuilder; private IWebHost _identityServerBuilder;
private readonly Steps _steps; private readonly Steps _steps;
private Action<IdentityServerAuthenticationOptions> _options; private readonly Action<IdentityServerAuthenticationOptions> _options;
private string _identityServerRootUrl = "http://localhost:51888"; private string _identityServerRootUrl = "http://localhost:51888";
private readonly ServiceHandler _serviceHandler;
public AuthorisationTests() public AuthorisationTests()
{ {
_serviceHandler = new ServiceHandler();
_steps = new Steps(); _steps = new Steps();
_options = o => _options = o =>
{ {
@ -42,6 +41,8 @@ namespace Ocelot.AcceptanceTests
[Fact] [Fact]
public void should_return_response_200_authorising_route() public void should_return_response_200_authorising_route()
{ {
int port = 52875;
var configuration = new FileConfiguration var configuration = new FileConfiguration
{ {
ReRoutes = new List<FileReRoute> ReRoutes = new List<FileReRoute>
@ -54,7 +55,7 @@ namespace Ocelot.AcceptanceTests
new FileHostAndPort new FileHostAndPort
{ {
Host = "localhost", Host = "localhost",
Port = 51876, Port = port,
} }
}, },
DownstreamScheme = "http", DownstreamScheme = "http",
@ -86,7 +87,7 @@ namespace Ocelot.AcceptanceTests
}; };
this.Given(x => x.GivenThereIsAnIdentityServerOn("http://localhost:51888", "api", AccessTokenType.Jwt)) this.Given(x => x.GivenThereIsAnIdentityServerOn("http://localhost:51888", "api", AccessTokenType.Jwt))
.And(x => x.GivenThereIsAServiceRunningOn("http://localhost:51876", 200, "Hello from Laura")) .And(x => x.GivenThereIsAServiceRunningOn($"http://localhost:{port}", 200, "Hello from Laura"))
.And(x => _steps.GivenIHaveAToken("http://localhost:51888")) .And(x => _steps.GivenIHaveAToken("http://localhost:51888"))
.And(x => _steps.GivenThereIsAConfiguration(configuration)) .And(x => _steps.GivenThereIsAConfiguration(configuration))
.And(x => _steps.GivenOcelotIsRunning(_options, "Test")) .And(x => _steps.GivenOcelotIsRunning(_options, "Test"))
@ -100,6 +101,8 @@ namespace Ocelot.AcceptanceTests
[Fact] [Fact]
public void should_return_response_403_authorising_route() public void should_return_response_403_authorising_route()
{ {
int port = 59471;
var configuration = new FileConfiguration var configuration = new FileConfiguration
{ {
ReRoutes = new List<FileReRoute> ReRoutes = new List<FileReRoute>
@ -112,7 +115,7 @@ namespace Ocelot.AcceptanceTests
new FileHostAndPort new FileHostAndPort
{ {
Host = "localhost", Host = "localhost",
Port = 51876, Port = port,
} }
}, },
DownstreamScheme = "http", DownstreamScheme = "http",
@ -143,7 +146,7 @@ namespace Ocelot.AcceptanceTests
}; };
this.Given(x => x.GivenThereIsAnIdentityServerOn("http://localhost:51888", "api", AccessTokenType.Jwt)) this.Given(x => x.GivenThereIsAnIdentityServerOn("http://localhost:51888", "api", AccessTokenType.Jwt))
.And(x => x.GivenThereIsAServiceRunningOn("http://localhost:51876", 200, "Hello from Laura")) .And(x => x.GivenThereIsAServiceRunningOn($"http://localhost:{port}", 200, "Hello from Laura"))
.And(x => _steps.GivenIHaveAToken("http://localhost:51888")) .And(x => _steps.GivenIHaveAToken("http://localhost:51888"))
.And(x => _steps.GivenThereIsAConfiguration(configuration)) .And(x => _steps.GivenThereIsAConfiguration(configuration))
.And(x => _steps.GivenOcelotIsRunning(_options, "Test")) .And(x => _steps.GivenOcelotIsRunning(_options, "Test"))
@ -156,6 +159,8 @@ namespace Ocelot.AcceptanceTests
[Fact] [Fact]
public void should_return_response_200_using_identity_server_with_allowed_scope() public void should_return_response_200_using_identity_server_with_allowed_scope()
{ {
int port = 63471;
var configuration = new FileConfiguration var configuration = new FileConfiguration
{ {
ReRoutes = new List<FileReRoute> ReRoutes = new List<FileReRoute>
@ -168,7 +173,7 @@ namespace Ocelot.AcceptanceTests
new FileHostAndPort new FileHostAndPort
{ {
Host = "localhost", Host = "localhost",
Port = 51876, Port = port,
} }
}, },
DownstreamScheme = "http", DownstreamScheme = "http",
@ -184,7 +189,7 @@ namespace Ocelot.AcceptanceTests
}; };
this.Given(x => x.GivenThereIsAnIdentityServerOn("http://localhost:51888", "api", AccessTokenType.Jwt)) this.Given(x => x.GivenThereIsAnIdentityServerOn("http://localhost:51888", "api", AccessTokenType.Jwt))
.And(x => x.GivenThereIsAServiceRunningOn("http://localhost:51876", 200, "Hello from Laura")) .And(x => x.GivenThereIsAServiceRunningOn($"http://localhost:{port}", 200, "Hello from Laura"))
.And(x => _steps.GivenIHaveATokenForApiReadOnlyScope("http://localhost:51888")) .And(x => _steps.GivenIHaveATokenForApiReadOnlyScope("http://localhost:51888"))
.And(x => _steps.GivenThereIsAConfiguration(configuration)) .And(x => _steps.GivenThereIsAConfiguration(configuration))
.And(x => _steps.GivenOcelotIsRunning(_options, "Test")) .And(x => _steps.GivenOcelotIsRunning(_options, "Test"))
@ -197,6 +202,8 @@ namespace Ocelot.AcceptanceTests
[Fact] [Fact]
public void should_return_response_403_using_identity_server_with_scope_not_allowed() public void should_return_response_403_using_identity_server_with_scope_not_allowed()
{ {
int port = 60571;
var configuration = new FileConfiguration var configuration = new FileConfiguration
{ {
ReRoutes = new List<FileReRoute> ReRoutes = new List<FileReRoute>
@ -209,7 +216,7 @@ namespace Ocelot.AcceptanceTests
new FileHostAndPort new FileHostAndPort
{ {
Host = "localhost", Host = "localhost",
Port = 51876, Port = port,
} }
}, },
DownstreamScheme = "http", DownstreamScheme = "http",
@ -225,7 +232,7 @@ namespace Ocelot.AcceptanceTests
}; };
this.Given(x => x.GivenThereIsAnIdentityServerOn("http://localhost:51888", "api", AccessTokenType.Jwt)) this.Given(x => x.GivenThereIsAnIdentityServerOn("http://localhost:51888", "api", AccessTokenType.Jwt))
.And(x => x.GivenThereIsAServiceRunningOn("http://localhost:51876", 200, "Hello from Laura")) .And(x => x.GivenThereIsAServiceRunningOn($"http://localhost:{port}", 200, "Hello from Laura"))
.And(x => _steps.GivenIHaveATokenForApiReadOnlyScope("http://localhost:51888")) .And(x => _steps.GivenIHaveATokenForApiReadOnlyScope("http://localhost:51888"))
.And(x => _steps.GivenThereIsAConfiguration(configuration)) .And(x => _steps.GivenThereIsAConfiguration(configuration))
.And(x => _steps.GivenOcelotIsRunning(_options, "Test")) .And(x => _steps.GivenOcelotIsRunning(_options, "Test"))
@ -238,6 +245,8 @@ namespace Ocelot.AcceptanceTests
[Fact] [Fact]
public void should_fix_issue_240() public void should_fix_issue_240()
{ {
int port = 61071;
var configuration = new FileConfiguration var configuration = new FileConfiguration
{ {
ReRoutes = new List<FileReRoute> ReRoutes = new List<FileReRoute>
@ -250,7 +259,7 @@ namespace Ocelot.AcceptanceTests
new FileHostAndPort new FileHostAndPort
{ {
Host = "localhost", Host = "localhost",
Port = 51876, Port = port,
} }
}, },
DownstreamScheme = "http", DownstreamScheme = "http",
@ -284,7 +293,7 @@ namespace Ocelot.AcceptanceTests
}; };
this.Given(x => x.GivenThereIsAnIdentityServerOn("http://localhost:51888", "api", AccessTokenType.Jwt, users)) this.Given(x => x.GivenThereIsAnIdentityServerOn("http://localhost:51888", "api", AccessTokenType.Jwt, users))
.And(x => x.GivenThereIsAServiceRunningOn("http://localhost:51876", 200, "Hello from Laura")) .And(x => x.GivenThereIsAServiceRunningOn($"http://localhost:{port}", 200, "Hello from Laura"))
.And(x => _steps.GivenIHaveAToken("http://localhost:51888")) .And(x => _steps.GivenIHaveAToken("http://localhost:51888"))
.And(x => _steps.GivenThereIsAConfiguration(configuration)) .And(x => _steps.GivenThereIsAConfiguration(configuration))
.And(x => _steps.GivenOcelotIsRunning(_options, "Test")) .And(x => _steps.GivenOcelotIsRunning(_options, "Test"))
@ -297,23 +306,11 @@ namespace Ocelot.AcceptanceTests
private void GivenThereIsAServiceRunningOn(string url, int statusCode, string responseBody) private void GivenThereIsAServiceRunningOn(string url, int statusCode, string responseBody)
{ {
_servicebuilder = new WebHostBuilder() _serviceHandler.GivenThereIsAServiceRunningOn(url, async context =>
.UseUrls(url)
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseUrls(url)
.Configure(app =>
{
app.Run(async context =>
{ {
context.Response.StatusCode = statusCode; context.Response.StatusCode = statusCode;
await context.Response.WriteAsync(responseBody); await context.Response.WriteAsync(responseBody);
}); });
})
.Build();
_servicebuilder.Start();
} }
private void GivenThereIsAnIdentityServerOn(string url, string apiName, AccessTokenType tokenType) private void GivenThereIsAnIdentityServerOn(string url, string apiName, AccessTokenType tokenType)
@ -465,7 +462,7 @@ namespace Ocelot.AcceptanceTests
public void Dispose() public void Dispose()
{ {
_servicebuilder?.Dispose(); _serviceHandler?.Dispose();
_steps.Dispose(); _steps.Dispose();
_identityServerBuilder?.Dispose(); _identityServerBuilder?.Dispose();
} }

View File

@ -1,24 +1,22 @@
namespace Ocelot.AcceptanceTests
{
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Net; using System.Net;
using System.Threading; using System.Threading;
using Microsoft.AspNetCore.Builder;
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
{
public class CachingTests : IDisposable public class CachingTests : IDisposable
{ {
private IWebHost _builder;
private readonly Steps _steps; private readonly Steps _steps;
private readonly ServiceHandler _serviceHandler;
public CachingTests() public CachingTests()
{ {
_serviceHandler = new ServiceHandler();
_steps = new Steps(); _steps = new Steps();
} }
@ -201,21 +199,13 @@ namespace Ocelot.AcceptanceTests
private void GivenTheServiceNowReturns(string url, int statusCode, string responseBody) private void GivenTheServiceNowReturns(string url, int statusCode, string responseBody)
{ {
_builder.Dispose(); _serviceHandler.Dispose();
GivenThereIsAServiceRunningOn(url, statusCode, responseBody, null, null); GivenThereIsAServiceRunningOn(url, statusCode, responseBody, null, null);
} }
private void GivenThereIsAServiceRunningOn(string url, int statusCode, string responseBody, string key, string value) private void GivenThereIsAServiceRunningOn(string url, int statusCode, string responseBody, string key, string value)
{ {
_builder = new WebHostBuilder() _serviceHandler.GivenThereIsAServiceRunningOn(url, async context =>
.UseUrls(url)
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseUrls(url)
.Configure(app =>
{
app.Run(async context =>
{ {
if (!string.IsNullOrEmpty(key) && !string.IsNullOrEmpty(key)) if (!string.IsNullOrEmpty(key) && !string.IsNullOrEmpty(key))
{ {
@ -224,15 +214,11 @@ namespace Ocelot.AcceptanceTests
context.Response.StatusCode = statusCode; context.Response.StatusCode = statusCode;
await context.Response.WriteAsync(responseBody); await context.Response.WriteAsync(responseBody);
}); });
})
.Build();
_builder.Start();
} }
public void Dispose() public void Dispose()
{ {
_builder?.Dispose(); _serviceHandler?.Dispose();
_steps.Dispose(); _steps.Dispose();
} }
} }

View File

@ -1,23 +1,21 @@
namespace Ocelot.AcceptanceTests
{
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Net; using System.Net;
using Microsoft.AspNetCore.Builder;
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
{
public class CaseSensitiveRoutingTests : IDisposable public class CaseSensitiveRoutingTests : IDisposable
{ {
private IWebHost _builder;
private readonly Steps _steps; private readonly Steps _steps;
private readonly ServiceHandler _serviceHandler;
public CaseSensitiveRoutingTests() public CaseSensitiveRoutingTests()
{ {
_serviceHandler = new ServiceHandler();
_steps = new Steps(); _steps = new Steps();
} }
@ -226,29 +224,16 @@ namespace Ocelot.AcceptanceTests
private void GivenThereIsAServiceRunningOn(string baseUrl, string basePath, int statusCode, string responseBody) private void GivenThereIsAServiceRunningOn(string baseUrl, string basePath, int statusCode, string responseBody)
{ {
_builder = new WebHostBuilder() _serviceHandler.GivenThereIsAServiceRunningOn(baseUrl, basePath, async context =>
.UseUrls(baseUrl)
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseUrls(baseUrl)
.Configure(app =>
{
app.UsePathBase(basePath);
app.Run(async context =>
{ {
context.Response.StatusCode = statusCode; context.Response.StatusCode = statusCode;
await context.Response.WriteAsync(responseBody); await context.Response.WriteAsync(responseBody);
}); });
})
.Build();
_builder.Start();
} }
public void Dispose() public void Dispose()
{ {
_builder?.Dispose(); _serviceHandler?.Dispose();
_steps.Dispose(); _steps.Dispose();
} }
} }

View File

@ -1,4 +1,10 @@
using System; using Xunit;
[assembly: CollectionBehavior(DisableTestParallelization = true)]
namespace Ocelot.AcceptanceTests
{
using IdentityServer4.Test;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
@ -12,24 +18,18 @@ using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Ocelot.Configuration.File; using Ocelot.Configuration.File;
using TestStack.BDDfy; using TestStack.BDDfy;
using Xunit;
[assembly: CollectionBehavior(DisableTestParallelization = true)]
namespace Ocelot.AcceptanceTests
{
using IdentityServer4;
using IdentityServer4.Test;
public class ClaimsToHeadersForwardingTests : IDisposable public class ClaimsToHeadersForwardingTests : IDisposable
{ {
private IWebHost _servicebuilder;
private IWebHost _identityServerBuilder; private IWebHost _identityServerBuilder;
private readonly Steps _steps; private readonly Steps _steps;
private Action<IdentityServerAuthenticationOptions> _options; private Action<IdentityServerAuthenticationOptions> _options;
private string _identityServerRootUrl = "http://localhost:52888"; private string _identityServerRootUrl = "http://localhost:52888";
private readonly ServiceHandler _serviceHandler;
public ClaimsToHeadersForwardingTests() public ClaimsToHeadersForwardingTests()
{ {
_serviceHandler = new ServiceHandler();
_steps = new Steps(); _steps = new Steps();
_options = o => _options = o =>
{ {
@ -107,15 +107,7 @@ namespace Ocelot.AcceptanceTests
private void GivenThereIsAServiceRunningOn(string url, int statusCode) private void GivenThereIsAServiceRunningOn(string url, int statusCode)
{ {
_servicebuilder = new WebHostBuilder() _serviceHandler.GivenThereIsAServiceRunningOn(url, async context =>
.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 customerId = context.Request.Headers.First(x => x.Key == "CustomerId").Value.First();
var locationId = context.Request.Headers.First(x => x.Key == "LocationId").Value.First(); var locationId = context.Request.Headers.First(x => x.Key == "LocationId").Value.First();
@ -126,10 +118,6 @@ namespace Ocelot.AcceptanceTests
context.Response.StatusCode = statusCode; context.Response.StatusCode = statusCode;
await context.Response.WriteAsync(responseBody); await context.Response.WriteAsync(responseBody);
}); });
})
.Build();
_servicebuilder.Start();
} }
private void GivenThereIsAnIdentityServerOn(string url, string apiName, AccessTokenType tokenType, TestUser user) private void GivenThereIsAnIdentityServerOn(string url, string apiName, AccessTokenType tokenType, TestUser user)
@ -203,7 +191,7 @@ namespace Ocelot.AcceptanceTests
public void Dispose() public void Dispose()
{ {
_servicebuilder?.Dispose(); _serviceHandler?.Dispose();
_steps.Dispose(); _steps.Dispose();
_identityServerBuilder?.Dispose(); _identityServerBuilder?.Dispose();
} }

View File

@ -1,36 +1,25 @@
using Microsoft.AspNetCore.Builder; namespace Ocelot.AcceptanceTests
using Microsoft.AspNetCore.Hosting; {
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Ocelot.Configuration.File; using Ocelot.Configuration.File;
using Shouldly;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks; using System.Threading.Tasks;
using TestStack.BDDfy; using TestStack.BDDfy;
using Xunit; using Xunit;
namespace Ocelot.AcceptanceTests
{
public class ClientRateLimitTests : IDisposable public class ClientRateLimitTests : IDisposable
{ {
private IWebHost _builder;
private readonly Steps _steps; private readonly Steps _steps;
private int _counterOne; private int _counterOne;
private readonly ServiceHandler _serviceHandler;
public ClientRateLimitTests() public ClientRateLimitTests()
{ {
_serviceHandler = new ServiceHandler();
_steps = new Steps(); _steps = new Steps();
} }
public void Dispose()
{
_builder?.Dispose();
_steps.Dispose();
}
[Fact] [Fact]
public void should_call_withratelimiting() public void should_call_withratelimiting()
{ {
@ -158,6 +147,8 @@ namespace Ocelot.AcceptanceTests
[Fact] [Fact]
public void should_call_middleware_withWhitelistClient() public void should_call_middleware_withWhitelistClient()
{ {
int port = 61876;
var configuration = new FileConfiguration var configuration = new FileConfiguration
{ {
ReRoutes = new List<FileReRoute> ReRoutes = new List<FileReRoute>
@ -170,7 +161,7 @@ namespace Ocelot.AcceptanceTests
new FileHostAndPort new FileHostAndPort
{ {
Host = "localhost", Host = "localhost",
Port = 51876, Port = port,
} }
}, },
DownstreamScheme = "http", DownstreamScheme = "http",
@ -201,7 +192,7 @@ namespace Ocelot.AcceptanceTests
} }
}; };
this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:51876", "/api/ClientRateLimit")) this.Given(x => x.GivenThereIsAServiceRunningOn($"http://localhost:{port}", "/api/ClientRateLimit"))
.And(x => _steps.GivenThereIsAConfiguration(configuration)) .And(x => _steps.GivenThereIsAConfiguration(configuration))
.And(x => _steps.GivenOcelotIsRunning()) .And(x => _steps.GivenOcelotIsRunning())
.When(x => _steps.WhenIGetUrlOnTheApiGatewayMultipleTimesForRateLimit("/api/ClientRateLimit", 4)) .When(x => _steps.WhenIGetUrlOnTheApiGatewayMultipleTimesForRateLimit("/api/ClientRateLimit", 4))
@ -211,26 +202,18 @@ namespace Ocelot.AcceptanceTests
private void GivenThereIsAServiceRunningOn(string baseUrl, string basePath) private void GivenThereIsAServiceRunningOn(string baseUrl, string basePath)
{ {
_builder = new WebHostBuilder() _serviceHandler.GivenThereIsAServiceRunningOn(baseUrl, basePath, context =>
.UseUrls(baseUrl)
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseUrls(baseUrl)
.Configure(app =>
{
app.UsePathBase(basePath);
app.Run(context =>
{ {
_counterOne++; _counterOne++;
context.Response.StatusCode = 200; context.Response.StatusCode = 200;
context.Response.WriteAsync(_counterOne.ToString()); context.Response.WriteAsync(_counterOne.ToString());
return Task.CompletedTask; return Task.CompletedTask;
}); });
}) }
.Build();
_builder.Start(); public void Dispose()
{
_steps.Dispose();
} }
} }
} }

View File

@ -1,27 +1,25 @@
namespace Ocelot.AcceptanceTests
{
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Net; using System.Net;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Ocelot.Configuration.File; using Ocelot.Configuration.File;
using Shouldly; using Shouldly;
using TestStack.BDDfy; using TestStack.BDDfy;
using Xunit; using Xunit;
namespace Ocelot.AcceptanceTests
{
public class ContentTests : IDisposable public class ContentTests : IDisposable
{ {
private IWebHost _builder;
private readonly Steps _steps; private readonly Steps _steps;
private string _contentType; private string _contentType;
private long? _contentLength; private long? _contentLength;
private bool _contentTypeHeaderExists; private bool _contentTypeHeaderExists;
private readonly ServiceHandler _serviceHandler;
public ContentTests() public ContentTests()
{ {
_serviceHandler = new ServiceHandler();
_steps = new Steps(); _steps = new Steps();
} }
@ -160,15 +158,7 @@ namespace Ocelot.AcceptanceTests
private void GivenThereIsAServiceRunningOn(string baseUrl, string basePath, int statusCode, string responseBody) private void GivenThereIsAServiceRunningOn(string baseUrl, string basePath, int statusCode, string responseBody)
{ {
_builder = new WebHostBuilder() _serviceHandler.GivenThereIsAServiceRunningOn(baseUrl, basePath, async context =>
.UseUrls(baseUrl)
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.Configure(app =>
{
app.UsePathBase(basePath);
app.Run(async context =>
{ {
_contentType = context.Request.ContentType; _contentType = context.Request.ContentType;
_contentLength = context.Request.ContentLength; _contentLength = context.Request.ContentLength;
@ -176,14 +166,11 @@ namespace Ocelot.AcceptanceTests
context.Response.StatusCode = statusCode; context.Response.StatusCode = statusCode;
await context.Response.WriteAsync(responseBody); await context.Response.WriteAsync(responseBody);
}); });
})
.Build();
_builder.Start();
} }
public void Dispose() public void Dispose()
{ {
_builder?.Dispose(); _serviceHandler?.Dispose();
_steps.Dispose(); _steps.Dispose();
} }
} }

View File

@ -1,11 +1,10 @@
using System; namespace Ocelot.AcceptanceTests
{
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.IO;
using System.Net; using System.Net;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Ocelot.Configuration.File; using Ocelot.Configuration.File;
using Ocelot.Middleware; using Ocelot.Middleware;
@ -13,17 +12,16 @@ using Shouldly;
using TestStack.BDDfy; using TestStack.BDDfy;
using Xunit; using Xunit;
namespace Ocelot.AcceptanceTests
{
public class CustomMiddlewareTests : IDisposable public class CustomMiddlewareTests : IDisposable
{ {
private readonly string _configurationPath; private readonly string _configurationPath;
private IWebHost _builder;
private readonly Steps _steps; private readonly Steps _steps;
private int _counter; private int _counter;
private readonly ServiceHandler _serviceHandler;
public CustomMiddlewareTests() public CustomMiddlewareTests()
{ {
_serviceHandler = new ServiceHandler();
_counter = 0; _counter = 0;
_steps = new Steps(); _steps = new Steps();
_configurationPath = "ocelot.json"; _configurationPath = "ocelot.json";
@ -340,16 +338,7 @@ namespace Ocelot.AcceptanceTests
private void GivenThereIsAServiceRunningOn(string url, int statusCode, string basePath) private void GivenThereIsAServiceRunningOn(string url, int statusCode, string basePath)
{ {
_builder = new WebHostBuilder() _serviceHandler.GivenThereIsAServiceRunningOn(url, context =>
.UseUrls(url)
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseUrls(url)
.Configure(app =>
{
app.UsePathBase(basePath);
app.Run(context =>
{ {
if (string.IsNullOrEmpty(basePath)) if (string.IsNullOrEmpty(basePath))
{ {
@ -362,15 +351,11 @@ namespace Ocelot.AcceptanceTests
return Task.CompletedTask; return Task.CompletedTask;
}); });
})
.Build();
_builder.Start();
} }
public void Dispose() public void Dispose()
{ {
_builder?.Dispose(); _serviceHandler?.Dispose();
_steps.Dispose(); _steps.Dispose();
} }

View File

@ -1,29 +1,25 @@
namespace Ocelot.AcceptanceTests
{
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.IO.Compression; using System.IO.Compression;
using System.Linq; using System.Linq;
using System.Net; using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Ocelot.Configuration.File; using Ocelot.Configuration.File;
using Shouldly; using Shouldly;
using TestStack.BDDfy; using TestStack.BDDfy;
using Xunit; using Xunit;
namespace Ocelot.AcceptanceTests
{
public class GzipTests : IDisposable public class GzipTests : IDisposable
{ {
private IWebHost _builder;
private readonly Steps _steps; private readonly Steps _steps;
private readonly ServiceHandler _serviceHandler;
public GzipTests() public GzipTests()
{ {
_serviceHandler = new ServiceHandler();
_steps = new Steps(); _steps = new Steps();
} }
@ -66,15 +62,7 @@ namespace Ocelot.AcceptanceTests
private void GivenThereIsAServiceRunningOn(string baseUrl, string basePath, int statusCode, string responseBody, string expected) private void GivenThereIsAServiceRunningOn(string baseUrl, string basePath, int statusCode, string responseBody, string expected)
{ {
_builder = new WebHostBuilder() _serviceHandler.GivenThereIsAServiceRunningOn(baseUrl, basePath, async context =>
.UseUrls(baseUrl)
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.Configure(app =>
{
app.UsePathBase(basePath);
app.Run(async context =>
{ {
if (context.Request.Headers.TryGetValue("Content-Encoding", out var contentEncoding)) if (context.Request.Headers.TryGetValue("Content-Encoding", out var contentEncoding))
{ {
@ -83,7 +71,8 @@ namespace Ocelot.AcceptanceTests
string text = null; string text = null;
using (var decompress = new GZipStream(context.Request.Body, CompressionMode.Decompress)) using (var decompress = new GZipStream(context.Request.Body, CompressionMode.Decompress))
{ {
using (var sr = new StreamReader(decompress)) { using (var sr = new StreamReader(decompress))
{
text = sr.ReadToEnd(); text = sr.ReadToEnd();
} }
} }
@ -102,15 +91,11 @@ namespace Ocelot.AcceptanceTests
await context.Response.WriteAsync("downstream path didnt match base path"); await context.Response.WriteAsync("downstream path didnt match base path");
} }
}); });
})
.Build();
_builder.Start();
} }
public void Dispose() public void Dispose()
{ {
_builder?.Dispose(); _serviceHandler?.Dispose();
_steps.Dispose(); _steps.Dispose();
} }
} }

View File

@ -1,26 +1,24 @@
namespace Ocelot.AcceptanceTests
{
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
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.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
{
public class HeaderTests : IDisposable public class HeaderTests : IDisposable
{ {
private IWebHost _builder;
private int _count; private int _count;
private readonly Steps _steps; private readonly Steps _steps;
private readonly ServiceHandler _serviceHandler;
public HeaderTests() public HeaderTests()
{ {
_serviceHandler = new ServiceHandler();
_steps = new Steps(); _steps = new Steps();
} }
@ -387,15 +385,7 @@ namespace Ocelot.AcceptanceTests
private void GivenThereIsAServiceRunningOn(string baseUrl, string basePath, int statusCode) private void GivenThereIsAServiceRunningOn(string baseUrl, string basePath, int statusCode)
{ {
_builder = new WebHostBuilder() _serviceHandler.GivenThereIsAServiceRunningOn(baseUrl, basePath, context =>
.UseUrls(baseUrl)
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.Configure(app =>
{
app.UsePathBase(basePath);
app.Run(context =>
{ {
if (_count == 0) if (_count == 0)
{ {
@ -417,23 +407,11 @@ namespace Ocelot.AcceptanceTests
context.Response.StatusCode = 500; context.Response.StatusCode = 500;
return Task.CompletedTask; return Task.CompletedTask;
}); });
})
.Build();
_builder.Start();
} }
private void GivenThereIsAServiceRunningOn(string baseUrl, string basePath, int statusCode, string headerKey) private void GivenThereIsAServiceRunningOn(string baseUrl, string basePath, int statusCode, string headerKey)
{ {
_builder = new WebHostBuilder() _serviceHandler.GivenThereIsAServiceRunningOn(baseUrl, basePath, async context =>
.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)) if (context.Request.Headers.TryGetValue(headerKey, out var values))
{ {
@ -442,23 +420,11 @@ namespace Ocelot.AcceptanceTests
await context.Response.WriteAsync(result); await context.Response.WriteAsync(result);
} }
}); });
})
.Build();
_builder.Start();
} }
private void GivenThereIsAServiceRunningOn(string baseUrl, string basePath, int statusCode, string headerKey, string headerValue) private void GivenThereIsAServiceRunningOn(string baseUrl, string basePath, int statusCode, string headerKey, string headerValue)
{ {
_builder = new WebHostBuilder() _serviceHandler.GivenThereIsAServiceRunningOn(baseUrl, basePath, context =>
.UseUrls(baseUrl)
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.Configure(app =>
{
app.UsePathBase(basePath);
app.Run(context =>
{ {
context.Response.OnStarting(() => context.Response.OnStarting(() =>
{ {
@ -469,15 +435,11 @@ namespace Ocelot.AcceptanceTests
return Task.CompletedTask; return Task.CompletedTask;
}); });
})
.Build();
_builder.Start();
} }
public void Dispose() public void Dispose()
{ {
_builder?.Dispose(); _serviceHandler?.Dispose();
_steps.Dispose(); _steps.Dispose();
} }
} }

View File

@ -1,28 +1,26 @@
using System; namespace Ocelot.AcceptanceTests
{
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Net; using System.Net;
using System.Net.Http; using System.Net.Http;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Ocelot.Configuration.File; using Ocelot.Configuration.File;
using Shouldly; using Shouldly;
using TestStack.BDDfy; using TestStack.BDDfy;
using Xunit; using Xunit;
namespace Ocelot.AcceptanceTests public class HttpDelegatingHandlersTests : IDisposable
{ {
public class HttpDelegatingHandlersTests
{
private IWebHost _builder;
private readonly Steps _steps; private readonly Steps _steps;
private string _downstreamPath; private string _downstreamPath;
private readonly ServiceHandler _serviceHandler;
public HttpDelegatingHandlersTests() public HttpDelegatingHandlersTests()
{ {
_serviceHandler = new ServiceHandler();
_steps = new Steps(); _steps = new Steps();
} }
@ -249,6 +247,7 @@ namespace Ocelot.AcceptanceTests
return base.SendAsync(request, cancellationToken); return base.SendAsync(request, cancellationToken);
} }
} }
// ReSharper disable once ClassNeverInstantiated.Local // ReSharper disable once ClassNeverInstantiated.Local
private class FakeHandlerAgain : DelegatingHandler private class FakeHandlerAgain : DelegatingHandler
{ {
@ -263,15 +262,7 @@ namespace Ocelot.AcceptanceTests
private void GivenThereIsAServiceRunningOn(string baseUrl, string basePath, int statusCode, string responseBody) private void GivenThereIsAServiceRunningOn(string baseUrl, string basePath, int statusCode, string responseBody)
{ {
_builder = new WebHostBuilder() _serviceHandler.GivenThereIsAServiceRunningOn(baseUrl, basePath, async context =>
.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; _downstreamPath = !string.IsNullOrEmpty(context.Request.PathBase.Value) ? context.Request.PathBase.Value : context.Request.Path.Value;
@ -286,10 +277,12 @@ namespace Ocelot.AcceptanceTests
await context.Response.WriteAsync(responseBody); await context.Response.WriteAsync(responseBody);
} }
}); });
}) }
.Build();
_builder.Start(); public void Dispose()
{
_steps?.Dispose();
_serviceHandler?.Dispose();
} }
} }
} }

View File

@ -1,8 +1,7 @@
namespace Ocelot.AcceptanceTests
{
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Ocelot.Configuration.File; using Ocelot.Configuration.File;
using Ocelot.LoadBalancer.LoadBalancers; using Ocelot.LoadBalancer.LoadBalancers;
@ -10,27 +9,28 @@ using Shouldly;
using TestStack.BDDfy; using TestStack.BDDfy;
using Xunit; using Xunit;
namespace Ocelot.AcceptanceTests
{
public class LoadBalancerTests : IDisposable public class LoadBalancerTests : IDisposable
{ {
private IWebHost _builderOne;
private IWebHost _builderTwo;
private readonly Steps _steps; private readonly Steps _steps;
private int _counterOne; private int _counterOne;
private int _counterTwo; private int _counterTwo;
private static readonly object _syncLock = new object(); private static readonly object _syncLock = new object();
private readonly ServiceHandler _serviceHandler;
public LoadBalancerTests() public LoadBalancerTests()
{ {
_serviceHandler = new ServiceHandler();
_steps = new Steps(); _steps = new Steps();
} }
[Fact] [Fact]
public void should_load_balance_request_with_least_connection() public void should_load_balance_request_with_least_connection()
{ {
var downstreamServiceOneUrl = "http://localhost:50881"; int portOne = 50591;
var downstreamServiceTwoUrl = "http://localhost:50892"; int portTwo = 51482;
var downstreamServiceOneUrl = $"http://localhost:{portOne}";
var downstreamServiceTwoUrl = $"http://localhost:{portTwo}";
var configuration = new FileConfiguration var configuration = new FileConfiguration
{ {
@ -48,12 +48,12 @@ namespace Ocelot.AcceptanceTests
new FileHostAndPort new FileHostAndPort
{ {
Host = "localhost", Host = "localhost",
Port = 50881 Port = portOne
}, },
new FileHostAndPort new FileHostAndPort
{ {
Host = "localhost", Host = "localhost",
Port = 50892 Port = portTwo
} }
} }
} }
@ -76,8 +76,8 @@ namespace Ocelot.AcceptanceTests
[Fact] [Fact]
public void should_load_balance_request_with_round_robin() public void should_load_balance_request_with_round_robin()
{ {
var downstreamPortOne = 51881; var downstreamPortOne = 51701;
var downstreamPortTwo = 51892; var downstreamPortTwo = 53802;
var downstreamServiceOneUrl = $"http://localhost:{downstreamPortOne}"; var downstreamServiceOneUrl = $"http://localhost:{downstreamPortOne}";
var downstreamServiceTwoUrl = $"http://localhost:{downstreamPortTwo}"; var downstreamServiceTwoUrl = $"http://localhost:{downstreamPortTwo}";
@ -136,15 +136,7 @@ namespace Ocelot.AcceptanceTests
private void GivenProductServiceOneIsRunning(string url, int statusCode) private void GivenProductServiceOneIsRunning(string url, int statusCode)
{ {
_builderOne = new WebHostBuilder() _serviceHandler.GivenThereIsAServiceRunningOn(url, async context =>
.UseUrls(url)
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseUrls(url)
.Configure(app =>
{
app.Run(async context =>
{ {
try try
{ {
@ -154,6 +146,7 @@ namespace Ocelot.AcceptanceTests
_counterOne++; _counterOne++;
response = _counterOne.ToString(); response = _counterOne.ToString();
} }
context.Response.StatusCode = statusCode; context.Response.StatusCode = statusCode;
await context.Response.WriteAsync(response); await context.Response.WriteAsync(response);
} }
@ -162,23 +155,11 @@ namespace Ocelot.AcceptanceTests
await context.Response.WriteAsync(exception.StackTrace); await context.Response.WriteAsync(exception.StackTrace);
} }
}); });
})
.Build();
_builderOne.Start();
} }
private void GivenProductServiceTwoIsRunning(string url, int statusCode) private void GivenProductServiceTwoIsRunning(string url, int statusCode)
{ {
_builderTwo = new WebHostBuilder() _serviceHandler.GivenThereIsAServiceRunningOn(url, async context =>
.UseUrls(url)
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseUrls(url)
.Configure(app =>
{
app.Run(async context =>
{ {
try try
{ {
@ -192,21 +173,16 @@ namespace Ocelot.AcceptanceTests
context.Response.StatusCode = statusCode; context.Response.StatusCode = statusCode;
await context.Response.WriteAsync(response); await context.Response.WriteAsync(response);
} }
catch (System.Exception exception) catch (Exception exception)
{ {
await context.Response.WriteAsync(exception.StackTrace); await context.Response.WriteAsync(exception.StackTrace);
} }
}); });
})
.Build();
_builderTwo.Start();
} }
public void Dispose() public void Dispose()
{ {
_builderOne?.Dispose(); _serviceHandler?.Dispose();
_builderTwo?.Dispose();
_steps.Dispose(); _steps.Dispose();
} }
} }

View File

@ -1,27 +1,24 @@
using System; namespace Ocelot.AcceptanceTests
{
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Net; using System.Net;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
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
{
public class QoSTests : IDisposable public class QoSTests : IDisposable
{ {
private IWebHost _brokenService;
private readonly Steps _steps; private readonly Steps _steps;
private int _requestCount; private int _requestCount;
private IWebHost _workingService; private readonly ServiceHandler _serviceHandler;
public QoSTests() public QoSTests()
{ {
_serviceHandler = new ServiceHandler();
_steps = new Steps(); _steps = new Steps();
} }
@ -227,15 +224,7 @@ namespace Ocelot.AcceptanceTests
private void GivenThereIsAPossiblyBrokenServiceRunningOn(string url, string responseBody) private void GivenThereIsAPossiblyBrokenServiceRunningOn(string url, string responseBody)
{ {
_brokenService = new WebHostBuilder() _serviceHandler.GivenThereIsAServiceRunningOn(url, async context =>
.UseUrls(url)
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseUrls(url)
.Configure(app =>
{
app.Run(async context =>
{ {
//circuit starts closed //circuit starts closed
if (_requestCount == 0) if (_requestCount == 0)
@ -260,41 +249,23 @@ namespace Ocelot.AcceptanceTests
{ {
context.Response.StatusCode = 200; context.Response.StatusCode = 200;
await context.Response.WriteAsync(responseBody); await context.Response.WriteAsync(responseBody);
return;
} }
}); });
})
.Build();
_brokenService.Start();
} }
private void GivenThereIsAServiceRunningOn(string url, int statusCode, string responseBody, int timeout) private void GivenThereIsAServiceRunningOn(string url, int statusCode, string responseBody, int timeout)
{ {
_workingService = new WebHostBuilder() _serviceHandler.GivenThereIsAServiceRunningOn(url, async context =>
.UseUrls(url)
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseUrls(url)
.Configure(app =>
{
app.Run(async context =>
{ {
Thread.Sleep(timeout); Thread.Sleep(timeout);
context.Response.StatusCode = statusCode; context.Response.StatusCode = statusCode;
await context.Response.WriteAsync(responseBody); await context.Response.WriteAsync(responseBody);
}); });
})
.Build();
_workingService.Start();
} }
public void Dispose() public void Dispose()
{ {
_workingService?.Dispose(); _serviceHandler?.Dispose();
_brokenService?.Dispose();
_steps.Dispose(); _steps.Dispose();
} }
} }

View File

@ -1,26 +1,21 @@
using System; namespace Ocelot.AcceptanceTests
{
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Linq; using System.Linq;
using System.Net;
using System.Threading.Tasks; 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 Ocelot.Configuration.File;
using TestStack.BDDfy; using TestStack.BDDfy;
using Xunit; using Xunit;
namespace Ocelot.AcceptanceTests
{
public class RequestIdTests : IDisposable public class RequestIdTests : IDisposable
{ {
private IWebHost _builder;
private readonly Steps _steps; private readonly Steps _steps;
private readonly ServiceHandler _serviceHandler;
public RequestIdTests() public RequestIdTests()
{ {
_serviceHandler = new ServiceHandler();
_steps = new Steps(); _steps = new Steps();
} }
@ -171,30 +166,17 @@ namespace Ocelot.AcceptanceTests
private void GivenThereIsAServiceRunningOn(string url) private void GivenThereIsAServiceRunningOn(string url)
{ {
_builder = new WebHostBuilder() _serviceHandler.GivenThereIsAServiceRunningOn(url, context =>
.UseUrls(url)
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseUrls(url)
.Configure(app =>
{ {
app.Run(context => context.Request.Headers.TryGetValue(_steps.RequestIdKey, out var requestId);
{
StringValues requestId;
context.Request.Headers.TryGetValue(_steps.RequestIdKey, out requestId);
context.Response.Headers.Add(_steps.RequestIdKey, requestId.First()); context.Response.Headers.Add(_steps.RequestIdKey, requestId.First());
return Task.CompletedTask; return Task.CompletedTask;
}); });
})
.Build();
_builder.Start();
} }
public void Dispose() public void Dispose()
{ {
_builder?.Dispose(); _serviceHandler?.Dispose();
_steps.Dispose(); _steps.Dispose();
} }
} }

View File

@ -1,25 +1,20 @@
namespace Ocelot.AcceptanceTests
{
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Net; using System.Net;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Ocelot.Configuration.File; using Ocelot.Configuration.File;
using Shouldly;
using TestStack.BDDfy; using TestStack.BDDfy;
using Xunit; using Xunit;
namespace Ocelot.AcceptanceTests
{
public class ResponseCodeTests : IDisposable public class ResponseCodeTests : IDisposable
{ {
private IWebHost _builder;
private readonly Steps _steps; private readonly Steps _steps;
private string _downstreamPath; private readonly ServiceHandler _serviceHandler;
public ResponseCodeTests() public ResponseCodeTests()
{ {
_serviceHandler = new ServiceHandler();
_steps = new Steps(); _steps = new Steps();
} }
@ -58,26 +53,15 @@ namespace Ocelot.AcceptanceTests
private void GivenThereIsAServiceRunningOn(string baseUrl, string basePath, int statusCode) private void GivenThereIsAServiceRunningOn(string baseUrl, string basePath, int statusCode)
{ {
_builder = new WebHostBuilder() _serviceHandler.GivenThereIsAServiceRunningOn(baseUrl, basePath, async context =>
.UseUrls(baseUrl)
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.Configure(app =>
{
app.UsePathBase(basePath);
app.Run(async context =>
{ {
context.Response.StatusCode = statusCode; context.Response.StatusCode = statusCode;
}); });
})
.Build();
_builder.Start();
} }
public void Dispose() public void Dispose()
{ {
_builder?.Dispose(); _serviceHandler?.Dispose();
_steps.Dispose(); _steps.Dispose();
} }
} }

View File

@ -1,22 +1,20 @@
using System; namespace Ocelot.AcceptanceTests
{
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Net; using System.Net;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Ocelot.Configuration.File; using Ocelot.Configuration.File;
using TestStack.BDDfy; using TestStack.BDDfy;
using Xunit; using Xunit;
namespace Ocelot.AcceptanceTests
{
public class ReturnsErrorTests : IDisposable public class ReturnsErrorTests : IDisposable
{ {
private IWebHost _servicebuilder;
private readonly Steps _steps; private readonly Steps _steps;
private readonly ServiceHandler _serviceHandler;
public ReturnsErrorTests() public ReturnsErrorTests()
{ {
_serviceHandler = new ServiceHandler();
_steps = new Steps(); _steps = new Steps();
} }
@ -55,27 +53,12 @@ namespace Ocelot.AcceptanceTests
private void GivenThereIsAServiceRunningOn(string url) private void GivenThereIsAServiceRunningOn(string url)
{ {
_servicebuilder = new WebHostBuilder() _serviceHandler.GivenThereIsAServiceRunningOn(url, context => throw new Exception("BLAMMMM"));
.UseUrls(url)
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseUrls(url)
.Configure(app =>
{
app.Run(context =>
{
throw new Exception("BLAMMMM");
});
})
.Build();
_servicebuilder.Start();
} }
public void Dispose() public void Dispose()
{ {
_servicebuilder?.Dispose(); _serviceHandler?.Dispose();
_steps.Dispose(); _steps.Dispose();
} }
} }

View File

@ -1,25 +1,23 @@
namespace Ocelot.AcceptanceTests
{
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Net; using System.Net;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Ocelot.Configuration.File; using Ocelot.Configuration.File;
using Shouldly; using Shouldly;
using TestStack.BDDfy; using TestStack.BDDfy;
using Xunit; using Xunit;
namespace Ocelot.AcceptanceTests
{
public class RoutingTests : IDisposable public class RoutingTests : IDisposable
{ {
private IWebHost _builder;
private readonly Steps _steps; private readonly Steps _steps;
private string _downstreamPath; private string _downstreamPath;
private readonly ServiceHandler _serviceHandler;
public RoutingTests() public RoutingTests()
{ {
_serviceHandler = new ServiceHandler();
_steps = new Steps(); _steps = new Steps();
} }
@ -973,15 +971,7 @@ namespace Ocelot.AcceptanceTests
private void GivenThereIsAServiceRunningOn(string baseUrl, string basePath, int statusCode, string responseBody) private void GivenThereIsAServiceRunningOn(string baseUrl, string basePath, int statusCode, string responseBody)
{ {
_builder = new WebHostBuilder() _serviceHandler.GivenThereIsAServiceRunningOn(baseUrl, basePath, async context =>
.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; _downstreamPath = !string.IsNullOrEmpty(context.Request.PathBase.Value) ? context.Request.PathBase.Value : context.Request.Path.Value;
@ -996,10 +986,6 @@ namespace Ocelot.AcceptanceTests
await context.Response.WriteAsync(responseBody); await context.Response.WriteAsync(responseBody);
} }
}); });
})
.Build();
_builder.Start();
} }
internal void ThenTheDownstreamUrlPathShouldBe(string expectedDownstreamPath) internal void ThenTheDownstreamUrlPathShouldBe(string expectedDownstreamPath)
@ -1009,7 +995,7 @@ namespace Ocelot.AcceptanceTests
public void Dispose() public void Dispose()
{ {
_builder?.Dispose(); _serviceHandler.Dispose();
_steps.Dispose(); _steps.Dispose();
} }
} }

View File

@ -1,25 +1,21 @@
namespace Ocelot.AcceptanceTests
{
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Net; using System.Net;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Ocelot.Configuration.File; using Ocelot.Configuration.File;
using Shouldly;
using TestStack.BDDfy; using TestStack.BDDfy;
using Xunit; using Xunit;
namespace Ocelot.AcceptanceTests
{
public class RoutingWithQueryStringTests : IDisposable public class RoutingWithQueryStringTests : IDisposable
{ {
private IWebHost _builder;
private readonly Steps _steps; private readonly Steps _steps;
private string _downstreamPath; private readonly ServiceHandler _serviceHandler;
public RoutingWithQueryStringTests() public RoutingWithQueryStringTests()
{ {
_serviceHandler = new ServiceHandler();
_steps = new Steps(); _steps = new Steps();
} }
@ -208,15 +204,7 @@ namespace Ocelot.AcceptanceTests
private void GivenThereIsAServiceRunningOn(string baseUrl, string basePath, string queryString, int statusCode, string responseBody) private void GivenThereIsAServiceRunningOn(string baseUrl, string basePath, string queryString, int statusCode, string responseBody)
{ {
_builder = new WebHostBuilder() _serviceHandler.GivenThereIsAServiceRunningOn(baseUrl, basePath, async context =>
.UseUrls(baseUrl)
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.Configure(app =>
{
app.UsePathBase(basePath);
app.Run(async context =>
{ {
if (context.Request.PathBase.Value != basePath || context.Request.QueryString.Value != queryString) if (context.Request.PathBase.Value != basePath || context.Request.QueryString.Value != queryString)
{ {
@ -229,20 +217,11 @@ namespace Ocelot.AcceptanceTests
await context.Response.WriteAsync(responseBody); await context.Response.WriteAsync(responseBody);
} }
}); });
})
.Build();
_builder.Start();
}
internal void ThenTheDownstreamUrlPathShouldBe(string expectedDownstreamPath)
{
_downstreamPath.ShouldBe(expectedDownstreamPath);
} }
public void Dispose() public void Dispose()
{ {
_builder?.Dispose(); _serviceHandler?.Dispose();
_steps.Dispose(); _steps.Dispose();
} }
} }

View File

@ -2,12 +2,9 @@ namespace Ocelot.AcceptanceTests
{ {
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Linq; using System.Linq;
using System.Net; using System.Net;
using Consul; using Consul;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Ocelot.Configuration.File; using Ocelot.Configuration.File;
using Shouldly; using Shouldly;
@ -18,21 +15,19 @@ namespace Ocelot.AcceptanceTests
public class ServiceDiscoveryTests : IDisposable public class ServiceDiscoveryTests : IDisposable
{ {
private IWebHost _builderOne;
private IWebHost _builderTwo;
private IWebHost _fakeConsulBuilder;
private readonly Steps _steps; private readonly Steps _steps;
private readonly List<ServiceEntry> _consulServices; private readonly List<ServiceEntry> _consulServices;
private readonly List<IServiceInstance> _eurekaInstances; private readonly List<IServiceInstance> _eurekaInstances;
private int _counterOne; private int _counterOne;
private int _counterTwo; private int _counterTwo;
private static readonly object SyncLock = new object(); private static readonly object SyncLock = new object();
private IWebHost _builder;
private string _downstreamPath; private string _downstreamPath;
private string _receivedToken; private string _receivedToken;
private readonly ServiceHandler _serviceHandler;
public ServiceDiscoveryTests() public ServiceDiscoveryTests()
{ {
_serviceHandler = new ServiceHandler();
_steps = new Steps(); _steps = new Steps();
_consulServices = new List<ServiceEntry>(); _consulServices = new List<ServiceEntry>();
_eurekaInstances = new List<IServiceInstance>(); _eurekaInstances = new List<IServiceInstance>();
@ -74,7 +69,7 @@ namespace Ocelot.AcceptanceTests
} }
}; };
this.Given(x => x.GivenEurekaProductServiceOneIsRunning(downstreamServiceOneUrl, 200)) this.Given(x => x.GivenEurekaProductServiceOneIsRunning(downstreamServiceOneUrl))
.And(x => x.GivenThereIsAFakeEurekaServiceDiscoveryProvider(fakeEurekaServiceDiscoveryUrl, serviceName)) .And(x => x.GivenThereIsAFakeEurekaServiceDiscoveryProvider(fakeEurekaServiceDiscoveryUrl, serviceName))
.And(x => x.GivenTheServicesAreRegisteredWithEureka(instanceOne)) .And(x => x.GivenTheServicesAreRegisteredWithEureka(instanceOne))
.And(x => _steps.GivenThereIsAConfiguration(configuration)) .And(x => _steps.GivenThereIsAConfiguration(configuration))
@ -568,15 +563,7 @@ namespace Ocelot.AcceptanceTests
private void GivenThereIsAFakeEurekaServiceDiscoveryProvider(string url, string serviceName) private void GivenThereIsAFakeEurekaServiceDiscoveryProvider(string url, string serviceName)
{ {
_fakeConsulBuilder = new WebHostBuilder() _serviceHandler.GivenThereIsAServiceRunningOn(url, async context =>
.UseUrls(url)
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseUrls(url)
.Configure(app =>
{
app.Run(async context =>
{ {
if (context.Request.Path.Value == "/eureka/apps/") if (context.Request.Path.Value == "/eureka/apps/")
{ {
@ -642,23 +629,11 @@ namespace Ocelot.AcceptanceTests
await context.Response.WriteJsonAsync(applications); await context.Response.WriteJsonAsync(applications);
} }
}); });
})
.Build();
_fakeConsulBuilder.Start();
} }
private void GivenThereIsAFakeConsulServiceDiscoveryProvider(string url, string serviceName) private void GivenThereIsAFakeConsulServiceDiscoveryProvider(string url, string serviceName)
{ {
_fakeConsulBuilder = new WebHostBuilder() _serviceHandler.GivenThereIsAServiceRunningOn(url, async context =>
.UseUrls(url)
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseUrls(url)
.Configure(app =>
{
app.Run(async context =>
{ {
if (context.Request.Path.Value == $"/v1/health/service/{serviceName}") if (context.Request.Path.Value == $"/v1/health/service/{serviceName}")
{ {
@ -670,23 +645,11 @@ namespace Ocelot.AcceptanceTests
await context.Response.WriteJsonAsync(_consulServices); await context.Response.WriteJsonAsync(_consulServices);
} }
}); });
})
.Build();
_fakeConsulBuilder.Start();
} }
private void GivenProductServiceOneIsRunning(string url, int statusCode) private void GivenProductServiceOneIsRunning(string url, int statusCode)
{ {
_builderOne = new WebHostBuilder() _serviceHandler.GivenThereIsAServiceRunningOn(url, async context =>
.UseUrls(url)
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseUrls(url)
.Configure(app =>
{
app.Run(async context =>
{ {
try try
{ {
@ -696,6 +659,7 @@ namespace Ocelot.AcceptanceTests
_counterOne++; _counterOne++;
response = _counterOne.ToString(); response = _counterOne.ToString();
} }
context.Response.StatusCode = statusCode; context.Response.StatusCode = statusCode;
await context.Response.WriteAsync(response); await context.Response.WriteAsync(response);
} }
@ -704,23 +668,11 @@ namespace Ocelot.AcceptanceTests
await context.Response.WriteAsync(exception.StackTrace); await context.Response.WriteAsync(exception.StackTrace);
} }
}); });
})
.Build();
_builderOne.Start();
} }
private void GivenProductServiceTwoIsRunning(string url, int statusCode) private void GivenProductServiceTwoIsRunning(string url, int statusCode)
{ {
_builderTwo = new WebHostBuilder() _serviceHandler.GivenThereIsAServiceRunningOn(url, async context =>
.UseUrls(url)
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseUrls(url)
.Configure(app =>
{
app.Run(async context =>
{ {
try try
{ {
@ -739,23 +691,11 @@ namespace Ocelot.AcceptanceTests
await context.Response.WriteAsync(exception.StackTrace); await context.Response.WriteAsync(exception.StackTrace);
} }
}); });
})
.Build();
_builderTwo.Start();
} }
private void GivenEurekaProductServiceOneIsRunning(string url, int statusCode) private void GivenEurekaProductServiceOneIsRunning(string url)
{ {
_builderOne = new WebHostBuilder() _serviceHandler.GivenThereIsAServiceRunningOn(url, async context =>
.UseUrls(url)
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseUrls(url)
.Configure(app =>
{
app.Run(async context =>
{ {
try try
{ {
@ -767,23 +707,11 @@ namespace Ocelot.AcceptanceTests
await context.Response.WriteAsync(exception.StackTrace); await context.Response.WriteAsync(exception.StackTrace);
} }
}); });
})
.Build();
_builderOne.Start();
} }
private void GivenThereIsAServiceRunningOn(string baseUrl, string basePath, int statusCode, string responseBody) private void GivenThereIsAServiceRunningOn(string baseUrl, string basePath, int statusCode, string responseBody)
{ {
_builder = new WebHostBuilder() _serviceHandler.GivenThereIsAServiceRunningOn(baseUrl, basePath, async context =>
.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; _downstreamPath = !string.IsNullOrEmpty(context.Request.PathBase.Value) ? context.Request.PathBase.Value : context.Request.Path.Value;
@ -798,16 +726,11 @@ namespace Ocelot.AcceptanceTests
await context.Response.WriteAsync(responseBody); await context.Response.WriteAsync(responseBody);
} }
}); });
})
.Build();
_builder.Start();
} }
public void Dispose() public void Dispose()
{ {
_builderOne?.Dispose(); _serviceHandler?.Dispose();
_builderTwo?.Dispose();
_steps.Dispose(); _steps.Dispose();
} }
} }

View File

@ -1,28 +1,22 @@
using System.Linq;
using Microsoft.Extensions.Primitives;
namespace Ocelot.AcceptanceTests namespace Ocelot.AcceptanceTests
{ {
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Net; using System.Net;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Ocelot.Configuration.File; using Ocelot.Configuration.File;
using Shouldly;
using TestStack.BDDfy; using TestStack.BDDfy;
using Xunit; using Xunit;
public class ServiceFabricTests : IDisposable public class ServiceFabricTests : IDisposable
{ {
private IWebHost _builder;
private readonly Steps _steps; private readonly Steps _steps;
private string _downstreamPath; private string _downstreamPath;
private readonly ServiceHandler _serviceHandler;
public ServiceFabricTests() public ServiceFabricTests()
{ {
_serviceHandler = new ServiceHandler();
_steps = new Steps(); _steps = new Steps();
} }
@ -102,15 +96,7 @@ namespace Ocelot.AcceptanceTests
private void GivenThereIsAServiceRunningOn(string baseUrl, string basePath, int statusCode, string responseBody, string expectedQueryString) private void GivenThereIsAServiceRunningOn(string baseUrl, string basePath, int statusCode, string responseBody, string expectedQueryString)
{ {
_builder = new WebHostBuilder() _serviceHandler.GivenThereIsAServiceRunningOn(baseUrl, basePath, async context =>
.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; _downstreamPath = !string.IsNullOrEmpty(context.Request.PathBase.Value) ? context.Request.PathBase.Value : context.Request.Path.Value;
@ -133,15 +119,11 @@ namespace Ocelot.AcceptanceTests
} }
} }
}); });
})
.Build();
_builder.Start();
} }
public void Dispose() public void Dispose()
{ {
_builder?.Dispose(); _serviceHandler?.Dispose();
_steps.Dispose(); _steps.Dispose();
} }
} }

View File

@ -0,0 +1,108 @@
namespace Ocelot.AcceptanceTests
{
using System;
using System.IO;
using System.Net;
using System.Net.WebSockets;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
public class ServiceHandler : IDisposable
{
private IWebHost _builder;
public void GivenThereIsAServiceRunningOn(string baseUrl, RequestDelegate del)
{
_builder = new WebHostBuilder()
.UseUrls(baseUrl)
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.Configure(app =>
{
app.Run(del);
})
.Build();
_builder.Start();
}
public void GivenThereIsAServiceRunningOn(string baseUrl, string basePath, RequestDelegate del)
{
_builder = new WebHostBuilder()
.UseUrls(baseUrl)
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.Configure(app =>
{
app.UsePathBase(basePath);
app.Run(del);
})
.Build();
_builder.Start();
}
public void GivenThereIsAServiceRunningOn(string baseUrl, string basePath, string fileName, string password, int port, RequestDelegate del)
{
_builder = new WebHostBuilder()
.UseUrls(baseUrl)
.UseKestrel(options =>
{
options.Listen(IPAddress.Loopback, port, listenOptions =>
{
listenOptions.UseHttps(fileName, password);
});
})
.UseContentRoot(Directory.GetCurrentDirectory())
.Configure(app =>
{
app.UsePathBase(basePath);
app.Run(del);
})
.Build();
_builder.Start();
}
public async Task StartFakeDownstreamService(string url, string path, Func<HttpContext, Func<Task>, Task> middleware)
{
_builder = new WebHostBuilder()
.ConfigureServices(s => { }).UseKestrel()
.UseUrls(url)
.UseContentRoot(Directory.GetCurrentDirectory())
.ConfigureAppConfiguration((hostingContext, config) =>
{
config.SetBasePath(hostingContext.HostingEnvironment.ContentRootPath);
var env = hostingContext.HostingEnvironment;
config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: false)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: false);
config.AddEnvironmentVariables();
})
.ConfigureLogging((hostingContext, logging) =>
{
logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
logging.AddConsole();
})
.Configure(app =>
{
app.UseWebSockets();
app.Use(middleware);
})
.UseIISIntegration()
.Build();
await _builder.StartAsync();
}
public void Dispose()
{
_builder?.Dispose();
}
}
}

View File

@ -1,25 +1,22 @@
namespace Ocelot.AcceptanceTests
{
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Net; using System.Net;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Ocelot.Configuration.File; using Ocelot.Configuration.File;
using Shouldly;
using TestStack.BDDfy; using TestStack.BDDfy;
using Xunit; using Xunit;
namespace Ocelot.AcceptanceTests
{
public class SslTests : IDisposable public class SslTests : IDisposable
{ {
private IWebHost _builder;
private readonly Steps _steps; private readonly Steps _steps;
private string _downstreamPath; private string _downstreamPath;
private readonly ServiceHandler _serviceHandler;
public SslTests() public SslTests()
{ {
_serviceHandler = new ServiceHandler();
_steps = new Steps(); _steps = new Steps();
} }
@ -98,20 +95,7 @@ namespace Ocelot.AcceptanceTests
private void GivenThereIsAServiceRunningOn(string baseUrl, string basePath, int statusCode, string responseBody, int port) private void GivenThereIsAServiceRunningOn(string baseUrl, string basePath, int statusCode, string responseBody, int port)
{ {
_builder = new WebHostBuilder() _serviceHandler.GivenThereIsAServiceRunningOn(baseUrl, basePath, "idsrv3test.pfx", "idsrv3test", port, async context =>
.UseUrls(baseUrl)
.UseKestrel(options =>
{
options.Listen(IPAddress.Loopback, port, listenOptions =>
{
listenOptions.UseHttps("idsrv3test.pfx", "idsrv3test");
});
})
.UseContentRoot(Directory.GetCurrentDirectory())
.Configure(app =>
{
app.UsePathBase(basePath);
app.Run(async context =>
{ {
_downstreamPath = !string.IsNullOrEmpty(context.Request.PathBase.Value) ? context.Request.PathBase.Value : context.Request.Path.Value; _downstreamPath = !string.IsNullOrEmpty(context.Request.PathBase.Value) ? context.Request.PathBase.Value : context.Request.Path.Value;
@ -126,20 +110,11 @@ namespace Ocelot.AcceptanceTests
await context.Response.WriteAsync(responseBody); await context.Response.WriteAsync(responseBody);
} }
}); });
})
.Build();
_builder.Start();
}
internal void ThenTheDownstreamUrlPathShouldBe(string expectedDownstreamPath)
{
_downstreamPath.ShouldBe(expectedDownstreamPath);
} }
public void Dispose() public void Dispose()
{ {
_builder?.Dispose(); _serviceHandler?.Dispose();
_steps.Dispose(); _steps.Dispose();
} }
} }

View File

@ -1,34 +1,31 @@
namespace Ocelot.AcceptanceTests
{
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Ocelot.Configuration.File; using Ocelot.Configuration.File;
using Shouldly; using Shouldly;
using TestStack.BDDfy; using TestStack.BDDfy;
using Xunit; using Xunit;
namespace Ocelot.AcceptanceTests
{
public class StickySessionsTests : IDisposable public class StickySessionsTests : IDisposable
{ {
private IWebHost _builderOne;
private IWebHost _builderTwo;
private readonly Steps _steps; private readonly Steps _steps;
private int _counterOne; private int _counterOne;
private int _counterTwo; private int _counterTwo;
private static readonly object _syncLock = new object(); private static readonly object SyncLock = new object();
private readonly ServiceHandler _serviceHandler;
public StickySessionsTests() public StickySessionsTests()
{ {
_serviceHandler = new ServiceHandler();
_steps = new Steps(); _steps = new Steps();
} }
[Fact] [Fact]
public void should_use_same_downstream_host() public void should_use_same_downstream_host()
{ {
var downstreamPortOne = 51881; var downstreamPortOne = 51375;
var downstreamPortTwo = 51892; var downstreamPortTwo = 51892;
var downstreamServiceOneUrl = $"http://localhost:{downstreamPortOne}"; var downstreamServiceOneUrl = $"http://localhost:{downstreamPortOne}";
var downstreamServiceTwoUrl = $"http://localhost:{downstreamPortTwo}"; var downstreamServiceTwoUrl = $"http://localhost:{downstreamPortTwo}";
@ -244,20 +241,12 @@ namespace Ocelot.AcceptanceTests
private void GivenProductServiceOneIsRunning(string url, int statusCode) private void GivenProductServiceOneIsRunning(string url, int statusCode)
{ {
_builderOne = new WebHostBuilder() _serviceHandler.GivenThereIsAServiceRunningOn(url, async context =>
.UseUrls(url)
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseUrls(url)
.Configure(app =>
{
app.Run(async context =>
{ {
try try
{ {
var response = string.Empty; var response = string.Empty;
lock (_syncLock) lock (SyncLock)
{ {
_counterOne++; _counterOne++;
response = _counterOne.ToString(); response = _counterOne.ToString();
@ -270,28 +259,16 @@ namespace Ocelot.AcceptanceTests
await context.Response.WriteAsync(exception.StackTrace); await context.Response.WriteAsync(exception.StackTrace);
} }
}); });
})
.Build();
_builderOne.Start();
} }
private void GivenProductServiceTwoIsRunning(string url, int statusCode) private void GivenProductServiceTwoIsRunning(string url, int statusCode)
{ {
_builderTwo = new WebHostBuilder() _serviceHandler.GivenThereIsAServiceRunningOn(url, async context =>
.UseUrls(url)
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseUrls(url)
.Configure(app =>
{
app.Run(async context =>
{ {
try try
{ {
var response = string.Empty; var response = string.Empty;
lock (_syncLock) lock (SyncLock)
{ {
_counterTwo++; _counterTwo++;
response = _counterTwo.ToString(); response = _counterTwo.ToString();
@ -300,21 +277,16 @@ namespace Ocelot.AcceptanceTests
context.Response.StatusCode = statusCode; context.Response.StatusCode = statusCode;
await context.Response.WriteAsync(response); await context.Response.WriteAsync(response);
} }
catch (System.Exception exception) catch (Exception exception)
{ {
await context.Response.WriteAsync(exception.StackTrace); await context.Response.WriteAsync(exception.StackTrace);
} }
}); });
})
.Build();
_builderTwo.Start();
} }
public void Dispose() public void Dispose()
{ {
_builderOne?.Dispose(); _serviceHandler?.Dispose();
_builderTwo?.Dispose();
_steps.Dispose(); _steps.Dispose();
} }
} }

View File

@ -1,30 +1,25 @@
namespace Ocelot.AcceptanceTests
{
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Net; using System.Net;
using Consul; using Consul;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Ocelot.Configuration.File; using Ocelot.Configuration.File;
using Shouldly;
using TestStack.BDDfy; using TestStack.BDDfy;
using Xunit; using Xunit;
namespace Ocelot.AcceptanceTests
{
public class TwoDownstreamServicesTests : IDisposable public class TwoDownstreamServicesTests : IDisposable
{ {
private IWebHost _builderOne;
private IWebHost _builderTwo;
private IWebHost _fakeConsulBuilder;
private readonly Steps _steps; private readonly Steps _steps;
private readonly List<ServiceEntry> _serviceEntries; private readonly List<ServiceEntry> _serviceEntries;
private string _downstreamPathOne; private string _downstreamPathOne;
private string _downstreamPathTwo; private string _downstreamPathTwo;
private readonly ServiceHandler _serviceHandler;
public TwoDownstreamServicesTests() public TwoDownstreamServicesTests()
{ {
_serviceHandler = new ServiceHandler();
_steps = new Steps(); _steps = new Steps();
_serviceEntries = new List<ServiceEntry>(); _serviceEntries = new List<ServiceEntry>();
} }
@ -98,40 +93,22 @@ namespace Ocelot.AcceptanceTests
private void GivenThereIsAFakeConsulServiceDiscoveryProvider(string url) private void GivenThereIsAFakeConsulServiceDiscoveryProvider(string url)
{ {
_fakeConsulBuilder = new WebHostBuilder() _serviceHandler.GivenThereIsAServiceRunningOn(url, async context =>
.UseUrls(url)
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseUrls(url)
.Configure(app =>
{
app.Run(async context =>
{ {
if (context.Request.Path.Value == "/v1/health/service/product") if (context.Request.Path.Value == "/v1/health/service/product")
{ {
await context.Response.WriteJsonAsync(_serviceEntries); await context.Response.WriteJsonAsync(_serviceEntries);
} }
}); });
})
.Build();
_fakeConsulBuilder.Start();
} }
private void GivenProductServiceOneIsRunning(string baseUrl, string basePath, int statusCode, string responseBody) private void GivenProductServiceOneIsRunning(string baseUrl, string basePath, int statusCode, string responseBody)
{ {
_builderOne = new WebHostBuilder() _serviceHandler.GivenThereIsAServiceRunningOn(baseUrl, basePath, async context =>
.UseUrls(baseUrl)
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.Configure(app =>
{ {
app.UsePathBase(basePath); _downstreamPathOne = !string.IsNullOrEmpty(context.Request.PathBase.Value)
app.Run(async context => ? context.Request.PathBase.Value
{ : context.Request.Path.Value;
_downstreamPathOne = !string.IsNullOrEmpty(context.Request.PathBase.Value) ? context.Request.PathBase.Value : context.Request.Path.Value;
if (_downstreamPathOne != basePath) if (_downstreamPathOne != basePath)
{ {
@ -144,23 +121,11 @@ namespace Ocelot.AcceptanceTests
await context.Response.WriteAsync(responseBody); await context.Response.WriteAsync(responseBody);
} }
}); });
})
.Build();
_builderOne.Start();
} }
private void GivenProductServiceTwoIsRunning(string baseUrl, string basePath, int statusCode, string responseBody) private void GivenProductServiceTwoIsRunning(string baseUrl, string basePath, int statusCode, string responseBody)
{ {
_builderTwo = new WebHostBuilder() _serviceHandler.GivenThereIsAServiceRunningOn(baseUrl, basePath, async context =>
.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; _downstreamPathTwo = !string.IsNullOrEmpty(context.Request.PathBase.Value) ? context.Request.PathBase.Value : context.Request.Path.Value;
@ -175,16 +140,11 @@ namespace Ocelot.AcceptanceTests
await context.Response.WriteAsync(responseBody); await context.Response.WriteAsync(responseBody);
} }
}); });
})
.Build();
_builderTwo.Start();
} }
public void Dispose() public void Dispose()
{ {
_builderOne?.Dispose(); _serviceHandler?.Dispose();
_builderTwo?.Dispose();
_steps.Dispose(); _steps.Dispose();
} }
} }

View File

@ -1,31 +1,30 @@
namespace Ocelot.AcceptanceTests
{
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Net; using System.Net;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Ocelot.Configuration.File; using Ocelot.Configuration.File;
using Shouldly;
using TestStack.BDDfy; using TestStack.BDDfy;
using Xunit; using Xunit;
namespace Ocelot.AcceptanceTests
{
public class UpstreamHostTests : IDisposable public class UpstreamHostTests : IDisposable
{ {
private IWebHost _builder;
private readonly Steps _steps; private readonly Steps _steps;
private string _downstreamPath; private string _downstreamPath;
private readonly ServiceHandler _serviceHandler;
public UpstreamHostTests() public UpstreamHostTests()
{ {
_serviceHandler = new ServiceHandler();
_steps = new Steps(); _steps = new Steps();
} }
[Fact] [Fact]
public void should_return_response_200_with_simple_url_and_hosts_match() public void should_return_response_200_with_simple_url_and_hosts_match()
{ {
int port = 64905;
var configuration = new FileConfiguration var configuration = new FileConfiguration
{ {
ReRoutes = new List<FileReRoute> ReRoutes = new List<FileReRoute>
@ -39,7 +38,7 @@ namespace Ocelot.AcceptanceTests
new FileHostAndPort new FileHostAndPort
{ {
Host = "localhost", Host = "localhost",
Port = 51875, Port = port,
} }
}, },
UpstreamPathTemplate = "/", UpstreamPathTemplate = "/",
@ -49,7 +48,7 @@ namespace Ocelot.AcceptanceTests
} }
}; };
this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:51875", "/", 200, "Hello from Laura")) this.Given(x => x.GivenThereIsAServiceRunningOn($"http://localhost:{port}", "/", 200, "Hello from Laura"))
.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("/"))
@ -61,6 +60,8 @@ namespace Ocelot.AcceptanceTests
[Fact] [Fact]
public void should_return_response_200_with_simple_url_and_hosts_match_multiple_re_routes() public void should_return_response_200_with_simple_url_and_hosts_match_multiple_re_routes()
{ {
int port = 64904;
var configuration = new FileConfiguration var configuration = new FileConfiguration
{ {
ReRoutes = new List<FileReRoute> ReRoutes = new List<FileReRoute>
@ -74,7 +75,7 @@ namespace Ocelot.AcceptanceTests
new FileHostAndPort new FileHostAndPort
{ {
Host = "localhost", Host = "localhost",
Port = 51875, Port = port,
} }
}, },
UpstreamPathTemplate = "/", UpstreamPathTemplate = "/",
@ -100,7 +101,7 @@ namespace Ocelot.AcceptanceTests
} }
}; };
this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:51875", "/", 200, "Hello from Laura")) this.Given(x => x.GivenThereIsAServiceRunningOn($"http://localhost:{port}", "/", 200, "Hello from Laura"))
.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("/"))
@ -112,6 +113,8 @@ namespace Ocelot.AcceptanceTests
[Fact] [Fact]
public void should_return_response_200_with_simple_url_and_hosts_match_multiple_re_routes_reversed() public void should_return_response_200_with_simple_url_and_hosts_match_multiple_re_routes_reversed()
{ {
int port = 64903;
var configuration = new FileConfiguration var configuration = new FileConfiguration
{ {
ReRoutes = new List<FileReRoute> ReRoutes = new List<FileReRoute>
@ -141,7 +144,7 @@ namespace Ocelot.AcceptanceTests
new FileHostAndPort new FileHostAndPort
{ {
Host = "localhost", Host = "localhost",
Port = 51875, Port = port,
} }
}, },
UpstreamPathTemplate = "/", UpstreamPathTemplate = "/",
@ -151,7 +154,7 @@ namespace Ocelot.AcceptanceTests
} }
}; };
this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:51875", "/", 200, "Hello from Laura")) this.Given(x => x.GivenThereIsAServiceRunningOn($"http://localhost:{port}", "/", 200, "Hello from Laura"))
.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("/"))
@ -163,6 +166,8 @@ namespace Ocelot.AcceptanceTests
[Fact] [Fact]
public void should_return_response_200_with_simple_url_and_hosts_match_multiple_re_routes_reversed_with_no_host_first() public void should_return_response_200_with_simple_url_and_hosts_match_multiple_re_routes_reversed_with_no_host_first()
{ {
int port = 64902;
var configuration = new FileConfiguration var configuration = new FileConfiguration
{ {
ReRoutes = new List<FileReRoute> ReRoutes = new List<FileReRoute>
@ -191,7 +196,7 @@ namespace Ocelot.AcceptanceTests
new FileHostAndPort new FileHostAndPort
{ {
Host = "localhost", Host = "localhost",
Port = 51875, Port = port,
} }
}, },
UpstreamPathTemplate = "/", UpstreamPathTemplate = "/",
@ -201,7 +206,7 @@ namespace Ocelot.AcceptanceTests
} }
}; };
this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:51875", "/", 200, "Hello from Laura")) this.Given(x => x.GivenThereIsAServiceRunningOn($"http://localhost:{port}", "/", 200, "Hello from Laura"))
.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("/"))
@ -213,6 +218,8 @@ namespace Ocelot.AcceptanceTests
[Fact] [Fact]
public void should_return_response_404_with_simple_url_and_hosts_dont_match() public void should_return_response_404_with_simple_url_and_hosts_dont_match()
{ {
int port = 64901;
var configuration = new FileConfiguration var configuration = new FileConfiguration
{ {
ReRoutes = new List<FileReRoute> ReRoutes = new List<FileReRoute>
@ -226,7 +233,7 @@ namespace Ocelot.AcceptanceTests
new FileHostAndPort new FileHostAndPort
{ {
Host = "localhost", Host = "localhost",
Port = 51875, Port = port,
} }
}, },
UpstreamPathTemplate = "/", UpstreamPathTemplate = "/",
@ -236,7 +243,7 @@ namespace Ocelot.AcceptanceTests
} }
}; };
this.Given(x => x.GivenThereIsAServiceRunningOn("http://localhost:51875", "/", 200, "Hello from Laura")) this.Given(x => x.GivenThereIsAServiceRunningOn($"http://localhost:{port}", "/", 200, "Hello from Laura"))
.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("/"))
@ -246,15 +253,7 @@ namespace Ocelot.AcceptanceTests
private void GivenThereIsAServiceRunningOn(string baseUrl, string basePath, int statusCode, string responseBody) private void GivenThereIsAServiceRunningOn(string baseUrl, string basePath, int statusCode, string responseBody)
{ {
_builder = new WebHostBuilder() _serviceHandler.GivenThereIsAServiceRunningOn(baseUrl, basePath, async context =>
.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; _downstreamPath = !string.IsNullOrEmpty(context.Request.PathBase.Value) ? context.Request.PathBase.Value : context.Request.Path.Value;
@ -269,20 +268,11 @@ namespace Ocelot.AcceptanceTests
await context.Response.WriteAsync(responseBody); await context.Response.WriteAsync(responseBody);
} }
}); });
})
.Build();
_builder.Start();
}
internal void ThenTheDownstreamUrlPathShouldBe(string expectedDownstreamPath)
{
_downstreamPath.ShouldBe(expectedDownstreamPath);
} }
public void Dispose() public void Dispose()
{ {
_builder?.Dispose(); _serviceHandler?.Dispose();
_steps.Dispose(); _steps.Dispose();
} }
} }

View File

@ -1,35 +1,29 @@
namespace Ocelot.AcceptanceTests
{
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Net.WebSockets; using System.Net.WebSockets;
using System.Text; using System.Text;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Consul; using Consul;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Ocelot.Configuration.File; using Ocelot.Configuration.File;
using Shouldly; using Shouldly;
using TestStack.BDDfy; using TestStack.BDDfy;
using Xunit; using Xunit;
namespace Ocelot.AcceptanceTests
{
public class WebSocketTests : IDisposable public class WebSocketTests : IDisposable
{ {
private IWebHost _firstDownstreamHost;
private IWebHost _secondDownstreamHost;
private readonly List<string> _secondRecieved; private readonly List<string> _secondRecieved;
private readonly List<string> _firstRecieved; private readonly List<string> _firstRecieved;
private readonly List<ServiceEntry> _serviceEntries; private readonly List<ServiceEntry> _serviceEntries;
private readonly Steps _steps; private readonly Steps _steps;
private IWebHost _fakeConsulBuilder; private readonly ServiceHandler _serviceHandler;
public WebSocketTests() public WebSocketTests()
{ {
_serviceHandler = new ServiceHandler();
_steps = new Steps(); _steps = new Steps();
_firstRecieved = new List<string>(); _firstRecieved = new List<string>();
_secondRecieved = new List<string>(); _secondRecieved = new List<string>();
@ -211,25 +205,13 @@ namespace Ocelot.AcceptanceTests
private void GivenThereIsAFakeConsulServiceDiscoveryProvider(string url, string serviceName) private void GivenThereIsAFakeConsulServiceDiscoveryProvider(string url, string serviceName)
{ {
_fakeConsulBuilder = new WebHostBuilder() _serviceHandler.GivenThereIsAServiceRunningOn(url, async context =>
.UseUrls(url)
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseUrls(url)
.Configure(app =>
{
app.Run(async context =>
{ {
if (context.Request.Path.Value == $"/v1/health/service/{serviceName}") if (context.Request.Path.Value == $"/v1/health/service/{serviceName}")
{ {
await context.Response.WriteJsonAsync(_serviceEntries); await context.Response.WriteJsonAsync(_serviceEntries);
} }
}); });
})
.Build();
_fakeConsulBuilder.Start();
} }
private async Task WhenIStartTheClients() private async Task WhenIStartTheClients()
@ -333,33 +315,13 @@ namespace Ocelot.AcceptanceTests
private async Task StartFakeDownstreamService(string url, string path) private async Task StartFakeDownstreamService(string url, string path)
{ {
_firstDownstreamHost = new WebHostBuilder() await _serviceHandler.StartFakeDownstreamService(url, path, async(context, next) =>
.ConfigureServices(s => { }).UseKestrel()
.UseUrls(url)
.UseContentRoot(Directory.GetCurrentDirectory())
.ConfigureAppConfiguration((hostingContext, config) =>
{
config.SetBasePath(hostingContext.HostingEnvironment.ContentRootPath);
var env = hostingContext.HostingEnvironment;
config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: false)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: false);
config.AddEnvironmentVariables();
})
.ConfigureLogging((hostingContext, logging) =>
{
logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
logging.AddConsole();
})
.Configure(app =>
{
app.UseWebSockets();
app.Use(async (context, next) =>
{ {
if (context.Request.Path == path) if (context.Request.Path == path)
{ {
if (context.WebSockets.IsWebSocketRequest) if (context.WebSockets.IsWebSocketRequest)
{ {
WebSocket webSocket = await context.WebSockets.AcceptWebSocketAsync(); var webSocket = await context.WebSockets.AcceptWebSocketAsync();
await Echo(webSocket); await Echo(webSocket);
} }
else else
@ -372,34 +334,11 @@ namespace Ocelot.AcceptanceTests
await next(); await next();
} }
}); });
})
.UseIISIntegration().Build();
await _firstDownstreamHost.StartAsync();
} }
private async Task StartSecondFakeDownstreamService(string url, string path) private async Task StartSecondFakeDownstreamService(string url, string path)
{ {
_secondDownstreamHost = new WebHostBuilder() await _serviceHandler.StartFakeDownstreamService(url, path, async (context, next) =>
.ConfigureServices(s => { }).UseKestrel()
.UseUrls(url)
.UseContentRoot(Directory.GetCurrentDirectory())
.ConfigureAppConfiguration((hostingContext, config) =>
{
config.SetBasePath(hostingContext.HostingEnvironment.ContentRootPath);
var env = hostingContext.HostingEnvironment;
config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: false)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: false);
config.AddEnvironmentVariables();
})
.ConfigureLogging((hostingContext, logging) =>
{
logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
logging.AddConsole();
})
.Configure(app =>
{
app.UseWebSockets();
app.Use(async (context, next) =>
{ {
if (context.Request.Path == path) if (context.Request.Path == path)
{ {
@ -418,9 +357,6 @@ namespace Ocelot.AcceptanceTests
await next(); await next();
} }
}); });
})
.UseIISIntegration().Build();
await _secondDownstreamHost.StartAsync();
} }
private async Task Echo(WebSocket webSocket) private async Task Echo(WebSocket webSocket)
@ -473,10 +409,8 @@ namespace Ocelot.AcceptanceTests
public void Dispose() public void Dispose()
{ {
_serviceHandler?.Dispose();
_steps.Dispose(); _steps.Dispose();
_firstDownstreamHost?.Dispose();
_secondDownstreamHost?.Dispose();
_fakeConsulBuilder?.Dispose();
} }
} }
} }