2019-05-21 22:33:26 -05:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
|
|
|
|
/*
|
|
|
|
* 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 <config.h>
|
|
|
|
|
2020-11-01 08:49:06 -06:00
|
|
|
#include <Poco/Net/HTTPRequest.h>
|
|
|
|
|
2019-05-21 22:33:26 -05:00
|
|
|
#include <WopiTestServer.hpp>
|
|
|
|
#include <Log.hpp>
|
|
|
|
#include <Unit.hpp>
|
|
|
|
#include <UnitHTTP.hpp>
|
|
|
|
#include <helpers.hpp>
|
2020-11-01 08:49:06 -06:00
|
|
|
#include <net/HttpHelper.hpp>
|
2019-05-21 22:33:26 -05:00
|
|
|
|
|
|
|
class UnitWOPITemplate : public WopiTestServer
|
|
|
|
{
|
|
|
|
enum class Phase
|
|
|
|
{
|
|
|
|
LoadTemplate,
|
|
|
|
SaveDoc,
|
|
|
|
CloseDoc,
|
|
|
|
Polling
|
|
|
|
} _phase;
|
|
|
|
|
2021-02-23 21:44:27 -06:00
|
|
|
bool _savedTemplate;
|
|
|
|
|
2019-05-21 22:33:26 -05:00
|
|
|
public:
|
2020-12-20 21:11:33 -06:00
|
|
|
UnitWOPITemplate()
|
|
|
|
: WopiTestServer("UnitWOPITemplate")
|
|
|
|
, _phase(Phase::LoadTemplate)
|
2021-02-23 21:44:27 -06:00
|
|
|
, _savedTemplate(false)
|
2019-05-21 22:33:26 -05:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-02-23 21:44:27 -06:00
|
|
|
virtual bool handleHttpRequest(const Poco::Net::HTTPRequest& request,
|
|
|
|
Poco::MemoryInputStream& /*message*/,
|
|
|
|
std::shared_ptr<StreamSocket>& socket) override
|
2019-05-21 22:33:26 -05:00
|
|
|
{
|
2021-02-13 14:45:02 -06:00
|
|
|
const Poco::URI uriReq(request.getURI());
|
2023-01-23 15:53:27 -06:00
|
|
|
LOG_TST("FakeWOPIHost: " << request.getMethod() << " request: " << uriReq.toString());
|
2019-05-21 22:33:26 -05:00
|
|
|
|
|
|
|
// CheckFileInfo
|
|
|
|
if (request.getMethod() == "GET" && uriReq.getPath() == "/wopi/files/10")
|
|
|
|
{
|
2023-01-23 15:53:27 -06:00
|
|
|
LOG_TST("FakeWOPIHost: Handling CheckFileInfo: " << uriReq.getPath());
|
2019-05-21 22:33:26 -05:00
|
|
|
|
|
|
|
Poco::JSON::Object::Ptr fileInfo = new Poco::JSON::Object();
|
|
|
|
fileInfo->set("BaseFileName", "test.odt");
|
2021-02-13 17:33:51 -06:00
|
|
|
fileInfo->set("TemplateSource", helpers::getTestServerURI() + "/test.ott");
|
2019-07-25 00:58:19 -05:00
|
|
|
fileInfo->set("Size", getFileContent().size());
|
2019-05-21 22:33:26 -05:00
|
|
|
fileInfo->set("Version", "1.0");
|
|
|
|
fileInfo->set("OwnerId", "test");
|
|
|
|
fileInfo->set("UserId", "test");
|
|
|
|
fileInfo->set("UserFriendlyName", "test");
|
|
|
|
fileInfo->set("UserCanWrite", "true");
|
|
|
|
fileInfo->set("PostMessageOrigin", "localhost");
|
2019-09-27 11:56:16 -05:00
|
|
|
fileInfo->set("LastModifiedTime", Util::getIso8601FracformatTime(getFileLastModifiedTime()));
|
2019-05-21 22:33:26 -05:00
|
|
|
fileInfo->set("EnableOwnerTermination", "true");
|
|
|
|
|
|
|
|
std::ostringstream jsonStream;
|
|
|
|
fileInfo->stringify(jsonStream);
|
|
|
|
|
2023-01-16 17:38:59 -06:00
|
|
|
http::Response httpResponse(http::StatusCode::OK);
|
2022-02-01 07:33:25 -06:00
|
|
|
httpResponse.set("Last-Modified", Util::getHttpTime(getFileLastModifiedTime()));
|
|
|
|
httpResponse.setBody(jsonStream.str(), "application/json; charset=utf-8");
|
|
|
|
socket->sendAndShutdown(httpResponse);
|
2019-05-21 22:33:26 -05:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2021-02-23 21:44:27 -06:00
|
|
|
else if ((request.getMethod() == "OPTIONS" || request.getMethod() == "HEAD"
|
|
|
|
|| request.getMethod() == "PROPFIND")
|
|
|
|
&& uriReq.getPath() == "/test.ott")
|
2019-05-21 22:33:26 -05:00
|
|
|
{
|
2023-01-23 15:53:27 -06:00
|
|
|
LOG_TST("FakeWOPIHost: Handling " << request.getMethod() << " on " << uriReq.getPath());
|
2021-02-23 21:44:27 -06:00
|
|
|
|
2019-05-21 22:33:26 -05:00
|
|
|
std::ostringstream oss;
|
|
|
|
oss << "HTTP/1.1 200 OK\r\n"
|
|
|
|
<< "Allow: GET\r\n"
|
|
|
|
<< "User-Agent: " << WOPI_AGENT_STRING << "\r\n"
|
|
|
|
<< "\r\n";
|
|
|
|
|
|
|
|
socket->send(oss.str());
|
|
|
|
socket->shutdown();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// Get the template
|
|
|
|
else if (request.getMethod() == "GET" && uriReq.getPath() == "/test.ott")
|
|
|
|
{
|
2023-01-23 15:53:27 -06:00
|
|
|
LOG_TST("FakeWOPIHost: Handling template GetFile: " << uriReq.getPath());
|
2019-05-21 22:33:26 -05:00
|
|
|
|
2020-06-03 10:06:10 -05:00
|
|
|
HttpHelper::sendFileAndShutdown(socket, TDOC "/test.ott", "");
|
2019-05-21 22:33:26 -05:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// Save template
|
|
|
|
else if (request.getMethod() == "POST" && uriReq.getPath() == "/wopi/files/10/contents")
|
|
|
|
{
|
2023-01-23 15:53:27 -06:00
|
|
|
LOG_TST("FakeWOPIHost: Handling PutFile: " << uriReq.getPath());
|
2019-05-21 22:33:26 -05:00
|
|
|
|
2021-02-23 21:44:27 -06:00
|
|
|
if (!_savedTemplate)
|
|
|
|
{
|
|
|
|
// First, we expect to get a PutFile right after loading.
|
|
|
|
LOK_ASSERT_EQUAL(static_cast<int>(Phase::SaveDoc), static_cast<int>(_phase));
|
|
|
|
_savedTemplate = true;
|
2021-05-28 16:55:02 -05:00
|
|
|
LOG_TST("SaveDoc => CloseDoc");
|
2021-02-23 21:44:27 -06:00
|
|
|
_phase = Phase::CloseDoc;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// This is the save at shutting down.
|
|
|
|
LOK_ASSERT_EQUAL(static_cast<int>(Phase::Polling), static_cast<int>(_phase));
|
|
|
|
exitTest(TestResult::Ok);
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::streamsize size = request.getContentLength();
|
2020-01-20 10:45:53 -06:00
|
|
|
LOK_ASSERT( size > 0 );
|
2019-05-21 22:33:26 -05:00
|
|
|
|
2022-02-01 07:33:25 -06:00
|
|
|
const std::string body = "{\"LastModifiedTime\": \"" +
|
|
|
|
Util::getIso8601FracformatTime(getFileLastModifiedTime()) + "\" }";
|
2023-01-16 17:38:59 -06:00
|
|
|
http::Response httpResponse(http::StatusCode::OK);
|
2022-02-01 07:33:25 -06:00
|
|
|
httpResponse.setBody(body, "application/json; charset=utf-8");
|
|
|
|
socket->sendAndShutdown(httpResponse);
|
2019-05-21 22:33:26 -05:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-01-23 15:53:27 -06:00
|
|
|
LOG_TST("FakeWOPIHost: unknown request "
|
2021-05-28 16:55:02 -05:00
|
|
|
<< request.getMethod() << " request: " << uriReq.toString() << ". Defaulting.");
|
2019-05-21 22:33:26 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-12-24 11:15:37 -06:00
|
|
|
void invokeWSDTest() override
|
2019-05-21 22:33:26 -05:00
|
|
|
{
|
|
|
|
switch (_phase)
|
|
|
|
{
|
|
|
|
case Phase::LoadTemplate:
|
|
|
|
{
|
2021-05-28 16:55:02 -05:00
|
|
|
LOG_TST("LoadTemplate => SaveDoc");
|
2021-02-13 14:45:02 -06:00
|
|
|
_phase = Phase::SaveDoc;
|
|
|
|
|
2019-05-21 22:33:26 -05:00
|
|
|
initWebsocket("/wopi/files/10?access_token=anything");
|
2021-02-13 14:45:02 -06:00
|
|
|
WSD_CMD("load url=" + getWopiSrc());
|
2019-05-21 22:33:26 -05:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Phase::CloseDoc:
|
|
|
|
{
|
2021-05-28 16:55:02 -05:00
|
|
|
LOG_TST("CloseDoc => Polling");
|
2019-05-21 22:33:26 -05:00
|
|
|
_phase = Phase::Polling;
|
2021-02-13 14:45:02 -06:00
|
|
|
WSD_CMD("closedocument");
|
2019-05-21 22:33:26 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Phase::Polling:
|
|
|
|
{
|
|
|
|
exitTest(TestResult::Ok);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Phase::SaveDoc:
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
UnitBase *unit_create_wsd(void)
|
|
|
|
{
|
|
|
|
return new UnitWOPITemplate();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|