File tree 2 files changed +33
-7
lines changed
src/HttpClientSettings/Extensions
test/HttpClientSettings.Tests/Extensions
2 files changed +33
-7
lines changed Original file line number Diff line number Diff line change @@ -10,17 +10,28 @@ public static IServiceCollection AddHttpClientSettings(this IServiceCollection s
10
10
{
11
11
var section = configuration . GetRequiredSection ( Constants . AppSettings . SectionName ) ;
12
12
13
- services . Configure < HttpClientAppSettings > ( section )
14
- . PostConfigure < HttpClientAppSettings > ( config =>
13
+ services . AddOptions < HttpClientAppSettings > ( )
14
+ . Bind ( section )
15
+ . Validate ( httpClientAppSettings =>
15
16
{
16
- if ( validateSettings )
17
- {
18
- var validator = new HttpClientAppSettingsValidator ( config ) ;
19
- validator . Validate ( ) ;
20
- }
17
+ if ( validateSettings ) ValidateHttpClientAppSettings ( httpClientAppSettings ) ;
18
+
19
+ return true ;
21
20
} ) ;
22
21
23
22
return services ;
24
23
}
24
+
25
+ internal static void ValidateHttpClientAppSettings ( HttpClientAppSettings httpClientAppSettings )
26
+ {
27
+ var validator = new HttpClientAppSettingsValidator ( httpClientAppSettings ) ;
28
+
29
+ var validationResponse = validator . Validate ( ) ;
30
+
31
+ if ( ! validationResponse . IsSuccess )
32
+ {
33
+ throw new InvalidAppSettingsException ( validationResponse . Errors ) ;
34
+ }
35
+ }
25
36
}
26
37
}
Original file line number Diff line number Diff line change @@ -5,6 +5,8 @@ namespace HttpClientSettings.Tests.Extensions;
5
5
6
6
public class ServiceCollectionExtensionsTests
7
7
{
8
+ private readonly HttpClientAppSettings _settings = new ( ) ;
9
+
8
10
[ Fact ]
9
11
public void AddHttpClientSettings_ShouldAddSettingsToConfig ( )
10
12
{
@@ -15,4 +17,17 @@ public void AddHttpClientSettings_ShouldAddSettingsToConfig()
15
17
16
18
services . Received ( ) . Configure < HttpClientAppSettings > ( config ) ;
17
19
}
20
+
21
+ [ Fact ]
22
+ public void ValidateHttpClientAppSettings_GivenInvalidAppSettings_ShouldThrowException ( )
23
+ {
24
+ var clients = Builder < HttpClientSetting > . CreateListOfSize ( 1 )
25
+ . All ( )
26
+ . With ( x => x . Name , "" )
27
+ . Build ( ) ;
28
+
29
+ _settings . LoadClientsForUnitTesting ( clients ) ;
30
+
31
+ Assert . Throws < InvalidAppSettingsException > ( ( ) => ServiceCollectionExtensions . ValidateHttpClientAppSettings ( _settings ) ) ;
32
+ }
18
33
}
You can’t perform that action at this time.
0 commit comments