2016-04-20 22:09:04 -05:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
|
|
|
|
/*
|
|
|
|
* This file is part of the LibreOffice project.
|
|
|
|
*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*/
|
|
|
|
|
2017-12-20 07:06:26 -06:00
|
|
|
#include <config.h>
|
2016-04-20 22:09:04 -05:00
|
|
|
|
2018-10-19 11:14:48 -05:00
|
|
|
#include <Poco/DOM/AutoPtr.h>
|
|
|
|
#include <Poco/DOM/DOMParser.h>
|
|
|
|
#include <Poco/DOM/Document.h>
|
|
|
|
#include <Poco/DOM/Element.h>
|
|
|
|
#include <Poco/DOM/NodeList.h>
|
2016-04-20 22:09:04 -05:00
|
|
|
#include <Poco/Net/AcceptCertificateHandler.h>
|
|
|
|
#include <Poco/Net/FilePartSource.h>
|
|
|
|
#include <Poco/Net/HTMLForm.h>
|
|
|
|
#include <Poco/Net/HTTPClientSession.h>
|
|
|
|
#include <Poco/Net/HTTPRequest.h>
|
|
|
|
#include <Poco/Net/HTTPResponse.h>
|
2016-09-26 21:29:53 -05:00
|
|
|
#include <Poco/Net/HTTPSClientSession.h>
|
2016-04-20 22:09:04 -05:00
|
|
|
#include <Poco/Net/InvalidCertificateHandler.h>
|
|
|
|
#include <Poco/Net/PrivateKeyPassphraseHandler.h>
|
|
|
|
#include <Poco/Net/SSLManager.h>
|
2016-09-26 21:29:53 -05:00
|
|
|
#include <Poco/RegularExpression.h>
|
2016-04-20 22:09:04 -05:00
|
|
|
#include <Poco/StreamCopier.h>
|
|
|
|
#include <Poco/URI.h>
|
2016-09-26 21:29:53 -05:00
|
|
|
|
2016-04-20 22:09:04 -05:00
|
|
|
#include <cppunit/extensions/HelperMacros.h>
|
|
|
|
|
|
|
|
#include <Common.hpp>
|
2017-12-20 07:06:26 -06:00
|
|
|
#include <common/FileUtil.hpp>
|
2016-04-20 22:09:04 -05:00
|
|
|
#include <Util.hpp>
|
|
|
|
|
2017-12-20 07:06:26 -06:00
|
|
|
#include <countloolkits.hpp>
|
|
|
|
#include <helpers.hpp>
|
2016-04-26 04:32:01 -05:00
|
|
|
|
2016-04-20 22:09:04 -05:00
|
|
|
/// Tests the HTTP GET API of loolwsd.
|
2016-04-28 11:08:07 -05:00
|
|
|
class HTTPServerTest : public CPPUNIT_NS::TestFixture
|
2016-04-20 22:09:04 -05:00
|
|
|
{
|
2016-05-02 11:25:39 -05:00
|
|
|
const Poco::URI _uri;
|
2016-04-28 11:08:07 -05:00
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE(HTTPServerTest);
|
|
|
|
|
2016-04-20 22:09:04 -05:00
|
|
|
CPPUNIT_TEST(testDiscovery);
|
2018-10-19 11:14:48 -05:00
|
|
|
CPPUNIT_TEST(testCapabilities);
|
2016-04-28 11:08:07 -05:00
|
|
|
CPPUNIT_TEST(testLoleafletGet);
|
|
|
|
CPPUNIT_TEST(testLoleafletPost);
|
|
|
|
CPPUNIT_TEST(testScriptsAndLinksGet);
|
|
|
|
CPPUNIT_TEST(testScriptsAndLinksPost);
|
2018-10-20 06:41:43 -05:00
|
|
|
CPPUNIT_TEST(testConvertTo);
|
2019-12-11 23:33:09 -06:00
|
|
|
CPPUNIT_TEST(testConvertTo2);
|
2018-10-24 05:37:58 -05:00
|
|
|
CPPUNIT_TEST(testConvertToWithForwardedClientIP);
|
2016-04-28 11:08:07 -05:00
|
|
|
|
2016-04-20 22:09:04 -05:00
|
|
|
CPPUNIT_TEST_SUITE_END();
|
|
|
|
|
|
|
|
void testDiscovery();
|
2018-10-19 11:14:48 -05:00
|
|
|
void testCapabilities();
|
2016-04-28 11:08:07 -05:00
|
|
|
void testLoleafletGet();
|
|
|
|
void testLoleafletPost();
|
|
|
|
void testScriptsAndLinksGet();
|
|
|
|
void testScriptsAndLinksPost();
|
2016-07-21 02:22:14 -05:00
|
|
|
void testConvertTo();
|
2019-12-11 23:33:09 -06:00
|
|
|
void testConvertTo2();
|
2018-10-24 05:37:58 -05:00
|
|
|
void testConvertToWithForwardedClientIP();
|
2016-04-28 11:08:07 -05:00
|
|
|
|
2016-04-20 22:09:04 -05:00
|
|
|
public:
|
2016-04-28 11:08:07 -05:00
|
|
|
HTTPServerTest()
|
2016-05-03 02:40:25 -05:00
|
|
|
: _uri(helpers::getTestServerURI())
|
2016-04-20 22:09:04 -05:00
|
|
|
{
|
2016-05-04 06:39:09 -05:00
|
|
|
#if ENABLE_SSL
|
2016-04-20 22:09:04 -05:00
|
|
|
Poco::Net::initializeSSL();
|
|
|
|
// Just accept the certificate anyway for testing purposes
|
|
|
|
Poco::SharedPtr<Poco::Net::InvalidCertificateHandler> invalidCertHandler = new Poco::Net::AcceptCertificateHandler(false);
|
|
|
|
Poco::Net::Context::Params sslParams;
|
|
|
|
Poco::Net::Context::Ptr sslContext = new Poco::Net::Context(Poco::Net::Context::CLIENT_USE, sslParams);
|
2016-12-22 08:04:07 -06:00
|
|
|
Poco::Net::SSLManager::instance().initializeClient(nullptr, invalidCertHandler, sslContext);
|
2016-05-04 06:39:09 -05:00
|
|
|
#endif
|
2016-04-20 22:09:04 -05:00
|
|
|
}
|
|
|
|
|
2016-04-28 11:08:07 -05:00
|
|
|
~HTTPServerTest()
|
2016-04-20 22:09:04 -05:00
|
|
|
{
|
2016-05-04 06:39:09 -05:00
|
|
|
#if ENABLE_SSL
|
2016-04-20 22:09:04 -05:00
|
|
|
Poco::Net::uninitializeSSL();
|
|
|
|
#endif
|
2016-05-04 06:39:09 -05:00
|
|
|
}
|
2016-05-13 11:43:43 -05:00
|
|
|
|
|
|
|
void setUp()
|
|
|
|
{
|
2018-01-22 08:11:43 -06:00
|
|
|
helpers::resetTestStartTime();
|
2016-05-13 11:43:43 -05:00
|
|
|
testCountHowManyLoolkits();
|
2018-01-22 08:11:43 -06:00
|
|
|
helpers::resetTestStartTime();
|
2016-05-13 11:43:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void tearDown()
|
|
|
|
{
|
2018-01-22 08:11:43 -06:00
|
|
|
helpers::resetTestStartTime();
|
2016-05-13 11:43:43 -05:00
|
|
|
testNoExtraLoolKitsLeft();
|
2018-01-22 08:11:43 -06:00
|
|
|
helpers::resetTestStartTime();
|
2016-05-13 11:43:43 -05:00
|
|
|
}
|
2018-10-24 05:37:58 -05:00
|
|
|
|
|
|
|
// A server URI which was not added to loolwsd.xml as post_allow IP or a wopi storage host
|
|
|
|
Poco::URI getNotAllowedTestServerURI()
|
|
|
|
{
|
|
|
|
static const char* clientPort = std::getenv("LOOL_TEST_CLIENT_PORT");
|
|
|
|
|
|
|
|
static std::string serverURI(
|
|
|
|
#if ENABLE_SSL
|
|
|
|
"https://165.227.162.232:"
|
|
|
|
#else
|
|
|
|
"http://165.227.162.232:"
|
|
|
|
#endif
|
|
|
|
+ (clientPort? std::string(clientPort) : std::to_string(DEFAULT_CLIENT_PORT_NUMBER)));
|
|
|
|
|
|
|
|
return Poco::URI(serverURI);
|
|
|
|
}
|
2016-04-20 22:09:04 -05:00
|
|
|
};
|
|
|
|
|
2018-10-24 05:37:58 -05:00
|
|
|
|
2016-04-28 11:08:07 -05:00
|
|
|
void HTTPServerTest::testDiscovery()
|
2016-04-20 22:09:04 -05:00
|
|
|
{
|
2016-05-02 11:41:57 -05:00
|
|
|
std::unique_ptr<Poco::Net::HTTPClientSession> session(helpers::createSession(_uri));
|
2016-04-20 22:09:04 -05:00
|
|
|
|
|
|
|
Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, "/hosting/discovery");
|
2016-05-02 11:41:57 -05:00
|
|
|
session->sendRequest(request);
|
2016-04-20 22:09:04 -05:00
|
|
|
|
|
|
|
Poco::Net::HTTPResponse response;
|
2016-05-02 11:41:57 -05:00
|
|
|
session->receiveResponse(response);
|
2016-04-24 19:59:58 -05:00
|
|
|
CPPUNIT_ASSERT_EQUAL(Poco::Net::HTTPResponse::HTTP_OK, response.getStatus());
|
2016-04-20 22:09:04 -05:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("text/xml"), response.getContentType());
|
2019-11-19 02:57:41 -06:00
|
|
|
|
|
|
|
Poco::Net::HTTPRequest request2(Poco::Net::HTTPRequest::HTTP_GET, "/hosting/discovery/");
|
|
|
|
session->sendRequest(request2);
|
|
|
|
|
|
|
|
Poco::Net::HTTPResponse response2;
|
|
|
|
session->receiveResponse(response2);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(Poco::Net::HTTPResponse::HTTP_OK, response2.getStatus());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("text/xml"), response2.getContentType());
|
2016-04-20 22:09:04 -05:00
|
|
|
}
|
|
|
|
|
2018-10-19 11:14:48 -05:00
|
|
|
|
|
|
|
void HTTPServerTest::testCapabilities()
|
|
|
|
{
|
|
|
|
std::unique_ptr<Poco::Net::HTTPClientSession> session(helpers::createSession(_uri));
|
|
|
|
|
|
|
|
// Get discovery first and extract the urlsrc of the capabilities end point
|
2018-11-30 07:49:58 -06:00
|
|
|
std::string capabilitiesURI;
|
2018-10-19 11:14:48 -05:00
|
|
|
{
|
|
|
|
|
|
|
|
Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, "/hosting/discovery");
|
|
|
|
session->sendRequest(request);
|
|
|
|
|
|
|
|
Poco::Net::HTTPResponse response;
|
|
|
|
std::istream& rs = session->receiveResponse(response);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(Poco::Net::HTTPResponse::HTTP_OK, response.getStatus());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("text/xml"), response.getContentType());
|
|
|
|
|
|
|
|
std::string discoveryXML;
|
|
|
|
Poco::StreamCopier::copyToString(rs, discoveryXML);
|
|
|
|
|
|
|
|
Poco::XML::DOMParser parser;
|
|
|
|
Poco::XML::AutoPtr<Poco::XML::Document> docXML = parser.parseString(discoveryXML);
|
|
|
|
Poco::XML::AutoPtr<Poco::XML::NodeList> listNodes = docXML->getElementsByTagName("action");
|
|
|
|
bool foundCapabilities = false;
|
|
|
|
for (unsigned long index = 0; index < listNodes->length(); ++index)
|
|
|
|
{
|
|
|
|
Poco::XML::Element* elem = static_cast<Poco::XML::Element*>(listNodes->item(index));
|
|
|
|
Poco::XML::Element* parent = elem->parentNode() ? static_cast<Poco::XML::Element*>(elem->parentNode()) : nullptr;
|
|
|
|
if(parent && parent->getAttribute("name") == "Capabilities")
|
|
|
|
{
|
|
|
|
foundCapabilities = true;
|
2018-11-30 07:49:58 -06:00
|
|
|
capabilitiesURI = elem->getAttribute("urlsrc");
|
2018-10-19 11:14:48 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT(foundCapabilities);
|
2018-11-30 07:49:58 -06:00
|
|
|
CPPUNIT_ASSERT_EQUAL(_uri.toString() + CAPABILITIES_END_POINT, capabilitiesURI);
|
2018-10-19 11:14:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Then get the capabilities json
|
|
|
|
{
|
|
|
|
Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, CAPABILITIES_END_POINT);
|
|
|
|
session->sendRequest(request);
|
|
|
|
|
|
|
|
Poco::Net::HTTPResponse response;
|
|
|
|
std::istream& rs = session->receiveResponse(response);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(Poco::Net::HTTPResponse::HTTP_OK, response.getStatus());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("application/json"), response.getContentType());
|
|
|
|
|
|
|
|
std::ostringstream oss;
|
|
|
|
Poco::StreamCopier::copyStream(rs, oss);
|
|
|
|
std::string responseString = oss.str();
|
|
|
|
|
|
|
|
Poco::JSON::Parser parser;
|
|
|
|
Poco::Dynamic::Var jsonFile = parser.parse(responseString);
|
|
|
|
Poco::JSON::Object::Ptr features = jsonFile.extract<Poco::JSON::Object::Ptr>();
|
|
|
|
CPPUNIT_ASSERT(features);
|
|
|
|
CPPUNIT_ASSERT(features->has("convert-to"));
|
|
|
|
|
|
|
|
Poco::JSON::Object::Ptr convert_to = features->get("convert-to").extract<Poco::JSON::Object::Ptr>();
|
|
|
|
CPPUNIT_ASSERT(convert_to->has("available"));
|
2018-10-20 05:57:53 -05:00
|
|
|
CPPUNIT_ASSERT(convert_to->get("available"));
|
2018-10-19 11:14:48 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-04-28 11:08:07 -05:00
|
|
|
void HTTPServerTest::testLoleafletGet()
|
2016-04-24 15:41:51 -05:00
|
|
|
{
|
2016-05-02 11:41:57 -05:00
|
|
|
std::unique_ptr<Poco::Net::HTTPClientSession> session(helpers::createSession(_uri));
|
2016-04-24 15:41:51 -05:00
|
|
|
|
2016-04-28 11:08:07 -05:00
|
|
|
Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, "/loleaflet/dist/loleaflet.html?access_token=111111111");
|
|
|
|
Poco::Net::HTMLForm param(request);
|
2016-05-02 11:41:57 -05:00
|
|
|
session->sendRequest(request);
|
2016-04-24 15:41:51 -05:00
|
|
|
|
|
|
|
Poco::Net::HTTPResponse response;
|
2016-05-02 11:41:57 -05:00
|
|
|
std::istream& rs = session->receiveResponse(response);
|
2016-04-24 19:59:58 -05:00
|
|
|
CPPUNIT_ASSERT_EQUAL(Poco::Net::HTTPResponse::HTTP_OK, response.getStatus());
|
2016-04-24 15:41:51 -05:00
|
|
|
CPPUNIT_ASSERT_EQUAL(std::string("text/html"), response.getContentType());
|
2016-04-28 11:08:07 -05:00
|
|
|
|
|
|
|
std::string html;
|
|
|
|
Poco::StreamCopier::copyToString(rs, html);
|
|
|
|
|
|
|
|
CPPUNIT_ASSERT(html.find(param["access_token"]) != std::string::npos);
|
2016-05-02 11:25:39 -05:00
|
|
|
CPPUNIT_ASSERT(html.find(_uri.getHost()) != std::string::npos);
|
2016-05-10 04:19:32 -05:00
|
|
|
CPPUNIT_ASSERT(html.find(std::string(LOOLWSD_VERSION_HASH)) != std::string::npos);
|
2016-04-24 15:41:51 -05:00
|
|
|
}
|
|
|
|
|
2016-04-28 11:08:07 -05:00
|
|
|
void HTTPServerTest::testLoleafletPost()
|
2016-04-24 19:59:58 -05:00
|
|
|
{
|
2016-05-02 11:41:57 -05:00
|
|
|
std::unique_ptr<Poco::Net::HTTPClientSession> session(helpers::createSession(_uri));
|
2016-04-24 19:59:58 -05:00
|
|
|
|
2016-04-28 11:08:07 -05:00
|
|
|
Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_POST, "/loleaflet/dist/loleaflet.html");
|
|
|
|
Poco::Net::HTMLForm form;
|
|
|
|
form.set("access_token", "2222222222");
|
|
|
|
form.prepareSubmit(request);
|
2016-05-02 11:41:57 -05:00
|
|
|
std::ostream& ostr = session->sendRequest(request);
|
2016-04-28 11:08:07 -05:00
|
|
|
form.write(ostr);
|
2016-04-24 19:59:58 -05:00
|
|
|
|
|
|
|
Poco::Net::HTTPResponse response;
|
2016-05-02 11:41:57 -05:00
|
|
|
std::istream& rs = session->receiveResponse(response);
|
2016-04-24 19:59:58 -05:00
|
|
|
CPPUNIT_ASSERT_EQUAL(Poco::Net::HTTPResponse::HTTP_OK, response.getStatus());
|
|
|
|
|
|
|
|
std::string html;
|
|
|
|
Poco::StreamCopier::copyToString(rs, html);
|
|
|
|
|
2016-04-28 11:08:07 -05:00
|
|
|
CPPUNIT_ASSERT(html.find(form["access_token"]) != std::string::npos);
|
2016-05-02 11:25:39 -05:00
|
|
|
CPPUNIT_ASSERT(html.find(_uri.getHost()) != std::string::npos);
|
2016-04-24 19:59:58 -05:00
|
|
|
}
|
|
|
|
|
2016-05-22 17:47:04 -05:00
|
|
|
namespace
|
|
|
|
{
|
2016-04-28 11:17:37 -05:00
|
|
|
|
|
|
|
void assertHTTPFilesExist(const Poco::URI& uri, Poco::RegularExpression& expr, const std::string& html, const std::string& mimetype = std::string())
|
|
|
|
{
|
|
|
|
Poco::RegularExpression::MatchVec matches;
|
2016-04-28 11:35:22 -05:00
|
|
|
bool found = false;
|
2016-04-28 11:17:37 -05:00
|
|
|
|
|
|
|
for (int offset = 0; expr.match(html, offset, matches) > 0; offset = static_cast<int>(matches[0].offset + matches[0].length))
|
|
|
|
{
|
2016-04-28 11:35:22 -05:00
|
|
|
found = true;
|
2016-05-22 17:47:04 -05:00
|
|
|
CPPUNIT_ASSERT_EQUAL(2, (int)matches.size());
|
|
|
|
Poco::URI uriScript(html.substr(matches[1].offset, matches[1].length));
|
|
|
|
if (uriScript.getHost().empty())
|
|
|
|
{
|
|
|
|
std::string scriptString(uriScript.toString());
|
2016-04-28 11:17:37 -05:00
|
|
|
|
2016-05-22 17:47:04 -05:00
|
|
|
// ignore the branding bits, it's not an error when they aren't present.
|
|
|
|
if (scriptString.find("/branding.") != std::string::npos)
|
|
|
|
continue;
|
2016-04-28 11:17:37 -05:00
|
|
|
|
2016-05-02 11:41:57 -05:00
|
|
|
std::unique_ptr<Poco::Net::HTTPClientSession> session(helpers::createSession(uri));
|
2016-04-28 11:35:22 -05:00
|
|
|
|
2016-05-22 17:47:04 -05:00
|
|
|
Poco::Net::HTTPRequest requestScript(Poco::Net::HTTPRequest::HTTP_GET, scriptString);
|
|
|
|
session->sendRequest(requestScript);
|
2016-04-28 11:17:37 -05:00
|
|
|
|
2016-05-22 17:47:04 -05:00
|
|
|
Poco::Net::HTTPResponse responseScript;
|
|
|
|
session->receiveResponse(responseScript);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(Poco::Net::HTTPResponse::HTTP_OK, responseScript.getStatus());
|
2016-04-28 11:17:37 -05:00
|
|
|
|
2016-05-22 17:47:04 -05:00
|
|
|
if (!mimetype.empty())
|
|
|
|
CPPUNIT_ASSERT_EQUAL(mimetype, responseScript.getContentType());
|
|
|
|
}
|
2016-04-28 11:17:37 -05:00
|
|
|
}
|
2016-04-28 11:35:22 -05:00
|
|
|
|
|
|
|
CPPUNIT_ASSERT_MESSAGE("No match found", found);
|
2016-04-28 11:17:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-04-28 11:08:07 -05:00
|
|
|
void HTTPServerTest::testScriptsAndLinksGet()
|
2016-04-25 20:01:46 -05:00
|
|
|
{
|
2016-05-02 11:41:57 -05:00
|
|
|
std::unique_ptr<Poco::Net::HTTPClientSession> session(helpers::createSession(_uri));
|
2016-04-28 11:08:07 -05:00
|
|
|
|
2016-04-25 20:01:46 -05:00
|
|
|
Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, "/loleaflet/dist/loleaflet.html");
|
2016-05-02 11:41:57 -05:00
|
|
|
session->sendRequest(request);
|
2016-04-25 20:01:46 -05:00
|
|
|
|
|
|
|
Poco::Net::HTTPResponse response;
|
2016-05-02 11:41:57 -05:00
|
|
|
std::istream& rs = session->receiveResponse(response);
|
2016-04-25 20:01:46 -05:00
|
|
|
CPPUNIT_ASSERT_EQUAL(Poco::Net::HTTPResponse::HTTP_OK, response.getStatus());
|
|
|
|
|
|
|
|
std::string html;
|
|
|
|
Poco::StreamCopier::copyToString(rs, html);
|
|
|
|
|
|
|
|
Poco::RegularExpression script("<script.*?src=\"(.*?)\"");
|
2016-05-02 11:25:39 -05:00
|
|
|
assertHTTPFilesExist(_uri, script, html, "application/javascript");
|
2016-04-28 11:08:07 -05:00
|
|
|
|
|
|
|
Poco::RegularExpression link("<link.*?href=\"(.*?)\"");
|
2016-05-02 11:25:39 -05:00
|
|
|
assertHTTPFilesExist(_uri, link, html);
|
2016-04-25 20:01:46 -05:00
|
|
|
}
|
|
|
|
|
2016-04-28 11:08:07 -05:00
|
|
|
void HTTPServerTest::testScriptsAndLinksPost()
|
2016-04-25 20:40:24 -05:00
|
|
|
{
|
2016-05-02 11:41:57 -05:00
|
|
|
std::unique_ptr<Poco::Net::HTTPClientSession> session(helpers::createSession(_uri));
|
2016-04-28 11:08:07 -05:00
|
|
|
|
|
|
|
Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_POST, "/loleaflet/dist/loleaflet.html");
|
|
|
|
std::string body;
|
|
|
|
request.setContentLength((int) body.length());
|
2016-05-02 11:41:57 -05:00
|
|
|
session->sendRequest(request) << body;
|
2016-04-25 20:40:24 -05:00
|
|
|
|
|
|
|
Poco::Net::HTTPResponse response;
|
2016-05-02 11:41:57 -05:00
|
|
|
std::istream& rs = session->receiveResponse(response);
|
2016-04-25 20:40:24 -05:00
|
|
|
CPPUNIT_ASSERT_EQUAL(Poco::Net::HTTPResponse::HTTP_OK, response.getStatus());
|
|
|
|
|
|
|
|
std::string html;
|
|
|
|
Poco::StreamCopier::copyToString(rs, html);
|
|
|
|
|
2016-04-28 11:08:07 -05:00
|
|
|
Poco::RegularExpression script("<script.*?src=\"(.*?)\"");
|
2016-05-02 11:25:39 -05:00
|
|
|
assertHTTPFilesExist(_uri, script, html, "application/javascript");
|
2016-04-25 20:40:24 -05:00
|
|
|
|
2016-04-28 11:08:07 -05:00
|
|
|
Poco::RegularExpression link("<link.*?href=\"(.*?)\"");
|
2016-05-02 11:25:39 -05:00
|
|
|
assertHTTPFilesExist(_uri, link, html);
|
2016-04-25 20:40:24 -05:00
|
|
|
}
|
|
|
|
|
2016-07-21 02:22:14 -05:00
|
|
|
void HTTPServerTest::testConvertTo()
|
|
|
|
{
|
2018-02-07 03:17:42 -06:00
|
|
|
const std::string srcPath = FileUtil::getTempFilePath(TDOC, "hello.odt", "convertTo_");
|
2016-07-21 02:22:14 -05:00
|
|
|
std::unique_ptr<Poco::Net::HTTPClientSession> session(helpers::createSession(_uri));
|
2016-10-09 10:59:00 -05:00
|
|
|
session->setTimeout(Poco::Timespan(2, 0)); // 2 seconds.
|
2016-07-21 02:22:14 -05:00
|
|
|
|
|
|
|
Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_POST, "/lool/convert-to");
|
|
|
|
Poco::Net::HTMLForm form;
|
|
|
|
form.setEncoding(Poco::Net::HTMLForm::ENCODING_MULTIPART);
|
|
|
|
form.set("format", "txt");
|
|
|
|
form.addPart("data", new Poco::Net::FilePartSource(srcPath));
|
|
|
|
form.prepareSubmit(request);
|
2019-08-23 01:35:55 -05:00
|
|
|
try
|
|
|
|
{
|
|
|
|
form.write(session->sendRequest(request));
|
|
|
|
}
|
|
|
|
catch (const std::exception& ex)
|
|
|
|
{
|
|
|
|
// In case the server is still starting up.
|
|
|
|
sleep(2);
|
|
|
|
form.write(session->sendRequest(request));
|
|
|
|
}
|
2016-07-21 02:22:14 -05:00
|
|
|
|
|
|
|
Poco::Net::HTTPResponse response;
|
|
|
|
std::stringstream actualStream;
|
|
|
|
std::istream& responseStream = session->receiveResponse(response);
|
|
|
|
Poco::StreamCopier::copyStream(responseStream, actualStream);
|
|
|
|
|
|
|
|
std::ifstream fileStream(TDOC "/hello.txt");
|
|
|
|
std::stringstream expectedStream;
|
|
|
|
expectedStream << fileStream.rdbuf();
|
|
|
|
|
|
|
|
// Remove the temp files.
|
2016-11-12 15:38:13 -06:00
|
|
|
FileUtil::removeFile(srcPath);
|
2016-07-21 02:22:14 -05:00
|
|
|
|
|
|
|
// In some cases the result is prefixed with (the UTF-8 encoding of) the Unicode BOM
|
|
|
|
// (U+FEFF). Skip that.
|
|
|
|
std::string actualString = actualStream.str();
|
|
|
|
if (actualString.size() > 3 && actualString[0] == '\xEF' && actualString[1] == '\xBB' && actualString[2] == '\xBF')
|
|
|
|
actualString = actualString.substr(3);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(expectedStream.str(), actualString);
|
|
|
|
}
|
|
|
|
|
2019-12-11 23:33:09 -06:00
|
|
|
void HTTPServerTest::testConvertTo2()
|
|
|
|
{
|
|
|
|
const std::string srcPath = FileUtil::getTempFilePath(TDOC, "convert-to.xlsx", "convertTo_");
|
|
|
|
std::unique_ptr<Poco::Net::HTTPClientSession> session(helpers::createSession(_uri));
|
|
|
|
session->setTimeout(Poco::Timespan(5, 0)); // 5 seconds.
|
|
|
|
|
|
|
|
Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_POST, "/lool/convert-to");
|
|
|
|
Poco::Net::HTMLForm form;
|
|
|
|
form.setEncoding(Poco::Net::HTMLForm::ENCODING_MULTIPART);
|
|
|
|
form.set("format", "png");
|
|
|
|
form.addPart("data", new Poco::Net::FilePartSource(srcPath));
|
|
|
|
form.prepareSubmit(request);
|
|
|
|
try
|
|
|
|
{
|
|
|
|
form.write(session->sendRequest(request));
|
|
|
|
}
|
|
|
|
catch (const std::exception& ex)
|
|
|
|
{
|
|
|
|
// In case the server is still starting up.
|
|
|
|
sleep(2);
|
|
|
|
form.write(session->sendRequest(request));
|
|
|
|
}
|
|
|
|
|
|
|
|
Poco::Net::HTTPResponse response;
|
|
|
|
std::stringstream actualStream;
|
|
|
|
std::istream& responseStream = session->receiveResponse(response);
|
|
|
|
Poco::StreamCopier::copyStream(responseStream, actualStream);
|
|
|
|
|
|
|
|
// Remove the temp files.
|
|
|
|
FileUtil::removeFile(srcPath);
|
|
|
|
|
|
|
|
std::string actualString = actualStream.str();
|
|
|
|
CPPUNIT_ASSERT(actualString.size() >= 100);
|
|
|
|
// CPPUNIT_ASSERT_EQUAL(actualString[0], 0x89);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(actualString[1], 'P');
|
|
|
|
CPPUNIT_ASSERT_EQUAL(actualString[2], 'N');
|
|
|
|
CPPUNIT_ASSERT_EQUAL(actualString[3], 'G');
|
|
|
|
}
|
2018-10-24 05:37:58 -05:00
|
|
|
|
|
|
|
void HTTPServerTest::testConvertToWithForwardedClientIP()
|
|
|
|
{
|
2019-10-20 23:48:51 -05:00
|
|
|
const std::string testname = "convertToWithForwardedClientIP-";
|
|
|
|
constexpr int TimeoutSeconds = 10; // Sometimes dns resolving is slow.
|
|
|
|
|
2018-10-24 05:37:58 -05:00
|
|
|
// Test a forwarded IP which is not allowed to use convert-to feature
|
2019-10-20 23:48:51 -05:00
|
|
|
try
|
2018-10-24 05:37:58 -05:00
|
|
|
{
|
2019-10-20 23:48:51 -05:00
|
|
|
TST_LOG("Converting from a disallowed IP.");
|
|
|
|
|
|
|
|
const std::string srcPath = FileUtil::getTempFilePath(TDOC, "hello.odt", testname + "disallowed");
|
2018-10-24 05:37:58 -05:00
|
|
|
std::unique_ptr<Poco::Net::HTTPClientSession> session(helpers::createSession(_uri));
|
2019-10-20 23:48:51 -05:00
|
|
|
session->setTimeout(Poco::Timespan(TimeoutSeconds, 0));
|
2018-10-24 05:37:58 -05:00
|
|
|
|
|
|
|
Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_POST, "/lool/convert-to");
|
|
|
|
CPPUNIT_ASSERT(!request.has("X-Forwarded-For"));
|
|
|
|
request.add("X-Forwarded-For", getNotAllowedTestServerURI().getHost() + ", " + _uri.getHost());
|
|
|
|
Poco::Net::HTMLForm form;
|
|
|
|
form.setEncoding(Poco::Net::HTMLForm::ENCODING_MULTIPART);
|
|
|
|
form.set("format", "txt");
|
|
|
|
form.addPart("data", new Poco::Net::FilePartSource(srcPath));
|
|
|
|
form.prepareSubmit(request);
|
2019-08-23 01:35:55 -05:00
|
|
|
try
|
|
|
|
{
|
|
|
|
form.write(session->sendRequest(request));
|
|
|
|
}
|
|
|
|
catch (const std::exception& ex)
|
|
|
|
{
|
|
|
|
// In case the server is still starting up.
|
|
|
|
sleep(2);
|
|
|
|
form.write(session->sendRequest(request));
|
|
|
|
}
|
2018-10-24 05:37:58 -05:00
|
|
|
|
|
|
|
Poco::Net::HTTPResponse response;
|
|
|
|
std::stringstream actualStream;
|
|
|
|
std::istream& responseStream = session->receiveResponse(response);
|
|
|
|
Poco::StreamCopier::copyStream(responseStream, actualStream);
|
|
|
|
|
|
|
|
// Remove the temp files.
|
|
|
|
FileUtil::removeFile(srcPath);
|
|
|
|
|
|
|
|
std::string actualString = actualStream.str();
|
|
|
|
CPPUNIT_ASSERT(actualString.empty()); // <- we did not get the converted file
|
|
|
|
}
|
2019-10-20 23:48:51 -05:00
|
|
|
catch(const Poco::Exception& exc)
|
|
|
|
{
|
|
|
|
CPPUNIT_FAIL(exc.displayText() + ": " + (exc.nested() ? exc.nested()->displayText() : ""));
|
|
|
|
}
|
2018-10-24 05:37:58 -05:00
|
|
|
|
|
|
|
// Test a forwarded IP which is allowed to use convert-to feature
|
2019-10-20 23:48:51 -05:00
|
|
|
try
|
2018-10-24 05:37:58 -05:00
|
|
|
{
|
2019-10-20 23:48:51 -05:00
|
|
|
TST_LOG("Converting from an allowed IP.");
|
|
|
|
|
|
|
|
const std::string srcPath = FileUtil::getTempFilePath(TDOC, "hello.odt", testname + "allowed");
|
2018-10-24 05:37:58 -05:00
|
|
|
std::unique_ptr<Poco::Net::HTTPClientSession> session(helpers::createSession(_uri));
|
2019-10-20 23:48:51 -05:00
|
|
|
session->setTimeout(Poco::Timespan(TimeoutSeconds, 0));
|
2018-10-24 05:37:58 -05:00
|
|
|
|
|
|
|
Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_POST, "/lool/convert-to");
|
|
|
|
CPPUNIT_ASSERT(!request.has("X-Forwarded-For"));
|
|
|
|
request.add("X-Forwarded-For", _uri.getHost() + ", " + _uri.getHost());
|
|
|
|
Poco::Net::HTMLForm form;
|
|
|
|
form.setEncoding(Poco::Net::HTMLForm::ENCODING_MULTIPART);
|
|
|
|
form.set("format", "txt");
|
|
|
|
form.addPart("data", new Poco::Net::FilePartSource(srcPath));
|
|
|
|
form.prepareSubmit(request);
|
|
|
|
form.write(session->sendRequest(request));
|
|
|
|
|
|
|
|
Poco::Net::HTTPResponse response;
|
|
|
|
std::stringstream actualStream;
|
|
|
|
std::istream& responseStream = session->receiveResponse(response);
|
|
|
|
Poco::StreamCopier::copyStream(responseStream, actualStream);
|
|
|
|
|
|
|
|
std::ifstream fileStream(TDOC "/hello.txt");
|
|
|
|
std::stringstream expectedStream;
|
|
|
|
expectedStream << fileStream.rdbuf();
|
|
|
|
|
|
|
|
// Remove the temp files.
|
|
|
|
FileUtil::removeFile(srcPath);
|
|
|
|
|
|
|
|
// In some cases the result is prefixed with (the UTF-8 encoding of) the Unicode BOM
|
|
|
|
// (U+FEFF). Skip that.
|
|
|
|
std::string actualString = actualStream.str();
|
|
|
|
if (actualString.size() > 3 && actualString[0] == '\xEF' && actualString[1] == '\xBB' && actualString[2] == '\xBF')
|
|
|
|
actualString = actualString.substr(3);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(expectedStream.str(), actualString); // <- we got the converted file
|
|
|
|
}
|
2019-10-20 23:48:51 -05:00
|
|
|
catch(const Poco::Exception& exc)
|
|
|
|
{
|
|
|
|
CPPUNIT_FAIL(exc.displayText() + ": " + (exc.nested() ? exc.nested()->displayText() : ""));
|
|
|
|
}
|
2018-10-25 03:48:10 -05:00
|
|
|
|
|
|
|
// Test a forwarded header with three IPs, one is not allowed -> request is denied.
|
2019-10-20 23:48:51 -05:00
|
|
|
try
|
2018-10-25 03:48:10 -05:00
|
|
|
{
|
2019-10-20 23:48:51 -05:00
|
|
|
TST_LOG("Converting from multiple IPs, on disallowed.");
|
|
|
|
|
|
|
|
const std::string srcPath = FileUtil::getTempFilePath(TDOC, "hello.odt", testname + "disallowed-multi");
|
2018-10-25 03:48:10 -05:00
|
|
|
std::unique_ptr<Poco::Net::HTTPClientSession> session(helpers::createSession(_uri));
|
2019-10-20 23:48:51 -05:00
|
|
|
session->setTimeout(Poco::Timespan(TimeoutSeconds, 0));
|
2018-10-25 03:48:10 -05:00
|
|
|
|
|
|
|
Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_POST, "/lool/convert-to");
|
|
|
|
CPPUNIT_ASSERT(!request.has("X-Forwarded-For"));
|
|
|
|
request.add("X-Forwarded-For", _uri.getHost() + ", "
|
|
|
|
+ getNotAllowedTestServerURI().getHost() + ", "
|
|
|
|
+ _uri.getHost());
|
|
|
|
Poco::Net::HTMLForm form;
|
|
|
|
form.setEncoding(Poco::Net::HTMLForm::ENCODING_MULTIPART);
|
|
|
|
form.set("format", "txt");
|
|
|
|
form.addPart("data", new Poco::Net::FilePartSource(srcPath));
|
|
|
|
form.prepareSubmit(request);
|
|
|
|
form.write(session->sendRequest(request));
|
|
|
|
|
|
|
|
Poco::Net::HTTPResponse response;
|
|
|
|
std::stringstream actualStream;
|
|
|
|
std::istream& responseStream = session->receiveResponse(response);
|
|
|
|
Poco::StreamCopier::copyStream(responseStream, actualStream);
|
|
|
|
|
|
|
|
// Remove the temp files.
|
|
|
|
FileUtil::removeFile(srcPath);
|
|
|
|
|
|
|
|
std::string actualString = actualStream.str();
|
|
|
|
CPPUNIT_ASSERT(actualString.empty()); // <- we did not get the converted file
|
|
|
|
}
|
2019-10-20 23:48:51 -05:00
|
|
|
catch(const Poco::Exception& exc)
|
|
|
|
{
|
|
|
|
CPPUNIT_FAIL(exc.displayText() + ": " + (exc.nested() ? exc.nested()->displayText() : ""));
|
|
|
|
}
|
2018-10-24 05:37:58 -05:00
|
|
|
}
|
|
|
|
|
2016-04-28 11:08:07 -05:00
|
|
|
CPPUNIT_TEST_SUITE_REGISTRATION(HTTPServerTest);
|
2016-04-20 22:09:04 -05:00
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|