httpservertest.cpp.svn-base

来自「很好用的网络封装库,不熟悉网络编程的人也可以使用。使用风格良好的标准c++编写。」· SVN-BASE 代码 · 共 581 行 · 第 1/2 页

SVN-BASE
581
字号
	pParams->setKeepAlive(true);
	HTTPServer srv(new RequestHandlerFactory, svs, pParams);
	srv.start();
	
	HTTPClientSession cs("localhost", svs.address().port());
	cs.setKeepAlive(true);
	std::string body(5000, 'x');
	HTTPRequest request("POST", "/echoBody", HTTPMessage::HTTP_1_1);
	request.setContentType("text/plain");
	request.setChunkedTransferEncoding(true);
	cs.sendRequest(request) << body;
	HTTPResponse response;
	std::string rbody;
	cs.receiveResponse(response) >> rbody;
	assert (response.getContentLength() == HTTPMessage::UNKNOWN_CONTENT_LENGTH);
	assert (response.getContentType() == "text/plain");
	assert (response.getChunkedTransferEncoding());
	assert (rbody == body);

	body.assign(1000, 'y');
	request.setKeepAlive(false);
	cs.sendRequest(request) << body;
	cs.receiveResponse(response) >> rbody;
	assert (response.getContentLength() == HTTPMessage::UNKNOWN_CONTENT_LENGTH);
	assert (response.getContentType() == "text/plain");
	assert (response.getChunkedTransferEncoding());
	assert (!response.getKeepAlive());
	assert (rbody == body);
}


void HTTPServerTest::testClosedRequestKeepAlive()
{
	ServerSocket svs(0);
	HTTPServerParams* pParams = new HTTPServerParams;
	pParams->setKeepAlive(true);
	HTTPServer srv(new RequestHandlerFactory, svs, pParams);
	srv.start();
	
	HTTPClientSession cs("localhost", svs.address().port());
	std::string body(5000, 'x');
	HTTPRequest request("POST", "/echoBody");
	request.setContentType("text/plain");
	cs.sendRequest(request) << body;
	HTTPResponse response;
	std::string rbody;
	cs.receiveResponse(response) >> rbody;
	assert (response.getContentLength() == HTTPMessage::UNKNOWN_CONTENT_LENGTH);
	assert (response.getContentType() == "text/plain");
	assert (!response.getChunkedTransferEncoding());
	assert (!response.getKeepAlive());
	assert (rbody == body);
}


void HTTPServerTest::testMaxKeepAlive()
{
	ServerSocket svs(0);
	HTTPServerParams* pParams = new HTTPServerParams;
	pParams->setKeepAlive(true);
	pParams->setMaxKeepAliveRequests(4);
	HTTPServer srv(new RequestHandlerFactory, svs, pParams);
	srv.start();
	
	HTTPClientSession cs("localhost", svs.address().port());
	cs.setKeepAlive(true);
	HTTPRequest request("POST", "/echoBody", HTTPMessage::HTTP_1_1);
	request.setContentType("text/plain");
	request.setChunkedTransferEncoding(true);
	std::string body(5000, 'x');
	for (int i = 0; i < 3; ++i)
	{
		cs.sendRequest(request) << body;
		HTTPResponse response;
		std::string rbody;
		cs.receiveResponse(response) >> rbody;
		assert (response.getContentLength() == HTTPMessage::UNKNOWN_CONTENT_LENGTH);
		assert (response.getContentType() == "text/plain");
		assert (response.getChunkedTransferEncoding());
		assert (response.getKeepAlive());
		assert (rbody == body);
	}

	{
		cs.sendRequest(request) << body;
		HTTPResponse response;
		std::string rbody;
		cs.receiveResponse(response) >> rbody;
		assert (response.getContentLength() == HTTPMessage::UNKNOWN_CONTENT_LENGTH);
		assert (response.getContentType() == "text/plain");
		assert (response.getChunkedTransferEncoding());
		assert (!response.getKeepAlive());
		assert (rbody == body);
	}

	{
		cs.setKeepAlive(false);
		cs.sendRequest(request) << body;
		HTTPResponse response;
		std::string rbody;
		cs.receiveResponse(response) >> rbody;
		assert (response.getContentLength() == HTTPMessage::UNKNOWN_CONTENT_LENGTH);
		assert (response.getContentType() == "text/plain");
		assert (response.getChunkedTransferEncoding());
		assert (!response.getKeepAlive());
		assert (rbody == body);
	}
}


void HTTPServerTest::testKeepAliveTimeout()
{
	ServerSocket svs(0);
	HTTPServerParams* pParams = new HTTPServerParams;
	pParams->setKeepAlive(true);
	pParams->setMaxKeepAliveRequests(4);
	pParams->setKeepAliveTimeout(Poco::Timespan(3, 0));
	HTTPServer srv(new RequestHandlerFactory, svs, pParams);
	srv.start();
	
	HTTPClientSession cs("localhost", svs.address().port());
	cs.setKeepAlive(true);
	cs.setKeepAliveTimeout(Poco::Timespan(2, 0));
	HTTPRequest request("POST", "/echoBody", HTTPMessage::HTTP_1_1);
	request.setContentType("text/plain");
	request.setChunkedTransferEncoding(true);
	std::string body(5000, 'x');
	for (int i = 0; i < 3; ++i)
	{
		cs.sendRequest(request) << body;
		HTTPResponse response;
		std::string rbody;
		cs.receiveResponse(response) >> rbody;
		assert (response.getContentLength() == HTTPMessage::UNKNOWN_CONTENT_LENGTH);
		assert (response.getContentType() == "text/plain");
		assert (response.getChunkedTransferEncoding());
		assert (response.getKeepAlive());
		assert (rbody == body);
	}

	Poco::Thread::sleep(4000);

	{
		cs.sendRequest(request) << body;
		HTTPResponse response;
		std::string rbody;
		cs.receiveResponse(response) >> rbody;
		assert (response.getContentLength() == HTTPMessage::UNKNOWN_CONTENT_LENGTH);
		assert (response.getContentType() == "text/plain");
		assert (response.getChunkedTransferEncoding());
		assert (response.getKeepAlive());
		assert (rbody == body);
	}
}


