Browse Source

Tests and documentation for util::url.

pull/228/head
EinMByte 9 years ago
parent
commit
66754aac5a
  1. 72
      tests/Utility.cpp
  2. 26
      util.cpp
  3. 24
      util.h

72
tests/Utility.cpp

@ -14,6 +14,78 @@ BOOST_AUTO_TEST_CASE(DecodeUrl)
{ {
BOOST_CHECK_EQUAL(urlDecode("%20"), " "); BOOST_CHECK_EQUAL(urlDecode("%20"), " ");
} }
BOOST_AUTO_TEST_CASE(ParseUrlProtocol)
{
BOOST_CHECK_EQUAL(url("http://127.0.0.1:7070/asdasd?qqqqqqqqqqqq").protocol_, "http");
BOOST_CHECK_EQUAL(url("http://user:password@site.com:err_port/A/B?q").protocol_, "http");
BOOST_CHECK_EQUAL(url("ftp://user@localhost:123").protocol_, "ftp");
BOOST_CHECK_EQUAL(url("SSH://user:pass@localhost:123").protocol_, "ssh");
BOOST_CHECK_EQUAL(url("").protocol_, "");
}
BOOST_AUTO_TEST_CASE(ParseUrlHost)
{
BOOST_CHECK_EQUAL(url("http://127.0.0.1:7070/asdasd?qqqqqqqqqqqq").host_, "127.0.0.1");
BOOST_CHECK_EQUAL(url("http://user:password@site.com:err_port/A/B?q").host_, "site.com");
BOOST_CHECK_EQUAL(url("ftp://user@localhost:123").host_, "localhost");
BOOST_CHECK_EQUAL(url("SSH://user:pass@localhost:123").host_, "localhost");
BOOST_CHECK_EQUAL(url("").host_, "");
}
BOOST_AUTO_TEST_CASE(ParseUrlPath)
{
BOOST_CHECK_EQUAL(url("http://127.0.0.1:7070/asdasd?qqqqqqqqqqqq").path_, "/asdasd");
BOOST_CHECK_EQUAL(url("http://user:password@site.com:err_port/A/B?q").path_, "/A/B");
BOOST_CHECK_EQUAL(url("ftp://user@localhost:123/A/B/C/D?x=A").path_, "/A/B/C/D");
BOOST_CHECK_EQUAL(url("SSH://user:pass@localhost:123").path_, "");
BOOST_CHECK_EQUAL(url("").path_, "");
}
BOOST_AUTO_TEST_CASE(ParseUrlQuery)
{
BOOST_CHECK_EQUAL(url("http://127.0.0.1:7070/asdasd?qqqqqqqqqqqq").query_, "qqqqqqqqqqqq");
BOOST_CHECK_EQUAL(url("http://user:password@site.com:err_port/A/B?q").query_, "q");
BOOST_CHECK_EQUAL(url("ftp://user@localhost:123/A/B/C/D?x=A").query_, "x=A");
BOOST_CHECK_EQUAL(url("SSH://user:pass@localhost:123").query_, "");
BOOST_CHECK_EQUAL(url("").query_, "");
}
BOOST_AUTO_TEST_CASE(ParseUrlPortStr)
{
BOOST_CHECK_EQUAL(url("http://127.0.0.1:7070/asdasd?qqqqqqqqqqqq").portstr_, "7070");
BOOST_CHECK_EQUAL(url("http://user:password@site.com:err_port/A/B?q").portstr_, "err_port");
BOOST_CHECK_EQUAL(url("ftp://user@localhost:123/A/B/C/D?x=A").portstr_, "123");
BOOST_CHECK_EQUAL(url("SSH://user:pass@localhost:123").portstr_, "123");
BOOST_CHECK_EQUAL(url("").portstr_, "80");
}
BOOST_AUTO_TEST_CASE(ParseUrlPort)
{
BOOST_CHECK_EQUAL(url("http://127.0.0.1:7070/asdasd?qqqqqqqqqqqq").port_, 7070);
BOOST_CHECK_EQUAL(url("http://user:password@site.com:err_port/A/B?q").port_, 80);
BOOST_CHECK_EQUAL(url("ftp://user@localhost:123/A/B/C/D?x=A").port_, 123);
BOOST_CHECK_EQUAL(url("SSH://user:pass@localhost:123").port_, 123);
BOOST_CHECK_EQUAL(url("").port_, 80);
}
BOOST_AUTO_TEST_CASE(ParseUrlUser)
{
BOOST_CHECK_EQUAL(url("http://127.0.0.1:7070/asdasd?qqqqqqqqqqqq").user_, "");
BOOST_CHECK_EQUAL(url("http://user:password@site.com:err_port/A/B?q").user_, "user");
BOOST_CHECK_EQUAL(url("ftp://user@localhost:123/A/B/C/D?x=A").user_, "user");
BOOST_CHECK_EQUAL(url("SSH://@localhost:123").user_, "");
BOOST_CHECK_EQUAL(url("SSH://user:@localhost:123").user_, "user");
BOOST_CHECK_EQUAL(url("").user_, "");
}
BOOST_AUTO_TEST_CASE(ParseUrlPassword)
{
BOOST_CHECK_EQUAL(url("http://127.0.0.1:7070/asdasd?qqqqqqqqqqqq").pass_, "");
BOOST_CHECK_EQUAL(url("http://user:password@site.com:err_port/A/B?q").pass_, "password");
BOOST_CHECK_EQUAL(url("ftp://user@localhost:123/A/B/C/D?x=A").pass_, "");
BOOST_CHECK_EQUAL(url("SSH://@localhost:123").pass_, "");
BOOST_CHECK_EQUAL(url("SSH://:password@localhost:123").pass_, "password");
BOOST_CHECK_EQUAL(url("").pass_, "");
}
BOOST_AUTO_TEST_SUITE_END() BOOST_AUTO_TEST_SUITE_END()

