Skip to content

Commit 99a15d8

Browse files
authored
Fix/1157 fix UpstreamHost validation (#1169)
* #1157: fix reRoute UpstreamHost validation * change port number
1 parent 53d7f56 commit 99a15d8

File tree

3 files changed

+128
-2
lines changed

3 files changed

+128
-2
lines changed

src/Ocelot/Configuration/Validator/FileConfigurationFluentValidator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ private static bool IsNotDuplicateIn(FileReRoute reRoute,
135135
{
136136
var matchingReRoutes = reRoutes
137137
.Where(r => r.UpstreamPathTemplate == reRoute.UpstreamPathTemplate
138-
&& (r.UpstreamHost == reRoute.UpstreamHost || reRoute.UpstreamHost == null))
138+
&& r.UpstreamHost == reRoute.UpstreamHost)
139139
.ToList();
140140

141141
if (matchingReRoutes.Count == 1)

test/Ocelot.IntegrationTests/AdministrationTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ private void ThenTheConfigurationIsSavedCorrectly(FileConfiguration expected)
342342
public void should_get_file_configuration_edit_and_post_updated_version_redirecting_reroute()
343343
{
344344
var fooPort = 47689;
345-
var barPort = 47690;
345+
var barPort = 27654;
346346

347347
var initialConfiguration = new FileConfiguration
348348
{

test/Ocelot.UnitTests/Configuration/Validation/FileConfigurationFluentValidatorTests.cs

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1119,6 +1119,132 @@ public void configuration_is_valid_with_duplicate_reroutes_different_verbs()
11191119
.BDDfy();
11201120
}
11211121

1122+
[Fact]
1123+
public void configuration_is_not_valid_with_duplicate_reroutes_with_duplicated_upstreamhosts()
1124+
{
1125+
this.Given(x => x.GivenAConfiguration(new FileConfiguration
1126+
{
1127+
ReRoutes = new List<FileReRoute>
1128+
{
1129+
new FileReRoute
1130+
{
1131+
DownstreamPathTemplate = "/api/products/",
1132+
UpstreamPathTemplate = "/asdf/",
1133+
DownstreamHostAndPorts = new List<FileHostAndPort>
1134+
{
1135+
new FileHostAndPort
1136+
{
1137+
Host = "bbc.co.uk",
1138+
}
1139+
},
1140+
UpstreamHttpMethod = new List<string>(),
1141+
UpstreamHost = "upstreamhost"
1142+
},
1143+
new FileReRoute
1144+
{
1145+
DownstreamPathTemplate = "/www/test/",
1146+
UpstreamPathTemplate = "/asdf/",
1147+
DownstreamHostAndPorts = new List<FileHostAndPort>
1148+
{
1149+
new FileHostAndPort
1150+
{
1151+
Host = "bbc.co.uk",
1152+
}
1153+
},
1154+
UpstreamHttpMethod = new List<string>(),
1155+
UpstreamHost = "upstreamhost"
1156+
}
1157+
}
1158+
}))
1159+
.When(x => x.WhenIValidateTheConfiguration())
1160+
.Then(x => x.ThenTheResultIsNotValid())
1161+
.And(x => x.ThenTheErrorMessageAtPositionIs(0, "reRoute /asdf/ has duplicate"))
1162+
.BDDfy();
1163+
}
1164+
1165+
[Fact]
1166+
public void configuration_is_valid_with_duplicate_reroutes_but_different_upstreamhosts()
1167+
{
1168+
this.Given(x => x.GivenAConfiguration(new FileConfiguration
1169+
{
1170+
ReRoutes = new List<FileReRoute>
1171+
{
1172+
new FileReRoute
1173+
{
1174+
DownstreamPathTemplate = "/api/products/",
1175+
UpstreamPathTemplate = "/asdf/",
1176+
DownstreamHostAndPorts = new List<FileHostAndPort>
1177+
{
1178+
new FileHostAndPort
1179+
{
1180+
Host = "bbc.co.uk",
1181+
}
1182+
},
1183+
UpstreamHttpMethod = new List<string>(),
1184+
UpstreamHost = "upstreamhost111"
1185+
},
1186+
new FileReRoute
1187+
{
1188+
DownstreamPathTemplate = "/www/test/",
1189+
UpstreamPathTemplate = "/asdf/",
1190+
DownstreamHostAndPorts = new List<FileHostAndPort>
1191+
{
1192+
new FileHostAndPort
1193+
{
1194+
Host = "bbc.co.uk",
1195+
}
1196+
},
1197+
UpstreamHttpMethod = new List<string>(),
1198+
UpstreamHost = "upstreamhost222"
1199+
}
1200+
}
1201+
}))
1202+
.When(x => x.WhenIValidateTheConfiguration())
1203+
.Then(x => x.ThenTheResultIsValid())
1204+
.BDDfy();
1205+
}
1206+
1207+
[Fact]
1208+
public void configuration_is_valid_with_duplicate_reroutes_but_one_upstreamhost_is_not_set()
1209+
{
1210+
this.Given(x => x.GivenAConfiguration(new FileConfiguration
1211+
{
1212+
ReRoutes = new List<FileReRoute>
1213+
{
1214+
new FileReRoute
1215+
{
1216+
DownstreamPathTemplate = "/api/products/",
1217+
UpstreamPathTemplate = "/asdf/",
1218+
DownstreamHostAndPorts = new List<FileHostAndPort>
1219+
{
1220+
new FileHostAndPort
1221+
{
1222+
Host = "bbc.co.uk",
1223+
}
1224+
},
1225+
UpstreamHttpMethod = new List<string>(),
1226+
UpstreamHost = "upstreamhost"
1227+
},
1228+
new FileReRoute
1229+
{
1230+
DownstreamPathTemplate = "/www/test/",
1231+
UpstreamPathTemplate = "/asdf/",
1232+
DownstreamHostAndPorts = new List<FileHostAndPort>
1233+
{
1234+
new FileHostAndPort
1235+
{
1236+
Host = "bbc.co.uk",
1237+
}
1238+
},
1239+
UpstreamHttpMethod = new List<string>()
1240+
}
1241+
}
1242+
}))
1243+
.When(x => x.WhenIValidateTheConfiguration())
1244+
.Then(x => x.ThenTheResultIsValid())
1245+
.BDDfy();
1246+
}
1247+
11221248
[Fact]
11231249
public void configuration_is_invalid_with_invalid_rate_limit_configuration()
11241250
{

0 commit comments

Comments
 (0)