44using HtmlAgilityPack ;
55using Microsoft . AspNetCore . Localization ;
66using Microsoft . AspNetCore . Mvc . Razor ;
7- using Microsoft . AspNetCore . TestHost ;
7+ using Microsoft . AspNetCore . Mvc . Testing ;
88using Sitecore . AspNetCore . SDK . AutoFixture . Mocks ;
99using Sitecore . AspNetCore . SDK . LayoutService . Client . Extensions ;
1010using Sitecore . AspNetCore . SDK . RenderingEngine . Extensions ;
1313
1414namespace Sitecore . AspNetCore . SDK . RenderingEngine . Integration . Tests . Fixtures . Localization ;
1515
16- public class AdvanceLocalizationFixture : IDisposable
16+ public class AdvanceLocalizationFixture ( TestWebApplicationFactory < TestWebApplicationProgram > factory ) : IClassFixture < TestWebApplicationFactory < TestWebApplicationProgram > > , IDisposable
1717{
18- private readonly TestServer _server ;
19- private readonly MockHttpMessageHandler _mockClientHandler ;
18+ private readonly MockHttpMessageHandler _mockClientHandler = new ( ) ;
2019 private readonly Uri _layoutServiceUri = new ( "http://layout.service" ) ;
2120
22- public AdvanceLocalizationFixture ( )
23- {
24- TestServerBuilder testHostBuilder = new ( ) ;
25- _mockClientHandler = new MockHttpMessageHandler ( ) ;
26- testHostBuilder
27- . ConfigureServices ( builder =>
28- {
29- builder . AddLocalization ( options => options . ResourcesPath = "Resources" ) ;
30- builder . AddSitecoreLayoutService ( )
31- . AddHttpHandler ( "mock" , _ => new HttpClient ( _mockClientHandler ) { BaseAddress = _layoutServiceUri } )
32- . AsDefaultHandler ( ) ;
33-
34- builder . AddSitecoreRenderingEngine ( options =>
35- {
36- options . AddModelBoundView < ComponentModels . Component4 > ( "Component-4" , "Component4" )
37- . AddDefaultComponentRenderer ( ) ;
38- } ) ;
39- builder . AddMvc ( )
40- . AddViewLocalization ( LanguageViewLocationExpanderFormat . Suffix ) ;
41- } )
42- . Configure ( app =>
43- {
44- app . UseRouting ( ) ;
45- app . UseRequestLocalization ( options =>
46- {
47- List < CultureInfo > supportedCultures = [ new ( "en" ) , new ( "da" ) ] ;
48- options . DefaultRequestCulture = new RequestCulture ( culture : "en" , uiCulture : "en" ) ;
49- options . SupportedCultures = supportedCultures ;
50- options . SupportedUICultures = supportedCultures ;
51- options . UseSitecoreRequestLocalization ( ) ;
52- } ) ;
53- app . UseSitecoreRenderingEngine ( ) ;
54-
55- app . UseEndpoints ( endpoints =>
56- {
57- // ReSharper disable once RouteTemplates.RouteParameterConstraintNotResolved - Custom constraint
58- endpoints . MapControllerRoute (
59- name : "default" ,
60- pattern : "content/{culture:culture}/{**sitecoreRoute}" ,
61- defaults : new { controller = "Home" , action = "Index" } ) ;
62- endpoints . MapDefaultControllerRoute ( ) ;
63- } ) ;
64- } ) ;
65-
66- _server = testHostBuilder . BuildServer ( new Uri ( "http://localhost" ) ) ;
67- }
68-
6921 [ Fact ]
7022 public async Task LocalizationRouteProvider_SetsCorrectRequestsLanguage ( )
7123 {
@@ -76,7 +28,7 @@ public async Task LocalizationRouteProvider_SetsCorrectRequestsLanguage()
7628 Content = new StringContent ( Serializer . Serialize ( CannedResponses . WithNestedPlaceholder ) )
7729 } ) ;
7830
79- HttpClient client = _server . CreateClient ( ) ;
31+ HttpClient client = BuildAdvanceLocalizationWebApplicationFactory ( ) . CreateClient ( ) ;
8032
8133 // Act
8234 await client . GetStringAsync ( new Uri ( "content/da/UsingGlobalMiddleware" , UriKind . Relative ) ) ;
@@ -94,7 +46,7 @@ public async Task LocalizedRequest_PicksDefaultView()
9446 Content = new StringContent ( Serializer . Serialize ( CannedResponses . WithNestedPlaceholder ) )
9547 } ) ;
9648
97- HttpClient client = _server . CreateClient ( ) ;
49+ HttpClient client = BuildAdvanceLocalizationWebApplicationFactory ( ) . CreateClient ( ) ;
9850
9951 // Act
10052 string response = await client . GetStringAsync ( new Uri ( "/" , UriKind . Relative ) ) ;
@@ -118,7 +70,7 @@ public async Task LocalizedRequest_PicksLocalizedView()
11870 Content = new StringContent ( Serializer . Serialize ( CannedResponses . WithNestedPlaceholder ) )
11971 } ) ;
12072
121- HttpClient client = _server . CreateClient ( ) ;
73+ HttpClient client = BuildAdvanceLocalizationWebApplicationFactory ( ) . CreateClient ( ) ;
12274
12375 // Act
12476 string response = await client . GetStringAsync ( new Uri ( "content/da" , UriKind . Relative ) ) ;
@@ -135,7 +87,55 @@ public async Task LocalizedRequest_PicksLocalizedView()
13587 public void Dispose ( )
13688 {
13789 _mockClientHandler . Dispose ( ) ;
138- _server . Dispose ( ) ;
13990 GC . SuppressFinalize ( this ) ;
14091 }
92+
93+ private WebApplicationFactory < TestWebApplicationProgram > BuildAdvanceLocalizationWebApplicationFactory ( )
94+ {
95+ return factory . WithWebHostBuilder ( builder =>
96+ {
97+ builder . ConfigureServices ( services =>
98+ {
99+ services . AddLocalization ( options => options . ResourcesPath = "Resources" ) ;
100+
101+ services
102+ . AddSitecoreLayoutService ( )
103+ . AddHttpHandler ( "mock" , _ => new HttpClient ( _mockClientHandler ) { BaseAddress = _layoutServiceUri } )
104+ . AsDefaultHandler ( ) ;
105+
106+ services . AddSitecoreRenderingEngine ( options =>
107+ {
108+ options . AddModelBoundView < ComponentModels . Component4 > ( "Component-4" , "Component4" )
109+ . AddDefaultComponentRenderer ( ) ;
110+ } ) ;
111+
112+ services . AddMvc ( )
113+ . AddViewLocalization ( LanguageViewLocationExpanderFormat . Suffix ) ;
114+ } ) ;
115+
116+ builder . Configure ( app =>
117+ {
118+ app . UseRouting ( ) ;
119+ app . UseRequestLocalization ( options =>
120+ {
121+ List < CultureInfo > supportedCultures = new ( ) { new ( "en" ) , new ( "da" ) } ;
122+ options . DefaultRequestCulture = new RequestCulture ( culture : "en" , uiCulture : "en" ) ;
123+ options . SupportedCultures = supportedCultures ;
124+ options . SupportedUICultures = supportedCultures ;
125+ options . UseSitecoreRequestLocalization ( ) ;
126+ } ) ;
127+ app . UseSitecoreRenderingEngine ( ) ;
128+
129+ app . UseEndpoints ( endpoints =>
130+ {
131+ // ReSharper disable once RouteTemplates.RouteParameterConstraintNotResolved - Custom constraint
132+ endpoints . MapControllerRoute (
133+ name : "default" ,
134+ pattern : "content/{culture:culture}/{**sitecoreRoute}" ,
135+ defaults : new { controller = "Home" , action = "Index" } ) ;
136+ endpoints . MapDefaultControllerRoute ( ) ;
137+ } ) ;
138+ } ) ;
139+ } ) ;
140+ }
141141}
0 commit comments