From 99a15d866828270c810d4c62930c0cec67b9f977 Mon Sep 17 00:00:00 2001 From: jlukawska <56401969+jlukawska@users.noreply.github.com> Date: Mon, 13 Apr 2020 19:43:25 +0200 Subject: [PATCH] Fix/1157 fix UpstreamHost validation (#1169) * #1157: fix reRoute UpstreamHost validation * change port number --- .../FileConfigurationFluentValidator.cs | 2 +- .../AdministrationTests.cs | 2 +- .../FileConfigurationFluentValidatorTests.cs | 126 ++++++++++++++++++ 3 files changed, 128 insertions(+), 2 deletions(-) diff --git a/src/Ocelot/Configuration/Validator/FileConfigurationFluentValidator.cs b/src/Ocelot/Configuration/Validator/FileConfigurationFluentValidator.cs index e2f7f24d1..e40f17efa 100644 --- a/src/Ocelot/Configuration/Validator/FileConfigurationFluentValidator.cs +++ b/src/Ocelot/Configuration/Validator/FileConfigurationFluentValidator.cs @@ -135,7 +135,7 @@ private static bool IsNotDuplicateIn(FileReRoute reRoute, { var matchingReRoutes = reRoutes .Where(r => r.UpstreamPathTemplate == reRoute.UpstreamPathTemplate - && (r.UpstreamHost == reRoute.UpstreamHost || reRoute.UpstreamHost == null)) + && r.UpstreamHost == reRoute.UpstreamHost) .ToList(); if (matchingReRoutes.Count == 1) diff --git a/test/Ocelot.IntegrationTests/AdministrationTests.cs b/test/Ocelot.IntegrationTests/AdministrationTests.cs index b9d6b0686..4d3f464d8 100644 --- a/test/Ocelot.IntegrationTests/AdministrationTests.cs +++ b/test/Ocelot.IntegrationTests/AdministrationTests.cs @@ -342,7 +342,7 @@ private void ThenTheConfigurationIsSavedCorrectly(FileConfiguration expected) public void should_get_file_configuration_edit_and_post_updated_version_redirecting_reroute() { var fooPort = 47689; - var barPort = 47690; + var barPort = 27654; var initialConfiguration = new FileConfiguration { diff --git a/test/Ocelot.UnitTests/Configuration/Validation/FileConfigurationFluentValidatorTests.cs b/test/Ocelot.UnitTests/Configuration/Validation/FileConfigurationFluentValidatorTests.cs index ffb4bd975..1879b90d3 100644 --- a/test/Ocelot.UnitTests/Configuration/Validation/FileConfigurationFluentValidatorTests.cs +++ b/test/Ocelot.UnitTests/Configuration/Validation/FileConfigurationFluentValidatorTests.cs @@ -1119,6 +1119,132 @@ public void configuration_is_valid_with_duplicate_reroutes_different_verbs() .BDDfy(); } + [Fact] + public void configuration_is_not_valid_with_duplicate_reroutes_with_duplicated_upstreamhosts() + { + this.Given(x => x.GivenAConfiguration(new FileConfiguration + { + ReRoutes = new List + { + new FileReRoute + { + DownstreamPathTemplate = "/api/products/", + UpstreamPathTemplate = "/asdf/", + DownstreamHostAndPorts = new List + { + new FileHostAndPort + { + Host = "bbc.co.uk", + } + }, + UpstreamHttpMethod = new List(), + UpstreamHost = "upstreamhost" + }, + new FileReRoute + { + DownstreamPathTemplate = "/www/test/", + UpstreamPathTemplate = "/asdf/", + DownstreamHostAndPorts = new List + { + new FileHostAndPort + { + Host = "bbc.co.uk", + } + }, + UpstreamHttpMethod = new List(), + UpstreamHost = "upstreamhost" + } + } + })) + .When(x => x.WhenIValidateTheConfiguration()) + .Then(x => x.ThenTheResultIsNotValid()) + .And(x => x.ThenTheErrorMessageAtPositionIs(0, "reRoute /asdf/ has duplicate")) + .BDDfy(); + } + + [Fact] + public void configuration_is_valid_with_duplicate_reroutes_but_different_upstreamhosts() + { + this.Given(x => x.GivenAConfiguration(new FileConfiguration + { + ReRoutes = new List + { + new FileReRoute + { + DownstreamPathTemplate = "/api/products/", + UpstreamPathTemplate = "/asdf/", + DownstreamHostAndPorts = new List + { + new FileHostAndPort + { + Host = "bbc.co.uk", + } + }, + UpstreamHttpMethod = new List(), + UpstreamHost = "upstreamhost111" + }, + new FileReRoute + { + DownstreamPathTemplate = "/www/test/", + UpstreamPathTemplate = "/asdf/", + DownstreamHostAndPorts = new List + { + new FileHostAndPort + { + Host = "bbc.co.uk", + } + }, + UpstreamHttpMethod = new List(), + UpstreamHost = "upstreamhost222" + } + } + })) + .When(x => x.WhenIValidateTheConfiguration()) + .Then(x => x.ThenTheResultIsValid()) + .BDDfy(); + } + + [Fact] + public void configuration_is_valid_with_duplicate_reroutes_but_one_upstreamhost_is_not_set() + { + this.Given(x => x.GivenAConfiguration(new FileConfiguration + { + ReRoutes = new List + { + new FileReRoute + { + DownstreamPathTemplate = "/api/products/", + UpstreamPathTemplate = "/asdf/", + DownstreamHostAndPorts = new List + { + new FileHostAndPort + { + Host = "bbc.co.uk", + } + }, + UpstreamHttpMethod = new List(), + UpstreamHost = "upstreamhost" + }, + new FileReRoute + { + DownstreamPathTemplate = "/www/test/", + UpstreamPathTemplate = "/asdf/", + DownstreamHostAndPorts = new List + { + new FileHostAndPort + { + Host = "bbc.co.uk", + } + }, + UpstreamHttpMethod = new List() + } + } + })) + .When(x => x.WhenIValidateTheConfiguration()) + .Then(x => x.ThenTheResultIsValid()) + .BDDfy(); + } + [Fact] public void configuration_is_invalid_with_invalid_rate_limit_configuration() {