test-http-res.cpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #include <cassert>
  2. #include "../HTTP.h"
  3. using namespace i2p::http;
  4. int main() {
  5. HTTPRes *res;
  6. int ret = 0, len = 0;
  7. const char *buf;
  8. /* test: parsing valid response without body */
  9. buf =
  10. "HTTP/1.1 304 Not Modified\r\n"
  11. "Date: Thu, 14 Apr 2016 00:00:00 GMT\r\n"
  12. "Server: nginx/1.2.1\r\n"
  13. "Content-Length: 536\r\n"
  14. "\r\n";
  15. len = strlen(buf);
  16. res = new HTTPRes;
  17. assert((ret = res->parse(buf, len)) == len);
  18. assert(res->version == "HTTP/1.1");
  19. assert(res->status == "Not Modified");
  20. assert(res->code == 304);
  21. assert(res->headers.size() == 3);
  22. assert(res->headers.count("Date") == 1);
  23. assert(res->headers.count("Server") == 1);
  24. assert(res->headers.count("Content-Length") == 1);
  25. assert(res->headers.find("Date")->second == "Thu, 14 Apr 2016 00:00:00 GMT");
  26. assert(res->headers.find("Server")->second == "nginx/1.2.1");
  27. assert(res->headers.find("Content-Length")->second == "536");
  28. assert(res->is_chunked() == false);
  29. assert(res->content_length() == 536);
  30. delete res;
  31. /* test: building request */
  32. buf =
  33. "HTTP/1.0 304 Not Modified\r\n"
  34. "Content-Length: 0\r\n"
  35. "\r\n";
  36. res = new HTTPRes;
  37. res->version = "HTTP/1.0";
  38. res->code = 304;
  39. res->status = "Not Modified";
  40. res->add_header("Content-Length", "0");
  41. assert(res->to_string() == buf);
  42. return 0;
  43. }
  44. /* vim: expandtab:ts=2 */