Skip to content

Commit 5f953d9

Browse files
Migrate Localization fixtures
1 parent a7443f4 commit 5f953d9

File tree

4 files changed

+185
-190
lines changed

4 files changed

+185
-190
lines changed

tests/Sitecore.AspNetCore.SDK.RenderingEngine.Integration.Tests/Fixtures/Localization/AdvanceLocalizationFixture.cs

Lines changed: 55 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
using HtmlAgilityPack;
55
using Microsoft.AspNetCore.Localization;
66
using Microsoft.AspNetCore.Mvc.Razor;
7-
using Microsoft.AspNetCore.TestHost;
7+
using Microsoft.AspNetCore.Mvc.Testing;
88
using Sitecore.AspNetCore.SDK.AutoFixture.Mocks;
99
using Sitecore.AspNetCore.SDK.LayoutService.Client.Extensions;
1010
using Sitecore.AspNetCore.SDK.RenderingEngine.Extensions;
@@ -13,59 +13,11 @@
1313

1414
namespace 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
}
Lines changed: 39 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using System.Net;
22
using AwesomeAssertions;
3-
using Microsoft.AspNetCore.TestHost;
3+
using Microsoft.AspNetCore.Mvc.Testing;
44
using Sitecore.AspNetCore.SDK.AutoFixture.Mocks;
55
using Sitecore.AspNetCore.SDK.LayoutService.Client.Extensions;
66
using Sitecore.AspNetCore.SDK.LayoutService.Client.Request;
@@ -10,51 +10,11 @@
1010

1111
namespace Sitecore.AspNetCore.SDK.RenderingEngine.Integration.Tests.Fixtures.Localization;
1212

13-
public class DefaultLocalizationFixture : IDisposable
13+
public class DefaultLocalizationFixture(TestWebApplicationFactory<TestWebApplicationProgram> factory) : IClassFixture<TestWebApplicationFactory<TestWebApplicationProgram>>, IDisposable
1414
{
15-
private readonly TestServer _server;
16-
private readonly MockHttpMessageHandler _mockClientHandler;
15+
private readonly MockHttpMessageHandler _mockClientHandler = new();
1716
private readonly Uri _layoutServiceUri = new("http://layout.service");
1817

19-
public DefaultLocalizationFixture()
20-
{
21-
TestServerBuilder testHostBuilder = new();
22-
_mockClientHandler = new MockHttpMessageHandler();
23-
testHostBuilder
24-
.ConfigureServices(builder =>
25-
{
26-
builder.AddLocalization(options => options.ResourcesPath = "Resources");
27-
builder
28-
.AddSitecoreLayoutService().WithDefaultRequestOptions(request =>
29-
{
30-
request
31-
.Language("da");
32-
})
33-
.AddHttpHandler("mock", _ => new HttpClient(_mockClientHandler) { BaseAddress = _layoutServiceUri })
34-
.AsDefaultHandler();
35-
builder.AddSitecoreRenderingEngine(options =>
36-
{
37-
options
38-
.AddModelBoundView<ComponentModels.Component4>("Component-4", "Component4")
39-
.AddDefaultComponentRenderer();
40-
});
41-
})
42-
.Configure(app =>
43-
{
44-
app.UseRouting();
45-
46-
app.UseSitecoreRenderingEngine();
47-
48-
app.UseEndpoints(endpoints =>
49-
{
50-
endpoints.MapSitecoreLocalizedRoute("Localized", "Index", "UsingGlobalMiddleware");
51-
endpoints.MapDefaultControllerRoute();
52-
});
53-
});
54-
55-
_server = testHostBuilder.BuildServer(new Uri("http://localhost"));
56-
}
57-
5818
[Fact]
5919
public async Task LocalizationRouteProvider_SetsCorrectRequestsLanguage()
6020
{
@@ -65,7 +25,7 @@ public async Task LocalizationRouteProvider_SetsCorrectRequestsLanguage()
6525
Content = new StringContent(Serializer.Serialize(CannedResponses.WithNestedPlaceholder))
6626
});
6727

68-
HttpClient client = _server.CreateClient();
28+
HttpClient client = BuildDefaultLocalizationWebApplicationFactory().CreateClient();
6929

7030
// Act
7131
await client.GetStringAsync(new Uri("/da/UsingGlobalMiddleware", UriKind.Relative));
@@ -76,7 +36,41 @@ public async Task LocalizationRouteProvider_SetsCorrectRequestsLanguage()
7636
public void Dispose()
7737
{
7838
_mockClientHandler.Dispose();
79-
_server.Dispose();
8039
GC.SuppressFinalize(this);
8140
}
41+
42+
private WebApplicationFactory<TestWebApplicationProgram> BuildDefaultLocalizationWebApplicationFactory()
43+
{
44+
return factory.WithWebHostBuilder(builder =>
45+
{
46+
builder.ConfigureServices(services =>
47+
{
48+
services.AddLocalization(options => options.ResourcesPath = "Resources");
49+
services
50+
.AddSitecoreLayoutService().WithDefaultRequestOptions(request => { request.Language("da"); })
51+
.AddHttpHandler("mock", _ => new HttpClient(_mockClientHandler) { BaseAddress = _layoutServiceUri })
52+
.AsDefaultHandler();
53+
54+
services.AddSitecoreRenderingEngine(options =>
55+
{
56+
options
57+
.AddModelBoundView<ComponentModels.Component4>("Component-4", "Component4")
58+
.AddDefaultComponentRenderer();
59+
});
60+
});
61+
62+
builder.Configure(app =>
63+
{
64+
app.UseRouting();
65+
66+
app.UseSitecoreRenderingEngine();
67+
68+
app.UseEndpoints(endpoints =>
69+
{
70+
endpoints.MapSitecoreLocalizedRoute("Localized", "Index", "UsingGlobalMiddleware");
71+
endpoints.MapDefaultControllerRoute();
72+
});
73+
});
74+
});
75+
}
8276
}

