From dada014f4819f27381086ef23d212d3a1259f7e2 Mon Sep 17 00:00:00 2001 From: Tom Pallister Date: Fri, 19 Aug 2016 13:46:46 +0100 Subject: [PATCH] mucking about on lunch --- .../Middleware/ProxyMiddleware.cs | 7 ++-- src/Ocelot/Startup.cs | 16 +++++---- test/Ocelot.AcceptanceTests/RouterTests.cs | 34 +++++++++---------- 3 files changed, 31 insertions(+), 26 deletions(-) diff --git a/src/Ocelot.Library/Middleware/ProxyMiddleware.cs b/src/Ocelot.Library/Middleware/ProxyMiddleware.cs index cb5eb149..a56fbd17 100644 --- a/src/Ocelot.Library/Middleware/ProxyMiddleware.cs +++ b/src/Ocelot.Library/Middleware/ProxyMiddleware.cs @@ -8,6 +8,8 @@ using Ocelot.Library.Infrastructure.UrlPathTemplateRepository; namespace Ocelot.Library.Middleware { + using System.Net; + public class ProxyMiddleware { 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); diff --git a/src/Ocelot/Startup.cs b/src/Ocelot/Startup.cs index e13d551d..d95b6f65 100644 --- a/src/Ocelot/Startup.cs +++ b/src/Ocelot/Startup.cs @@ -8,6 +8,11 @@ using Ocelot.Library.Middleware; namespace Ocelot { + using Library.Infrastructure.HostUrlRepository; + using Library.Infrastructure.UrlPathMatcher; + using Library.Infrastructure.UrlPathReplacer; + using Library.Infrastructure.UrlPathTemplateRepository; + public class Startup { public Startup(IHostingEnvironment env) @@ -26,7 +31,11 @@ namespace Ocelot public void ConfigureServices(IServiceCollection services) { // Add framework services. - + services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. @@ -37,11 +46,6 @@ namespace Ocelot loggerFactory.AddDebug(); app.UseProxy(); - //app.Run() - // app.Run(async context => - // { - // await context.Response.WriteAsync("Hello from Tom"); - // }); } } } diff --git a/test/Ocelot.AcceptanceTests/RouterTests.cs b/test/Ocelot.AcceptanceTests/RouterTests.cs index 0381ff41..c3d5fafa 100644 --- a/test/Ocelot.AcceptanceTests/RouterTests.cs +++ b/test/Ocelot.AcceptanceTests/RouterTests.cs @@ -1,6 +1,5 @@ using System; using System.Net.Http; -using System.Threading.Tasks; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.TestHost; using Xunit; @@ -9,41 +8,40 @@ using Shouldly; namespace Ocelot.AcceptanceTests { + using System.Net; + public class RouterTests : IDisposable { - private FakeService _fakeService; + private readonly FakeService _fakeService; private readonly TestServer _server; private readonly HttpClient _client; + private HttpResponseMessage _response; public RouterTests() { _server = new TestServer(new WebHostBuilder() .UseStartup()); + _client = _server.CreateClient(); + _fakeService = new FakeService(); } [Fact] - public void hello_world() + public void should_return_response_404() { - var response = _client.GetAsync("/").Result; - response.EnsureSuccessStatusCode(); - - var responseString = response.Content.ReadAsStringAsync().Result; - responseString.ShouldBe("Hello from Tom"); + WhenIRequestTheUrl("/"); + ThenTheStatusCodeShouldBe(HttpStatusCode.NotFound); } - [Fact] - public async Task can_route_request() - { - _fakeService.Start("http://localhost:5001"); + private void WhenIRequestTheUrl(string url) + { + _response = _client.GetAsync("/").Result; + } - // Act - var response = await _client.GetAsync("/"); - response.EnsureSuccessStatusCode(); - - var responseString = await response.Content.ReadAsStringAsync(); - responseString.ShouldBe("Hello from Laura"); + private void ThenTheStatusCodeShouldBe(HttpStatusCode expectedHttpStatusCode) + { + _response.StatusCode.ShouldBe(expectedHttpStatusCode); } public void Dispose()