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
{
private IWebHost _serviceOneBuilder;
private IWebHost _serviceTwoBuilder;
private readonly Steps _steps;
private string _downstreamPathOne;
private string _downstreamPathTwo;
private readonly ServiceHandler _serviceHandler;
public AggregateTests()
{
_serviceHandler = new ServiceHandler();
_steps = new Steps();
}
@ -116,7 +116,7 @@ namespace Ocelot.AcceptanceTests
new FileHostAndPort
{
Host = "localhost",
Port = 51885,
Port = 51875,
}
},
UpstreamPathTemplate = "/laura",
@ -157,7 +157,7 @@ namespace Ocelot.AcceptanceTests
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}"))
.And(x => _steps.GivenThereIsAConfiguration(configuration))
.And(x => _steps.GivenOcelotIsRunning())
@ -370,64 +370,40 @@ namespace Ocelot.AcceptanceTests
private void GivenServiceOneIsRunning(string baseUrl, string basePath, int statusCode, string responseBody)
{
_serviceOneBuilder = new WebHostBuilder()
.UseUrls(baseUrl)
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.Configure(app =>
_serviceHandler.GivenThereIsAServiceRunningOn(baseUrl, basePath, async context =>
{
_downstreamPathOne = !string.IsNullOrEmpty(context.Request.PathBase.Value) ? context.Request.PathBase.Value : context.Request.Path.Value;
if (_downstreamPathOne != basePath)
{
app.UsePathBase(basePath);
app.Run(async context =>
{
_downstreamPathOne = !string.IsNullOrEmpty(context.Request.PathBase.Value) ? context.Request.PathBase.Value : context.Request.Path.Value;
if(_downstreamPathOne != basePath)
{
context.Response.StatusCode = statusCode;
await context.Response.WriteAsync("downstream path didnt match base path");
}
else
{
context.Response.StatusCode = statusCode;
await context.Response.WriteAsync(responseBody);
}
});
})
.Build();
_serviceOneBuilder.Start();
context.Response.StatusCode = statusCode;
await context.Response.WriteAsync("downstream path didnt match base path");
}
else
{
context.Response.StatusCode = statusCode;
await context.Response.WriteAsync(responseBody);
}
});
}
private void GivenServiceTwoIsRunning(string baseUrl, string basePath, int statusCode, string responseBody)
{
_serviceTwoBuilder = new WebHostBuilder()
.UseUrls(baseUrl)
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.Configure(app =>
_serviceHandler.GivenThereIsAServiceRunningOn(baseUrl, basePath, async context =>
{
_downstreamPathTwo = !string.IsNullOrEmpty(context.Request.PathBase.Value) ? context.Request.PathBase.Value : context.Request.Path.Value;
if (_downstreamPathTwo != basePath)
{
app.UsePathBase(basePath);
app.Run(async context =>
{
_downstreamPathTwo = !string.IsNullOrEmpty(context.Request.PathBase.Value) ? context.Request.PathBase.Value : context.Request.Path.Value;
if(_downstreamPathTwo != basePath)
{
context.Response.StatusCode = statusCode;
await context.Response.WriteAsync("downstream path didnt match base path");
}
else
{
context.Response.StatusCode = statusCode;
await context.Response.WriteAsync(responseBody);
}
});
})
.Build();
_serviceTwoBuilder.Start();
context.Response.StatusCode = statusCode;
await context.Response.WriteAsync("downstream path didnt match base path");
}
else
{
context.Response.StatusCode = statusCode;
await context.Response.WriteAsync(responseBody);
}
});
}
internal void ThenTheDownstreamUrlPathShouldBe(string expectedDownstreamPathOne, string expectedDownstreamPath)
@ -438,8 +414,7 @@ namespace Ocelot.AcceptanceTests
public void Dispose()
{
_serviceOneBuilder?.Dispose();
_serviceTwoBuilder?.Dispose();
_serviceHandler.Dispose();
_steps.Dispose();
}
}

View File

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

View File

