Skip to content
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
4 changes: 1 addition & 3 deletions libstuff/libstuff.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1433,9 +1433,7 @@ string SEncodeURIComponent(const string& value)
// it "escapes all characters except the following: alphabetic, decimal digits, - _ . ! ~ * ' ( )"
const char* hexChars = "0123456789ABCDEF";
string working;
for (int c = 0; c < (int) value.size(); ++c) {
// Test this character
char ch = value[c];
for (const char8_t ch : value) {
// Why isn't this just isalnum(ch)?
// http://cplusplus.com/reference/clibrary/cctype/isalnum/
if (SWITHIN('a', ch, 'z') || SWITHIN('A', ch, 'Z') || SWITHIN('0', ch, '9')) {
Expand Down
22 changes: 21 additions & 1 deletion test/tests/LibStuffTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ struct LibStuff : tpunit::TestFixture
TEST(LibStuff::SQResultTest),
TEST(LibStuff::testReturningClause),
TEST(LibStuff::SRedactSensitiveValuesTest),
TEST(LibStuff::SComposeHTTPTest)
TEST(LibStuff::SComposeHTTPTest),
TEST(LibStuff::testEncodeDecodeURIComponent)
)
{
}
Expand Down Expand Up @@ -992,4 +993,23 @@ struct LibStuff : tpunit::TestFixture
string methodLineWithControlChars = "500 Internal Server Error\r\nContent-Type: application/json";
ASSERT_THROW(SComposeHTTP(methodLineWithControlChars, {}, ""), SException);
}

void testEncodeDecodeURIComponent()
{
// ASCII passthrough
ASSERT_EQUAL(SEncodeURIComponent("hello"), "hello");
ASSERT_EQUAL(SDecodeURIComponent(SEncodeURIComponent("hello")), "hello");

// Space → +
ASSERT_EQUAL(SEncodeURIComponent("hello world"), "hello+world");
ASSERT_EQUAL(SDecodeURIComponent(SEncodeURIComponent("hello world")), "hello world");

// UTF-8: ā (U+0101) = bytes 0xC4 0x81 → %C4%81
ASSERT_EQUAL(SEncodeURIComponent("Sh\xC4\x81hrukh"), "Sh%C4%81hrukh");
ASSERT_EQUAL(SDecodeURIComponent(SEncodeURIComponent("Sh\xC4\x81hrukh")), "Sh\xC4\x81hrukh");

// UTF-8: ü (U+00FC) = bytes 0xC3 0xBC → %C3%BC
ASSERT_EQUAL(SEncodeURIComponent("\xC3\xBC"), "%C3%BC");
ASSERT_EQUAL(SDecodeURIComponent(SEncodeURIComponent("\xC3\xBC")), "\xC3\xBC");
}
} __LibStuff;
Loading