Skip to content

Commit ad54bc6

Browse files
authored
Add test that uses localization (#2039)
1 parent fc4d087 commit ad54bc6

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

test/FunctionalTests/Client/UnaryTests.cs

+29
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,35 @@ Task<HelloReply> UnaryThrowError(HelloRequest request, ServerCallContext context
6868
StringAssert.StartsWith("Failed to deserialize response message.", call.GetStatus().Detail);
6969
}
7070

71+
[TestCase("fr", "fr")]
72+
[TestCase(null, "en-US")]
73+
public async Task Unary_SetAcceptLanguage_ServerCultureChanged(string clientAcceptLanguage, string expectedServerCulture)
74+
{
75+
string? serverCulture = null;
76+
Task<HelloReply> UnaryThrowError(HelloRequest request, ServerCallContext context)
77+
{
78+
serverCulture = Thread.CurrentThread.CurrentCulture.Name;
79+
return Task.FromResult(new HelloReply { Message = serverCulture });
80+
}
81+
82+
// Arrange
83+
var method = Fixture.DynamicGrpc.AddUnaryMethod<HelloRequest, HelloReply>(UnaryThrowError);
84+
var channel = CreateChannel();
85+
var client = TestClientFactory.Create(channel, method);
86+
var metadata = new Metadata();
87+
if (clientAcceptLanguage != null)
88+
{
89+
metadata.Add("accept-language", clientAcceptLanguage);
90+
}
91+
92+
// Act
93+
var call = client.UnaryCall(new HelloRequest(), new CallOptions(headers: metadata));
94+
await call.ResponseAsync.DefaultTimeout();
95+
96+
// Assert
97+
Assert.AreEqual(expectedServerCulture, serverCulture);
98+
}
99+
71100
#if NET5_0_OR_GREATER
72101
[Test]
73102
public async Task MaxConcurrentStreams_StartConcurrently_AdditionalConnectionsCreated()

testassets/FunctionalTestsWebsite/Startup.cs

+18-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#endregion
1818

1919
using System.Diagnostics;
20+
using System.Globalization;
2021
using System.IdentityModel.Tokens.Jwt;
2122
using System.Security.Claims;
2223
using FunctionalTestsWebsite.Infrastructure;
@@ -26,6 +27,7 @@
2627
using Grpc.HealthCheck;
2728
using Grpc.Tests.Shared;
2829
using Microsoft.AspNetCore.Authentication.JwtBearer;
30+
using Microsoft.AspNetCore.Localization;
2931
using Microsoft.Extensions.DependencyInjection.Extensions;
3032
using Microsoft.IdentityModel.Tokens;
3133

@@ -123,6 +125,20 @@ static Uri GetCurrentAddress(IServiceProvider serviceProvider)
123125

124126
return new Uri($"{context.Request.Scheme}://{context.Request.Host.Value}");
125127
}
128+
129+
services.Configure<RequestLocalizationOptions>(options =>
130+
{
131+
const string enUSCulture = "en-US";
132+
var supportedCultures = new[]
133+
{
134+
new CultureInfo(enUSCulture),
135+
new CultureInfo("fr")
136+
};
137+
138+
options.DefaultRequestCulture = new RequestCulture(culture: enUSCulture, uiCulture: enUSCulture);
139+
options.SupportedCultures = supportedCultures;
140+
options.SupportedUICultures = supportedCultures;
141+
});
126142
}
127143

128144
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
@@ -168,9 +184,10 @@ public void Configure(IApplicationBuilder app)
168184
await next();
169185
}
170186
});
171-
172187
app.UseRouting();
173188

189+
app.UseRequestLocalization();
190+
174191
app.UseAuthorization();
175192
app.UseGrpcWeb(new GrpcWebOptions { DefaultEnabled = true });
176193
app.UseCors();

0 commit comments

Comments
 (0)