@ -1,33 +1,32 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Security.Claims;
using IdentityServer4.AccessTokenValidation;
using IdentityServer4.Models;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Ocelot.Configuration.File;
using TestStack.BDDfy;
using Xunit;
namespace Ocelot.AcceptanceTests
{
using IdentityServer4;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Security.Claims;
using IdentityServer4.AccessTokenValidation;
using IdentityServer4.Models;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Ocelot.Configuration.File;
using TestStack.BDDfy;
using Xunit;
using IdentityServer4.Test;
public class AuthorisationTests : IDisposable
{
private IWebHost _servicebuilder;
private IWebHost _identityServerBuilder;
private readonly Steps _steps;
private Action<IdentityServerAuthenticationOptions> _options;
private readonly Action<IdentityServerAuthenticationOptions> _options;
private string _identityServerRootUrl = "http://localhost:51888";
private readonly ServiceHandler _serviceHandler;
public AuthorisationTests()
{
_serviceHandler = new ServiceHandler();
_steps = new Steps();
_options = o =>
{
@ -42,8 +41,10 @@ namespace Ocelot.AcceptanceTests
[Fact]
public void should_return_response_200_authorising_route()
{
var configuration = new FileConfiguration
{
int port = 52875;
var configuration = new FileConfiguration
{
ReRoutes = new List<FileReRoute>
{
new FileReRoute
@ -54,7 +55,7 @@ namespace Ocelot.AcceptanceTests
new FileHostAndPort
{
Host = "localhost",
Port = 51876,
Port = port,
}
},
DownstreamScheme = "http",
@ -83,10 +84,10 @@ namespace Ocelot.AcceptanceTests
}
}
}
};
};
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.GivenThereIsAConfiguration(configuration))
.And(x => _steps.GivenOcelotIsRunning(_options, "Test"))
@ -100,7 +101,9 @@ namespace Ocelot.AcceptanceTests
[Fact]
public void should_return_response_403_authorising_route()
{
var configuration = new FileConfiguration
int port = 59471;
var configuration = new FileConfiguration
{
ReRoutes = new List<FileReRoute>
{
@ -112,7 +115,7 @@ namespace Ocelot.AcceptanceTests
new FileHostAndPort
{
Host = "localhost",
Port = 51876,
Port = port,
}
},
DownstreamScheme = "http",
@ -143,7 +146,7 @@ namespace Ocelot.AcceptanceTests
};
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.GivenThereIsAConfiguration(configuration))
.And(x => _steps.GivenOcelotIsRunning(_options, "Test"))
@ -156,7 +159,9 @@ namespace Ocelot.AcceptanceTests
[Fact]
public void should_return_response_200_using_identity_server_with_allowed_scope()
{
var configuration = new FileConfiguration
int port = 63471;
var configuration = new FileConfiguration
{
ReRoutes = new List<FileReRoute>
{
@ -168,7 +173,7 @@ namespace Ocelot.AcceptanceTests
new FileHostAndPort
{
Host = "localhost",
Port = 51876,
Port = port,
}
},
DownstreamScheme = "http",
@ -184,7 +189,7 @@ namespace Ocelot.AcceptanceTests
};
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.GivenThereIsAConfiguration(configuration))
.And(x => _steps.GivenOcelotIsRunning(_options, "Test"))
@ -197,7 +202,9 @@ namespace Ocelot.AcceptanceTests
[Fact]
public void should_return_response_403_using_identity_server_with_scope_not_allowed()
{
var configuration = new FileConfiguration
int port = 60571;
var configuration = new FileConfiguration
{
ReRoutes = new List<FileReRoute>
{
@ -209,7 +216,7 @@ namespace Ocelot.AcceptanceTests
new FileHostAndPort
{
Host = "localhost",
Port = 51876,
Port = port,
}
},
DownstreamScheme = "http",
@ -225,7 +232,7 @@ namespace Ocelot.AcceptanceTests
};
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.GivenThereIsAConfiguration(configuration))
.And(x => _steps.GivenOcelotIsRunning(_options, "Test"))
@ -238,7 +245,9 @@ namespace Ocelot.AcceptanceTests
[Fact]
public void should_fix_issue_240()
{
var configuration = new FileConfiguration
int port = 61071;
var configuration = new FileConfiguration
{
ReRoutes = new List<FileReRoute>
{
@ -250,7 +259,7 @@ namespace Ocelot.AcceptanceTests
new FileHostAndPort
{
Host = "localhost",
Port = 51876,
Port = port,
}
},
DownstreamScheme = "http",
@ -284,7 +293,7 @@ namespace Ocelot.AcceptanceTests
};
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.GivenThereIsAConfiguration(configuration))
.And(x => _steps.GivenOcelotIsRunning(_options, "Test"))
@ -297,23 +306,11 @@ namespace Ocelot.AcceptanceTests
private void GivenThereIsAServiceRunningOn(string url, int statusCode, string responseBody)
{
_servicebuilder = new WebHostBuilder()
.UseUrls(url)
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseUrls(url)
.Configure(app =>
{
app.Run(async context =>
{
context.Response.StatusCode = statusCode;
await context.Response.WriteAsync(responseBody);
});
})
.Build();
_servicebuilder.Start();
_serviceHandler.GivenThereIsAServiceRunningOn(url, async context =>
{
context.Response.StatusCode = statusCode;
await context.Response.WriteAsync(responseBody);
});
}
private void GivenThereIsAnIdentityServerOn(string url, string apiName, AccessTokenType tokenType)
@ -465,7 +462,7 @@ namespace Ocelot.AcceptanceTests
public void Dispose()
{
_servicebuilder?.Dispose();
_serviceHandler?.Dispose();
_steps.Dispose();
_identityServerBuilder?.Dispose();
}

View File

@ -1,24 +1,22 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Threading;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Ocelot.Configuration.File;
using TestStack.BDDfy;
using Xunit;
namespace Ocelot.AcceptanceTests
{
using System;
using System.Collections.Generic;
using System.Net;
using System.Threading;
using Microsoft.AspNetCore.Http;
using Ocelot.Configuration.File;
using TestStack.BDDfy;
using Xunit;
public class CachingTests : IDisposable
{
private IWebHost _builder;
private readonly Steps _steps;
private readonly ServiceHandler _serviceHandler;
public CachingTests()
{
_serviceHandler = new ServiceHandler();
_steps = new Steps();
}
@ -201,38 +199,26 @@ namespace Ocelot.AcceptanceTests
private void GivenTheServiceNowReturns(string url, int statusCode, string responseBody)
{
_builder.Dispose();
_serviceHandler.Dispose();
GivenThereIsAServiceRunningOn(url, statusCode, responseBody, null, null);
}
private void GivenThereIsAServiceRunningOn(string url, int statusCode, string responseBody, string key, string value)
{
_builder = new WebHostBuilder()
.UseUrls(url)
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseUrls(url)
.Configure(app =>
_serviceHandler.GivenThereIsAServiceRunningOn(url, async context =>
{
if (!string.IsNullOrEmpty(key) && !string.IsNullOrEmpty(key))
{
app.Run(async context =>
{
if(!string.IsNullOrEmpty(key) && !string.IsNullOrEmpty(key))
{
context.Response.Headers.Add(key, value);
}
context.Response.StatusCode = statusCode;
await context.Response.WriteAsync(responseBody);
});
})
.Build();
_builder.Start();
context.Response.Headers.Add(key, value);
}
context.Response.StatusCode = statusCode;
await context.Response.WriteAsync(responseBody);
});
}
public void Dispose()
{
_builder?.Dispose();
_serviceHandler?.Dispose();
_steps.Dispose();
}
}

View File

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

View File

@ -1,35 +1,35 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Security.Claims;
using IdentityServer4.AccessTokenValidation;
using IdentityServer4.Models;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Ocelot.Configuration.File;
using TestStack.BDDfy;
using Xunit;
using Xunit;
[assembly: CollectionBehavior(DisableTestParallelization = true)]
namespace Ocelot.AcceptanceTests
{
using IdentityServer4;
using IdentityServer4.Test;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Security.Claims;
using IdentityServer4.AccessTokenValidation;
using IdentityServer4.Models;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Ocelot.Configuration.File;
using TestStack.BDDfy;
public class ClaimsToHeadersForwardingTests : IDisposable
{
private IWebHost _servicebuilder;
private IWebHost _identityServerBuilder;
private readonly Steps _steps;
private Action<IdentityServerAuthenticationOptions> _options;
private string _identityServerRootUrl = "http://localhost:52888";
private readonly ServiceHandler _serviceHandler;
public ClaimsToHeadersForwardingTests()
{
_serviceHandler = new ServiceHandler();
_steps = new Steps();
_options = o =>
{
@ -107,29 +107,17 @@ namespace Ocelot.AcceptanceTests
private void GivenThereIsAServiceRunningOn(string url, int statusCode)
{
_servicebuilder = new WebHostBuilder()
.UseUrls(url)
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseUrls(url)
.Configure(app =>
{
app.Run(async context =>
{
var customerId = context.Request.Headers.First(x => x.Key == "CustomerId").Value.First();
var locationId = context.Request.Headers.First(x => x.Key == "LocationId").Value.First();
var userType = context.Request.Headers.First(x => x.Key == "UserType").Value.First();
var userId = context.Request.Headers.First(x => x.Key == "UserId").Value.First();
_serviceHandler.GivenThereIsAServiceRunningOn(url, async context =>
{
var customerId = context.Request.Headers.First(x => x.Key == "CustomerId").Value.First();
var locationId = context.Request.Headers.First(x => x.Key == "LocationId").Value.First();
var userType = context.Request.Headers.First(x => x.Key == "UserType").Value.First();
var userId = context.Request.Headers.First(x => x.Key == "UserId").Value.First();
var responseBody = $"CustomerId: {customerId} LocationId: {locationId} UserType: {userType} UserId: {userId}";
context.Response.StatusCode = statusCode;
await context.Response.WriteAsync(responseBody);
});
})
.Build();
_servicebuilder.Start();
var responseBody = $"CustomerId: {customerId} LocationId: {locationId} UserType: {userType} UserId: {userId}";
context.Response.StatusCode = statusCode;
await context.Response.WriteAsync(responseBody);
});
}
private void GivenThereIsAnIdentityServerOn(string url, string apiName, AccessTokenType tokenType, TestUser user)
@ -203,7 +191,7 @@ namespace Ocelot.AcceptanceTests
public void Dispose()
{
_servicebuilder?.Dispose();
_serviceHandler?.Dispose();
_steps.Dispose();
_identityServerBuilder?.Dispose();
}

View File

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

View File

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

View File

@ -1,29 +1,27 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Ocelot.Configuration.File;
using Ocelot.Middleware;
using Shouldly;
using TestStack.BDDfy;
using Xunit;
namespace Ocelot.AcceptanceTests
namespace Ocelot.AcceptanceTests
{
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Ocelot.Configuration.File;
using Ocelot.Middleware;
using Shouldly;
using TestStack.BDDfy;
using Xunit;
public class CustomMiddlewareTests : IDisposable
{
private readonly string _configurationPath;
private IWebHost _builder;
private readonly Steps _steps;
private int _counter;
private readonly ServiceHandler _serviceHandler;
public CustomMiddlewareTests()
{
_serviceHandler = new ServiceHandler();
_counter = 0;
_steps = new Steps();
_configurationPath = "ocelot.json";
@ -340,37 +338,24 @@ namespace Ocelot.AcceptanceTests
private void GivenThereIsAServiceRunningOn(string url, int statusCode, string basePath)
{
_builder = new WebHostBuilder()
.UseUrls(url)
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseUrls(url)
.Configure(app =>
_serviceHandler.GivenThereIsAServiceRunningOn(url, context =>
{
if (string.IsNullOrEmpty(basePath))
{
app.UsePathBase(basePath);
app.Run(context =>
{
if(string.IsNullOrEmpty(basePath))
{
context.Response.StatusCode = statusCode;
}
else if(context.Request.Path.Value != basePath)
{
context.Response.StatusCode = 404;
}
context.Response.StatusCode = statusCode;
}
else if (context.Request.Path.Value != basePath)
{
context.Response.StatusCode = 404;
}
return Task.CompletedTask;
});
})
.Build();
_builder.Start();
return Task.CompletedTask;
});
}
public void Dispose()
{
_builder?.Dispose();
_serviceHandler?.Dispose();
_steps.Dispose();
}

View File

@ -1,29 +1,25 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
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 Ocelot.Configuration.File;
using Shouldly;
using TestStack.BDDfy;
using Xunit;
namespace Ocelot.AcceptanceTests
{
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net;
using Microsoft.AspNetCore.Http;
using Ocelot.Configuration.File;
using Shouldly;
using TestStack.BDDfy;
using Xunit;
public class GzipTests : IDisposable
{
private IWebHost _builder;
private readonly Steps _steps;
private readonly ServiceHandler _serviceHandler;
public GzipTests()
{
_serviceHandler = new ServiceHandler();
_steps = new Steps();
}
@ -66,51 +62,40 @@ namespace Ocelot.AcceptanceTests
private void GivenThereIsAServiceRunningOn(string baseUrl, string basePath, int statusCode, string responseBody, string expected)
{
_builder = new WebHostBuilder()
.UseUrls(baseUrl)
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.Configure(app =>
_serviceHandler.GivenThereIsAServiceRunningOn(baseUrl, basePath, async context =>
{
if (context.Request.Headers.TryGetValue("Content-Encoding", out var contentEncoding))
{
app.UsePathBase(basePath);
app.Run(async context =>
contentEncoding.First().ShouldBe("gzip");
string text = null;
using (var decompress = new GZipStream(context.Request.Body, CompressionMode.Decompress))
{
if(context.Request.Headers.TryGetValue("Content-Encoding", out var contentEncoding))
using (var sr = new StreamReader(decompress))
{
contentEncoding.First().ShouldBe("gzip");
string text = null;
using (var decompress = new GZipStream(context.Request.Body, CompressionMode.Decompress))
{
using (var sr = new StreamReader(decompress)) {
text = sr.ReadToEnd();
}
}
if(text != expected)
{
throw new Exception("not gzipped");
}
context.Response.StatusCode = statusCode;
await context.Response.WriteAsync(responseBody);
text = sr.ReadToEnd();
}
else
{
context.Response.StatusCode = statusCode;
await context.Response.WriteAsync("downstream path didnt match base path");
}
});
})
.Build();
}
_builder.Start();
if (text != expected)
{
throw new Exception("not gzipped");
}
context.Response.StatusCode = statusCode;
await context.Response.WriteAsync(responseBody);
}
else
{
context.Response.StatusCode = statusCode;
await context.Response.WriteAsync("downstream path didnt match base path");
}
});
}
public void Dispose()
{
_builder?.Dispose();
_serviceHandler?.Dispose();
_steps.Dispose();
}
}

View File

@ -1,26 +1,24 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Ocelot.Configuration.File;
using TestStack.BDDfy;
using Xunit;
namespace Ocelot.AcceptanceTests
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Ocelot.Configuration.File;
using TestStack.BDDfy;
using Xunit;
public class HeaderTests : IDisposable
{
private IWebHost _builder;
private int _count;
private readonly Steps _steps;
private readonly ServiceHandler _serviceHandler;
public HeaderTests()
{
_serviceHandler = new ServiceHandler();
_steps = new Steps();
}
@ -387,97 +385,61 @@ namespace Ocelot.AcceptanceTests
private void GivenThereIsAServiceRunningOn(string baseUrl, string basePath, int statusCode)
{
_builder = new WebHostBuilder()
.UseUrls(baseUrl)
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.Configure(app =>
_serviceHandler.GivenThereIsAServiceRunningOn(baseUrl, basePath, context =>
{
if (_count == 0)
{
app.UsePathBase(basePath);
app.Run(context =>
context.Response.Cookies.Append("test", "0");
_count++;
context.Response.StatusCode = statusCode;
return Task.CompletedTask;
}
if (context.Request.Cookies.TryGetValue("test", out var cookieValue) || context.Request.Headers.TryGetValue("Set-Cookie", out var headerValue))
{
if (cookieValue == "0" || headerValue == "test=1; path=/")
{
if (_count == 0)
{
context.Response.Cookies.Append("test", "0");
_count++;
context.Response.StatusCode = statusCode;
return Task.CompletedTask;
}
if (context.Request.Cookies.TryGetValue("test", out var cookieValue) || context.Request.Headers.TryGetValue("Set-Cookie", out var headerValue))
{
if (cookieValue == "0" || headerValue == "test=1; path=/")
{
context.Response.StatusCode = statusCode;
return Task.CompletedTask;
}
}
context.Response.StatusCode = 500;
context.Response.StatusCode = statusCode;
return Task.CompletedTask;
});
})
.Build();
}
}
_builder.Start();
context.Response.StatusCode = 500;
return Task.CompletedTask;
});
}
private void GivenThereIsAServiceRunningOn(string baseUrl, string basePath, int statusCode, string headerKey)
{
_builder = new WebHostBuilder()
.UseUrls(baseUrl)
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.Configure(app =>
_serviceHandler.GivenThereIsAServiceRunningOn(baseUrl, basePath, async context =>
{
if (context.Request.Headers.TryGetValue(headerKey, out var values))
{
app.UsePathBase(basePath);
app.Run(async context =>
{
if (context.Request.Headers.TryGetValue(headerKey, out var values))
{
var result = values.First();
context.Response.StatusCode = statusCode;
await context.Response.WriteAsync(result);
}
});
})
.Build();
_builder.Start();
var result = values.First();
context.Response.StatusCode = statusCode;
await context.Response.WriteAsync(result);
}
});
}
private void GivenThereIsAServiceRunningOn(string baseUrl, string basePath, int statusCode, string headerKey, string headerValue)
{
_builder = new WebHostBuilder()
.UseUrls(baseUrl)
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.Configure(app =>
_serviceHandler.GivenThereIsAServiceRunningOn(baseUrl, basePath, context =>
{
context.Response.OnStarting(() =>
{
app.UsePathBase(basePath);
app.Run(context =>
{
context.Response.OnStarting(() =>
{
context.Response.Headers.Add(headerKey, headerValue);
context.Response.StatusCode = statusCode;
return Task.CompletedTask;
});
context.Response.Headers.Add(headerKey, headerValue);
context.Response.StatusCode = statusCode;
return Task.CompletedTask;
});
return Task.CompletedTask;
});
})
.Build();
_builder.Start();
return Task.CompletedTask;
});
}
public void Dispose()
{
_builder?.Dispose();
_serviceHandler?.Dispose();
_steps.Dispose();
}
}

View File

@ -1,28 +1,26 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Ocelot.Configuration.File;
using Shouldly;
using TestStack.BDDfy;
using Xunit;
namespace Ocelot.AcceptanceTests
namespace Ocelot.AcceptanceTests
{
public class HttpDelegatingHandlersTests
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Ocelot.Configuration.File;
using Shouldly;
using TestStack.BDDfy;
using Xunit;
public class HttpDelegatingHandlersTests : IDisposable
{
private IWebHost _builder;
private readonly Steps _steps;
private string _downstreamPath;
private readonly ServiceHandler _serviceHandler;
public HttpDelegatingHandlersTests()
{
_serviceHandler = new ServiceHandler();
_steps = new Steps();
}
@ -249,6 +247,7 @@ namespace Ocelot.AcceptanceTests
return base.SendAsync(request, cancellationToken);
}
}
// ReSharper disable once ClassNeverInstantiated.Local
private class FakeHandlerAgain : DelegatingHandler
{
@ -263,33 +262,27 @@ namespace Ocelot.AcceptanceTests
private void GivenThereIsAServiceRunningOn(string baseUrl, string basePath, int statusCode, string responseBody)
{
_builder = new WebHostBuilder()
.UseUrls(baseUrl)
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.Configure(app =>
_serviceHandler.GivenThereIsAServiceRunningOn(baseUrl, basePath, async context =>
{
_downstreamPath = !string.IsNullOrEmpty(context.Request.PathBase.Value) ? context.Request.PathBase.Value : context.Request.Path.Value;
if (_downstreamPath != basePath)
{
app.UsePathBase(basePath);
app.Run(async context =>
{
_downstreamPath = !string.IsNullOrEmpty(context.Request.PathBase.Value) ? context.Request.PathBase.Value : context.Request.Path.Value;
context.Response.StatusCode = statusCode;
await context.Response.WriteAsync("downstream path didnt match base path");
}
else
{
context.Response.StatusCode = statusCode;
await context.Response.WriteAsync(responseBody);
}
});
}
if (_downstreamPath != basePath)
{
context.Response.StatusCode = statusCode;
await context.Response.WriteAsync("downstream path didnt match base path");
}
else
{
context.Response.StatusCode = statusCode;
await context.Response.WriteAsync(responseBody);
}
});
})
.Build();
_builder.Start();
public void Dispose()
{
_steps?.Dispose();
_serviceHandler?.Dispose();
}
}
}

