HTTPSClientSessionTest.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. //
  2. // HTTPSClientSessionTest.cpp
  3. //
  4. // $Id: //poco/Main/NetSSL_OpenSSL/testsuite/src/HTTPSClientSessionTest.cpp#10 $
  5. //
  6. // Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
  7. // and Contributors.
  8. //
  9. // Permission is hereby granted, free of charge, to any person or organization
  10. // obtaining a copy of the software and accompanying documentation covered by
  11. // this license (the "Software") to use, reproduce, display, distribute,
  12. // execute, and transmit the Software, and to prepare derivative works of the
  13. // Software, and to permit third-parties to whom the Software is furnished to
  14. // do so, all subject to the following:
  15. //
  16. // The copyright notices in the Software and this entire statement, including
  17. // the above license grant, this restriction and the following disclaimer,
  18. // must be included in all copies of the Software, in whole or in part, and
  19. // all derivative works of the Software, unless such copies or derivative
  20. // works are solely in the form of machine-executable object code generated by
  21. // a source language processor.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  24. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  25. // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
  26. // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
  27. // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
  28. // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  29. // DEALINGS IN THE SOFTWARE.
  30. //
  31. #include "HTTPSClientSessionTest.h"
  32. #include "CppUnit/TestCaller.h"
  33. #include "CppUnit/TestSuite.h"
  34. #include "Poco/Net/HTTPSClientSession.h"
  35. #include "Poco/Net/HTTPRequest.h"
  36. #include "Poco/Net/HTTPRequestHandler.h"
  37. #include "Poco/Net/HTTPRequestHandlerFactory.h"
  38. #include "Poco/Net/HTTPResponse.h"
  39. #include "Poco/Net/HTTPServer.h"
  40. #include "Poco/Net/HTTPServerResponse.h"
  41. #include "Poco/Net/HTTPServerRequest.h"
  42. #include "Poco/Net/HTTPServerParams.h"
  43. #include "Poco/Net/SecureStreamSocket.h"
  44. #include "Poco/StreamCopier.h"
  45. #include "Poco/Exception.h"
  46. #include "Poco/DateTimeFormatter.h"
  47. #include "Poco/DateTimeFormat.h"
  48. #include "HTTPSTestServer.h"
  49. #include <istream>
  50. #include <ostream>
  51. #include <sstream>
  52. using namespace Poco::Net;
  53. using Poco::StreamCopier;
  54. class TestRequestHandler: public HTTPRequestHandler
  55. /// Return a HTML document with the current date and time.
  56. {
  57. public:
  58. TestRequestHandler()
  59. {
  60. }
  61. void handleRequest(HTTPServerRequest& request, HTTPServerResponse& response)
  62. {
  63. response.setChunkedTransferEncoding(true);
  64. response.setContentType(request.getContentType());
  65. std::ostream& ostr = response.send();
  66. Poco::StreamCopier::copyStream(request.stream(), ostr);
  67. }
  68. };
  69. class TestRequestHandlerFactory: public HTTPRequestHandlerFactory
  70. {
  71. public:
  72. TestRequestHandlerFactory()
  73. {
  74. }
  75. HTTPRequestHandler* createRequestHandler(const HTTPServerRequest& request)
  76. {
  77. return new TestRequestHandler();
  78. }
  79. };
  80. HTTPSClientSessionTest::HTTPSClientSessionTest(const std::string& name): CppUnit::TestCase(name)
  81. {
  82. }
  83. HTTPSClientSessionTest::~HTTPSClientSessionTest()
  84. {
  85. }
  86. void HTTPSClientSessionTest::testGetSmall()
  87. {
  88. HTTPSTestServer srv;
  89. HTTPSClientSession s("localhost", srv.port());
  90. HTTPRequest request(HTTPRequest::HTTP_GET, "/small");
  91. s.sendRequest(request);
  92. HTTPResponse response;
  93. std::istream& rs = s.receiveResponse(response);
  94. assert (response.getContentLength() == HTTPSTestServer::SMALL_BODY.length());
  95. assert (response.getContentType() == "text/plain");
  96. std::ostringstream ostr;
  97. StreamCopier::copyStream(rs, ostr);
  98. assert (ostr.str() == HTTPSTestServer::SMALL_BODY);
  99. }
  100. void HTTPSClientSessionTest::testGetLarge()
  101. {
  102. HTTPSTestServer srv;
  103. HTTPSClientSession s("localhost", srv.port());
  104. HTTPRequest request(HTTPRequest::HTTP_GET, "/large");
  105. s.sendRequest(request);
  106. HTTPResponse response;
  107. std::istream& rs = s.receiveResponse(response);
  108. assert (response.getContentLength() == HTTPSTestServer::LARGE_BODY.length());
  109. assert (response.getContentType() == "text/plain");
  110. std::ostringstream ostr;
  111. StreamCopier::copyStream(rs, ostr);
  112. assert (ostr.str() == HTTPSTestServer::LARGE_BODY);
  113. }
  114. void HTTPSClientSessionTest::testHead()
  115. {
  116. HTTPSTestServer srv;
  117. HTTPSClientSession s("localhost", srv.port());
  118. HTTPRequest request(HTTPRequest::HTTP_HEAD, "/large");
  119. s.sendRequest(request);
  120. HTTPResponse response;
  121. std::istream& rs = s.receiveResponse(response);
  122. assert (response.getContentLength() == HTTPSTestServer::LARGE_BODY.length());
  123. assert (response.getContentType() == "text/plain");
  124. std::ostringstream ostr;
  125. assert (StreamCopier::copyStream(rs, ostr) == 0);
  126. }
  127. void HTTPSClientSessionTest::testPostSmallIdentity()
  128. {
  129. HTTPSTestServer srv;
  130. HTTPSClientSession s("localhost", srv.port());
  131. HTTPRequest request(HTTPRequest::HTTP_POST, "/echo");
  132. std::string body("this is a random request body\r\n0\r\n");
  133. request.setContentLength((int) body.length());
  134. s.sendRequest(request) << body;
  135. HTTPResponse response;
  136. std::istream& rs = s.receiveResponse(response);
  137. assert (response.getContentLength() == body.length());
  138. std::ostringstream ostr;
  139. StreamCopier::copyStream(rs, ostr);
  140. assert (ostr.str() == body);
  141. }
  142. void HTTPSClientSessionTest::testPostLargeIdentity()
  143. {
  144. HTTPSTestServer srv;
  145. HTTPSClientSession s("localhost", srv.port());
  146. HTTPRequest request(HTTPRequest::HTTP_POST, "/echo");
  147. std::string body(8000, 'x');
  148. body.append("\r\n0\r\n");
  149. request.setContentLength((int) body.length());
  150. s.sendRequest(request) << body;
  151. HTTPResponse response;
  152. std::istream& rs = s.receiveResponse(response);
  153. assert (response.getContentLength() == body.length());
  154. std::ostringstream ostr;
  155. StreamCopier::copyStream(rs, ostr);
  156. assert (ostr.str() == body);
  157. }
  158. void HTTPSClientSessionTest::testPostSmallChunked()
  159. {
  160. HTTPSTestServer srv;
  161. HTTPSClientSession s("localhost", srv.port());
  162. HTTPRequest request(HTTPRequest::HTTP_POST, "/echo");
  163. std::string body("this is a random request body");
  164. request.setChunkedTransferEncoding(true);
  165. s.sendRequest(request) << body;
  166. HTTPResponse response;
  167. std::istream& rs = s.receiveResponse(response);
  168. assert (response.getChunkedTransferEncoding());
  169. assert (response.getContentLength() == HTTPMessage::UNKNOWN_CONTENT_LENGTH);
  170. std::ostringstream ostr;
  171. StreamCopier::copyStream(rs, ostr);
  172. assert (ostr.str() == body);
  173. }
  174. void HTTPSClientSessionTest::testPostLargeChunked()
  175. {
  176. HTTPSTestServer srv;
  177. HTTPSClientSession s("localhost", srv.port());
  178. HTTPRequest request(HTTPRequest::HTTP_POST, "/echo");
  179. std::string body(16000, 'x');
  180. request.setChunkedTransferEncoding(true);
  181. s.sendRequest(request) << body;
  182. HTTPResponse response;
  183. std::istream& rs = s.receiveResponse(response);
  184. assert (response.getChunkedTransferEncoding());
  185. assert (response.getContentLength() == HTTPMessage::UNKNOWN_CONTENT_LENGTH);
  186. std::ostringstream ostr;
  187. StreamCopier::copyStream(rs, ostr);
  188. assert (ostr.str() == body);
  189. }
  190. void HTTPSClientSessionTest::testPostLargeChunkedKeepAlive()
  191. {
  192. SecureServerSocket svs(32322);
  193. HTTPServer srv(new TestRequestHandlerFactory(), svs, new HTTPServerParams());
  194. srv.start();
  195. try
  196. {
  197. HTTPSClientSession s("localhost", srv.port());
  198. s.setKeepAlive(true);
  199. for (int i = 0; i < 10; ++i)
  200. {
  201. HTTPRequest request(HTTPRequest::HTTP_POST, "/keepAlive", HTTPMessage::HTTP_1_1);
  202. std::string body(16000, 'x');
  203. request.setChunkedTransferEncoding(true);
  204. s.sendRequest(request) << body;
  205. HTTPResponse response;
  206. std::istream& rs = s.receiveResponse(response);
  207. assert (response.getChunkedTransferEncoding());
  208. assert (response.getContentLength() == HTTPMessage::UNKNOWN_CONTENT_LENGTH);
  209. std::ostringstream ostr;
  210. StreamCopier::copyStream(rs, ostr);
  211. assert (ostr.str() == body);
  212. }
  213. srv.stop();
  214. }
  215. catch (...)
  216. {
  217. srv.stop();
  218. throw;
  219. }
  220. }
  221. void HTTPSClientSessionTest::testKeepAlive()
  222. {
  223. HTTPSTestServer srv;
  224. HTTPSClientSession s("localhost", srv.port());
  225. s.setKeepAlive(true);
  226. HTTPRequest request(HTTPRequest::HTTP_HEAD, "/keepAlive", HTTPMessage::HTTP_1_1);
  227. s.sendRequest(request);
  228. HTTPResponse response;
  229. std::istream& rs1 = s.receiveResponse(response);
  230. assert (response.getContentLength() == HTTPSTestServer::SMALL_BODY.length());
  231. assert (response.getContentType() == "text/plain");
  232. assert (response.getKeepAlive());
  233. std::ostringstream ostr1;
  234. assert (StreamCopier::copyStream(rs1, ostr1) == 0);
  235. request.setMethod(HTTPRequest::HTTP_GET);
  236. request.setURI("/small");
  237. s.sendRequest(request);
  238. std::istream& rs2 = s.receiveResponse(response);
  239. assert (response.getContentLength() == HTTPSTestServer::SMALL_BODY.length());
  240. assert (response.getKeepAlive());
  241. std::ostringstream ostr2;
  242. StreamCopier::copyStream(rs2, ostr2);
  243. assert (ostr2.str() == HTTPSTestServer::SMALL_BODY);
  244. request.setMethod(HTTPRequest::HTTP_GET);
  245. request.setURI("/large");
  246. s.sendRequest(request);
  247. std::istream& rs3 = s.receiveResponse(response);
  248. assert (response.getContentLength() == HTTPMessage::UNKNOWN_CONTENT_LENGTH);
  249. assert (response.getChunkedTransferEncoding());
  250. assert (response.getKeepAlive());
  251. std::ostringstream ostr3;
  252. int n = StreamCopier::copyStream(rs3, ostr3);
  253. assert (ostr3.str() == HTTPSTestServer::LARGE_BODY);
  254. request.setMethod(HTTPRequest::HTTP_HEAD);
  255. request.setURI("/large");
  256. s.sendRequest(request);
  257. std::istream& rs4= s.receiveResponse(response);
  258. assert (response.getContentLength() == HTTPSTestServer::LARGE_BODY.length());
  259. assert (response.getContentType() == "text/plain");
  260. assert (!response.getKeepAlive());
  261. std::ostringstream ostr4;
  262. assert (StreamCopier::copyStream(rs4, ostr4) == 0);
  263. }
  264. void HTTPSClientSessionTest::testInterop()
  265. {
  266. HTTPSClientSession s("secure.appinf.com");
  267. HTTPRequest request(HTTPRequest::HTTP_GET, "/public/poco/NetSSL.txt");
  268. s.sendRequest(request);
  269. X509Certificate cert = s.serverCertificate();
  270. HTTPResponse response;
  271. std::istream& rs = s.receiveResponse(response);
  272. std::ostringstream ostr;
  273. StreamCopier::copyStream(rs, ostr);
  274. std::string str(ostr.str());
  275. assert (str == "This is a test file for NetSSL.\n");
  276. assert (cert.commonName() == "secure.appinf.com");
  277. }
  278. void HTTPSClientSessionTest::testProxy()
  279. {
  280. HTTPSTestServer srv;
  281. HTTPSClientSession s("secure.appinf.com");
  282. s.setProxy("proxy.aon.at", 8080);
  283. HTTPRequest request(HTTPRequest::HTTP_GET, "/public/poco/NetSSL.txt");
  284. s.sendRequest(request);
  285. X509Certificate cert = s.serverCertificate();
  286. HTTPResponse response;
  287. std::istream& rs = s.receiveResponse(response);
  288. std::ostringstream ostr;
  289. StreamCopier::copyStream(rs, ostr);
  290. std::string str(ostr.str());
  291. assert (str == "This is a test file for NetSSL.\n");
  292. assert (cert.commonName() == "secure.appinf.com");
  293. }
  294. void HTTPSClientSessionTest::setUp()
  295. {
  296. }
  297. void HTTPSClientSessionTest::tearDown()
  298. {
  299. }
  300. CppUnit::Test* HTTPSClientSessionTest::suite()
  301. {
  302. CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("HTTPSClientSessionTest");
  303. CppUnit_addTest(pSuite, HTTPSClientSessionTest, testGetSmall);
  304. CppUnit_addTest(pSuite, HTTPSClientSessionTest, testGetLarge);
  305. CppUnit_addTest(pSuite, HTTPSClientSessionTest, testHead);
  306. CppUnit_addTest(pSuite, HTTPSClientSessionTest, testPostSmallIdentity);
  307. CppUnit_addTest(pSuite, HTTPSClientSessionTest, testPostLargeIdentity);
  308. CppUnit_addTest(pSuite, HTTPSClientSessionTest, testPostSmallChunked);
  309. CppUnit_addTest(pSuite, HTTPSClientSessionTest, testPostLargeChunked);
  310. CppUnit_addTest(pSuite, HTTPSClientSessionTest, testPostLargeChunkedKeepAlive);
  311. CppUnit_addTest(pSuite, HTTPSClientSessionTest, testKeepAlive);
  312. CppUnit_addTest(pSuite, HTTPSClientSessionTest, testInterop);
  313. CppUnit_addTest(pSuite, HTTPSClientSessionTest, testProxy);
  314. return pSuite;
  315. }