-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support localized faker data generation based on "Accept-Language" re…
…quest header, fix #6
- Loading branch information
Showing
11 changed files
with
168 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
using Bogus; | ||
using Bogus.Extensions; | ||
using Microsoft.Extensions.Logging; | ||
using System.Collections.Generic; | ||
using System.Globalization; | ||
using System.Linq; | ||
|
||
namespace Mockaco | ||
{ | ||
public class HttpRequestFakerFactory : IFakerFactory | ||
{ | ||
private readonly ILogger<HttpRequestFakerFactory> _logger; | ||
|
||
public HttpRequestFakerFactory(ILogger<HttpRequestFakerFactory> logger) | ||
{ | ||
_logger = logger; | ||
} | ||
|
||
public Faker GetDefaultFaker() | ||
{ | ||
var currentCultureBogusLocale = CultureInfo.DefaultThreadCurrentCulture?.ToBogusLocale(); | ||
|
||
if (currentCultureBogusLocale != null) | ||
{ | ||
return new Faker(currentCultureBogusLocale); | ||
} | ||
|
||
return new Faker(); | ||
} | ||
|
||
public Faker GetFaker(IEnumerable<string> acceptLanguages) | ||
{ | ||
var supportedBogusLocales = GetSupportedBogusLocales(acceptLanguages); | ||
|
||
var firstSupportedBogusLocale = supportedBogusLocales.FirstOrDefault(); | ||
|
||
if (firstSupportedBogusLocale == default) | ||
{ | ||
return GetDefaultFaker(); | ||
} | ||
|
||
return new Faker(firstSupportedBogusLocale); | ||
} | ||
|
||
private IEnumerable<string> GetSupportedBogusLocales(IEnumerable<string> acceptLanguages) | ||
{ | ||
var bogusLocales = new List<string>(); | ||
|
||
foreach (var acceptLanguage in acceptLanguages) | ||
{ | ||
try | ||
{ | ||
var cultureInfo = CultureInfo.GetCultureInfo(acceptLanguage); | ||
|
||
var bogusLocale = cultureInfo.ToBogusLocale(); | ||
|
||
if (bogusLocale == default) | ||
{ | ||
_logger.LogWarning("Accept-Language not supported by Bogus: {language}", acceptLanguage); | ||
} | ||
|
||
bogusLocales.Add(bogusLocale); | ||
} | ||
catch (CultureNotFoundException) | ||
{ | ||
_logger.LogWarning("Accept-Language not supported: {language}", acceptLanguage); | ||
} | ||
} | ||
|
||
return bogusLocales; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using Bogus; | ||
using Microsoft.AspNetCore.Http; | ||
using System.Collections.Generic; | ||
|
||
namespace Mockaco | ||
{ | ||
public interface IFakerFactory | ||
{ | ||
Faker GetDefaultFaker(); | ||
Faker GetFaker(IEnumerable<string> acceptLanguages); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
test/Mockaco.Tests/Scripting/HttpRequestFakerFactoryTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using FluentAssertions; | ||
using Microsoft.Extensions.Logging; | ||
using Moq; | ||
using System.Linq; | ||
using Xunit; | ||
|
||
namespace Mockaco.Tests.Scripting | ||
{ | ||
public class HttpRequestFakerFactoryTest | ||
{ | ||
private HttpRequestFakerFactory _httpRequestFakerFactory; | ||
|
||
public HttpRequestFakerFactoryTest() | ||
{ | ||
_httpRequestFakerFactory = new HttpRequestFakerFactory(Mock.Of<ILogger<HttpRequestFakerFactory>>()); | ||
} | ||
|
||
[Fact] | ||
public void Gets_Localized_Faker_Based_On_Http_Accept_Language_Header() | ||
{ | ||
var faker = _httpRequestFakerFactory.GetFaker(new[] { "pt-BR" }); | ||
|
||
faker.Locale.Should().Be("pt_BR"); | ||
} | ||
|
||
[Fact] | ||
public void Gets_Default_Faker_When_No_Accept_Language_Header_Is_Present() | ||
{ | ||
var faker = _httpRequestFakerFactory.GetFaker(Enumerable.Empty<string>()); | ||
|
||
faker.Locale.Should().Be("en"); | ||
} | ||
} | ||
} |