View File

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

View File

@ -1,27 +1,24 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Ocelot.Configuration.File;
using TestStack.BDDfy;
using Xunit;
namespace Ocelot.AcceptanceTests
namespace Ocelot.AcceptanceTests
{
using System;
using System.Collections.Generic;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Ocelot.Configuration.File;
using TestStack.BDDfy;
using Xunit;
public class QoSTests : IDisposable
{
private IWebHost _brokenService;
private readonly Steps _steps;
private int _requestCount;
private IWebHost _workingService;
private readonly ServiceHandler _serviceHandler;
public QoSTests()
{
_serviceHandler = new ServiceHandler();
_steps = new Steps();
}
@ -227,74 +224,48 @@ namespace Ocelot.AcceptanceTests
private void GivenThereIsAPossiblyBrokenServiceRunningOn(string url, string responseBody)
{
_brokenService = new WebHostBuilder()
.UseUrls(url)
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseUrls(url)
.Configure(app =>
_serviceHandler.GivenThereIsAServiceRunningOn(url, async context =>
{
//circuit starts closed
if (_requestCount == 0)
{
app.Run(async context =>
{
//circuit starts closed
if (_requestCount == 0)
{
_requestCount++;
context.Response.StatusCode = 200;
await context.Response.WriteAsync(responseBody);
return;
}
_requestCount++;
context.Response.StatusCode = 200;
await context.Response.WriteAsync(responseBody);
return;
}
//request one times out and polly throws exception, circuit opens
if (_requestCount == 1)
{
_requestCount++;
await Task.Delay(1000);
context.Response.StatusCode = 200;
return;
}
//request one times out and polly throws exception, circuit opens
if (_requestCount == 1)
{
_requestCount++;
await Task.Delay(1000);
context.Response.StatusCode = 200;
return;
}
//after break closes we return 200 OK
if (_requestCount == 2)
{
context.Response.StatusCode = 200;
await context.Response.WriteAsync(responseBody);
return;
}
});
})
.Build();
_brokenService.Start();
//after break closes we return 200 OK
if (_requestCount == 2)
{
context.Response.StatusCode = 200;
await context.Response.WriteAsync(responseBody);
}
});
}
private void GivenThereIsAServiceRunningOn(string url, int statusCode, string responseBody, int timeout)
{
_workingService = new WebHostBuilder()
.UseUrls(url)
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseUrls(url)
.Configure(app =>
{
app.Run(async context =>
{
Thread.Sleep(timeout);
context.Response.StatusCode = statusCode;
await context.Response.WriteAsync(responseBody);
});
})
.Build();
_workingService.Start();
_serviceHandler.GivenThereIsAServiceRunningOn(url, async context =>
{
Thread.Sleep(timeout);
context.Response.StatusCode = statusCode;
await context.Response.WriteAsync(responseBody);
});
}
public void Dispose()
{
_workingService?.Dispose();
_brokenService?.Dispose();
_serviceHandler?.Dispose();
_steps.Dispose();
}
}

