Skip to content

Commit

Permalink
add http media type generation (#230)
Browse files Browse the repository at this point in the history
  • Loading branch information
cieslarmichal authored Nov 10, 2023
1 parent 4d44bcd commit 6b8d51c
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 0 deletions.
11 changes: 11 additions & 0 deletions include/faker-cxx/Internet.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
6 changes: 6 additions & 0 deletions src/modules/internet/Internet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -170,6 +171,11 @@ std::string Internet::httpResponseHeader()
return Helper::arrayElement<std::string>(httpResponseHeaders);
}

std::string Internet::httpMediaType()
{
return Helper::arrayElement<std::string>(httpMediaTypes);
}

std::string Internet::ipv4(IPv4Class ipv4class)
{
IPv4Address sectors;
Expand Down
9 changes: 9 additions & 0 deletions src/modules/internet/InternetTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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);
Expand Down
53 changes: 53 additions & 0 deletions src/modules/internet/data/HttpMediaType.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#pragma once

#include <string>
#include <vector>

namespace faker
{
const std::vector<std::string> 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"};
}

0 comments on commit 6b8d51c

Please sign in to comment.