diff --git a/test/FunctionalTests/Client/UnaryTests.cs b/test/FunctionalTests/Client/UnaryTests.cs index f5e954f5e..b0638a7ec 100644 --- a/test/FunctionalTests/Client/UnaryTests.cs +++ b/test/FunctionalTests/Client/UnaryTests.cs @@ -68,6 +68,35 @@ Task UnaryThrowError(HelloRequest request, ServerCallContext context StringAssert.StartsWith("Failed to deserialize response message.", call.GetStatus().Detail); } + [TestCase("fr", "fr")] + [TestCase(null, "en-US")] + public async Task Unary_SetAcceptLanguage_ServerCultureChanged(string clientAcceptLanguage, string expectedServerCulture) + { + string? serverCulture = null; + Task UnaryThrowError(HelloRequest request, ServerCallContext context) + { + serverCulture = Thread.CurrentThread.CurrentCulture.Name; + return Task.FromResult(new HelloReply { Message = serverCulture }); + } + + // Arrange + var method = Fixture.DynamicGrpc.AddUnaryMethod(UnaryThrowError); + var channel = CreateChannel(); + var client = TestClientFactory.Create(channel, method); + var metadata = new Metadata(); + if (clientAcceptLanguage != null) + { + metadata.Add("accept-language", clientAcceptLanguage); + } + + // Act + var call = client.UnaryCall(new HelloRequest(), new CallOptions(headers: metadata)); + await call.ResponseAsync.DefaultTimeout(); + + // Assert + Assert.AreEqual(expectedServerCulture, serverCulture); + } + #if NET5_0_OR_GREATER [Test] public async Task MaxConcurrentStreams_StartConcurrently_AdditionalConnectionsCreated() diff --git a/testassets/FunctionalTestsWebsite/Startup.cs b/testassets/FunctionalTestsWebsite/Startup.cs index 4ac8f6249..624356cef 100644 --- a/testassets/FunctionalTestsWebsite/Startup.cs +++ b/testassets/FunctionalTestsWebsite/Startup.cs @@ -17,6 +17,7 @@ #endregion using System.Diagnostics; +using System.Globalization; using System.IdentityModel.Tokens.Jwt; using System.Security.Claims; using FunctionalTestsWebsite.Infrastructure; @@ -26,6 +27,7 @@ using Grpc.HealthCheck; using Grpc.Tests.Shared; using Microsoft.AspNetCore.Authentication.JwtBearer; +using Microsoft.AspNetCore.Localization; using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.IdentityModel.Tokens; @@ -123,6 +125,20 @@ static Uri GetCurrentAddress(IServiceProvider serviceProvider) return new Uri($"{context.Request.Scheme}://{context.Request.Host.Value}"); } + + services.Configure(options => + { + const string enUSCulture = "en-US"; + var supportedCultures = new[] + { + new CultureInfo(enUSCulture), + new CultureInfo("fr") + }; + + options.DefaultRequestCulture = new RequestCulture(culture: enUSCulture, uiCulture: enUSCulture); + options.SupportedCultures = supportedCultures; + options.SupportedUICultures = supportedCultures; + }); } // 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) await next(); } }); - app.UseRouting(); + app.UseRequestLocalization(); + app.UseAuthorization(); app.UseGrpcWeb(new GrpcWebOptions { DefaultEnabled = true }); app.UseCors();