messing around with the proxy mdh the proxy middleware

This commit is contained in:
Tom Gardham-Pallister
2016-07-21 20:28:22 +01:00
parent 5e8719cde4
commit dbff2b9530
15 changed files with 213 additions and 35 deletions

View File

@ -0,0 +1,34 @@
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
namespace Ocelot.AcceptanceTests.Fake
{
public class FakeService
{
private Task _handler;
private IWebHost _webHostBuilder;
public void Start(string url)
{
_webHostBuilder = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseUrls(url)
.UseStartup<FakeStartup>()
.Build();
_handler = Task.Run(() => _webHostBuilder.Run());
}
public void Stop()
{
if(_webHostBuilder != null)
{
_webHostBuilder.Dispose();
_handler.Wait();
}
}
}
}

View File

@ -0,0 +1,40 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
namespace Ocelot.AcceptanceTests.Fake
{
public class FakeStartup
{
public FakeStartup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
Configuration = builder.Build();
}
public IConfigurationRoot Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
app.Run(async context =>
{
await context.Response.WriteAsync("Hello from Laura");
});
}
}
}

View File

@ -4,38 +4,51 @@ using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.TestHost;
using Xunit;
using Ocelot.AcceptanceTests.Fake;
using Shouldly;
namespace Ocelot.AcceptanceTests
{
public class RouterTests : IDisposable
{
private FakeService _fakeService;
private readonly TestServer _server;
private readonly HttpClient _client;
public RouterTests()
{
// Arrange
_server = new TestServer(new WebHostBuilder()
.UseStartup<Startup>());
_client = _server.CreateClient();
_fakeService = new FakeService();
}
[Fact]
public async Task ReturnHelloWorld()
public void hello_world()
{
var response = _client.GetAsync("/").Result;
response.EnsureSuccessStatusCode();
var responseString = response.Content.ReadAsStringAsync().Result;
responseString.ShouldBe("Hello from Tom");
}
[Fact]
public async Task can_route_request()
{
_fakeService.Start("http://localhost:5001");
// Act
var response = await _client.GetAsync("/");
response.EnsureSuccessStatusCode();
var responseString = await response.Content.ReadAsStringAsync();
// Assert
Assert.Equal("Hello from Tom",
responseString);
responseString.ShouldBe("Hello from Laura");
}
public void Dispose()
{
{
_fakeService.Stop();
_client.Dispose();
_server.Dispose();
}

View File

@ -77,7 +77,7 @@ namespace Ocelot.UnitTests
private void ThenTheRouteIsReturned()
{
_getRouteResponse.Data.DownstreamHostUrl.ShouldBe(_downstreamBaseUrl);
_getRouteResponse.Data.UrlPathTemplate.ShouldBe(_downstreamBaseUrl);
_getRouteResponse.Data.UpstreamHostUrl.ShouldBe(_upstreamBaseUrl);
}

View File

@ -1,3 +1,4 @@
using System.Collections.Generic;
using Ocelot.Library.Infrastructure.Responses;
using Ocelot.Library.Infrastructure.UrlPathTemplateRepository;
using Shouldly;
@ -12,6 +13,7 @@ namespace Ocelot.UnitTests
private IUrlPathTemplateMapRepository _repository;
private Response _response;
private Response<UrlPathTemplateMap> _getResponse;
private Response<List<UrlPathTemplateMap>> _listResponse;
public UrlPathTemplateMapRepositoryTests()
{
@ -34,6 +36,14 @@ namespace Ocelot.UnitTests
WhenIRetrieveTheUrlPathByDownstreamUrl();
ThenTheUrlPathIsReturned();
}
[Fact]
public void can_get_all_urls()
{
GivenIHaveSetUpADownstreamUrlPathAndAnUpstreamUrlPath("/api2", "http://www.someapi.com/api2");
WhenIRetrieveTheUrls();
ThenTheUrlsAreReturned();
}
[Fact]
public void should_return_error_response_when_url_path_already_used()
@ -75,12 +85,22 @@ namespace Ocelot.UnitTests
_getResponse = _repository.GetUrlPathTemplateMap(_downstreamUrlPath);
}
private void WhenIRetrieveTheUrls()
{
_listResponse = _repository.All;
}
private void ThenTheUrlPathIsReturned()
{
_getResponse.Data.DownstreamUrlPathTemplate.ShouldBe(_downstreamUrlPath);
_getResponse.Data.UpstreamUrlPathTemplate.ShouldBe(_upstreamUrlPath);
}
private void ThenTheUrlsAreReturned()
{
_listResponse.Data.Count.ShouldBeGreaterThan(0);
}
private void GivenIHaveSetUpADownstreamUrlPathAndAnUpstreamUrlPath(string downstream, string upstreamApiUrl)
{
GivenIHaveAnUpstreamUrlPath(upstreamApiUrl);

View File

@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Ocelot.Library.Infrastructure.UrlPathMatcher;
@ -25,7 +26,7 @@ namespace Ocelot.UnitTests
WhenIMatchThePaths();
ThenTheResultIsTrue();
ThenTheTemplatesDictionaryIs(new List<TemplateVariableNameAndValue>());
ThenTheUrlPathTemplateIs("api/product/products/");
}
[Fact]
@ -41,6 +42,7 @@ namespace Ocelot.UnitTests
WhenIMatchThePaths();
ThenTheResultIsTrue();
ThenTheTemplatesDictionaryIs(expectedTemplates);
ThenTheUrlPathTemplateIs("api/product/products/{productId}/variants/");
}
[Fact]
@ -56,6 +58,8 @@ namespace Ocelot.UnitTests
WhenIMatchThePaths();
ThenTheResultIsTrue();
ThenTheTemplatesDictionaryIs(expectedTemplates);
ThenTheUrlPathTemplateIs("api/product/products/{productId}");
}
[Fact]
@ -72,6 +76,8 @@ namespace Ocelot.UnitTests
WhenIMatchThePaths();
ThenTheResultIsTrue();
ThenTheTemplatesDictionaryIs(expectedTemplates);
ThenTheUrlPathTemplateIs("api/product/products/{productId}/{categoryId}");
}
[Fact]
@ -88,6 +94,8 @@ namespace Ocelot.UnitTests
WhenIMatchThePaths();
ThenTheResultIsTrue();
ThenTheTemplatesDictionaryIs(expectedTemplates);
ThenTheUrlPathTemplateIs("api/product/products/{productId}/categories/{categoryId}");
}
[Fact]
@ -105,6 +113,8 @@ namespace Ocelot.UnitTests
WhenIMatchThePaths();
ThenTheResultIsTrue();
ThenTheTemplatesDictionaryIs(expectedTemplates);
ThenTheUrlPathTemplateIs("api/product/products/{productId}/categories/{categoryId}/variant/{variantId}");
}
[Fact]
@ -121,6 +131,8 @@ namespace Ocelot.UnitTests
WhenIMatchThePaths();
ThenTheResultIsTrue();
ThenTheTemplatesDictionaryIs(expectedTemplates);
ThenTheUrlPathTemplateIs("api/product/products/{productId}/categories/{categoryId}/variant/");
}
private void ThenTheTemplatesDictionaryIs(List<TemplateVariableNameAndValue> expectedResults)
@ -133,6 +145,10 @@ namespace Ocelot.UnitTests
}
}
private void ThenTheUrlPathTemplateIs(string expectedUrlPathTemplate)
{
_result.UrlPathTemplate.ShouldBe(expectedUrlPathTemplate);
}
private void GivenIHaveADownstreamPath(string downstreamPath)
{
_downstreamPath = downstreamPath;