2021-06-18 09:50:40 -05:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
|
|
|
|
/*
|
2023-11-09 12:23:00 -06:00
|
|
|
* Copyright the Collabora Online contributors.
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: MPL-2.0
|
|
|
|
*
|
2021-06-18 09:50:40 -05:00
|
|
|
* 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>
|
|
|
|
|
|
|
|
#include "WopiTestServer.hpp"
|
|
|
|
#include <Log.hpp>
|
|
|
|
#include <Unit.hpp>
|
|
|
|
#include <UnitHTTP.hpp>
|
|
|
|
#include <helpers.hpp>
|
2023-05-22 16:54:01 -05:00
|
|
|
#include <wsd/Storage.hpp>
|
2021-06-18 09:50:40 -05:00
|
|
|
|
|
|
|
#include <Poco/Net/HTTPRequest.h>
|
|
|
|
|
2023-05-22 16:54:01 -05:00
|
|
|
#include <string>
|
|
|
|
|
2021-06-18 09:50:40 -05:00
|
|
|
class UnitWopiHttpRedirect : public WopiTestServer
|
|
|
|
{
|
2023-05-22 16:06:29 -05:00
|
|
|
STATE_ENUM(Phase, Load, Redirected, GetFile, Redirected2, Done) _phase;
|
2021-06-18 09:50:40 -05:00
|
|
|
|
|
|
|
const std::string params = "access_token=anything";
|
|
|
|
|
|
|
|
public:
|
|
|
|
UnitWopiHttpRedirect()
|
2023-05-22 16:06:29 -05:00
|
|
|
: WopiTestServer("UnitWopiHttpRedirect")
|
2021-06-18 09:50:40 -05:00
|
|
|
, _phase(Phase::Load)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool handleHttpRequest(const Poco::Net::HTTPRequest& request,
|
|
|
|
Poco::MemoryInputStream& message,
|
|
|
|
std::shared_ptr<StreamSocket>& socket) override
|
|
|
|
{
|
|
|
|
Poco::URI uriReq(request.getURI());
|
|
|
|
Poco::RegularExpression regInfo("/wopi/files/1");
|
|
|
|
std::string redirectUri = "/wopi/files/0";
|
|
|
|
Poco::RegularExpression regRedirected(redirectUri);
|
2021-06-21 07:23:18 -05:00
|
|
|
Poco::RegularExpression regContents("/wopi/files/0/contents");
|
2021-06-22 10:38:45 -05:00
|
|
|
std::string redirectUri2 = "/wopi/files/2/contents";
|
|
|
|
Poco::RegularExpression regContentsRedirected(redirectUri2);
|
2021-06-18 09:50:40 -05:00
|
|
|
|
2024-03-03 08:03:35 -06:00
|
|
|
LOG_TST("FakeWOPIHost: Request URI [" << uriReq.toString() << "]:\n");
|
2021-06-18 09:50:40 -05:00
|
|
|
|
|
|
|
// CheckFileInfo - returns redirect response
|
2021-06-21 07:23:18 -05:00
|
|
|
if (request.getMethod() == "GET" && regInfo.match(uriReq.getPath()))
|
2021-06-18 09:50:40 -05:00
|
|
|
{
|
2024-03-03 08:03:35 -06:00
|
|
|
LOG_TST("FakeWOPIHost: Handling CheckFileInfo (1/2)");
|
2021-06-18 09:50:40 -05:00
|
|
|
|
|
|
|
assertCheckFileInfoRequest(request);
|
|
|
|
|
2024-03-03 08:37:32 -06:00
|
|
|
LOK_ASSERT_STATE(_phase, Phase::Load);
|
2024-03-03 08:07:36 -06:00
|
|
|
TRANSITION_STATE(_phase, Phase::Redirected);
|
2021-06-21 07:23:18 -05:00
|
|
|
|
2023-05-22 16:06:43 -05:00
|
|
|
http::Response httpResponse(http::StatusCode::Found);
|
|
|
|
httpResponse.set("Location", helpers::getTestServerURI() + redirectUri + '?' + params);
|
|
|
|
socket->sendAndShutdown(httpResponse);
|
2021-06-18 09:50:40 -05:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// CheckFileInfo - for redirected URI
|
|
|
|
else if (request.getMethod() == "GET" && regRedirected.match(uriReq.getPath()) && !regContents.match(uriReq.getPath()))
|
|
|
|
{
|
2024-03-03 08:03:35 -06:00
|
|
|
LOG_TST("FakeWOPIHost: Handling CheckFileInfo: (2/2)");
|
2021-06-18 09:50:40 -05:00
|
|
|
|
|
|
|
assertCheckFileInfoRequest(request);
|
|
|
|
|
2023-05-22 16:06:29 -05:00
|
|
|
LOK_ASSERT_MESSAGE("Expected to be in Phase::Redirected or Phase::Done",
|
|
|
|
_phase == Phase::Redirected || _phase == Phase::Done);
|
2021-06-21 07:23:18 -05:00
|
|
|
if (_phase == Phase::Redirected)
|
2023-05-22 16:06:29 -05:00
|
|
|
TRANSITION_STATE(_phase, Phase::GetFile);
|
2021-06-21 07:23:18 -05:00
|
|
|
|
2023-05-30 06:20:10 -05:00
|
|
|
Poco::JSON::Object::Ptr fileInfo = getDefaultCheckFileInfoPayload(uriReq);
|
|
|
|
const std::string fileName(uriReq.getPath() == "/wopi/files/3" ? "he%llo.txt"
|
|
|
|
: "hello.txt");
|
2021-06-18 09:50:40 -05:00
|
|
|
fileInfo->set("BaseFileName", fileName);
|
|
|
|
|
|
|
|
std::ostringstream jsonStream;
|
|
|
|
fileInfo->stringify(jsonStream);
|
|
|
|
|
2023-01-16 17:38:59 -06:00
|
|
|
http::Response httpResponse(http::StatusCode::OK);
|
2022-02-10 07:35:46 -06:00
|
|
|
httpResponse.set("Last-Modified", Util::getHttpTime(getFileLastModifiedTime()));
|
2022-02-01 07:33:25 -06:00
|
|
|
httpResponse.setBody(jsonStream.str(), "application/json; charset=utf-8");
|
|
|
|
socket->sendAndShutdown(httpResponse);
|
2021-06-18 09:50:40 -05:00
|
|
|
|
2021-06-21 07:23:18 -05:00
|
|
|
return true;
|
|
|
|
}
|
2021-06-22 10:38:45 -05:00
|
|
|
// GetFile - first try
|
2021-06-21 07:23:18 -05:00
|
|
|
else if (request.getMethod() == "GET" && regContents.match(uriReq.getPath()))
|
|
|
|
{
|
2023-01-23 15:53:27 -06:00
|
|
|
LOG_TST("FakeWOPIHost: Handling GetFile: " << uriReq.getPath());
|
2021-06-21 07:23:18 -05:00
|
|
|
|
|
|
|
assertGetFileRequest(request);
|
|
|
|
|
2024-03-03 08:37:32 -06:00
|
|
|
LOK_ASSERT_STATE(_phase, Phase::GetFile);
|
2023-05-22 16:06:29 -05:00
|
|
|
TRANSITION_STATE(_phase, Phase::Redirected2);
|
2021-06-22 10:38:45 -05:00
|
|
|
|
2023-05-22 16:06:43 -05:00
|
|
|
http::Response httpResponse(http::StatusCode::Found);
|
|
|
|
httpResponse.set("Location", helpers::getTestServerURI() + redirectUri2 + '?' + params);
|
|
|
|
socket->sendAndShutdown(httpResponse);
|
2021-06-22 10:38:45 -05:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// GetFile - redirected
|
|
|
|
else if (request.getMethod() == "GET" && regContentsRedirected.match(uriReq.getPath()))
|
|
|
|
{
|
2023-01-23 15:53:27 -06:00
|
|
|
LOG_TST("FakeWOPIHost: Handling GetFile: " << uriReq.getPath());
|
2021-06-22 10:38:45 -05:00
|
|
|
|
|
|
|
assertGetFileRequest(request);
|
|
|
|
|
2024-03-03 08:37:32 -06:00
|
|
|
LOK_ASSERT_STATE(_phase, Phase::Redirected2);
|
2023-05-22 16:06:29 -05:00
|
|
|
TRANSITION_STATE(_phase, Phase::Done);
|
2021-06-21 07:23:18 -05:00
|
|
|
|
2023-01-16 17:38:59 -06:00
|
|
|
http::Response httpResponse(http::StatusCode::OK);
|
2022-02-10 07:35:46 -06:00
|
|
|
httpResponse.set("Last-Modified", Util::getHttpTime(getFileLastModifiedTime()));
|
|
|
|
httpResponse.setBody(getFileContent(), "text/plain; charset=utf-8");
|
2022-02-01 07:33:25 -06:00
|
|
|
socket->sendAndShutdown(httpResponse);
|
2021-06-21 07:23:18 -05:00
|
|
|
|
2021-06-18 09:50:40 -05:00
|
|
|
exitTest(TestResult::Ok);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return WopiTestServer::handleHttpRequest(request, message, socket);
|
|
|
|
}
|
|
|
|
|
|
|
|
void invokeWSDTest() override
|
|
|
|
{
|
|
|
|
switch (_phase)
|
|
|
|
{
|
|
|
|
case Phase::Load:
|
|
|
|
{
|
|
|
|
initWebsocket("/wopi/files/1?" + params);
|
|
|
|
|
|
|
|
WSD_CMD("load url=" + getWopiSrc());
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Phase::Redirected:
|
2021-06-22 10:38:45 -05:00
|
|
|
case Phase::Redirected2:
|
2021-06-21 07:23:18 -05:00
|
|
|
case Phase::GetFile:
|
2023-05-22 16:06:29 -05:00
|
|
|
case Phase::Done:
|
2021-06-21 07:23:18 -05:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2021-06-18 09:50:40 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-05-22 16:54:01 -05:00
|
|
|
class UnitWopiHttpRedirectLoop : public WopiTestServer
|
|
|
|
{
|
|
|
|
STATE_ENUM(Phase, Load, Redirected) _phase;
|
|
|
|
|
|
|
|
const std::string params = "access_token=anything";
|
|
|
|
|
|
|
|
public:
|
|
|
|
UnitWopiHttpRedirectLoop()
|
|
|
|
: WopiTestServer("UnitWopiHttpRedirectLoop")
|
|
|
|
, _phase(Phase::Load)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool handleHttpRequest(const Poco::Net::HTTPRequest& request,
|
|
|
|
Poco::MemoryInputStream& /*message*/,
|
|
|
|
std::shared_ptr<StreamSocket>& socket) override
|
|
|
|
{
|
|
|
|
Poco::URI uriReq(request.getURI());
|
|
|
|
Poco::RegularExpression regInfo("/wopi/files/[0-9]+");
|
|
|
|
static unsigned redirectionCount = 0;
|
|
|
|
|
2024-03-03 08:03:35 -06:00
|
|
|
LOG_TST("FakeWOPIHost: Request URI [" << uriReq.toString() << "]:\n");
|
2023-05-22 16:54:01 -05:00
|
|
|
|
|
|
|
// CheckFileInfo - always returns redirect response
|
|
|
|
if (request.getMethod() == "GET" && regInfo.match(uriReq.getPath()))
|
|
|
|
{
|
2024-03-03 08:03:35 -06:00
|
|
|
LOG_TST("FakeWOPIHost: Handling CheckFileInfo");
|
2023-05-22 16:54:01 -05:00
|
|
|
|
|
|
|
assertCheckFileInfoRequest(request);
|
|
|
|
|
|
|
|
std::string sExpectedMessage = "It is expected to stop requesting after " +
|
|
|
|
std::to_string(RedirectionLimit) + " redirections";
|
|
|
|
LOK_ASSERT_MESSAGE(sExpectedMessage, redirectionCount <= RedirectionLimit);
|
|
|
|
|
|
|
|
LOK_ASSERT_MESSAGE("Expected to be in Phase::Load or Phase::Redirected",
|
|
|
|
_phase == Phase::Load || _phase == Phase::Redirected);
|
|
|
|
|
|
|
|
if (redirectionCount == RedirectionLimit)
|
|
|
|
{
|
|
|
|
exitTest(TestResult::Ok);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
TRANSITION_STATE(_phase, Phase::Redirected);
|
|
|
|
|
|
|
|
http::Response httpResponse(http::StatusCode::Found);
|
|
|
|
const std::string location = helpers::getTestServerURI() + "/wopi/files/" +
|
|
|
|
std::to_string(redirectionCount) + '?' + params;
|
|
|
|
httpResponse.set("Location", location);
|
|
|
|
socket->sendAndShutdown(httpResponse);
|
|
|
|
|
|
|
|
redirectionCount++;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void invokeWSDTest() override
|
|
|
|
{
|
|
|
|
switch (_phase)
|
|
|
|
{
|
|
|
|
case Phase::Load:
|
|
|
|
{
|
|
|
|
initWebsocket("/wopi/files/0?" + params);
|
|
|
|
|
|
|
|
WSD_CMD("load url=" + getWopiSrc());
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case Phase::Redirected:
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
UnitBase** unit_create_wsd_multi(void)
|
|
|
|
{
|
|
|
|
return new UnitBase* [3]
|
|
|
|
{ new UnitWopiHttpRedirect(), new UnitWopiHttpRedirectLoop(), nullptr };
|
|
|
|
}
|
2021-06-18 09:50:40 -05:00
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|