libreoffice-online/loolwsd/test/httpposttest.cpp
Ashod Nakashian d48d9044a1 loolwsd: run tests on doc copies
Tests can modify the test documents they use.
Currently there is data-loss protection that
saves an open doc if connection is lost with
the client. For tests this means modification
are saved when a connection is terminated
ungracefully and this both adds noise
to the git checkout and makes subsequent
tests fail.

This patch makes temp copies of the original
doc before a test is run and deletes them
afterwards.

Change-Id: I1dd6ff2b839701e85c8bd502ba75170c01fa106e
Reviewed-on: https://gerrit.libreoffice.org/23447
Reviewed-by: Ashod Nakashian <ashnakash@gmail.com>
Tested-by: Ashod Nakashian <ashnakash@gmail.com>
2016-03-23 00:00:34 +00:00

92 lines
3.4 KiB
C++

/* -*- 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/.
*/
#include <Poco/Net/AcceptCertificateHandler.h>
#include <Poco/Net/FilePartSource.h>
#include <Poco/Net/HTMLForm.h>
#include <Poco/Net/HTTPClientSession.h>
#include <Poco/Net/HTTPSClientSession.h>
#include <Poco/Net/HTTPRequest.h>
#include <Poco/Net/HTTPResponse.h>
#include <Poco/Net/InvalidCertificateHandler.h>
#include <Poco/Net/SSLManager.h>
#include <Poco/StreamCopier.h>
#include <Poco/URI.h>
#include <cppunit/extensions/HelperMacros.h>
#include <Common.hpp>
#include <Util.hpp>
#include <ChildProcessSession.hpp>
/// Tests the HTTP POST API of loolwsd. The server has to be started manually before running this test.
class HTTPPostTest : public CPPUNIT_NS::TestFixture
{
CPPUNIT_TEST_SUITE(HTTPPostTest);
CPPUNIT_TEST(testConvertTo);
CPPUNIT_TEST_SUITE_END();
void testConvertTo();
public:
HTTPPostTest()
{
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);
Poco::Net::SSLManager::instance().initializeClient(0, invalidCertHandler, sslContext);
}
~HTTPPostTest()
{
Poco::Net::uninitializeSSL();
}
};
void HTTPPostTest::testConvertTo()
{
const auto srcPath = Util::getTempFilePath(TDOC, "hello.odt");
Poco::URI uri("https://127.0.0.1:" + std::to_string(ClientPortNumber));
Poco::Net::HTTPSClientSession session(uri.getHost(), uri.getPort());
Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_POST, "/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);
// If this results in a Poco::Net::ConnectionRefusedException, loolwsd is not running.
form.write(session.sendRequest(request));
Poco::Net::HTTPResponse response;
std::stringstream actualStream;
// receiveResponse() resulted in a Poco::Net::NoMessageException.
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.
Util::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);
}
CPPUNIT_TEST_SUITE_REGISTRATION(HTTPPostTest);
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */