Skip to content
Merged

t #149

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
28 changes: 28 additions & 0 deletions src/Orbit.Api/Extensions/HttpContextExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ public static bool IsReadOnlyCredential(this ClaimsPrincipal user)
return countryCode;
}

var acceptLanguageCountryCode = NormalizeCountryCodeFromAcceptLanguage(
context.Request.Headers.AcceptLanguage.ToString());
if (acceptLanguageCountryCode is not null)
return acceptLanguageCountryCode;

return null;
}

Expand Down Expand Up @@ -96,6 +101,29 @@ public static bool IsReadOnlyCredential(this ClaimsPrincipal user)
: null;
}

private static string? NormalizeCountryCodeFromAcceptLanguage(string? acceptLanguage)
{
if (string.IsNullOrWhiteSpace(acceptLanguage))
return null;

var languageTags = acceptLanguage.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);

foreach (var entry in languageTags)
{
var languageTag = entry.Split(';', 2, StringSplitOptions.TrimEntries)[0]
.Replace('_', '-');
var segments = languageTag.Split('-', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
if (segments.Length < 2)
continue;

var countryCode = NormalizeCountryCode(segments[^1]);
if (countryCode is not null)
return countryCode;
}

return null;
}

private static bool TryNormalizeIpAddress(string? ipAddress, out string? normalizedIpAddress)
{
normalizedIpAddress = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,23 @@
Arg.Any<CancellationToken>());
}

[Fact]
public async Task CreateCheckout_UsesAcceptLanguageCountryFallback()
{
_mediator.Send(Arg.Any<CreateCheckoutCommand>(), Arg.Any<CancellationToken>())
.Returns(Result.Success(default(CheckoutResponse)!));

_controller.HttpContext.Request.Headers["Accept-Language"] = "pt-BR,pt;q=0.9,en;q=0.8";

Check warning on line 119 in tests/Orbit.Infrastructure.Tests/Controllers/SubscriptionControllerTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

The header 'Accept-Language' can be accessed using the AcceptLanguage property

See more on https://sonarcloud.io/project/issues?id=thomasluizon_orbit-api&issues=AZ2N3Cv1GiS5llmB-Wor&open=AZ2N3Cv1GiS5llmB-Wor&pullRequest=149

var request = new SubscriptionController.CreateCheckoutRequest("month");

await _controller.CreateCheckout(request, CancellationToken.None);

await _mediator.Received(1).Send(
Arg.Is<CreateCheckoutCommand>(c => c.CountryCode == "BR" && c.IpAddress == "127.0.0.1"),
Arg.Any<CancellationToken>());
}

// --- CreatePortal ---

[Fact]
Expand Down Expand Up @@ -241,6 +258,23 @@
Arg.Any<CancellationToken>());
}

[Fact]
public async Task GetPlans_UsesAcceptLanguageCountryFallback()
{
_mediator.Send(Arg.Any<GetPlansQuery>(), Arg.Any<CancellationToken>())
.Returns(Result.Success(default(PlansResponse)!));

_controller.HttpContext.Request.Headers["Accept-Language"] = "pt-BR,pt;q=0.9,en;q=0.8";

Check warning on line 267 in tests/Orbit.Infrastructure.Tests/Controllers/SubscriptionControllerTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

The header 'Accept-Language' can be accessed using the AcceptLanguage property

See more on https://sonarcloud.io/project/issues?id=thomasluizon_orbit-api&issues=AZ2N3Cv1GiS5llmB-Wos&open=AZ2N3Cv1GiS5llmB-Wos&pullRequest=149

await _controller.GetPlans(CancellationToken.None);

await _mediator.Received(1).Send(
Arg.Is<GetPlansQuery>(query =>
query.CountryCode == "BR" &&
query.IpAddress == "127.0.0.1"),
Arg.Any<CancellationToken>());
}

// --- ClaimAdReward ---

[Fact]
Expand Down
Loading