View File

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

View File

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

View File

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

View File

@ -1,25 +1,23 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Ocelot.Configuration.File;
using Shouldly;
using TestStack.BDDfy;
using Xunit;
namespace Ocelot.AcceptanceTests
{
using System;
using System.Collections.Generic;
using System.Net;
using Microsoft.AspNetCore.Http;
using Ocelot.Configuration.File;
using Shouldly;
using TestStack.BDDfy;
using Xunit;
public class RoutingTests : IDisposable
{
private IWebHost _builder;
private readonly Steps _steps;
private string _downstreamPath;
private readonly ServiceHandler _serviceHandler;
public RoutingTests()
{
_serviceHandler = new ServiceHandler();
_steps = new Steps();
}
@ -973,33 +971,21 @@ namespace Ocelot.AcceptanceTests
private void GivenThereIsAServiceRunningOn(string baseUrl, string basePath, int statusCode, string responseBody)
{
_builder = new WebHostBuilder()
.UseUrls(baseUrl)
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.Configure(app =>
_serviceHandler.GivenThereIsAServiceRunningOn(baseUrl, basePath, async context =>
{
_downstreamPath = !string.IsNullOrEmpty(context.Request.PathBase.Value) ? context.Request.PathBase.Value : context.Request.Path.Value;
if (_downstreamPath != basePath)
{
app.UsePathBase(basePath);
app.Run(async context =>
{
_downstreamPath = !string.IsNullOrEmpty(context.Request.PathBase.Value) ? context.Request.PathBase.Value : context.Request.Path.Value;
if(_downstreamPath != basePath)
{
context.Response.StatusCode = statusCode;
await context.Response.WriteAsync("downstream path didnt match base path");
}
else
{
context.Response.StatusCode = statusCode;
await context.Response.WriteAsync(responseBody);
}
});
})
.Build();
_builder.Start();
context.Response.StatusCode = statusCode;
await context.Response.WriteAsync("downstream path didnt match base path");
}
else
{
context.Response.StatusCode = statusCode;
await context.Response.WriteAsync(responseBody);
}
});
}
internal void ThenTheDownstreamUrlPathShouldBe(string expectedDownstreamPath)
@ -1009,7 +995,7 @@ namespace Ocelot.AcceptanceTests
public void Dispose()
{
_builder?.Dispose();
_serviceHandler.Dispose();
_steps.Dispose();
}
}

