Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue: 157 , made HeaderField map case-insensitive #203

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
19 changes: 18 additions & 1 deletion include/restclient-cpp/restclient.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,24 @@ namespace RestClient {
/**
* public data definitions
*/
typedef std::map<std::string, std::string> HeaderFields;

// comparator for case-insensitive comparison in HeaderFeild map
struct ci_comp
{
struct nocase_compare
{
bool operator() (const unsigned char& c1, const unsigned char& c2) const {
return tolower (c1) < tolower (c2);
}
};
bool operator() (const std::string & s1, const std::string & s2) const {
return std::lexicographical_compare
(s1.begin (), s1.end (),
s2.begin (), s2.end (),
nocase_compare ());
}
};
typedef std::map<std::string, std::string, ci_comp> HeaderFields;

/** @struct Response
* @brief This structure represents the HTTP response data
Expand Down
33 changes: 33 additions & 0 deletions test/test_restclient.cc
Original file line number Diff line number Diff line change
Expand Up @@ -253,3 +253,36 @@ TEST_F(RestClientTest, TestRestClientHeadCode)
EXPECT_EQ(200, res.code);
EXPECT_EQ("", res.body);
}

// check for header map case-insensitivity
TEST_F(RestClientTest, TestHeaderCaseInsensitivity)
{
RestClient::Response res = RestClient::get("https://httpbin.org/get");
bool caseInsensitive = false;

try {
std::string& lowerDate = res.headers.at("date");
std::string& upperDate = res.headers.at("Date");
caseInsensitive = (lowerDate == upperDate);
} catch (std::out_of_range&) {
caseInsensitive = false;
}

EXPECT_TRUE(caseInsensitive) << "Headers should be case-insensitive but were not.";
}

TEST_F(RestClientTest, TestHeaderContentTypeCaseInsensitivity)
{
RestClient::Response res = RestClient::get("https://httpbin.org/get");
bool caseInsensitive = false;

try {
std::string& lowerContentType = res.headers.at("content-type");
std::string& upperContentType = res.headers.at("Content-Type");
caseInsensitive = (lowerContentType == upperContentType);
} catch (std::out_of_range&) {
caseInsensitive = false;
}

EXPECT_TRUE(caseInsensitive) << "Content-Type header should be case-insensitive but was not.";
}