26
util.cpp

@ -48,12 +48,12 @@ http://stackoverflow.com/questions/15660203/inet-pton-identifier-not-found */
{ {
switch (af) switch (af)
{ {
case AF_INET: case AF_INET:
*(struct in_addr *)dst = ((struct sockaddr_in *)&ss)->sin_addr; *(struct in_addr *)dst = ((struct sockaddr_in *)&ss)->sin_addr;
return 1; return 1;
case AF_INET6: case AF_INET6:
*(struct in6_addr *)dst = ((struct sockaddr_in6 *)&ss)->sin6_addr; *(struct in6_addr *)dst = ((struct sockaddr_in6 *)&ss)->sin6_addr;
return 1; return 1;
} }
} }
return 0; return 0;
@ -62,15 +62,15 @@ http://stackoverflow.com/questions/15660203/inet-pton-identifier-not-found */
namespace i2p namespace i2p
{ {
namespace util namespace util
{ {
namespace config namespace config
{ {
std::map<std::string, std::string> mapArgs; std::map<std::string, std::string> mapArgs;
std::map<std::string, std::vector<std::string> > mapMultiArgs; std::map<std::string, std::vector<std::string> > mapMultiArgs;
void OptionParser(int argc, const char* const argv[]) void OptionParser(int argc, const char* const argv[])
{ {
mapArgs.clear(); mapArgs.clear();
mapMultiArgs.clear(); mapMultiArgs.clear();

24
util.h

@ -52,16 +52,22 @@ namespace util
int httpRequestViaI2pProxy(const std::string& address, std::string &content); // return http code int httpRequestViaI2pProxy(const std::string& address, std::string &content); // return http code
std::string urlDecode(const std::string& data); std::string urlDecode(const std::string& data);
/**
* Provides functionality for parsing URLs.
*/
struct url { struct url {
url(const std::string& url_s); // omitted copy, ==, accessors, ... /**
private: * Parse a url given as a string.
void parse(const std::string& url_s); */
public: url(const std::string& url_s);
std::string protocol_, host_, path_, query_; private:
std::string portstr_; void parse(const std::string& url_s);
unsigned int port_; public:
std::string user_; std::string protocol_, host_, path_, query_;
std::string pass_; std::string portstr_;
unsigned int port_;
std::string user_;
std::string pass_;
}; };
} }

Loading…
Cancel
Save