View File

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

View File

@ -2,12 +2,9 @@ namespace Ocelot.AcceptanceTests
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using Consul;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Ocelot.Configuration.File;
using Shouldly;
@ -18,21 +15,19 @@ namespace Ocelot.AcceptanceTests
public class ServiceDiscoveryTests : IDisposable
{
private IWebHost _builderOne;
private IWebHost _builderTwo;
private IWebHost _fakeConsulBuilder;
private readonly Steps _steps;
private readonly List<ServiceEntry> _consulServices;
private readonly List<IServiceInstance> _eurekaInstances;
private int _counterOne;
private int _counterTwo;
private static readonly object SyncLock = new object();
private IWebHost _builder;
private string _downstreamPath;
private string _receivedToken;
private readonly ServiceHandler _serviceHandler;
public ServiceDiscoveryTests()
{
_serviceHandler = new ServiceHandler();
_steps = new Steps();
_consulServices = new List<ServiceEntry>();
_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.GivenTheServicesAreRegisteredWithEureka(instanceOne))
.And(x => _steps.GivenThereIsAConfiguration(configuration))
@ -568,26 +563,18 @@ namespace Ocelot.AcceptanceTests
private void GivenThereIsAFakeEurekaServiceDiscoveryProvider(string url, string serviceName)
{
_fakeConsulBuilder = new WebHostBuilder()
.UseUrls(url)
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseUrls(url)
.Configure(app =>
_serviceHandler.GivenThereIsAServiceRunningOn(url, async context =>
{
if (context.Request.Path.Value == "/eureka/apps/")
{
app.Run(async context =>
{
if (context.Request.Path.Value == "/eureka/apps/")
{
var apps = new List<Application>();
var apps = new List<Application>();
foreach (var serviceInstance in _eurekaInstances)
{
var a = new Application
{
name = serviceName,
instance = new List<Instance>
foreach (var serviceInstance in _eurekaInstances)
{
var a = new Application
{
name = serviceName,
instance = new List<Instance>
{
new Instance
{
@ -624,190 +611,126 @@ namespace Ocelot.AcceptanceTests
actionType = "ADDED"
}
}
};
};
apps.Add(a);
}
apps.Add(a);
}
var applications = new EurekaApplications
{
applications = new Applications
{
application = apps,
apps__hashcode = "UP_1_",
versions__delta = "1"
}
};
await context.Response.WriteJsonAsync(applications);
var applications = new EurekaApplications
{
applications = new Applications
{
application = apps,
apps__hashcode = "UP_1_",
versions__delta = "1"
}
});
})
.Build();
};
_fakeConsulBuilder.Start();
await context.Response.WriteJsonAsync(applications);
}
});
}
private void GivenThereIsAFakeConsulServiceDiscoveryProvider(string url, string serviceName)
{
_fakeConsulBuilder = new WebHostBuilder()
.UseUrls(url)
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseUrls(url)
.Configure(app =>
{
app.Run(async context =>
{
if(context.Request.Path.Value == $"/v1/health/service/{serviceName}")
{
if (context.Request.Headers.TryGetValue("X-Consul-Token", out var values))
{
_receivedToken = values.First();
}
_serviceHandler.GivenThereIsAServiceRunningOn(url, async context =>
{
if (context.Request.Path.Value == $"/v1/health/service/{serviceName}")
{
if (context.Request.Headers.TryGetValue("X-Consul-Token", out var values))
{
_receivedToken = values.First();
}
await context.Response.WriteJsonAsync(_consulServices);
}
});
})
.Build();
_fakeConsulBuilder.Start();
await context.Response.WriteJsonAsync(_consulServices);
}
});
}
private void GivenProductServiceOneIsRunning(string url, int statusCode)
{
_builderOne = new WebHostBuilder()
.UseUrls(url)
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseUrls(url)
.Configure(app =>
_serviceHandler.GivenThereIsAServiceRunningOn(url, async context =>
{
try
{
app.Run(async context =>
string response;
lock (SyncLock)
{
try
{
string response;
lock (SyncLock)
{
_counterOne++;
response = _counterOne.ToString();
}
context.Response.StatusCode = statusCode;
await context.Response.WriteAsync(response);
}
catch (Exception exception)
{
await context.Response.WriteAsync(exception.StackTrace);
}
});
})
.Build();
_counterOne++;
response = _counterOne.ToString();
}
_builderOne.Start();
context.Response.StatusCode = statusCode;
await context.Response.WriteAsync(response);
}
catch (Exception exception)
{
await context.Response.WriteAsync(exception.StackTrace);
}
});
}
private void GivenProductServiceTwoIsRunning(string url, int statusCode)
{
_builderTwo = new WebHostBuilder()
.UseUrls(url)
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseUrls(url)
.Configure(app =>
_serviceHandler.GivenThereIsAServiceRunningOn(url, async context =>
{
try
{
app.Run(async context =>
string response;
lock (SyncLock)
{
try
{
string response;
lock (SyncLock)
{
_counterTwo++;
response = _counterTwo.ToString();
}
_counterTwo++;
response = _counterTwo.ToString();
}
context.Response.StatusCode = statusCode;
await context.Response.WriteAsync(response);
}
catch (Exception exception)
{
await context.Response.WriteAsync(exception.StackTrace);
}
});
})
.Build();
_builderTwo.Start();
context.Response.StatusCode = statusCode;
await context.Response.WriteAsync(response);
}
catch (Exception exception)
{
await context.Response.WriteAsync(exception.StackTrace);
}
});
}
private void GivenEurekaProductServiceOneIsRunning(string url, int statusCode)
private void GivenEurekaProductServiceOneIsRunning(string url)
{
_builderOne = new WebHostBuilder()
.UseUrls(url)
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseUrls(url)
.Configure(app =>
_serviceHandler.GivenThereIsAServiceRunningOn(url, async context =>
{
try
{
app.Run(async context =>
{
try
{
context.Response.StatusCode = 200;
await context.Response.WriteAsync(nameof(ServiceDiscoveryTests));
}
catch (Exception exception)
{
await context.Response.WriteAsync(exception.StackTrace);
}
});
})
.Build();
_builderOne.Start();
context.Response.StatusCode = 200;
await context.Response.WriteAsync(nameof(ServiceDiscoveryTests));
}
catch (Exception exception)
{
await context.Response.WriteAsync(exception.StackTrace);
}
});
}
private void GivenThereIsAServiceRunningOn(string baseUrl, string basePath, int statusCode, string responseBody)
{
_builder = new WebHostBuilder()
.UseUrls(baseUrl)
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.Configure(app =>
_serviceHandler.GivenThereIsAServiceRunningOn(baseUrl, basePath, async context =>
{
_downstreamPath = !string.IsNullOrEmpty(context.Request.PathBase.Value) ? context.Request.PathBase.Value : context.Request.Path.Value;
if (_downstreamPath != basePath)
{
app.UsePathBase(basePath);
app.Run(async context =>
{
_downstreamPath = !string.IsNullOrEmpty(context.Request.PathBase.Value) ? context.Request.PathBase.Value : context.Request.Path.Value;
if(_downstreamPath != basePath)
{
context.Response.StatusCode = statusCode;
await context.Response.WriteAsync("downstream path didnt match base path");
}
else
{
context.Response.StatusCode = statusCode;
await context.Response.WriteAsync(responseBody);
}
});
})
.Build();
_builder.Start();
context.Response.StatusCode = statusCode;
await context.Response.WriteAsync("downstream path didnt match base path");
}
else
{
context.Response.StatusCode = statusCode;
await context.Response.WriteAsync(responseBody);
}
});
}
public void Dispose()
{
_builderOne?.Dispose();
_builderTwo?.Dispose();
_serviceHandler?.Dispose();
_steps.Dispose();
}
}

