2007-07-01 10:40:30 +00:00
|
|
|
#include "HttpHeaderProcessor.h"
|
2008-02-08 15:53:45 +00:00
|
|
|
#include "HttpHeader.h"
|
2007-07-01 10:40:30 +00:00
|
|
|
#include "DlRetryEx.h"
|
|
|
|
#include "DlAbortEx.h"
|
2008-03-13 13:13:53 +00:00
|
|
|
#include <iostream>
|
2007-07-01 10:40:30 +00:00
|
|
|
#include <cppunit/extensions/HelperMacros.h>
|
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
namespace aria2 {
|
|
|
|
|
2007-07-01 10:40:30 +00:00
|
|
|
class HttpHeaderProcessorTest:public CppUnit::TestFixture {
|
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE(HttpHeaderProcessorTest);
|
|
|
|
CPPUNIT_TEST(testUpdate1);
|
|
|
|
CPPUNIT_TEST(testUpdate2);
|
|
|
|
CPPUNIT_TEST(testGetPutBackDataLength);
|
|
|
|
CPPUNIT_TEST(testGetPutBackDataLength_nullChar);
|
2008-04-21 10:48:11 +00:00
|
|
|
CPPUNIT_TEST(testGetHttpResponseHeader);
|
|
|
|
CPPUNIT_TEST(testGetHttpResponseHeader_empty);
|
|
|
|
CPPUNIT_TEST(testGetHttpResponseHeader_statusOnly);
|
|
|
|
CPPUNIT_TEST(testGetHttpResponseHeader_insufficientStatusLength);
|
2007-07-01 10:40:30 +00:00
|
|
|
CPPUNIT_TEST(testBeyondLimit);
|
|
|
|
CPPUNIT_TEST(testGetHeaderString);
|
2009-01-25 09:58:40 +00:00
|
|
|
CPPUNIT_TEST(testGetHttpRequestHeader);
|
2007-07-01 10:40:30 +00:00
|
|
|
CPPUNIT_TEST_SUITE_END();
|
|
|
|
|
|
|
|
public:
|
|
|
|
void testUpdate1();
|
|
|
|
void testUpdate2();
|
|
|
|
void testGetPutBackDataLength();
|
|
|
|
void testGetPutBackDataLength_nullChar();
|
2008-04-21 10:48:11 +00:00
|
|
|
void testGetHttpResponseHeader();
|
|
|
|
void testGetHttpResponseHeader_empty();
|
|
|
|
void testGetHttpResponseHeader_statusOnly();
|
|
|
|
void testGetHttpResponseHeader_insufficientStatusLength();
|
2007-07-01 10:40:30 +00:00
|
|
|
void testBeyondLimit();
|
|
|
|
void testGetHeaderString();
|
2008-04-21 10:48:11 +00:00
|
|
|
void testGetHttpRequestHeader();
|
2007-07-01 10:40:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE_REGISTRATION( HttpHeaderProcessorTest );
|
|
|
|
|
|
|
|
void HttpHeaderProcessorTest::testUpdate1()
|
|
|
|
{
|
|
|
|
HttpHeaderProcessor proc;
|
2008-02-08 15:53:45 +00:00
|
|
|
std::string hd1 = "HTTP/1.1 200 OK\r\n";
|
2007-07-01 10:40:30 +00:00
|
|
|
proc.update(hd1);
|
|
|
|
CPPUNIT_ASSERT(!proc.eoh());
|
|
|
|
proc.update("\r\n");
|
|
|
|
CPPUNIT_ASSERT(proc.eoh());
|
|
|
|
}
|
|
|
|
|
|
|
|
void HttpHeaderProcessorTest::testUpdate2()
|
|
|
|
{
|
|
|
|
HttpHeaderProcessor proc;
|
2008-02-08 15:53:45 +00:00
|
|
|
std::string hd1 = "HTTP/1.1 200 OK\n";
|
2007-07-01 10:40:30 +00:00
|
|
|
proc.update(hd1);
|
|
|
|
CPPUNIT_ASSERT(!proc.eoh());
|
|
|
|
proc.update("\n");
|
|
|
|
CPPUNIT_ASSERT(proc.eoh());
|
|
|
|
}
|
|
|
|
|
|
|
|
void HttpHeaderProcessorTest::testGetPutBackDataLength()
|
|
|
|
{
|
|
|
|
HttpHeaderProcessor proc;
|
2008-02-08 15:53:45 +00:00
|
|
|
std::string hd1 = "HTTP/1.1 200 OK\r\n"
|
2007-07-01 10:40:30 +00:00
|
|
|
"\r\nputbackme";
|
|
|
|
proc.update(hd1);
|
|
|
|
CPPUNIT_ASSERT(proc.eoh());
|
2008-03-09 12:24:01 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)9, proc.getPutBackDataLength());
|
2007-07-01 10:40:30 +00:00
|
|
|
|
|
|
|
proc.clear();
|
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
std::string hd2 = "HTTP/1.1 200 OK\n"
|
2007-07-01 10:40:30 +00:00
|
|
|
"\nputbackme";
|
|
|
|
proc.update(hd2);
|
|
|
|
CPPUNIT_ASSERT(proc.eoh());
|
2008-03-09 12:24:01 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)9, proc.getPutBackDataLength());
|
2007-07-01 10:40:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void HttpHeaderProcessorTest::testGetPutBackDataLength_nullChar()
|
|
|
|
{
|
|
|
|
HttpHeaderProcessor proc;
|
2008-03-07 12:05:50 +00:00
|
|
|
const char* x = "HTTP/1.1 200 OK\r\n"
|
|
|
|
"foo: foo\0bar\r\n"
|
|
|
|
"\r\nputbackme";
|
|
|
|
std::string hd1(&x[0], &x[42]);
|
|
|
|
proc.update(hd1);
|
2007-07-01 10:40:30 +00:00
|
|
|
CPPUNIT_ASSERT(proc.eoh());
|
2008-03-09 12:24:01 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((size_t)9, proc.getPutBackDataLength());
|
2007-07-01 10:40:30 +00:00
|
|
|
}
|
|
|
|
|
2008-04-21 10:48:11 +00:00
|
|
|
void HttpHeaderProcessorTest::testGetHttpResponseHeader()
|
2007-07-01 10:40:30 +00:00
|
|
|
{
|
|
|
|
HttpHeaderProcessor proc;
|
2008-02-08 15:53:45 +00:00
|
|
|
std::string hd = "HTTP/1.1 200 OK\r\n"
|
2007-07-01 10:40:30 +00:00
|
|
|
"Date: Mon, 25 Jun 2007 16:04:59 GMT\r\n"
|
|
|
|
"Server: Apache/2.2.3 (Debian)\r\n"
|
|
|
|
"Last-Modified: Tue, 12 Jun 2007 14:28:43 GMT\r\n"
|
|
|
|
"ETag: \"594065-23e3-50825cc0\"\r\n"
|
|
|
|
"Accept-Ranges: bytes\r\n"
|
|
|
|
"Content-Length: 9187\r\n"
|
|
|
|
"Connection: close\r\n"
|
|
|
|
"Content-Type: text/html; charset=UTF-8\r\n"
|
|
|
|
"\r\n";
|
|
|
|
|
|
|
|
proc.update(hd);
|
|
|
|
|
2008-04-21 10:48:11 +00:00
|
|
|
SharedHandle<HttpHeader> header = proc.getHttpResponseHeader();
|
2010-11-15 12:52:03 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(200, header->getStatusCode());
|
2008-04-21 10:48:11 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("HTTP/1.1"), header->getVersion());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("Mon, 25 Jun 2007 16:04:59 GMT"),
|
2010-01-05 16:01:46 +00:00
|
|
|
header->getFirst("Date"));
|
2008-04-21 10:48:11 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("Apache/2.2.3 (Debian)"),
|
2010-01-05 16:01:46 +00:00
|
|
|
header->getFirst("Server"));
|
2008-07-31 12:28:12 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL((uint64_t)9187ULL, header->getFirstAsULLInt("Content-Length"));
|
2008-04-21 10:48:11 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("text/html; charset=UTF-8"),
|
2010-01-05 16:01:46 +00:00
|
|
|
header->getFirst("Content-Type"));
|
2007-07-01 10:40:30 +00:00
|
|
|
}
|
|
|
|
|
2008-04-21 10:48:11 +00:00
|
|
|
void HttpHeaderProcessorTest::testGetHttpResponseHeader_empty()
|
2007-07-01 10:40:30 +00:00
|
|
|
{
|
|
|
|
HttpHeaderProcessor proc;
|
|
|
|
|
|
|
|
try {
|
2008-04-21 10:48:11 +00:00
|
|
|
proc.getHttpResponseHeader();
|
2007-08-09 14:54:36 +00:00
|
|
|
CPPUNIT_FAIL("Exception must be thrown.");
|
2008-04-27 02:22:14 +00:00
|
|
|
} catch(DlRetryEx& ex) {
|
|
|
|
std::cout << ex.stackTrace() << std::endl;
|
2007-07-01 10:40:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2008-04-21 10:48:11 +00:00
|
|
|
void HttpHeaderProcessorTest::testGetHttpResponseHeader_statusOnly()
|
2007-07-01 10:40:30 +00:00
|
|
|
{
|
|
|
|
HttpHeaderProcessor proc;
|
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
std::string hd = "HTTP/1.1 200\r\n\r\n";
|
2007-07-01 10:40:30 +00:00
|
|
|
proc.update(hd);
|
2008-04-21 10:48:11 +00:00
|
|
|
SharedHandle<HttpHeader> header = proc.getHttpResponseHeader();
|
2010-11-15 12:52:03 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(200, header->getStatusCode());
|
2007-07-01 10:40:30 +00:00
|
|
|
}
|
|
|
|
|
2008-04-21 10:48:11 +00:00
|
|
|
void HttpHeaderProcessorTest::testGetHttpResponseHeader_insufficientStatusLength()
|
2007-07-01 10:40:30 +00:00
|
|
|
{
|
|
|
|
HttpHeaderProcessor proc;
|
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
std::string hd = "HTTP/1.1 20\r\n\r\n";
|
2007-07-01 10:40:30 +00:00
|
|
|
proc.update(hd);
|
|
|
|
try {
|
2008-04-21 10:48:11 +00:00
|
|
|
proc.getHttpResponseHeader();
|
2007-08-09 14:54:36 +00:00
|
|
|
CPPUNIT_FAIL("Exception must be thrown.");
|
2008-04-27 02:22:14 +00:00
|
|
|
} catch(DlRetryEx& ex) {
|
|
|
|
std::cout << ex.stackTrace() << std::endl;
|
2007-07-01 10:40:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void HttpHeaderProcessorTest::testBeyondLimit()
|
|
|
|
{
|
|
|
|
HttpHeaderProcessor proc;
|
|
|
|
proc.setHeaderLimit(20);
|
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
std::string hd1 = "HTTP/1.1 200 OK\r\n";
|
|
|
|
std::string hd2 = "Date: Mon, 25 Jun 2007 16:04:59 GMT\r\n";
|
2007-07-01 10:40:30 +00:00
|
|
|
|
|
|
|
proc.update(hd1);
|
|
|
|
|
|
|
|
try {
|
|
|
|
proc.update(hd2);
|
2007-08-09 14:54:36 +00:00
|
|
|
CPPUNIT_FAIL("Exception must be thrown.");
|
2008-04-27 02:22:14 +00:00
|
|
|
} catch(DlAbortEx& ex) {
|
|
|
|
std::cout << ex.stackTrace() << std::endl;
|
2007-07-01 10:40:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void HttpHeaderProcessorTest::testGetHeaderString()
|
|
|
|
{
|
|
|
|
HttpHeaderProcessor proc;
|
2008-02-08 15:53:45 +00:00
|
|
|
std::string hd = "HTTP/1.1 200 OK\r\n"
|
2007-07-01 10:40:30 +00:00
|
|
|
"Date: Mon, 25 Jun 2007 16:04:59 GMT\r\n"
|
|
|
|
"Server: Apache/2.2.3 (Debian)\r\n"
|
|
|
|
"Last-Modified: Tue, 12 Jun 2007 14:28:43 GMT\r\n"
|
|
|
|
"ETag: \"594065-23e3-50825cc0\"\r\n"
|
|
|
|
"Accept-Ranges: bytes\r\n"
|
|
|
|
"Content-Length: 9187\r\n"
|
|
|
|
"Connection: close\r\n"
|
|
|
|
"Content-Type: text/html; charset=UTF-8\r\n"
|
|
|
|
"\r\nputbackme";
|
|
|
|
|
|
|
|
proc.update(hd);
|
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("HTTP/1.1 200 OK\r\n"
|
2010-01-05 16:01:46 +00:00
|
|
|
"Date: Mon, 25 Jun 2007 16:04:59 GMT\r\n"
|
|
|
|
"Server: Apache/2.2.3 (Debian)\r\n"
|
|
|
|
"Last-Modified: Tue, 12 Jun 2007 14:28:43 GMT\r\n"
|
|
|
|
"ETag: \"594065-23e3-50825cc0\"\r\n"
|
|
|
|
"Accept-Ranges: bytes\r\n"
|
|
|
|
"Content-Length: 9187\r\n"
|
|
|
|
"Connection: close\r\n"
|
|
|
|
"Content-Type: text/html; charset=UTF-8"),
|
|
|
|
proc.getHeaderString());
|
2007-07-01 10:40:30 +00:00
|
|
|
}
|
2008-02-08 15:53:45 +00:00
|
|
|
|
2009-01-25 09:58:40 +00:00
|
|
|
void HttpHeaderProcessorTest::testGetHttpRequestHeader()
|
|
|
|
{
|
|
|
|
HttpHeaderProcessor proc;
|
|
|
|
std::string request = "GET /index.html HTTP/1.1\r\n"
|
|
|
|
"Host: host\r\n"
|
|
|
|
"Connection: close\r\n"
|
|
|
|
"\r\n";
|
|
|
|
|
|
|
|
proc.update(request);
|
|
|
|
|
|
|
|
SharedHandle<HttpHeader> httpHeader = proc.getHttpRequestHeader();
|
2010-11-12 12:48:48 +00:00
|
|
|
CPPUNIT_ASSERT(httpHeader);
|
2009-01-25 09:58:40 +00:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("GET"), httpHeader->getMethod());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("/index.html"),httpHeader->getRequestPath());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("HTTP/1.1"), httpHeader->getVersion());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("close"),httpHeader->getFirst("Connection"));
|
|
|
|
}
|
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
} // namespace aria2
|