test-http-url.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #include <cassert>
  2. #include "../HTTP.h"
  3. using namespace i2p::http;
  4. int main() {
  5. std::map<std::string, std::string> params;
  6. URL *url;
  7. url = new URL;
  8. assert(url->parse("https://127.0.0.1:7070/asdasd?12345") == true);
  9. assert(url->schema == "https");
  10. assert(url->user == "");
  11. assert(url->pass == "");
  12. assert(url->host == "127.0.0.1");
  13. assert(url->port == 7070);
  14. assert(url->path == "/asdasd");
  15. assert(url->query == "12345");
  16. assert(url->to_string() == "https://127.0.0.1:7070/asdasd?12345");
  17. delete url;
  18. url = new URL;
  19. assert(url->parse("http://user:password@site.com:8080/asdasd?123456") == true);
  20. assert(url->schema == "http");
  21. assert(url->user == "user");
  22. assert(url->pass == "password");
  23. assert(url->host == "site.com");
  24. assert(url->port == 8080);
  25. assert(url->path == "/asdasd");
  26. assert(url->query == "123456");
  27. delete url;
  28. url = new URL;
  29. assert(url->parse("http://user:password@site.com/asdasd?name=value") == true);
  30. assert(url->schema == "http");
  31. assert(url->user == "user");
  32. assert(url->pass == "password");
  33. assert(url->host == "site.com");
  34. assert(url->port == 0);
  35. assert(url->path == "/asdasd");
  36. assert(url->query == "name=value");
  37. delete url;
  38. url = new URL;
  39. assert(url->parse("http://user:@site.com/asdasd?name=value1&name=value2") == true);
  40. assert(url->schema == "http");
  41. assert(url->user == "user");
  42. assert(url->pass == "");
  43. assert(url->host == "site.com");
  44. assert(url->port == 0);
  45. assert(url->path == "/asdasd");
  46. assert(url->query == "name=value1&name=value2");
  47. delete url;
  48. url = new URL;
  49. assert(url->parse("http://user@site.com/asdasd?name1=value1&name2&name3=value2") == true);
  50. assert(url->schema == "http");
  51. assert(url->user == "user");
  52. assert(url->pass == "");
  53. assert(url->host == "site.com");
  54. assert(url->port == 0);
  55. assert(url->path == "/asdasd");
  56. assert(url->query == "name1=value1&name2&name3=value2");
  57. assert(url->parse_query(params));
  58. assert(params.size() == 3);
  59. assert(params.count("name1") == 1);
  60. assert(params.count("name2") == 1);
  61. assert(params.count("name3") == 1);
  62. assert(params.find("name1")->second == "value1");
  63. assert(params.find("name2")->second == "");
  64. assert(params.find("name3")->second == "value2");
  65. delete url;
  66. url = new URL;
  67. assert(url->parse("http://@site.com:800/asdasd?") == true);
  68. assert(url->schema == "http");
  69. assert(url->user == "");
  70. assert(url->pass == "");
  71. assert(url->host == "site.com");
  72. assert(url->port == 800);
  73. assert(url->path == "/asdasd");
  74. assert(url->query == "");
  75. delete url;
  76. url = new URL;
  77. assert(url->parse("http://@site.com:17") == true);
  78. assert(url->schema == "http");
  79. assert(url->user == "");
  80. assert(url->pass == "");
  81. assert(url->host == "site.com");
  82. assert(url->port == 17);
  83. assert(url->path == "");
  84. assert(url->query == "");
  85. delete url;
  86. url = new URL;
  87. assert(url->parse("http://user:password@site.com:err_port/asdasd") == false);
  88. assert(url->schema == "http");
  89. assert(url->user == "user");
  90. assert(url->pass == "password");
  91. assert(url->host == "site.com");
  92. assert(url->port == 0);
  93. assert(url->path == "");
  94. assert(url->query == "");
  95. delete url;
  96. url = new URL;
  97. assert(url->parse("http://user:password@site.com:84/asdasd/@17#frag") == true);
  98. assert(url->schema == "http");
  99. assert(url->user == "user");
  100. assert(url->pass == "password");
  101. assert(url->host == "site.com");
  102. assert(url->port == 84);
  103. assert(url->path == "/asdasd/@17");
  104. assert(url->query == "");
  105. assert(url->frag == "frag");
  106. delete url;
  107. return 0;
  108. }
  109. /* vim: expandtab:ts=2 */