View File

@ -1,28 +1,22 @@
using System.Linq;
using Microsoft.Extensions.Primitives;
namespace Ocelot.AcceptanceTests
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Ocelot.Configuration.File;
using Shouldly;
using TestStack.BDDfy;
using Xunit;
public class ServiceFabricTests : IDisposable
{
private IWebHost _builder;
private readonly Steps _steps;
private string _downstreamPath;
private readonly ServiceHandler _serviceHandler;
public ServiceFabricTests()
{
_serviceHandler = new ServiceHandler();
_steps = new Steps();
}
@ -102,46 +96,34 @@ namespace Ocelot.AcceptanceTests
private void GivenThereIsAServiceRunningOn(string baseUrl, string basePath, int statusCode, string responseBody, string expectedQueryString)
{
_builder = new WebHostBuilder()
.UseUrls(baseUrl)
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.Configure(app =>
_serviceHandler.GivenThereIsAServiceRunningOn(baseUrl, basePath, async context =>
{
_downstreamPath = !string.IsNullOrEmpty(context.Request.PathBase.Value) ? context.Request.PathBase.Value : context.Request.Path.Value;
if (_downstreamPath != basePath)
{
app.UsePathBase(basePath);
app.Run(async context =>
context.Response.StatusCode = statusCode;
await context.Response.WriteAsync("downstream path didnt match base path");
}
else
{
if (context.Request.QueryString.Value.Contains(expectedQueryString))
{
_downstreamPath = !string.IsNullOrEmpty(context.Request.PathBase.Value) ? context.Request.PathBase.Value : context.Request.Path.Value;
if(_downstreamPath != basePath)
{
context.Response.StatusCode = statusCode;
await context.Response.WriteAsync("downstream path didnt match base path");
}
else
{
if (context.Request.QueryString.Value.Contains(expectedQueryString))
{
context.Response.StatusCode = statusCode;
await context.Response.WriteAsync(responseBody);
}
else
{
context.Response.StatusCode = statusCode;
await context.Response.WriteAsync("downstream path didnt match base path");
}
}
});
})
.Build();
_builder.Start();
context.Response.StatusCode = statusCode;
await context.Response.WriteAsync(responseBody);
}
else
{
context.Response.StatusCode = statusCode;
await context.Response.WriteAsync("downstream path didnt match base path");
}
}
});
}
public void Dispose()
{
_builder?.Dispose();
_serviceHandler?.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 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Ocelot.Configuration.File;
using Shouldly;
using TestStack.BDDfy;
using Xunit;
namespace Ocelot.AcceptanceTests
{
using System;
using System.Collections.Generic;
using System.Net;
using Microsoft.AspNetCore.Http;
using Ocelot.Configuration.File;
using TestStack.BDDfy;
using Xunit;
public class SslTests : IDisposable
{
private IWebHost _builder;
private readonly Steps _steps;
private string _downstreamPath;
private readonly ServiceHandler _serviceHandler;
public SslTests()
{
_serviceHandler = new ServiceHandler();
_steps = new Steps();
}
@ -98,48 +95,26 @@ namespace Ocelot.AcceptanceTests
private void GivenThereIsAServiceRunningOn(string baseUrl, string basePath, int statusCode, string responseBody, int port)
{
_builder = new WebHostBuilder()
.UseUrls(baseUrl)
.UseKestrel(options =>
_serviceHandler.GivenThereIsAServiceRunningOn(baseUrl, basePath, "idsrv3test.pfx", "idsrv3test", port, async context =>
{
_downstreamPath = !string.IsNullOrEmpty(context.Request.PathBase.Value) ? context.Request.PathBase.Value : context.Request.Path.Value;
if (_downstreamPath != basePath)
{
options.Listen(IPAddress.Loopback, port, listenOptions =>
{
listenOptions.UseHttps("idsrv3test.pfx", "idsrv3test");
});
})
.UseContentRoot(Directory.GetCurrentDirectory())
.Configure(app =>
context.Response.StatusCode = statusCode;
await context.Response.WriteAsync("downstream path didnt match base path");
}
else
{
app.UsePathBase(basePath);
app.Run(async context =>
{
_downstreamPath = !string.IsNullOrEmpty(context.Request.PathBase.Value) ? context.Request.PathBase.Value : context.Request.Path.Value;
if(_downstreamPath != basePath)
{
context.Response.StatusCode = statusCode;
await context.Response.WriteAsync("downstream path didnt match base path");
}
else
{
context.Response.StatusCode = statusCode;
await context.Response.WriteAsync(responseBody);
}
});
})
.Build();
_builder.Start();
}
internal void ThenTheDownstreamUrlPathShouldBe(string expectedDownstreamPath)
{
_downstreamPath.ShouldBe(expectedDownstreamPath);
context.Response.StatusCode = statusCode;
await context.Response.WriteAsync(responseBody);
}
});
}
public void Dispose()
{
_builder?.Dispose();
_serviceHandler?.Dispose();
_steps.Dispose();
}
}