tests/Sitecore.AspNetCore.SDK.RenderingEngine.Integration.Tests/Fixtures/Localization/LocalizationFixture.cs

Lines changed: 39 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
using System.Net;
33
using AwesomeAssertions;
44
using Microsoft.AspNetCore.Localization;
5-
using Microsoft.AspNetCore.TestHost;
5+
using Microsoft.AspNetCore.Mvc.Testing;
66
using Sitecore.AspNetCore.SDK.AutoFixture.Mocks;
77
using Sitecore.AspNetCore.SDK.LayoutService.Client.Extensions;
88
using Sitecore.AspNetCore.SDK.RenderingEngine.Extensions;
@@ -11,32 +11,57 @@
1111

1212
namespace Sitecore.AspNetCore.SDK.RenderingEngine.Integration.Tests.Fixtures.Localization;
1313

14-
public class LocalizationFixture : IDisposable
14+
public class LocalizationFixture(TestWebApplicationFactory<TestWebApplicationProgram> factory) : IClassFixture<TestWebApplicationFactory<TestWebApplicationProgram>>, IDisposable
1515
{
16-
private readonly TestServer _server;
17-
private readonly MockHttpMessageHandler _mockClientHandler;
16+
private readonly MockHttpMessageHandler _mockClientHandler = new();
1817
private readonly Uri _layoutServiceUri = new("http://layout.service");
1918

20-
public LocalizationFixture()
19+
[Fact]
20+
public async Task LocalizationRouteProvider_SetsCorrectRequestsLanguage()
21+
{
22+
// Arrange
23+
_mockClientHandler.Responses.Push(new HttpResponseMessage
24+
{
25+
StatusCode = HttpStatusCode.OK,
26+
Content = new StringContent(Serializer.Serialize(CannedResponses.WithNestedPlaceholder))
27+
});
28+
29+
HttpClient client = BuildLocalizationWebApplicationFactory().CreateClient();
30+
31+
// Act
32+
await client.GetStringAsync(new Uri("/ru-RU/UsingGlobalMiddleware", UriKind.Relative));
33+
34+
_mockClientHandler.Requests.Single().RequestUri!.AbsoluteUri.Should().Contain("sc_lang=ru-RU");
35+
}
36+
37+
public void Dispose()
2138
{
22-
TestServerBuilder testHostBuilder = new();
23-
_mockClientHandler = new MockHttpMessageHandler();
24-
testHostBuilder
25-
.ConfigureServices(builder =>
39+
_mockClientHandler.Dispose();
40+
GC.SuppressFinalize(this);
41+
}
42+
43+
private WebApplicationFactory<TestWebApplicationProgram> BuildLocalizationWebApplicationFactory()
44+
{
45+
return factory.WithWebHostBuilder(builder =>
46+
{
47+
builder.ConfigureServices(services =>
2648
{
27-
builder.AddLocalization(options => options.ResourcesPath = "Resources");
28-
builder
49+
services.AddLocalization(options => options.ResourcesPath = "Resources");
50+
51+
services
2952
.AddSitecoreLayoutService()
3053
.AddHttpHandler("mock", _ => new HttpClient(_mockClientHandler) { BaseAddress = _layoutServiceUri })
3154
.AsDefaultHandler();
32-
builder.AddSitecoreRenderingEngine(options =>
55+
56+
services.AddSitecoreRenderingEngine(options =>
3357
{
3458
options
3559
.AddModelBoundView<ComponentModels.Component4>("Component-4", "Component4")
3660
.AddDefaultComponentRenderer();
3761
});
38-
})
39-
.Configure(app =>
62+
});
63+
64+
builder.Configure(app =>
4065
{
4166
app.UseRouting();
4267
app.UseRequestLocalization(options =>
@@ -56,32 +81,6 @@ public LocalizationFixture()
5681
endpoints.MapDefaultControllerRoute();
5782
});
5883
});
59-
60-
_server = testHostBuilder.BuildServer(new Uri("http://localhost"));
61-
}
62-
63-
[Fact]
64-
public async Task LocalizationRouteProvider_SetsCorrectRequestsLanguage()
65-
{
66-
// Arrange
67-
_mockClientHandler.Responses.Push(new HttpResponseMessage
68-
{
69-
StatusCode = HttpStatusCode.OK,
70-
Content = new StringContent(Serializer.Serialize(CannedResponses.WithNestedPlaceholder))
7184
});
72-
73-
HttpClient client = _server.CreateClient();
74-
75-
// Act
76-
await client.GetStringAsync(new Uri("/ru-RU/UsingGlobalMiddleware", UriKind.Relative));
77-
78-
_mockClientHandler.Requests.Single().RequestUri!.AbsoluteUri.Should().Contain("sc_lang=ru-RU");
79-
}
80-
81-
public void Dispose()
82-
{
83-
_mockClientHandler.Dispose();
84-
_server.Dispose();
85-
GC.SuppressFinalize(this);
8685
}
8786
}

0 commit comments

Comments
 (0)