i2pd/tests/test-http-req.cpp

90 lines
2.3 KiB
C++
Raw Normal View History

2016-04-27 00:00:00 +00:00
#include <cassert>
2022-11-23 03:28:33 +00:00
#include "HTTP.h"
2016-04-27 00:00:00 +00:00
using namespace i2p::http;
2016-05-24 00:00:00 +00:00
int main() {
2016-04-27 00:00:00 +00:00
HTTPReq *req;
int ret = 0, len = 0;
const char *buf;
2016-05-24 00:00:00 +00:00
/* test: parsing request with body */
2016-04-27 00:00:00 +00:00
buf =
"GET / HTTP/1.0\r\n"
"User-Agent: curl/7.26.0\r\n"
"Host: inr.i2p\r\n"
"Accept: */*\r\n"
"\r\n"
"test";
len = strlen(buf);
req = new HTTPReq;
assert((ret = req->parse(buf, len)) == len - 4);
assert(req->version == "HTTP/1.0");
assert(req->method == "GET");
assert(req->uri == "/");
2023-01-28 22:18:54 +00:00
assert(req->GetNumHeaders () == 3);
assert(req->GetNumHeaders("Host") == 1);
assert(req->GetNumHeaders("Accept") == 1);
assert(req->GetNumHeaders("User-Agent") == 1);
assert(req->GetHeader("Host") == "inr.i2p");
assert(req->GetHeader("Accept") == "*/*");
assert(req->GetHeader("User-Agent") == "curl/7.26.0");
2016-04-27 00:00:00 +00:00
delete req;
2016-05-24 00:00:00 +00:00
/* test: parsing request without body */
2016-04-27 00:00:00 +00:00
buf =
"GET / HTTP/1.0\r\n"
"\r\n";
len = strlen(buf);
req = new HTTPReq;
assert((ret = req->parse(buf, len)) == len);
assert(req->version == "HTTP/1.0");
assert(req->method == "GET");
assert(req->uri == "/");
2023-01-28 22:18:54 +00:00
assert(req->GetNumHeaders () == 0);
2016-04-27 00:00:00 +00:00
delete req;
2016-05-24 00:00:00 +00:00
/* test: parsing request without body */
2016-04-27 00:00:00 +00:00
buf =
"GET / HTTP/1.1\r\n"
"\r\n";
len = strlen(buf);
req = new HTTPReq;
2016-06-27 13:00:00 +00:00
assert((ret = req->parse(buf, len)) > 0);
2016-04-27 00:00:00 +00:00
delete req;
2016-05-24 00:00:00 +00:00
/* test: parsing incomplete request */
2016-04-27 00:00:00 +00:00
buf =
"GET / HTTP/1.0\r\n"
"";
len = strlen(buf);
req = new HTTPReq;
assert((ret = req->parse(buf, len)) == 0); /* request not completed */
delete req;
2016-05-24 00:00:00 +00:00
/* test: parsing slightly malformed request */
2016-04-27 00:00:00 +00:00
buf =
"GET http://inr.i2p HTTP/1.1\r\n"
"Host: stats.i2p\r\n"
"Accept-Encoding: \r\n"
2016-04-27 00:00:00 +00:00
"Accept: */*\r\n"
"\r\n";
len = strlen(buf);
req = new HTTPReq;
assert((ret = req->parse(buf, len)) == len); /* no host header */
assert(req->method == "GET");
assert(req->uri == "http://inr.i2p");
2023-01-28 22:18:54 +00:00
assert(req->GetNumHeaders () == 3);
assert(req->GetNumHeaders("Host") == 1);
assert(req->GetNumHeaders("Accept") == 1);
assert(req->GetNumHeaders("Accept-Encoding") == 1);
assert(req->GetHeader("Host") == "stats.i2p");
assert(req->GetHeader("Accept") == "*/*");
assert(req->GetHeader("Accept-Encoding") == "");
2016-04-27 00:00:00 +00:00
delete req;
return 0;
}
/* vim: expandtab:ts=2 */