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() diff --git a/test/Ocelot.Benchmarks/BenchmarkDotNet.Artifacts/results/UrlPathToUrlPathTemplateMatcherBenchmarks-report-github.md b/test/Ocelot.Benchmarks/BenchmarkDotNet.Artifacts/results/UrlPathToUrlPathTemplateMatcherBenchmarks-report-github.md index ca7d330c..500c4d6e 100755 --- a/test/Ocelot.Benchmarks/BenchmarkDotNet.Artifacts/results/UrlPathToUrlPathTemplateMatcherBenchmarks-report-github.md +++ b/test/Ocelot.Benchmarks/BenchmarkDotNet.Artifacts/results/UrlPathToUrlPathTemplateMatcherBenchmarks-report-github.md @@ -14,7 +14,7 @@ Type=UrlPathToUrlPathTemplateMatcherBenchmarks Mode=Throughput Toolchain=Core GarbageCollection=Concurrent Workstation ``` - Method | Median | StdDev | Mean | StdError | StdDev | Op/s | Min | Q1 | Median | Q3 | Max | ------------ |------------ |---------- |------------ |---------- |---------- |----------- |------------ |------------ |------------ |------------ |------------ | - Benchmark1 | 180.4251 ns | 4.1294 ns | 180.4400 ns | 0.9234 ns | 4.1294 ns | 5542007.02 | 174.5503 ns | 177.6286 ns | 180.4251 ns | 182.5334 ns | 190.9792 ns | - Benchmark2 | 178.7267 ns | 6.1670 ns | 180.6081 ns | 1.3148 ns | 6.1670 ns | 5536849.8 | 174.0821 ns | 177.0992 ns | 178.7267 ns | 182.1962 ns | 198.1308 ns | + Method | Median | StdDev | Mean | StdError | StdDev | Op/s | Min | Q1 | Median | Q3 | Max | +----------- |------------ |----------- |------------ |---------- |----------- |----------- |------------ |------------ |------------ |------------ |------------ | + Benchmark1 | 184.4215 ns | 5.1537 ns | 185.3322 ns | 1.1524 ns | 5.1537 ns | 5395716.74 | 178.2386 ns | 181.8117 ns | 184.4215 ns | 188.2762 ns | 196.7310 ns | + Benchmark2 | 186.1899 ns | 35.7006 ns | 202.4315 ns | 3.9425 ns | 35.7006 ns | 4939943.34 | 176.9750 ns | 182.9672 ns | 186.1899 ns | 205.8946 ns | 369.0701 ns | diff --git a/test/Ocelot.Benchmarks/BenchmarkDotNet.Artifacts/results/UrlPathToUrlPathTemplateMatcherBenchmarks-report.csv b/test/Ocelot.Benchmarks/BenchmarkDotNet.Artifacts/results/UrlPathToUrlPathTemplateMatcherBenchmarks-report.csv index 2f7488c0..9aaa893e 100755 --- a/test/Ocelot.Benchmarks/BenchmarkDotNet.Artifacts/results/UrlPathToUrlPathTemplateMatcherBenchmarks-report.csv +++ b/test/Ocelot.Benchmarks/BenchmarkDotNet.Artifacts/results/UrlPathToUrlPathTemplateMatcherBenchmarks-report.csv @@ -1,3 +1,3 @@ Type;Method;Mode;Platform;Jit;Toolchain;Runtime;GarbageCollection;LaunchCount;WarmupCount;TargetCount;Affinity;Median;StdDev;Mean;StdError;StdDev;Op/s;Min;Q1;Median;Q3;Max -UrlPathToUrlPathTemplateMatcherBenchmarks;Benchmark1;Throughput;Host;Host;Core;Host;Concurrent Workstation;Auto;Auto;Auto;Auto;180.4251 ns;4.1294 ns;180.4400 ns;0.9234 ns;4.1294 ns;5542007.02;174.5503 ns;177.6286 ns;180.4251 ns;182.5334 ns;190.9792 ns -UrlPathToUrlPathTemplateMatcherBenchmarks;Benchmark2;Throughput;Host;Host;Core;Host;Concurrent Workstation;Auto;Auto;Auto;Auto;178.7267 ns;6.1670 ns;180.6081 ns;1.3148 ns;6.1670 ns;5536849.8;174.0821 ns;177.0992 ns;178.7267 ns;182.1962 ns;198.1308 ns +UrlPathToUrlPathTemplateMatcherBenchmarks;Benchmark1;Throughput;Host;Host;Core;Host;Concurrent Workstation;Auto;Auto;Auto;Auto;184.4215 ns;5.1537 ns;185.3322 ns;1.1524 ns;5.1537 ns;5395716.74;178.2386 ns;181.8117 ns;184.4215 ns;188.2762 ns;196.7310 ns +UrlPathToUrlPathTemplateMatcherBenchmarks;Benchmark2;Throughput;Host;Host;Core;Host;Concurrent Workstation;Auto;Auto;Auto;Auto;186.1899 ns;35.7006 ns;202.4315 ns;3.9425 ns;35.7006 ns;4939943.34;176.9750 ns;182.9672 ns;186.1899 ns;205.8946 ns;369.0701 ns diff --git a/test/Ocelot.Benchmarks/BenchmarkDotNet.Artifacts/results/UrlPathToUrlPathTemplateMatcherBenchmarks-report.html b/test/Ocelot.Benchmarks/BenchmarkDotNet.Artifacts/results/UrlPathToUrlPathTemplateMatcherBenchmarks-report.html index 229b23f1..6631b3b4 100755 --- a/test/Ocelot.Benchmarks/BenchmarkDotNet.Artifacts/results/UrlPathToUrlPathTemplateMatcherBenchmarks-report.html +++ b/test/Ocelot.Benchmarks/BenchmarkDotNet.Artifacts/results/UrlPathToUrlPathTemplateMatcherBenchmarks-report.html @@ -15,6 +15,6 @@ GarbageCollection=Concurrent Workstation - - + +
MethodMedianStdDev MeanStdErrorStdDevOp/s Min Q1Median Q3 Max
Benchmark1180.4251 ns4.1294 ns180.4400 ns0.9234 ns4.1294 ns5542007.02174.5503 ns177.6286 ns180.4251 ns182.5334 ns190.9792 ns
Benchmark2178.7267 ns6.1670 ns180.6081 ns1.3148 ns6.1670 ns5536849.8174.0821 ns177.0992 ns178.7267 ns182.1962 ns198.1308 ns
Benchmark1184.4215 ns5.1537 ns185.3322 ns1.1524 ns5.1537 ns5395716.74178.2386 ns181.8117 ns184.4215 ns188.2762 ns196.7310 ns
Benchmark2186.1899 ns35.7006 ns202.4315 ns3.9425 ns35.7006 ns4939943.34176.9750 ns182.9672 ns186.1899 ns205.8946 ns369.0701 ns