Skip to content

Commit

Permalink
Added Locale to internet module
Browse files Browse the repository at this point in the history
Fixed the test cases to take locale as parameter
Updated the header files to reflect changes made to the cpp files
  • Loading branch information
SgtApone117 committed Dec 3, 2024
1 parent 53d2d5a commit caebac9
Show file tree
Hide file tree
Showing 4 changed files with 209 additions and 83 deletions.
19 changes: 10 additions & 9 deletions include/faker-cxx/internet.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ FAKER_CXX_EXPORT std::string username(std::optional<std::string> firstName = std
*/
FAKER_CXX_EXPORT std::string email(std::optional<std::string> firstName = std::nullopt,
std::optional<std::string> lastName = std::nullopt,
std::optional<std::string> emailHost = std::nullopt);
std::optional<std::string> emailHost = std::nullopt, Locale locale = Locale::en_US);

/**
* @brief Generates an email address using the given person's name as base with example domain.
Expand All @@ -96,7 +96,8 @@ FAKER_CXX_EXPORT std::string email(std::optional<std::string> firstName = std::n
* @endcode
*/
FAKER_CXX_EXPORT std::string exampleEmail(std::optional<std::string> firstName = std::nullopt,
std::optional<std::string> lastName = std::nullopt);
std::optional<std::string> lastName = std::nullopt,
Locale locale = Locale::en_US);

/**
* @brief Generates a random password-like string. Do not use this method for generating actual passwords for users.
Expand Down Expand Up @@ -201,7 +202,7 @@ FAKER_CXX_EXPORT unsigned httpStatusCode(std::optional<HttpResponseType> respons
* faker::internet::httpRequestHeader() // "Authorization"
* @endcode
*/
FAKER_CXX_EXPORT std::string_view httpRequestHeader();
FAKER_CXX_EXPORT std::string_view httpRequestHeader(Locale locale = Locale::en_US);

/**
* @brief Generates a random http response header.
Expand All @@ -212,7 +213,7 @@ FAKER_CXX_EXPORT std::string_view httpRequestHeader();
* faker::internet::httpResponseHeader() // "Location"
* @endcode
*/
FAKER_CXX_EXPORT std::string_view httpResponseHeader();
FAKER_CXX_EXPORT std::string_view httpResponseHeader(Locale locale = Locale::en_US);

/**
* @brief Generates a random http media type.
Expand All @@ -223,7 +224,7 @@ FAKER_CXX_EXPORT std::string_view httpResponseHeader();
* faker::internet::httpMediaType() // "application/json"
* @endcode
*/
FAKER_CXX_EXPORT std::string_view httpMediaType();
FAKER_CXX_EXPORT std::string_view httpMediaType(Locale locale = Locale::en_US);

/**
* @brief Returns a string containing randomized ipv4 address of the given class.
Expand Down Expand Up @@ -305,7 +306,7 @@ FAKER_CXX_EXPORT unsigned port();
* faker::internet::url() // "https://slow-timer.info"
* @endcode
*/
FAKER_CXX_EXPORT std::string url(const WebProtocol& webProtocol = WebProtocol::Https);
FAKER_CXX_EXPORT std::string url(const WebProtocol& webProtocol = WebProtocol::Https, Locale locale = Locale::en_US);

/**
* @brief Generates a random domain name.
Expand All @@ -316,7 +317,7 @@ FAKER_CXX_EXPORT std::string url(const WebProtocol& webProtocol = WebProtocol::H
* faker::internet::domainName() // "slow-timer.info"
* @endcode
*/
FAKER_CXX_EXPORT std::string domainName();
FAKER_CXX_EXPORT std::string domainName(Locale locale = Locale::en_US);

/**
* @brief Generates a random domain word.
Expand All @@ -338,7 +339,7 @@ FAKER_CXX_EXPORT std::string domainWord();
* faker::internet::domainSuffix() // "com"
* @endcode
*/
FAKER_CXX_EXPORT std::string_view domainSuffix();
FAKER_CXX_EXPORT std::string_view domainSuffix(Locale locale = Locale::en_US);