View File

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

View File

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

View File

@ -1,31 +1,30 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Ocelot.Configuration.File;
using Shouldly;
using TestStack.BDDfy;
using Xunit;
namespace Ocelot.AcceptanceTests
{
using System;
using System.Collections.Generic;
using System.Net;
using Microsoft.AspNetCore.Http;
using Ocelot.Configuration.File;
using TestStack.BDDfy;
using Xunit;
public class UpstreamHostTests : IDisposable
{
private IWebHost _builder;
private readonly Steps _steps;
private string _downstreamPath;
private readonly ServiceHandler _serviceHandler;
public UpstreamHostTests()
{
_serviceHandler = new ServiceHandler();
_steps = new Steps();
}
[Fact]
public void should_return_response_200_with_simple_url_and_hosts_match()
{
int port = 64905;
var configuration = new FileConfiguration
{
ReRoutes = new List<FileReRoute>
@ -39,7 +38,7 @@ namespace Ocelot.AcceptanceTests
new FileHostAndPort
{
Host = "localhost",
Port = 51875,
Port = port,
}
},
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.GivenOcelotIsRunning())
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
@ -61,6 +60,8 @@ namespace Ocelot.AcceptanceTests
[Fact]
public void should_return_response_200_with_simple_url_and_hosts_match_multiple_re_routes()
{
int port = 64904;
var configuration = new FileConfiguration
{
ReRoutes = new List<FileReRoute>
@ -74,7 +75,7 @@ namespace Ocelot.AcceptanceTests
new FileHostAndPort
{
Host = "localhost",
Port = 51875,
Port = port,
}
},
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.GivenOcelotIsRunning())
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
@ -112,6 +113,8 @@ namespace Ocelot.AcceptanceTests
[Fact]
public void should_return_response_200_with_simple_url_and_hosts_match_multiple_re_routes_reversed()
{
int port = 64903;
var configuration = new FileConfiguration
{
ReRoutes = new List<FileReRoute>
@ -141,7 +144,7 @@ namespace Ocelot.AcceptanceTests
new FileHostAndPort
{
Host = "localhost",
Port = 51875,
Port = port,
}
},
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.GivenOcelotIsRunning())
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
@ -163,6 +166,8 @@ namespace Ocelot.AcceptanceTests
[Fact]
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
{
ReRoutes = new List<FileReRoute>
@ -191,7 +196,7 @@ namespace Ocelot.AcceptanceTests
new FileHostAndPort
{
Host = "localhost",
Port = 51875,
Port = port,
}
},
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.GivenOcelotIsRunning())
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
@ -213,6 +218,8 @@ namespace Ocelot.AcceptanceTests
[Fact]
public void should_return_response_404_with_simple_url_and_hosts_dont_match()
{
int port = 64901;
var configuration = new FileConfiguration
{
ReRoutes = new List<FileReRoute>
@ -226,7 +233,7 @@ namespace Ocelot.AcceptanceTests
new FileHostAndPort
{
Host = "localhost",
Port = 51875,
Port = port,
}
},
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.GivenOcelotIsRunning())
.When(x => _steps.WhenIGetUrlOnTheApiGateway("/"))
@ -246,43 +253,26 @@ namespace Ocelot.AcceptanceTests
private void GivenThereIsAServiceRunningOn(string baseUrl, string basePath, int statusCode, string responseBody)
{
_builder = new WebHostBuilder()
.UseUrls(baseUrl)
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.Configure(app =>
_serviceHandler.GivenThereIsAServiceRunningOn(baseUrl, basePath, async context =>
{
_downstreamPath = !string.IsNullOrEmpty(context.Request.PathBase.Value) ? context.Request.PathBase.Value : context.Request.Path.Value;
if (_downstreamPath != basePath)
{
app.UsePathBase(basePath);
app.Run(async context =>
{
_downstreamPath = !string.IsNullOrEmpty(context.Request.PathBase.Value) ? context.Request.PathBase.Value : context.Request.Path.Value;
if(_downstreamPath != basePath)
{
context.Response.StatusCode = statusCode;
await context.Response.WriteAsync("downstream path didnt match base path");
}
else
{
context.Response.StatusCode = statusCode;
await context.Response.WriteAsync(responseBody);
}
});
})
.Build();
_builder.Start();
}
internal void ThenTheDownstreamUrlPathShouldBe(string expectedDownstreamPath)
{
_downstreamPath.ShouldBe(expectedDownstreamPath);
context.Response.StatusCode = statusCode;
await context.Response.WriteAsync("downstream path didnt match base path");
}
else
{
context.Response.StatusCode = statusCode;
await context.Response.WriteAsync(responseBody);
}
});
}
public void Dispose()
{
_builder?.Dispose();
_serviceHandler?.Dispose();
_steps.Dispose();
}
}

