Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test that uses localization #2039

Merged
merged 3 commits into from
Feb 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")]
JamesNK marked this conversation as resolved.
Show resolved Hide resolved
[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);
JamesNK marked this conversation as resolved.
Show resolved Hide resolved
}

// 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