Skip to content

Commit

Permalink
Add test that uses localization (#2039)
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesNK authored Feb 6, 2023
1 parent fc4d087 commit ad54bc6
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
29 changes: 29 additions & 0 deletions test/FunctionalTests/Client/UnaryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,35 @@ Task<HelloReply> 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<HelloReply> UnaryThrowError(HelloRequest request, ServerCallContext context)
{
serverCulture = Thread.CurrentThread.CurrentCulture.Name;
return Task.FromResult(new HelloReply { Message = serverCulture });
}

// Arrange
var method = Fixture.DynamicGrpc.AddUnaryMethod<HelloRequest, HelloReply>(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()
Expand Down
19 changes: 18 additions & 1 deletion testassets/FunctionalTestsWebsite/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#endregion

using System.Diagnostics;
using System.Globalization;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using FunctionalTestsWebsite.Infrastructure;
Expand All @@ -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;

Expand Down Expand Up @@ -123,6 +125,20 @@ static Uri GetCurrentAddress(IServiceProvider serviceProvider)

return new Uri($"{context.Request.Scheme}://{context.Request.Host.Value}");
}

services.Configure<RequestLocalizationOptions>(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.
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit ad54bc6

Please sign in to comment.