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