From 70c0fbc8a384303a727cc350e3430d6c61201e02 Mon Sep 17 00:00:00 2001 From: James Newton-King Date: Fri, 3 Feb 2023 20:46:13 +0800 Subject: [PATCH 1/3] Add test that uses localization --- test/FunctionalTests/Client/UnaryTests.cs | 30 ++++++++++++++++++++ testassets/FunctionalTestsWebsite/Startup.cs | 21 +++++++++++++- 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/test/FunctionalTests/Client/UnaryTests.cs b/test/FunctionalTests/Client/UnaryTests.cs index f5e954f5e..71f0c4165 100644 --- a/test/FunctionalTests/Client/UnaryTests.cs +++ b/test/FunctionalTests/Client/UnaryTests.cs @@ -68,6 +68,36 @@ 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)); + + // Assert + await call.ResponseAsync.DefaultTimeout(); + + 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..6a02336c7 100644 --- a/testassets/FunctionalTestsWebsite/Startup.cs +++ b/testassets/FunctionalTestsWebsite/Startup.cs @@ -17,8 +17,10 @@ #endregion using System.Diagnostics; +using System.Globalization; using System.IdentityModel.Tokens.Jwt; using System.Security.Claims; +using System.Text.RegularExpressions; using FunctionalTestsWebsite.Infrastructure; using FunctionalTestsWebsite.Services; using Greet; @@ -26,7 +28,9 @@ using Grpc.HealthCheck; using Grpc.Tests.Shared; using Microsoft.AspNetCore.Authentication.JwtBearer; +using Microsoft.AspNetCore.Localization; using Microsoft.Extensions.DependencyInjection.Extensions; +using Microsoft.Extensions.Localization; using Microsoft.IdentityModel.Tokens; namespace FunctionalTestsWebsite; @@ -123,6 +127,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 +186,10 @@ public void Configure(IApplicationBuilder app) await next(); } }); - app.UseRouting(); + app.UseRequestLocalization(); + app.UseAuthorization(); app.UseGrpcWeb(new GrpcWebOptions { DefaultEnabled = true }); app.UseCors(); From 350f3bdea6e593b4eb4ce5951607f3d80868dfd4 Mon Sep 17 00:00:00 2001 From: James Newton-King Date: Fri, 3 Feb 2023 20:47:09 +0800 Subject: [PATCH 2/3] Clean up --- testassets/FunctionalTestsWebsite/Startup.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/testassets/FunctionalTestsWebsite/Startup.cs b/testassets/FunctionalTestsWebsite/Startup.cs index 6a02336c7..624356cef 100644 --- a/testassets/FunctionalTestsWebsite/Startup.cs +++ b/testassets/FunctionalTestsWebsite/Startup.cs @@ -20,7 +20,6 @@ using System.Globalization; using System.IdentityModel.Tokens.Jwt; using System.Security.Claims; -using System.Text.RegularExpressions; using FunctionalTestsWebsite.Infrastructure; using FunctionalTestsWebsite.Services; using Greet; @@ -30,7 +29,6 @@ using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Localization; using Microsoft.Extensions.DependencyInjection.Extensions; -using Microsoft.Extensions.Localization; using Microsoft.IdentityModel.Tokens; namespace FunctionalTestsWebsite; From ac847d6bd4a81f535b420680d9ff38fb56caef6c Mon Sep 17 00:00:00 2001 From: James Newton-King Date: Fri, 3 Feb 2023 20:47:59 +0800 Subject: [PATCH 3/3] Clean up --- test/FunctionalTests/Client/UnaryTests.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test/FunctionalTests/Client/UnaryTests.cs b/test/FunctionalTests/Client/UnaryTests.cs index 71f0c4165..b0638a7ec 100644 --- a/test/FunctionalTests/Client/UnaryTests.cs +++ b/test/FunctionalTests/Client/UnaryTests.cs @@ -91,10 +91,9 @@ Task UnaryThrowError(HelloRequest request, ServerCallContext context // Act var call = client.UnaryCall(new HelloRequest(), new CallOptions(headers: metadata)); - - // Assert await call.ResponseAsync.DefaultTimeout(); + // Assert Assert.AreEqual(expectedServerCulture, serverCulture); }