File: test-http-res.cpp

package info (click to toggle)
i2pd 2.58.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,612 kB
  • sloc: cpp: 59,663; makefile: 224; sh: 138
file content (50 lines) | stat: -rw-r--r-- 1,360 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <cassert>
#include "HTTP.h"

using namespace i2p::http;

int main() {
  HTTPRes *res;
  int ret = 0, len = 0;
  const char *buf;

  /* test: parsing valid response without body */
  buf =
    "HTTP/1.1 304 Not Modified\r\n"
    "Date: Thu, 14 Apr 2016 00:00:00 GMT\r\n"
    "Server: nginx/1.2.1\r\n"
    "Content-Length: 536\r\n"
    "\r\n";
  len = strlen(buf);
  res = new HTTPRes;
  assert((ret = res->parse(buf, len)) == len);
  assert(res->version == "HTTP/1.1");
  assert(res->status == "Not Modified");
  assert(res->code == 304);
  assert(res->headers.size() == 3);
  assert(res->headers.count("Date") == 1);
  assert(res->headers.count("Server") == 1);
  assert(res->headers.count("Content-Length") == 1);
  assert(res->headers.find("Date")->second == "Thu, 14 Apr 2016 00:00:00 GMT");
  assert(res->headers.find("Server")->second == "nginx/1.2.1");
  assert(res->headers.find("Content-Length")->second == "536");
  assert(res->is_chunked() == false);
  assert(res->content_length() == 536);
  delete res;

  /* test: building request */
  buf =
    "HTTP/1.0 304 Not Modified\r\n"
    "Content-Length: 0\r\n"
    "\r\n";
  res = new HTTPRes;
  res->version = "HTTP/1.0";
  res->code = 304;
  res->status = "Not Modified";
  res->add_header("Content-Length", "0");
  assert(res->to_string() == buf);

  return 0;
}

/* vim: expandtab:ts=2 */