From 6b8d51c5abcff840bc031b4cd242b2e1e5ce4cd9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Cie=C5=9Blar?= Date: Fri, 10 Nov 2023 12:43:45 +0100 Subject: [PATCH] add http media type generation (#230) --- include/faker-cxx/Internet.h | 11 +++++ src/modules/internet/Internet.cpp | 6 +++ src/modules/internet/InternetTest.cpp | 9 ++++ src/modules/internet/data/HttpMediaType.h | 53 +++++++++++++++++++++++ 4 files changed, 79 insertions(+) create mode 100644 src/modules/internet/data/HttpMediaType.h diff --git a/include/faker-cxx/Internet.h b/include/faker-cxx/Internet.h index cbe08dcf..c05bc0fa 100644 --- a/include/faker-cxx/Internet.h +++ b/include/faker-cxx/Internet.h @@ -168,6 +168,17 @@ class Internet */ static std::string httpResponseHeader(); + /** + * @brief Generates a random http media type. + * + * @returns Http media type. + * + * @code + * Internet::httpMediaType() // "application/json" + * @endcode + */ + static std::string httpMediaType(); + /** * @brief Returns a string containing randomized ipv4 address of the given class. * diff --git a/src/modules/internet/Internet.cpp b/src/modules/internet/Internet.cpp index 2c1f8243..633ba64d 100644 --- a/src/modules/internet/Internet.cpp +++ b/src/modules/internet/Internet.cpp @@ -7,6 +7,7 @@ #include "data/DomainSuffixes.h" #include "data/EmailHosts.h" #include "data/Emojis.h" +#include "data/HttpMediaType.h" #include "data/HttpRequestHeaders.h" #include "data/HttpResponseHeaders.h" #include "faker-cxx/Helper.h" @@ -170,6 +171,11 @@ std::string Internet::httpResponseHeader() return Helper::arrayElement(httpResponseHeaders); } +std::string Internet::httpMediaType() +{ + return Helper::arrayElement(httpMediaTypes); +} + std::string Internet::ipv4(IPv4Class ipv4class) { IPv4Address sectors; diff --git a/src/modules/internet/InternetTest.cpp b/src/modules/internet/InternetTest.cpp index 012e95e8..fa768be8 100644 --- a/src/modules/internet/InternetTest.cpp +++ b/src/modules/internet/InternetTest.cpp @@ -16,6 +16,7 @@ #include "data/DomainSuffixes.h" #include "data/EmailHosts.h" #include "data/Emojis.h" +#include "data/HttpMediaType.h" #include "data/HttpRequestHeaders.h" #include "data/HttpResponseHeaders.h" @@ -527,6 +528,14 @@ TEST_F(InternetTest, shouldGenerateHttpResponseHeader) { return generatedHttpResponseHeader == httpHeader; })); } +TEST_F(InternetTest, shouldGenerateHttpMediaType) +{ + const auto generatedHttpMediaType = Internet::httpMediaType(); + + ASSERT_TRUE(std::ranges::any_of(httpMediaTypes, [generatedHttpMediaType](const std::string& httpMediaType) + { return generatedHttpMediaType == httpMediaType; })); +} + TEST_F(InternetTest, shouldGenerateHttpInformationalSuccessCode) { const auto generatedHttpStatusCode = Internet::httpStatusCode(HttpResponseType::Informational); diff --git a/src/modules/internet/data/HttpMediaType.h b/src/modules/internet/data/HttpMediaType.h new file mode 100644 index 00000000..32255f12 --- /dev/null +++ b/src/modules/internet/data/HttpMediaType.h @@ -0,0 +1,53 @@ +#pragma once + +#include +#include + +namespace faker +{ +const std::vector httpMediaTypes = {"audio/aac", + "application/x-abiword", + "application/x-freearc", + "video/x-msvideo", + "application/vnd.amazon.ebook", + "application/octet-stream", + "image/bmp", + "application/x-bzip", + "application/x-bzip2", + "application/x-csh", + "text/css", + "text/csv", + "application/gzip", + "image/gif", + "text/html", + "text/calendar", + "application/java-archive", + "image/jpeg", + "text/javascript", + "application/json", + "audio/mpeg", + "video/mpeg", + "font/otf", + "image/png", + "application/pdf", + "application/rtf", + "image/tiff", + "video/mp2t", + "font/ttf", + "text/plain", + "audio/wav", + "audio/webm", + "video/webm", + "image/webp", + "font/woff", + "font/woff2", + "application/xml", + "text/xml", + "application/vnd.mozilla.xul+xml", + "application/zip", + "video/3gpp", + "audio/3gpp", + "video/3gpp2", + "audio/3gpp2", + "application/x-7z-compressed"}; +}