mucking about on lunch

This commit is contained in:
Tom Pallister 2016-08-19 13:46:46 +01:00
parent d3b9fddcfd
commit dada014f48
3 changed files with 31 additions and 26 deletions

View File

@ -8,6 +8,8 @@ using Ocelot.Library.Infrastructure.UrlPathTemplateRepository;
namespace Ocelot.Library.Middleware namespace Ocelot.Library.Middleware
{ {
using System.Net;
public class ProxyMiddleware public class ProxyMiddleware
{ {
private readonly RequestDelegate _next; private readonly RequestDelegate _next;
@ -49,9 +51,10 @@ namespace Ocelot.Library.Middleware
} }
} }
if (!urlPathMatch.Match) if (urlPathMatch == null || !urlPathMatch.Match)
{ {
throw new Exception("BOOOM TING! no match"); context.Response.StatusCode = (int)HttpStatusCode.NotFound;
return;
} }
var upstreamHostUrl = _hostUrlRepository.GetBaseUrlMap(urlPathMatch.DownstreamUrlPathTemplate); var upstreamHostUrl = _hostUrlRepository.GetBaseUrlMap(urlPathMatch.DownstreamUrlPathTemplate);

View File

@ -8,6 +8,11 @@ using Ocelot.Library.Middleware;
namespace Ocelot namespace Ocelot
{ {
using Library.Infrastructure.HostUrlRepository;
using Library.Infrastructure.UrlPathMatcher;
using Library.Infrastructure.UrlPathReplacer;
using Library.Infrastructure.UrlPathTemplateRepository;
public class Startup public class Startup
{ {
public Startup(IHostingEnvironment env) public Startup(IHostingEnvironment env)
@ -26,7 +31,11 @@ namespace Ocelot
public void ConfigureServices(IServiceCollection services) public void ConfigureServices(IServiceCollection services)
{ {
// Add framework services. // Add framework services.
services.AddSingleton<IHostUrlMapRepository, InMemoryHostUrlMapRepository>();
services.AddSingleton<IUrlPathToUrlPathTemplateMatcher, UrlPathToUrlPathTemplateMatcher>();
services.AddSingleton<IHostUrlMapRepository, InMemoryHostUrlMapRepository>();
services.AddSingleton<IUpstreamUrlPathTemplateVariableReplacer, UpstreamUrlPathTemplateVariableReplacer>();
services.AddSingleton<IUrlPathTemplateMapRepository, InMemoryUrlPathTemplateMapRepository>();
} }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
@ -37,11 +46,6 @@ namespace Ocelot
loggerFactory.AddDebug(); loggerFactory.AddDebug();
app.UseProxy(); app.UseProxy();
//app.Run()
// app.Run(async context =>
// {
// await context.Response.WriteAsync("Hello from Tom");
// });
} }
} }
} }

View File

@ -1,6 +1,5 @@
using System; using System;
using System.Net.Http; using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.TestHost; using Microsoft.AspNetCore.TestHost;
using Xunit; using Xunit;
@ -9,41 +8,40 @@ using Shouldly;
namespace Ocelot.AcceptanceTests namespace Ocelot.AcceptanceTests
{ {
using System.Net;
public class RouterTests : IDisposable public class RouterTests : IDisposable
{ {
private FakeService _fakeService; private readonly FakeService _fakeService;
private readonly TestServer _server; private readonly TestServer _server;
private readonly HttpClient _client; private readonly HttpClient _client;
private HttpResponseMessage _response;
public RouterTests() public RouterTests()
{ {
_server = new TestServer(new WebHostBuilder() _server = new TestServer(new WebHostBuilder()
.UseStartup<Startup>()); .UseStartup<Startup>());
_client = _server.CreateClient(); _client = _server.CreateClient();
_fakeService = new FakeService(); _fakeService = new FakeService();
} }
[Fact] [Fact]
public void hello_world() public void should_return_response_404()
{ {
var response = _client.GetAsync("/").Result; WhenIRequestTheUrl("/");
response.EnsureSuccessStatusCode(); ThenTheStatusCodeShouldBe(HttpStatusCode.NotFound);
var responseString = response.Content.ReadAsStringAsync().Result;
responseString.ShouldBe("Hello from Tom");
} }
[Fact] private void WhenIRequestTheUrl(string url)
public async Task can_route_request()
{ {
_fakeService.Start("http://localhost:5001"); _response = _client.GetAsync("/").Result;
}
// Act private void ThenTheStatusCodeShouldBe(HttpStatusCode expectedHttpStatusCode)
var response = await _client.GetAsync("/"); {
response.EnsureSuccessStatusCode(); _response.StatusCode.ShouldBe(expectedHttpStatusCode);
var responseString = await response.Content.ReadAsStringAsync();
responseString.ShouldBe("Hello from Laura");
} }
public void Dispose() public void Dispose()