| 1 | // |
| 2 | // HTTPSStreamFactoryTest.cpp |
| 3 | // |
| 4 | // Copyright (c) 2006, Applied Informatics Software Engineering GmbH. |
| 5 | // and Contributors. |
| 6 | // |
| 7 | // SPDX-License-Identifier: BSL-1.0 |
| 8 | // |
| 9 | |
| 10 | |
| 11 | #include "HTTPSStreamFactoryTest.h" |
| 12 | #include "Poco/CppUnit/TestCaller.h" |
| 13 | #include "Poco/CppUnit/TestSuite.h" |
| 14 | #include "Poco/Net/HTTPSStreamFactory.h" |
| 15 | #include "Poco/Net/NetException.h" |
| 16 | #include "Poco/Util/Application.h" |
| 17 | #include "Poco/Util/AbstractConfiguration.h" |
| 18 | #include "Poco/URI.h" |
| 19 | #include "Poco/Exception.h" |
| 20 | #include "Poco/StreamCopier.h" |
| 21 | #include "HTTPSTestServer.h" |
| 22 | #include <iostream> |
| 23 | #include <sstream> |
| 24 | #include <memory> |
| 25 | |
| 26 | |
| 27 | using Poco::Net::HTTPSStreamFactory; |
| 28 | using Poco::Net::NetException; |
| 29 | using Poco::Net::HTTPException; |
| 30 | using Poco::Util::Application; |
| 31 | using Poco::URI; |
| 32 | using Poco::StreamCopier; |
| 33 | |
| 34 | |
| 35 | HTTPSStreamFactoryTest::HTTPSStreamFactoryTest(const std::string& name): CppUnit::TestCase(name) |
| 36 | { |
| 37 | } |
| 38 | |
| 39 | |
| 40 | HTTPSStreamFactoryTest::~HTTPSStreamFactoryTest() |
| 41 | { |
| 42 | } |
| 43 | |
| 44 | |
| 45 | void HTTPSStreamFactoryTest::testNoRedirect() |
| 46 | { |
| 47 | HTTPSTestServer server; |
| 48 | HTTPSStreamFactory factory; |
| 49 | URI uri("https://127.0.0.1/large" ); |
| 50 | uri.setPort(server.port()); |
| 51 | std::unique_ptr<std::istream> pStr(factory.open(uri)); |
| 52 | std::ostringstream ostr; |
| 53 | StreamCopier::copyStream(*pStr.get(), ostr); |
| 54 | assertTrue (ostr.str() == HTTPSTestServer::LARGE_BODY); |
| 55 | } |
| 56 | |
| 57 | |
| 58 | void HTTPSStreamFactoryTest::testEmptyPath() |
| 59 | { |
| 60 | HTTPSTestServer server; |
| 61 | HTTPSStreamFactory factory; |
| 62 | URI uri("https://127.0.0.1" ); |
| 63 | uri.setPort(server.port()); |
| 64 | std::unique_ptr<std::istream> pStr(factory.open(uri)); |
| 65 | std::ostringstream ostr; |
| 66 | StreamCopier::copyStream(*pStr.get(), ostr); |
| 67 | assertTrue (ostr.str() == HTTPSTestServer::SMALL_BODY); |
| 68 | } |
| 69 | |
| 70 | |
| 71 | void HTTPSStreamFactoryTest::testRedirect() |
| 72 | { |
| 73 | HTTPSTestServer server; |
| 74 | HTTPSStreamFactory factory; |
| 75 | URI uri("https://127.0.0.1/redirect" ); |
| 76 | uri.setPort(server.port()); |
| 77 | std::unique_ptr<std::istream> pStr(factory.open(uri)); |
| 78 | std::ostringstream ostr; |
| 79 | StreamCopier::copyStream(*pStr.get(), ostr); |
| 80 | assertTrue (ostr.str() == HTTPSTestServer::LARGE_BODY); |
| 81 | } |
| 82 | |
| 83 | |
| 84 | void HTTPSStreamFactoryTest::testProxy() |
| 85 | { |
| 86 | HTTPSTestServer server; |
| 87 | HTTPSStreamFactory factory( |
| 88 | Application::instance().config().getString("testsuite.proxy.host" ), |
| 89 | Application::instance().config().getInt("testsuite.proxy.port" ) |
| 90 | ); |
| 91 | URI uri("https://secure.appinf.com/public/poco/NetSSL.txt" ); |
| 92 | std::unique_ptr<std::istream> pStr(factory.open(uri)); |
| 93 | std::ostringstream ostr; |
| 94 | StreamCopier::copyStream(*pStr.get(), ostr); |
| 95 | assertTrue (ostr.str().length() > 0); |
| 96 | |
| 97 | } |
| 98 | |
| 99 | |
| 100 | void HTTPSStreamFactoryTest::testError() |
| 101 | { |
| 102 | HTTPSTestServer server; |
| 103 | HTTPSStreamFactory factory; |
| 104 | URI uri("https://127.0.0.1/notfound" ); |
| 105 | uri.setPort(server.port()); |
| 106 | try |
| 107 | { |
| 108 | std::istream* pStr = factory.open(uri); |
| 109 | fail("not found - must throw" ); |
| 110 | } |
| 111 | catch (HTTPException& exc) |
| 112 | { |
| 113 | std::string m = exc.displayText(); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | |
| 118 | void HTTPSStreamFactoryTest::setUp() |
| 119 | { |
| 120 | } |
| 121 | |
| 122 | |
| 123 | void HTTPSStreamFactoryTest::tearDown() |
| 124 | { |
| 125 | } |
| 126 | |
| 127 | |
| 128 | CppUnit::Test* HTTPSStreamFactoryTest::suite() |
| 129 | { |
| 130 | CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("HTTPSStreamFactoryTest" ); |
| 131 | |
| 132 | CppUnit_addTest(pSuite, HTTPSStreamFactoryTest, testNoRedirect); |
| 133 | CppUnit_addTest(pSuite, HTTPSStreamFactoryTest, testEmptyPath); |
| 134 | CppUnit_addTest(pSuite, HTTPSStreamFactoryTest, testRedirect); |
| 135 | #ifdef FIXME |
| 136 | testProxy should use a public proxy server |
| 137 | http://www.publicproxyservers.com/proxy/list1.html |
| 138 | Really working public proxy servers - page 1 of 6. |
| 139 | #endif |
| 140 | CppUnit_addTest(pSuite, HTTPSStreamFactoryTest, testProxy); |
| 141 | CppUnit_addTest(pSuite, HTTPSStreamFactoryTest, testError); |
| 142 | |
| 143 | return pSuite; |
| 144 | } |
| 145 | |