forked from ThreeMammals/Ocelot
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
190 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.AspNetCore.TestHost; | ||
using Microsoft.Extensions.Configuration; | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Linq; | ||
using Ocelot.Configuration.File; | ||
using Ocelot.DependencyInjection; | ||
using Ocelot.Middleware; | ||
using Shouldly; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Net.Http; | ||
using TestStack.BDDfy; | ||
using Xunit; | ||
|
||
namespace Ocelot.AcceptanceTests | ||
{ | ||
public class ConfigurationMergeTests | ||
{ | ||
private Steps _steps; | ||
private IWebHostBuilder _webHostBuilder; | ||
private TestServer _ocelotServer; | ||
private HttpClient _ocelotClient; | ||
|
||
public ConfigurationMergeTests() | ||
{ | ||
_steps = new Steps(); | ||
} | ||
|
||
[Fact] | ||
public void should_merge_reroutes_custom_properties() | ||
{ | ||
this.Given(x => GivenOcelotIsRunningWithMultipleConfigs()) | ||
.And(x => ThenConfigContentShouldBeMerged()) | ||
.BDDfy(); | ||
} | ||
|
||
private void GivenOcelotIsRunningWithMultipleConfigs() | ||
{ | ||
_webHostBuilder = new WebHostBuilder(); | ||
|
||
_webHostBuilder | ||
.UseContentRoot(Directory.GetCurrentDirectory()) | ||
.ConfigureAppConfiguration((hostingContext, config) => | ||
{ | ||
config.SetBasePath(hostingContext.HostingEnvironment.ContentRootPath); | ||
var env = hostingContext.HostingEnvironment; | ||
config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: false) | ||
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: false); | ||
config.AddOcelot("MergeConfiguration", hostingContext.HostingEnvironment); | ||
config.AddEnvironmentVariables(); | ||
}) | ||
.ConfigureServices(s => | ||
{ | ||
s.AddOcelot(); | ||
}) | ||
.Configure(app => | ||
{ | ||
app.UseOcelot().Wait(); | ||
}); | ||
|
||
_ocelotServer = new TestServer(_webHostBuilder); | ||
|
||
_ocelotClient = _ocelotServer.CreateClient(); | ||
} | ||
|
||
private void ThenConfigContentShouldBeMerged() | ||
{ | ||
var mergedConfigFileName = "ocelot.json"; | ||
File.Exists(mergedConfigFileName).ShouldBeTrue(); | ||
var lines = File.ReadAllText(mergedConfigFileName); | ||
var config = JObject.Parse(lines); | ||
|
||
config[nameof(FileConfiguration.ReRoutes)].ShouldNotBeNull(); | ||
config[nameof(FileConfiguration.ReRoutes)].Children().Count().ShouldBe(3); | ||
|
||
var routeWithCustomProperty = config[nameof(FileConfiguration.ReRoutes)].Children().SingleOrDefault(c => c["CustomStrategyProperty"] != null); | ||
routeWithCustomProperty.ShouldNotBeNull(); | ||
var customProperty = routeWithCustomProperty["CustomStrategyProperty"]; | ||
customProperty["GET"].ShouldNotBeNull(); | ||
customProperty["GET"].Children().Count().ShouldBe(1); | ||
customProperty["GET"].Children().FirstOrDefault().ShouldBe("SomeCustomStrategyMethodA"); | ||
customProperty["POST"].ShouldNotBeNull(); | ||
customProperty["POST"].Children().Count().ShouldBe(1); | ||
customProperty["POST"].Children().FirstOrDefault().ShouldBe("SomeCustomStrategyMethodB"); | ||
|
||
var routeWithCustomProperty2 = config[nameof(FileConfiguration.ReRoutes)].Children().SingleOrDefault(c => c["somethingmore"] != null); | ||
routeWithCustomProperty2.ShouldNotBeNull(); | ||
routeWithCustomProperty2["somethingmore"].ShouldBe("something"); | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
test/Ocelot.AcceptanceTests/MergeConfiguration/ocelot.global.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"ReRoutes": [ | ||
{ | ||
"DownstreamPathTemplate": "/{global}", | ||
"DownstreamScheme": "http", | ||
"DownstreamHostAndPorts": [ | ||
{ | ||
"Host": "localhost", | ||
"Port": 54001 | ||
} | ||
], | ||
"UpstreamPathTemplate": "/{global}", | ||
"UpstreamHttpMethod": [ "Get" ], | ||
"somethingmore": "something" | ||
} | ||
], | ||
"GlobalConfiguration": { | ||
"BaseUrl": "http://api.test.com" | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
test/Ocelot.AcceptanceTests/MergeConfiguration/ocelot.xservices.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{ | ||
"ReRoutes": [ | ||
{ | ||
"DownstreamPathTemplate": "/xservice.svc/serviceMethodA/byNumber/{number}", | ||
"UpstreamPathTemplate": "/gw/serviceMethodA/byNumber/{number}", | ||
"UpstreamHttpMethod": [ | ||
"Get", | ||
"Post" | ||
], | ||
"DownstreamScheme": "http", | ||
"DownstreamHostAndPorts": [ | ||
{ | ||
"Host": "host", | ||
"Port": 80 | ||
} | ||
], | ||
"CustomStrategyProperty": { | ||
"GET": [ | ||
"SomeCustomStrategyMethodA" | ||
], | ||
"POST": [ | ||
"SomeCustomStrategyMethodB" | ||
] | ||
} | ||
} | ||
] | ||
} |
17 changes: 17 additions & 0 deletions
17
test/Ocelot.AcceptanceTests/MergeConfiguration/ocelot.yservices.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"ReRoutes": [ | ||
{ | ||
"DownstreamPathTemplate": "/b/{action}", | ||
"DownstreamScheme": "http", | ||
"DownstreamHostAndPorts": [ | ||
{ | ||
"Host": "localhost", | ||
"Port": 54001 | ||
} | ||
], | ||
"UpstreamPathTemplate": "/b/{action}", | ||
"UpstreamHttpMethod": [ "Get" ], | ||
"MyCustomProperty": "bbb" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters