1+ using System ;
2+ using System . Reflection ;
3+ using Xunit ;
4+ using Contentstack . Core ;
5+ using Contentstack . Core . Configuration ;
6+ using Contentstack . Core . Internals ;
7+ using Microsoft . Extensions . Options ;
8+
9+ namespace Contentstack . Core . Tests
10+ {
11+ public class RegionHandlerTest
12+ {
13+ #region ContentstackRegion Enum Tests
14+
15+ [ Theory ]
16+ [ InlineData ( ContentstackRegion . US , 0 ) ]
17+ [ InlineData ( ContentstackRegion . EU , 1 ) ]
18+ [ InlineData ( ContentstackRegion . AZURE_EU , 2 ) ]
19+ [ InlineData ( ContentstackRegion . AZURE_NA , 3 ) ]
20+ [ InlineData ( ContentstackRegion . GCP_NA , 4 ) ]
21+ [ InlineData ( ContentstackRegion . AU , 5 ) ]
22+ public void ContentstackRegion_EnumValues_AreCorrect ( ContentstackRegion region , int expectedValue )
23+ {
24+ Assert . Equal ( expectedValue , ( int ) region ) ;
25+ }
26+
27+ [ Fact ]
28+ public void ContentstackRegion_AllValues_AreDefined ( )
29+ {
30+ var regions = Enum . GetValues < ContentstackRegion > ( ) ;
31+ Assert . Equal ( 6 , regions . Length ) ;
32+ Assert . Contains ( ContentstackRegion . US , regions ) ;
33+ Assert . Contains ( ContentstackRegion . EU , regions ) ;
34+ Assert . Contains ( ContentstackRegion . AZURE_EU , regions ) ;
35+ Assert . Contains ( ContentstackRegion . AZURE_NA , regions ) ;
36+ Assert . Contains ( ContentstackRegion . GCP_NA , regions ) ;
37+ Assert . Contains ( ContentstackRegion . AU , regions ) ;
38+ }
39+
40+ #endregion
41+
42+ #region ContentstackOptions Region Tests
43+
44+ [ Fact ]
45+ public void ContentstackOptions_Region_DefaultValue_IsUS ( )
46+ {
47+ var options = new ContentstackOptions ( ) ;
48+ Assert . Equal ( ContentstackRegion . US , options . Region ) ;
49+ }
50+
51+ [ Theory ]
52+ [ InlineData ( ContentstackRegion . US ) ]
53+ [ InlineData ( ContentstackRegion . EU ) ]
54+ [ InlineData ( ContentstackRegion . AZURE_EU ) ]
55+ [ InlineData ( ContentstackRegion . AZURE_NA ) ]
56+ [ InlineData ( ContentstackRegion . GCP_NA ) ]
57+ [ InlineData ( ContentstackRegion . AU ) ]
58+ public void ContentstackOptions_Region_CanBeSet ( ContentstackRegion region )
59+ {
60+ var options = new ContentstackOptions ( ) ;
61+ options . Region = region ;
62+ Assert . Equal ( region , options . Region ) ;
63+ }
64+
65+ #endregion
66+
67+ #region ContentstackClient Region Tests
68+
69+ [ Theory ]
70+ [ InlineData ( ContentstackRegion . US ) ]
71+ [ InlineData ( ContentstackRegion . EU ) ]
72+ [ InlineData ( ContentstackRegion . AZURE_EU ) ]
73+ [ InlineData ( ContentstackRegion . AZURE_NA ) ]
74+ [ InlineData ( ContentstackRegion . GCP_NA ) ]
75+ [ InlineData ( ContentstackRegion . AU ) ]
76+ public void ContentstackClient_Constructor_WithRegion_SetsCorrectRegion ( ContentstackRegion region )
77+ {
78+ var options = new ContentstackOptions
79+ {
80+ ApiKey = "test_api_key" ,
81+ DeliveryToken = "test_delivery_token" ,
82+ Environment = "test_environment" ,
83+ Region = region
84+ } ;
85+
86+ var client = new ContentstackClient ( options ) ;
87+
88+ // Access the private Config field to verify region is set
89+ var configField = typeof ( ContentstackClient ) . GetField ( "Config" , BindingFlags . NonPublic | BindingFlags . Instance ) ;
90+ var config = configField . GetValue ( client ) ;
91+ var regionProperty = config . GetType ( ) . GetProperty ( "Region" ) ;
92+ var actualRegion = ( ContentstackRegion ) regionProperty . GetValue ( config ) ;
93+
94+ Assert . Equal ( region , actualRegion ) ;
95+ }
96+
97+ [ Theory ]
98+ [ InlineData ( ContentstackRegion . US ) ]
99+ [ InlineData ( ContentstackRegion . EU ) ]
100+ [ InlineData ( ContentstackRegion . AZURE_EU ) ]
101+ [ InlineData ( ContentstackRegion . AZURE_NA ) ]
102+ [ InlineData ( ContentstackRegion . GCP_NA ) ]
103+ [ InlineData ( ContentstackRegion . AU ) ]
104+ public void ContentstackClient_Constructor_WithRegionParameter_SetsCorrectRegion ( ContentstackRegion region )
105+ {
106+ var client = new ContentstackClient ( "test_api_key" , "test_delivery_token" , "test_environment" , region : region ) ;
107+
108+ // Access the private Config field to verify region is set
109+ var configField = typeof ( ContentstackClient ) . GetField ( "Config" , BindingFlags . NonPublic | BindingFlags . Instance ) ;
110+ var config = configField . GetValue ( client ) ;
111+ var regionProperty = config . GetType ( ) . GetProperty ( "Region" ) ;
112+ var actualRegion = ( ContentstackRegion ) regionProperty . GetValue ( config ) ;
113+
114+ Assert . Equal ( region , actualRegion ) ;
115+ }
116+
117+ [ Fact ]
118+ public void ContentstackClient_Constructor_WithOptionsWrapper_SetsCorrectRegion ( )
119+ {
120+ var options = new ContentstackOptions
121+ {
122+ ApiKey = "test_api_key" ,
123+ DeliveryToken = "test_delivery_token" ,
124+ Environment = "test_environment" ,
125+ Region = ContentstackRegion . AU
126+ } ;
127+
128+ var optionsWrapper = new OptionsWrapper < ContentstackOptions > ( options ) ;
129+ var client = new ContentstackClient ( optionsWrapper ) ;
130+
131+ // Access the private Config field to verify region is set
132+ var configField = typeof ( ContentstackClient ) . GetField ( "Config" , BindingFlags . NonPublic | BindingFlags . Instance ) ;
133+ var config = configField . GetValue ( client ) ;
134+ var regionProperty = config . GetType ( ) . GetProperty ( "Region" ) ;
135+ var actualRegion = ( ContentstackRegion ) regionProperty . GetValue ( config ) ;
136+
137+ Assert . Equal ( ContentstackRegion . AU , actualRegion ) ;
138+ }
139+
140+ [ Fact ]
141+ public void ContentstackClient_Constructor_DefaultRegion_IsUS ( )
142+ {
143+ var options = new ContentstackOptions
144+ {
145+ ApiKey = "test_api_key" ,
146+ DeliveryToken = "test_delivery_token" ,
147+ Environment = "test_environment"
148+ // Region not set, should default to US
149+ } ;
150+
151+ var client = new ContentstackClient ( options ) ;
152+
153+ // Access the private Config field to verify region is set
154+ var configField = typeof ( ContentstackClient ) . GetField ( "Config" , BindingFlags . NonPublic | BindingFlags . Instance ) ;
155+ var config = configField . GetValue ( client ) ;
156+ var regionProperty = config . GetType ( ) . GetProperty ( "Region" ) ;
157+ var actualRegion = ( ContentstackRegion ) regionProperty . GetValue ( config ) ;
158+
159+ Assert . Equal ( ContentstackRegion . US , actualRegion ) ;
160+ }
161+
162+ #endregion
163+
164+ #region Integration Tests
165+
166+ [ Theory ]
167+ [ InlineData ( ContentstackRegion . US , "https://cdn.contentstack.io/v3" ) ]
168+ [ InlineData ( ContentstackRegion . EU , "https://eu-cdn.contentstack.com/v3" ) ]
169+ [ InlineData ( ContentstackRegion . AZURE_EU , "https://azure-eu-cdn.contentstack.com/v3" ) ]
170+ [ InlineData ( ContentstackRegion . AZURE_NA , "https://azure-na-cdn.contentstack.com/v3" ) ]
171+ [ InlineData ( ContentstackRegion . GCP_NA , "https://gcp-na-cdn.contentstack.com/v3" ) ]
172+ [ InlineData ( ContentstackRegion . AU , "https://au-cdn.contentstack.com/v3" ) ]
173+ public void ContentstackClient_Integration_GeneratesCorrectBaseUrl ( ContentstackRegion region , string expectedBaseUrl )
174+ {
175+ var options = new ContentstackOptions
176+ {
177+ ApiKey = "test_api_key" ,
178+ DeliveryToken = "test_delivery_token" ,
179+ Environment = "test_environment" ,
180+ Region = region
181+ } ;
182+
183+ var client = new ContentstackClient ( options ) ;
184+
185+ // Access the private Config field to get BaseUrl
186+ var configField = typeof ( ContentstackClient ) . GetField ( "Config" , BindingFlags . NonPublic | BindingFlags . Instance ) ;
187+ var config = configField . GetValue ( client ) ;
188+ var baseUrlProperty = config . GetType ( ) . GetProperty ( "BaseUrl" ) ;
189+ var actualBaseUrl = baseUrlProperty . GetValue ( config ) as string ;
190+
191+ Assert . Equal ( expectedBaseUrl , actualBaseUrl ) ;
192+ }
193+
194+ [ Fact ]
195+ public void ContentstackClient_Integration_WithCustomHost_OverridesRegionHost ( )
196+ {
197+ var options = new ContentstackOptions
198+ {
199+ ApiKey = "test_api_key" ,
200+ DeliveryToken = "test_delivery_token" ,
201+ Environment = "test_environment" ,
202+ Region = ContentstackRegion . EU ,
203+ Host = "custom.contentstack.com"
204+ } ;
205+
206+ var client = new ContentstackClient ( options ) ;
207+
208+ // Access the private Config field to get BaseUrl
209+ var configField = typeof ( ContentstackClient ) . GetField ( "Config" , BindingFlags . NonPublic | BindingFlags . Instance ) ;
210+ var config = configField . GetValue ( client ) ;
211+ var baseUrlProperty = config . GetType ( ) . GetProperty ( "BaseUrl" ) ;
212+ var actualBaseUrl = baseUrlProperty . GetValue ( config ) as string ;
213+
214+ // Should use custom host instead of region-specific host
215+ Assert . Equal ( "https://eu-custom.contentstack.com/v3" , actualBaseUrl ) ;
216+ }
217+
218+ [ Fact ]
219+ public void ContentstackClient_Integration_WithCustomVersion_AppendsToBaseUrl ( )
220+ {
221+ var options = new ContentstackOptions
222+ {
223+ ApiKey = "test_api_key" ,
224+ DeliveryToken = "test_delivery_token" ,
225+ Environment = "test_environment" ,
226+ Region = ContentstackRegion . AU ,
227+ Version = "v2"
228+ } ;
229+
230+ var client = new ContentstackClient ( options ) ;
231+
232+ // Access the private Config field to get BaseUrl
233+ var configField = typeof ( ContentstackClient ) . GetField ( "Config" , BindingFlags . NonPublic | BindingFlags . Instance ) ;
234+ var config = configField . GetValue ( client ) ;
235+ var baseUrlProperty = config . GetType ( ) . GetProperty ( "BaseUrl" ) ;
236+ var actualBaseUrl = baseUrlProperty . GetValue ( config ) as string ;
237+
238+ Assert . Equal ( "https://au-cdn.contentstack.com/v2" , actualBaseUrl ) ;
239+ }
240+
241+ #endregion
242+
243+ #region Edge Cases and Error Scenarios
244+
245+ [ Fact ]
246+ public void ContentstackRegion_Enum_CanBeParsedFromString ( )
247+ {
248+ Assert . True ( Enum . TryParse < ContentstackRegion > ( "US" , out var usRegion ) ) ;
249+ Assert . Equal ( ContentstackRegion . US , usRegion ) ;
250+
251+ Assert . True ( Enum . TryParse < ContentstackRegion > ( "EU" , out var euRegion ) ) ;
252+ Assert . Equal ( ContentstackRegion . EU , euRegion ) ;
253+
254+ Assert . True ( Enum . TryParse < ContentstackRegion > ( "AU" , out var auRegion ) ) ;
255+ Assert . Equal ( ContentstackRegion . AU , auRegion ) ;
256+ }
257+
258+ [ Fact ]
259+ public void ContentstackRegion_Enum_CanBeParsedFromStringIgnoreCase ( )
260+ {
261+ Assert . True ( Enum . TryParse < ContentstackRegion > ( "us" , true , out var usRegion ) ) ;
262+ Assert . Equal ( ContentstackRegion . US , usRegion ) ;
263+
264+ Assert . True ( Enum . TryParse < ContentstackRegion > ( "eu" , true , out var euRegion ) ) ;
265+ Assert . Equal ( ContentstackRegion . EU , euRegion ) ;
266+
267+ Assert . True ( Enum . TryParse < ContentstackRegion > ( "au" , true , out var auRegion ) ) ;
268+ Assert . Equal ( ContentstackRegion . AU , auRegion ) ;
269+ }
270+
271+ [ Fact ]
272+ public void ContentstackRegion_Enum_InvalidString_ReturnsFalse ( )
273+ {
274+ Assert . False ( Enum . TryParse < ContentstackRegion > ( "INVALID" , out var invalidRegion ) ) ;
275+ Assert . Equal ( default ( ContentstackRegion ) , invalidRegion ) ;
276+ }
277+
278+ [ Fact ]
279+ public void ContentstackOptions_Region_CanBeChangedAfterCreation ( )
280+ {
281+ var options = new ContentstackOptions
282+ {
283+ Region = ContentstackRegion . US
284+ } ;
285+
286+ Assert . Equal ( ContentstackRegion . US , options . Region ) ;
287+
288+ options . Region = ContentstackRegion . AU ;
289+ Assert . Equal ( ContentstackRegion . AU , options . Region ) ;
290+ }
291+
292+ [ Fact ]
293+ public void ContentstackClient_WithDifferentRegions_CreatesDifferentInstances ( )
294+ {
295+ var usOptions = new ContentstackOptions
296+ {
297+ ApiKey = "test_api_key" ,
298+ DeliveryToken = "test_delivery_token" ,
299+ Environment = "test_environment" ,
300+ Region = ContentstackRegion . US
301+ } ;
302+
303+ var auOptions = new ContentstackOptions
304+ {
305+ ApiKey = "test_api_key" ,
306+ DeliveryToken = "test_delivery_token" ,
307+ Environment = "test_environment" ,
308+ Region = ContentstackRegion . AU
309+ } ;
310+
311+ var usClient = new ContentstackClient ( usOptions ) ;
312+ var auClient = new ContentstackClient ( auOptions ) ;
313+
314+ // Access the private Config field to verify different regions
315+ var configField = typeof ( ContentstackClient ) . GetField ( "Config" , BindingFlags . NonPublic | BindingFlags . Instance ) ;
316+
317+ var usConfig = configField . GetValue ( usClient ) ;
318+ var usRegionProperty = usConfig . GetType ( ) . GetProperty ( "Region" ) ;
319+ var usRegion = ( ContentstackRegion ) usRegionProperty . GetValue ( usConfig ) ;
320+
321+ var auConfig = configField . GetValue ( auClient ) ;
322+ var auRegionProperty = auConfig . GetType ( ) . GetProperty ( "Region" ) ;
323+ var auRegion = ( ContentstackRegion ) auRegionProperty . GetValue ( auConfig ) ;
324+
325+ Assert . NotEqual ( usRegion , auRegion ) ;
326+ Assert . Equal ( ContentstackRegion . US , usRegion ) ;
327+ Assert . Equal ( ContentstackRegion . AU , auRegion ) ;
328+ }
329+
330+ #endregion
331+ }
332+ }
0 commit comments