1+ using System ;
2+ using System . Reflection ;
3+
4+ using NUnit . Framework ;
5+
6+ using Route4MeSDKLibrary ;
7+
8+ namespace Route4MeSdkV5UnitTest . V5
9+ {
10+ [ TestFixture ]
11+ public class ConfigurationTests
12+ {
13+ [ Test ]
14+ public void HttpClient_UsesConfiguredTimeout_WhenCreated ( )
15+ {
16+ // Arrange
17+ var originalTimeout = Route4MeConfig . HttpTimeout ;
18+ var customTimeout = TimeSpan . FromSeconds ( 45 ) ;
19+
20+ try
21+ {
22+ // Set custom timeout before creating HttpClient
23+ Route4MeConfig . HttpTimeout = customTimeout ;
24+
25+ // Use a unique base address to ensure we get a fresh HttpClient instance
26+ var uniqueBaseAddress = $ "https://test-{ Guid . NewGuid ( ) } .route4me.com";
27+
28+ // Act - Use reflection to access the internal HttpClientHolderManager
29+ var holderManagerType = Type . GetType ( "Route4MeSDKLibrary.HttpClientHolderManager, Route4MeSDKLibrary" ) ;
30+ Assert . IsNotNull ( holderManagerType , "HttpClientHolderManager type not found" ) ;
31+
32+ var acquireMethod = holderManagerType . GetMethod ( "AcquireHttpClientHolder" ,
33+ BindingFlags . Public | BindingFlags . Static ) ;
34+ Assert . IsNotNull ( acquireMethod , "AcquireHttpClientHolder method not found" ) ;
35+
36+ var holder = acquireMethod . Invoke ( null , new object [ ] { uniqueBaseAddress , null } ) ;
37+ Assert . IsNotNull ( holder , "HttpClientHolder should not be null" ) ;
38+
39+ // Get the HttpClient from the holder
40+ var httpClientProperty = holder . GetType ( ) . GetProperty ( "HttpClient" ) ;
41+ Assert . IsNotNull ( httpClientProperty , "HttpClient property not found" ) ;
42+
43+ var httpClient = httpClientProperty . GetValue ( holder ) as System . Net . Http . HttpClient ;
44+ Assert . IsNotNull ( httpClient , "HttpClient should not be null" ) ;
45+
46+ // Assert - Verify the HttpClient has the custom timeout
47+ Assert . AreEqual ( customTimeout , httpClient . Timeout ,
48+ $ "HttpClient timeout should be { customTimeout . TotalSeconds } seconds") ;
49+
50+ // Cleanup - Release the holder
51+ var releaseMethod = holderManagerType . GetMethod ( "ReleaseHttpClientHolder" ,
52+ BindingFlags . Public | BindingFlags . Static ) ;
53+ releaseMethod ? . Invoke ( null , new object [ ] { uniqueBaseAddress } ) ;
54+ }
55+ finally
56+ {
57+ // Restore original timeout
58+ Route4MeConfig . HttpTimeout = originalTimeout ;
59+ }
60+ }
61+
62+ [ Test ]
63+ public void HttpClient_DefaultTimeout_Is30Seconds_WhenNoConfigurationSet ( )
64+ {
65+ // Arrange
66+ var originalTimeout = Route4MeConfig . HttpTimeout ;
67+
68+ try
69+ {
70+ // Reset to default
71+ Route4MeConfig . HttpTimeout = TimeSpan . FromSeconds ( 30 ) ;
72+
73+ // Use a unique base address to ensure we get a fresh HttpClient instance
74+ var uniqueBaseAddress = $ "https://test-default-{ Guid . NewGuid ( ) } .route4me.com";
75+
76+ // Act - Use reflection to access the internal HttpClientHolderManager
77+ var holderManagerType = Type . GetType ( "Route4MeSDKLibrary.HttpClientHolderManager, Route4MeSDKLibrary" ) ;
78+ var acquireMethod = holderManagerType . GetMethod ( "AcquireHttpClientHolder" ,
79+ BindingFlags . Public | BindingFlags . Static ) ;
80+ var holder = acquireMethod . Invoke ( null , new object [ ] { uniqueBaseAddress , null } ) ;
81+ var httpClientProperty = holder . GetType ( ) . GetProperty ( "HttpClient" ) ;
82+ var httpClient = httpClientProperty . GetValue ( holder ) as System . Net . Http . HttpClient ;
83+
84+ // Assert - Verify the HttpClient has the default 30-second timeout
85+ Assert . AreEqual ( TimeSpan . FromSeconds ( 30 ) , httpClient . Timeout ,
86+ "HttpClient should have default 30-second timeout" ) ;
87+
88+ // Cleanup
89+ var releaseMethod = holderManagerType . GetMethod ( "ReleaseHttpClientHolder" ,
90+ BindingFlags . Public | BindingFlags . Static ) ;
91+ releaseMethod ? . Invoke ( null , new object [ ] { uniqueBaseAddress } ) ;
92+ }
93+ finally
94+ {
95+ // Restore original timeout
96+ Route4MeConfig . HttpTimeout = originalTimeout ;
97+ }
98+ }
99+ }
100+ }
0 commit comments