View File

@ -1,35 +1,29 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.WebSockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Consul;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Ocelot.Configuration.File;
using Shouldly;
using TestStack.BDDfy;
using Xunit;
namespace Ocelot.AcceptanceTests
{
using System;
using System.Collections.Generic;
using System.Net.WebSockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Consul;
using Microsoft.AspNetCore.Http;
using Ocelot.Configuration.File;
using Shouldly;
using TestStack.BDDfy;
using Xunit;
public class WebSocketTests : IDisposable
{
private IWebHost _firstDownstreamHost;
private IWebHost _secondDownstreamHost;
private readonly List<string> _secondRecieved;
private readonly List<string> _firstRecieved;
private readonly List<ServiceEntry> _serviceEntries;
private readonly Steps _steps;
private IWebHost _fakeConsulBuilder;
private readonly ServiceHandler _serviceHandler;
public WebSocketTests()
{
_serviceHandler = new ServiceHandler();
_steps = new Steps();
_firstRecieved = new List<string>();
_secondRecieved = new List<string>();
@ -211,25 +205,13 @@ namespace Ocelot.AcceptanceTests
private void GivenThereIsAFakeConsulServiceDiscoveryProvider(string url, string serviceName)
{
_fakeConsulBuilder = new WebHostBuilder()
.UseUrls(url)
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseUrls(url)
.Configure(app =>
_serviceHandler.GivenThereIsAServiceRunningOn(url, async context =>
{
if (context.Request.Path.Value == $"/v1/health/service/{serviceName}")
{
app.Run(async context =>
{
if (context.Request.Path.Value == $"/v1/health/service/{serviceName}")
{
await context.Response.WriteJsonAsync(_serviceEntries);
}
});
})
.Build();
_fakeConsulBuilder.Start();
await context.Response.WriteJsonAsync(_serviceEntries);
}
});
}
private async Task WhenIStartTheClients()
@ -333,94 +315,48 @@ namespace Ocelot.AcceptanceTests
private async Task StartFakeDownstreamService(string url, string path)
{
_firstDownstreamHost = new WebHostBuilder()
.ConfigureServices(s => { }).UseKestrel()
.UseUrls(url)
.UseContentRoot(Directory.GetCurrentDirectory())
.ConfigureAppConfiguration((hostingContext, config) =>
await _serviceHandler.StartFakeDownstreamService(url, path, async(context, next) =>
{
if (context.Request.Path == path)
{
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.WebSockets.IsWebSocketRequest)
{
if (context.Request.Path == path)
{
if (context.WebSockets.IsWebSocketRequest)
{
WebSocket webSocket = await context.WebSockets.AcceptWebSocketAsync();
await Echo(webSocket);
}
else
{
context.Response.StatusCode = 400;
}
}
else
{
await next();
}
});
})
.UseIISIntegration().Build();
await _firstDownstreamHost.StartAsync();
var webSocket = await context.WebSockets.AcceptWebSocketAsync();
await Echo(webSocket);
}
else
{
context.Response.StatusCode = 400;
}
}
else
{
await next();
}
});
}
private async Task StartSecondFakeDownstreamService(string url, string path)
{
_secondDownstreamHost = new WebHostBuilder()
.ConfigureServices(s => { }).UseKestrel()
.UseUrls(url)
.UseContentRoot(Directory.GetCurrentDirectory())
.ConfigureAppConfiguration((hostingContext, config) =>
await _serviceHandler.StartFakeDownstreamService(url, path, async (context, next) =>
{
if (context.Request.Path == path)
{
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.WebSockets.IsWebSocketRequest)
{
if (context.Request.Path == path)
{
if (context.WebSockets.IsWebSocketRequest)
{
WebSocket webSocket = await context.WebSockets.AcceptWebSocketAsync();
await Message(webSocket);
}
else
{
context.Response.StatusCode = 400;
}
}
else
{
await next();
}
});
})
.UseIISIntegration().Build();
await _secondDownstreamHost.StartAsync();
WebSocket webSocket = await context.WebSockets.AcceptWebSocketAsync();
await Message(webSocket);
}
else
{
context.Response.StatusCode = 400;
}
}
else
{
await next();
}
});
}
private async Task Echo(WebSocket webSocket)
@ -473,10 +409,8 @@ namespace Ocelot.AcceptanceTests
public void Dispose()
{
_serviceHandler?.Dispose();
_steps.Dispose();
_firstDownstreamHost?.Dispose();
_secondDownstreamHost?.Dispose();
_fakeConsulBuilder?.Dispose();
}
}
}