Add more tests for decoding and encoding URL functions

This commit is contained in:
Luis Diaz Mas 2018-08-26 14:15:46 +02:00 committed by Luis Díaz Más
parent eefee8125b
commit 5d76036af5
2 changed files with 26 additions and 0 deletions

View File

@ -69,6 +69,7 @@ namespace Exiv2
@param str The url needs encoding.
@return the url-encoded version of str.
@note Source: http://www.geekhideout.com/urlcode.shtml
@todo This function can probably be hidden into the implementation details
*/
EXIV2API std::string urlencode(const char* str);
@ -79,11 +80,13 @@ namespace Exiv2
@note Be sure to free() the returned string after use
Source: http://www.geekhideout.com/urlcode.shtml
@todo This function can probably be hidden into the implementation details
*/
EXIV2API char* urldecode(const char* str);
/*!
@brief Like urlencode(char* str) but accept the input url in the std::string and modify it.
@todo This function can probably be hidden into the implementation details
*/
EXIV2API void urldecode(std::string& str);

View File

@ -83,3 +83,26 @@ TEST(urlencode, encodesGivenUrl)
const std::string url = urlencode("http://www.geekhideout.com/urlcode.shtml");
ASSERT_STREQ("http%3a%2f%2fwww.geekhideout.com%2furlcode.shtml", url.c_str());
}
TEST(urlencode, encodesGivenUrlWithSpace)
{
const std::string url = urlencode("http://www.geekhideout.com/url code.shtml");
ASSERT_STREQ("http%3a%2f%2fwww.geekhideout.com%2furl+code.shtml", url.c_str());
}
TEST(urldecode, decodesGivenUrl)
{
const std::string expectedDecodedUrl ("http://www.geekhideout.com/urlcode.shtml");
const std::string url ("http%3a%2f%2fwww.geekhideout.com%2furlcode.shtml");
char * url3 = urldecode(url.c_str());
ASSERT_STREQ(expectedDecodedUrl.c_str(), url3);
free(url3);
}
TEST(urldecode, decodesGivenUrlInPlace)
{
const std::string expectedDecodedUrl ("http://www.geekhideout.com/urlcode.shtml");
std::string url ("http%3a%2f%2fwww.geekhideout.com%2furlcode.shtml");
urldecode(url);
ASSERT_STREQ(expectedDecodedUrl.c_str(), url.c_str());
}