/**
* @brief Generates a random username.
Expand Down Expand Up @@ -388,5 +389,5 @@ getJWTToken(const std::optional<std::map<std::string, std::string>>& header = st
* faker::internet::getJWTAlgorithm(); // "HS256"
* @endcode
*/
FAKER_CXX_EXPORT std::string_view getJWTAlgorithm();
FAKER_CXX_EXPORT std::string_view getJWTAlgorithm(Locale locale = Locale::en_US);
}
62 changes: 44 additions & 18 deletions src/modules/internet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,18 @@ std::string toJSON(std::map<std::string, std::string>& data)

namespace faker::internet
{
namespace
{
const struct InternetDefinition& getInternetDefinition(Locale locale)
{
switch (locale)
{
default:
return enUSInternetDefinition;
}
}
}

namespace
{
const std::array<std::string_view, 2> webProtocols{"http", "https"};
Expand Down Expand Up @@ -194,16 +206,18 @@ std::string username(std::optional<std::string> firstName, std::optional<std::st
}

std::string email(std::optional<std::string> firstName, std::optional<std::string> lastName,
std::optional<std::string> emailHost)
std::optional<std::string> emailHost, Locale locale)
{
const auto& internetDefinition = getInternetDefinition(locale);
return common::format("{}@{}", username(std::move(firstName), std::move(lastName)),
emailHost ? *emailHost : helper::randomElement(emailHosts));
emailHost ? *emailHost : helper::randomElement(internetDefinition.emailHosts));
}

std::string exampleEmail(std::optional<std::string> firstName, std::optional<std::string> lastName)
std::string exampleEmail(std::optional<std::string> firstName, std::optional<std::string> lastName, Locale locale)
{
const auto& internetDefinition = getInternetDefinition(locale);
return common::format("{}@{}", username(std::move(firstName), std::move(lastName)),
helper::randomElement(emailExampleHosts));
helper::randomElement(internetDefinition.emailExampleHosts));
}

std::string password(int length, const PasswordOptions& options)
Expand Down Expand Up @@ -294,19 +308,25 @@ unsigned httpStatusCode(std::optional<HttpResponseType> responseType)
return helper::randomElement(statusCodes);
}

std::string_view httpRequestHeader()
std::string_view httpRequestHeader(Locale locale)
{
return helper::randomElement(httpRequestHeaders);
const auto& internetDefinition = getInternetDefinition(locale);

return helper::randomElement(internetDefinition.httpRequestHeaders);
}

std::string_view httpResponseHeader()
std::string_view httpResponseHeader(Locale locale)
{
return helper::randomElement(httpResponseHeaders);
const auto& internetDefinition = getInternetDefinition(locale);

return helper::randomElement(internetDefinition.httpResponseHeaders);
}

std::string_view httpMediaType()
std::string_view httpMediaType(Locale locale)
{
return helper::randomElement(httpMediaTypes);
const auto& internetDefinition = getInternetDefinition(locale);

return helper::randomElement(internetDefinition.httpMediaTypes);
}

std::string ipv4(const IPv4Class& ipv4class)
Expand Down Expand Up @@ -396,26 +416,30 @@ unsigned port()
return number::integer(65535u);
}

std::string url(const WebProtocol& webProtocol)
std::string url(const WebProtocol& webProtocol, Locale locale)
{
const auto& internetDefinition = getInternetDefinition(locale);

const auto protocolStr = webProtocol == WebProtocol::Https ? "https" : "http";

return common::format("{}://{}", protocolStr, domainName());
return common::format("{}://{}", protocolStr, domainName(locale));
}

std::string domainName()
std::string domainName(Locale locale)
{
return common::format("{}.{}", domainWord(), domainSuffix());
return common::format("{}.{}", domainWord(), domainSuffix(locale));
}

std::string domainWord()
{
return common::toLower(common::format("{}-{}", word::adjective(), word::noun()));
}

std::string_view domainSuffix()
std::string_view domainSuffix(Locale locale)
{
return helper::randomElement(domainSuffixes);
const auto& internetDefinition = getInternetDefinition(locale);

return helper::randomElement(internetDefinition.domainSuffixes);
}

