-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* fix: create building the servicecollection * fix: handle default configuration * chore: extension methods invocation * Update src/Ocelot/DependencyInjection/ServiceCollectionExtensions.cs * Code review by @raman-m * No warnings for CS0618 * Given developer forgot to add environment, then?... Ups! Your Duck, duck! Hunting for `new FileInfo("")` bug... Done! * Coverage 100% --------- Co-authored-by: Adrien HUPOND <adrien.hupond@csgroup.eu> Co-authored-by: Raman Maksimchuk <dotnet044@gmail.com>
- Loading branch information
1 parent
d6eefc8
commit ded4d7e
Showing
6 changed files
with
201 additions
and
21 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
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
109 changes: 109 additions & 0 deletions
109
test/Ocelot.UnitTests/DependencyInjection/ServiceCollectionExtensionsTests.cs
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,109 @@ | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.DependencyInjection.Extensions; | ||
using Ocelot.DependencyInjection; | ||
using System.Reflection; | ||
using Extensions = Ocelot.DependencyInjection.ServiceCollectionExtensions; | ||
|
||
namespace Ocelot.UnitTests.DependencyInjection; | ||
|
||
public class ServiceCollectionExtensionsTests | ||
{ | ||
[Fact] | ||
[Trait("PR", "1986")] | ||
[Trait("Issue", "1518")] | ||
public void AddOcelot_NoConfiguration_DefaultConfiguration() | ||
{ | ||
// Arrange | ||
var services = new ServiceCollection(); | ||
|
||
// Act | ||
var ocelot = services.AddOcelot(); | ||
|
||
// Assert | ||
ocelot.ShouldNotBeNull() | ||
.Configuration.ShouldNotBeNull(); | ||
} | ||
|
||
[Theory] | ||
[Trait("PR", "1986")] | ||
[Trait("Issue", "1518")] | ||
[InlineData(false)] | ||
[InlineData(true)] | ||
public void FindConfiguration_HasDescriptor_HappyPath(bool hasConfig) | ||
{ | ||
// Arrange | ||
IConfiguration config = hasConfig ? new ConfigurationBuilder().Build() : null; | ||
var descriptor = new ServiceDescriptor(typeof(IConfiguration), (p) => config, ServiceLifetime.Transient); | ||
var services = new ServiceCollection().Add(descriptor); | ||
IWebHostEnvironment env = null; | ||
|
||
// Act | ||
var method = typeof(Extensions).GetMethod("FindConfiguration", BindingFlags.NonPublic | BindingFlags.Static); | ||
var actual = (IConfiguration)method.Invoke(null, [services, env]); | ||
|
||
// Assert | ||
actual.ShouldNotBeNull(); | ||
if (hasConfig) | ||
{ | ||
actual.Equals(config).ShouldBeTrue(); | ||
} | ||
} | ||
|
||
[Fact] | ||
[Trait("PR", "1986")] | ||
[Trait("Issue", "1518")] | ||
public void AddOcelotUsingBuilder_NoConfigurationParam_ShouldFindConfiguration() | ||
{ | ||
// Arrange | ||
var services = new ServiceCollection(); | ||
var config = new ConfigurationBuilder().Build(); | ||
services.AddSingleton<IConfiguration>(config); | ||
|
||
// Act | ||
var ocelot = services.AddOcelotUsingBuilder(null, CustomBuilder); | ||
|
||
// Assert | ||
AssertConfiguration(ocelot, config); | ||
} | ||
|
||
[Theory] | ||
[Trait("PR", "1986")] | ||
[Trait("Issue", "1518")] | ||
[InlineData(false)] | ||
[InlineData(true)] | ||
public void AddOcelotUsingBuilder_WithConfigurationParam_ShouldFindConfiguration(bool shouldFind) | ||
{ | ||
// Arrange | ||
var services = new ServiceCollection(); | ||
var config = new ConfigurationBuilder().Build(); | ||
if (shouldFind) | ||
{ | ||
services.AddSingleton<IConfiguration>(config); | ||
} | ||
|
||
// Act | ||
var ocelot = services.AddOcelotUsingBuilder(shouldFind ? null : config, CustomBuilder); | ||
|
||
// Assert | ||
AssertConfiguration(ocelot, config); | ||
} | ||
|
||
private void AssertConfiguration(IOcelotBuilder ocelot, IConfiguration config) | ||
{ | ||
ocelot.ShouldNotBeNull(); | ||
var actual = ocelot.Configuration.ShouldNotBeNull(); | ||
actual.Equals(config).ShouldBeTrue(); // check references equality | ||
actual.ShouldBe(config); | ||
Assert.Equal(1, _count); | ||
} | ||
|
||
private int _count; | ||
|
||
private IMvcCoreBuilder CustomBuilder(IMvcCoreBuilder builder, Assembly assembly) | ||
{ | ||
_count++; | ||
return builder; | ||
} | ||
} |
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