void HTTPServerTest::test100Continue()
{
	ServerSocket svs(0);
	HTTPServerParams* pParams = new HTTPServerParams;
	pParams->setKeepAlive(false);
	HTTPServer srv(new RequestHandlerFactory, svs, pParams);
	srv.start();
	
	HTTPClientSession cs("localhost", svs.address().port());
	std::string body(5000, 'x');
	HTTPRequest request("POST", "/echoBody");
	request.setContentLength((int) body.length());
	request.setContentType("text/plain");
	request.set("Expect", "100-Continue");
	cs.sendRequest(request) << body;
	HTTPResponse response;
	std::string rbody;
	cs.receiveResponse(response) >> rbody;
	assert (response.getContentLength() == body.size());
	assert (response.getContentType() == "text/plain");
	assert (rbody == body);
}


void HTTPServerTest::testRedirect()
{
	ServerSocket svs(0);
	HTTPServerParams* pParams = new HTTPServerParams;
	pParams->setKeepAlive(false);
	HTTPServer srv(new RequestHandlerFactory, svs, pParams);
	srv.start();
	
	HTTPClientSession cs("localhost", svs.address().port());
	HTTPRequest request("GET", "/redirect");
	cs.sendRequest(request);
	HTTPResponse response;
	std::string rbody;
	cs.receiveResponse(response) >> rbody;
	assert (response.getStatus() == HTTPResponse::HTTP_FOUND);
	assert (response.get("Location") == "http://www.appinf.com/");
	assert (rbody.empty());
}


void HTTPServerTest::testAuth()
{
	ServerSocket svs(0);
	HTTPServerParams* pParams = new HTTPServerParams;
	pParams->setKeepAlive(false);
	HTTPServer srv(new RequestHandlerFactory, svs, pParams);
	srv.start();
	
	HTTPClientSession cs("localhost", svs.address().port());
	HTTPRequest request("GET", "/auth");
	cs.sendRequest(request);
	HTTPResponse response;
	std::string rbody;
	cs.receiveResponse(response) >> rbody;
	assert (response.getStatus() == HTTPResponse::HTTP_UNAUTHORIZED);
	assert (response.get("WWW-Authenticate") == "Basic realm=\"/auth\"");
	assert (rbody.empty());
}


void HTTPServerTest::testNotImpl()
{
	ServerSocket svs(0);
	HTTPServerParams* pParams = new HTTPServerParams;
	pParams->setKeepAlive(false);
	HTTPServer srv(new RequestHandlerFactory, svs, pParams);
	srv.start();
	
	HTTPClientSession cs("localhost", svs.address().port());
	HTTPRequest request("GET", "/notImpl");
	cs.sendRequest(request);
	HTTPResponse response;
	std::string rbody;
	cs.receiveResponse(response) >> rbody;
	assert (response.getStatus() == HTTPResponse::HTTP_NOT_IMPLEMENTED);
	assert (rbody.empty());
}


void HTTPServerTest::testBuffer()
{
	ServerSocket svs(0);
	HTTPServerParams* pParams = new HTTPServerParams;
	pParams->setKeepAlive(false);
	HTTPServer srv(new RequestHandlerFactory, svs, pParams);
	srv.start();
	
	HTTPClientSession cs("localhost", svs.address().port());
	HTTPRequest request("GET", "/buffer");
	cs.sendRequest(request);
	HTTPResponse response;
	std::string rbody;
	cs.receiveResponse(response) >> rbody;
	assert (response.getStatus() == HTTPResponse::HTTP_OK);
	assert (rbody == "xxxxxxxxxx");
}


void HTTPServerTest::setUp()
{
}


void HTTPServerTest::tearDown()
{
}


CppUnit::Test* HTTPServerTest::suite()
{
	CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("HTTPServerTest");

	CppUnit_addTest(pSuite, HTTPServerTest, testIdentityRequest);
	CppUnit_addTest(pSuite, HTTPServerTest, testPutIdentityRequest);
	CppUnit_addTest(pSuite, HTTPServerTest, testChunkedRequest);
	CppUnit_addTest(pSuite, HTTPServerTest, testClosedRequest);
	CppUnit_addTest(pSuite, HTTPServerTest, testIdentityRequestKeepAlive);
	CppUnit_addTest(pSuite, HTTPServerTest, testChunkedRequestKeepAlive);
	CppUnit_addTest(pSuite, HTTPServerTest, testClosedRequestKeepAlive);
	CppUnit_addTest(pSuite, HTTPServerTest, testMaxKeepAlive);
	CppUnit_addTest(pSuite, HTTPServerTest, testKeepAliveTimeout);
	CppUnit_addTest(pSuite, HTTPServerTest, test100Continue);
	CppUnit_addTest(pSuite, HTTPServerTest, testRedirect);
	CppUnit_addTest(pSuite, HTTPServerTest, testAuth);
	CppUnit_addTest(pSuite, HTTPServerTest, testNotImpl);
	CppUnit_addTest(pSuite, HTTPServerTest, testBuffer);

	return pSuite;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?