std::string anonymousUsername(unsigned maxLength)
Expand All @@ -435,9 +459,11 @@ std::string anonymousUsername(unsigned maxLength)
return common::format("{}{}", word::adjective(adjectiveLength), word::noun(nounLength));
}

std::string_view getJWTAlgorithm()
std::string_view getJWTAlgorithm(Locale locale)
{
return helper::randomElement(jwtAlgorithms);
const auto& internetDefinition = getInternetDefinition(locale);

return helper::randomElement(internetDefinition.jwtAlgorithms);
}

std::string getJWTToken(const std::optional<std::map<std::string, std::string>>& header,
Expand Down
37 changes: 30 additions & 7 deletions src/modules/internet_data.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
#pragma once

#include <array>
#include <span>
#include <string_view>

namespace faker::internet
{
const auto domainSuffixes = std::to_array<std::string_view>({

struct InternetDefinition
{
std::span<const std::string_view> domainSuffixes;
std::span<const std::string_view> emailHosts;
std::span<const std::string_view> emailExampleHosts;
std::span<const std::string_view> httpMediaTypes;
std::span<const std::string_view> httpRequestHeaders;
std::span<const std::string_view> httpResponseHeaders;
std::span<const std::string_view> jwtAlgorithms;
};

const auto enUSdomainSuffixes = std::to_array<std::string_view>({
"biz",
"com",
"info",
Expand All @@ -14,14 +27,14 @@ const auto domainSuffixes = std::to_array<std::string_view>({
"org",
});

const auto emailHosts = std::to_array<std::string_view>({
const auto enUSemailHosts = std::to_array<std::string_view>({
"gmail.com",
"hotmail.com",
"outlook.com",
"yahoo.com",
});

const auto emailExampleHosts = std::to_array<std::string_view>({
const auto enUSemailExampleHosts = std::to_array<std::string_view>({
"example.com",
"example.net",
"example.org",
Expand Down Expand Up @@ -739,7 +752,7 @@ const auto flagEmojis = std::to_array<std::string_view>({
"🚩",
});

const auto httpMediaTypes = std::to_array<std::string_view>({
const auto enUShttpMediaTypes = std::to_array<std::string_view>({
"application/gzip",
"application/java-archive",
"application/json",
Expand Down Expand Up @@ -787,7 +800,7 @@ const auto httpMediaTypes = std::to_array<std::string_view>({
"video/x-msvideo",
});

const auto httpRequestHeaders = std::to_array<std::string_view>({
const auto enUShttpRequestHeaders = std::to_array<std::string_view>({
"A-IM",
"Accept",
"Accept-Charset",
Expand Down Expand Up @@ -831,7 +844,7 @@ const auto httpRequestHeaders = std::to_array<std::string_view>({
"Warning",
});

const auto httpResponseHeaders = std::to_array<std::string_view>({
const auto enUShttpResponseHeaders = std::to_array<std::string_view>({
"Accept-CH",
"Accept-Patch",
"Accept-Ranges",
Expand Down Expand Up @@ -878,7 +891,7 @@ const auto httpResponseHeaders = std::to_array<std::string_view>({
"Warning",
});

const auto jwtAlgorithms = std::to_array<std::string_view>({
const auto enUSjwtAlgorithms = std::to_array<std::string_view>({
"HS256",
"HS384",
"HS512",
Expand All @@ -894,4 +907,14 @@ const auto jwtAlgorithms = std::to_array<std::string_view>({
"none",
});

const InternetDefinition enUSInternetDefinition = {
.domainSuffixes = enUSdomainSuffixes,
.emailHosts = enUSemailHosts,
.emailExampleHosts = enUSemailExampleHosts,
.httpMediaTypes = enUShttpMediaTypes,
.httpRequestHeaders = enUShttpRequestHeaders,
.httpResponseHeaders = enUShttpResponseHeaders,
.jwtAlgorithms = enUSjwtAlgorithms,
};

}
Loading

0 comments on commit caebac9

Please sign in to comment.