diff options
author | Jan Holesovsky <kendy@collabora.com> | 2017-09-27 14:13:43 +0200 |
---|---|---|
committer | Jan Holesovsky <kendy@collabora.com> | 2017-09-27 16:37:16 +0200 |
commit | 3141cfc99ba2e2fb3878e95373a2fa48a76205a5 (patch) | |
tree | 1216c16e3f4163fcc55f56eb89cfd90e94aa5fc7 | |
parent | Separate the fake wopi server to an own class. (diff) | |
download | online-3141cfc99ba2e2fb3878e95373a2fa48a76205a5.tar.gz online-3141cfc99ba2e2fb3878e95373a2fa48a76205a5.zip |
PutFile ext: X-LOOL-WOPI-IsModifiedByUser unit test.
Change-Id: I0b1ffc74dbbc771f0dcb68f87d46af3ba469ae9e
Reviewed-on: https://gerrit.libreoffice.org/42855
Reviewed-by: pranavk <pranavk@collabora.co.uk>
Tested-by: pranavk <pranavk@collabora.co.uk>
-rw-r--r-- | test/Makefile.am | 7 | ||||
-rw-r--r-- | test/UnitOAuth.cpp | 13 | ||||
-rw-r--r-- | test/UnitWOPI.cpp | 129 | ||||
-rw-r--r-- | test/WopiTestServer.hpp | 22 |
4 files changed, 159 insertions, 12 deletions
diff --git a/test/Makefile.am b/test/Makefile.am index 5709496785..7ee4bcf2ba 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -16,7 +16,8 @@ noinst_LTLIBRARIES = \ unit-timeout.la unit-prefork.la \ unit-storage.la unit-client.la \ unit-admin.la unit-tilecache.la \ - unit-fuzz.la unit-oob.la unit-oauth.la + unit-fuzz.la unit-oob.la unit-oauth.la \ + unit-wopi.la MAGIC_TO_FORCE_SHLIB_CREATION = -rpath /dummy AM_LDFLAGS = -pthread -module $(MAGIC_TO_FORCE_SHLIB_CREATION) $(ZLIB_LIBS) @@ -77,6 +78,8 @@ unit_storage_la_SOURCES = UnitStorage.cpp unit_tilecache_la_SOURCES = UnitTileCache.cpp unit_oauth_la_SOURCES = UnitOAuth.cpp unit_oauth_la_LIBADD = $(CPPUNIT_LIBS) +unit_wopi_la_SOURCES = UnitWOPI.cpp +unit_wopi_la_LIBADD = $(CPPUNIT_LIBS) if HAVE_LO_PATH SYSTEM_STAMP = @SYSTEMPLATE_PATH@/system_stamp @@ -90,7 +93,7 @@ check-local: ./run_unit.sh --log-file test.log --trs-file test.trs # FIXME 2: unit-oob.la fails with symbol undefined: # UnitWSD::testHandleRequest(UnitWSD::TestRequest, UnitHTTPServerRequest&, UnitHTTPServerResponse&) , -TESTS = unit-prefork.la unit-tilecache.la unit-timeout.la unit-oauth.la +TESTS = unit-prefork.la unit-tilecache.la unit-timeout.la unit-oauth.la unit-wopi.la # TESTS = unit-client.la # TESTS += unit-admin.la # TESTS += unit-storage.la diff --git a/test/UnitOAuth.cpp b/test/UnitOAuth.cpp index baf05c0e98..dfba4aa135 100644 --- a/test/UnitOAuth.cpp +++ b/test/UnitOAuth.cpp @@ -26,7 +26,7 @@ class UnitOAuth : public WopiTestServer { LoadToken, // loading the document with Bearer token LoadHeader, // loading the document with Basic auth - Polling // let the loading progress, and when it succeeds, finish + Finish // assert all went fine and finish } _phase; bool _finishedToken; @@ -84,9 +84,9 @@ public: } } - bool wopiServerFinish() override + void assertPutFileRequest(const Poco::Net::HTTPRequest& /*request*/) override { - return _finishedToken && _finishedHeader; + // nothing to assert } void invokeTest() override @@ -116,12 +116,13 @@ public: if (_phase == Phase::LoadToken) _phase = Phase::LoadHeader; else - _phase = Phase::Polling; + _phase = Phase::Finish; break; } - case Phase::Polling: + case Phase::Finish: { - // let handleHttpRequest() perform the checks... + CPPUNIT_ASSERT(_finishedToken && _finishedHeader); + exitTest(TestResult::Ok); break; } } diff --git a/test/UnitWOPI.cpp b/test/UnitWOPI.cpp new file mode 100644 index 0000000000..fade8d0f6e --- /dev/null +++ b/test/UnitWOPI.cpp @@ -0,0 +1,129 @@ +/* -*- 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 "config.h" + +#include "WopiTestServer.hpp" +#include "Log.hpp" +#include "Unit.hpp" +#include "UnitHTTP.hpp" +#include "helpers.hpp" +#include <Poco/Net/HTTPRequest.h> +#include <Poco/Util/LayeredConfiguration.h> + +class UnitWOPI : public WopiTestServer +{ + enum class Phase + { + LoadAndSave, + Modify, + SaveModified, + Finish + } _phase; + + enum class SavingPhase + { + Unmodified, + Modified + } _savingPhase; + + bool _finishedSaveUnmodified; + bool _finishedSaveModified; + + std::unique_ptr<UnitWebSocket> _ws; + +public: + UnitWOPI() : + _phase(Phase::LoadAndSave), + _finishedSaveUnmodified(false), + _finishedSaveModified(false) + { + } + + void assertCheckFileInfoRequest(const Poco::Net::HTTPRequest& /*request*/) override + { + // nothing to assert in CheckFileInfo + } + + void assertGetFileRequest(const Poco::Net::HTTPRequest& /*request*/) override + { + // nothing to assert in GetFile + } + + void assertPutFileRequest(const Poco::Net::HTTPRequest& request) override + { + if (_savingPhase == SavingPhase::Unmodified) + { + CPPUNIT_ASSERT_EQUAL(std::string("false"), request.get("X-LOOL-WOPI-IsModifiedByUser")); + _finishedSaveUnmodified = true; + } + else if (_savingPhase == SavingPhase::Modified) + { + CPPUNIT_ASSERT_EQUAL(std::string("true"), request.get("X-LOOL-WOPI-IsModifiedByUser")); + _finishedSaveModified = true; + } + } + + void invokeTest() override + { + constexpr char testName[] = "UnitWOPI"; + + switch (_phase) + { + case Phase::LoadAndSave: + { + Poco::URI wopiURL(helpers::getTestServerURI() + "/wopi/files/0?access_token=anything"); + std::string wopiSrc; + Poco::URI::encode(wopiURL.toString(), ":/?", wopiSrc); + Poco::URI loolUri(helpers::getTestServerURI()); + + LOG_INF("Connecting to the fake WOPI server: /lool/" << wopiSrc << "/ws"); + + _ws.reset(new UnitWebSocket("/lool/" + wopiSrc + "/ws")); + assert(_ws.get()); + + helpers::sendTextFrame(*_ws->getLOOLWebSocket(), "load url=" + wopiSrc, testName); + helpers::sendTextFrame(*_ws->getLOOLWebSocket(), "save dontTerminateEdit=1 dontSaveIfUnmodified=0", testName); + + _phase = Phase::Modify; + _savingPhase = SavingPhase::Unmodified; + break; + } + case Phase::Modify: + { + helpers::sendTextFrame(*_ws->getLOOLWebSocket(), "key type=input char=97 key=0", testName); + helpers::sendTextFrame(*_ws->getLOOLWebSocket(), "key type=up char=0 key=512", testName); + + _phase = Phase::SaveModified; + break; + } + case Phase::SaveModified: + { + helpers::sendTextFrame(*_ws->getLOOLWebSocket(), "save dontTerminateEdit=0 dontSaveIfUnmodified=0", testName); + + _phase = Phase::Finish; + _savingPhase = SavingPhase::Modified; + break; + } + case Phase::Finish: + { + CPPUNIT_ASSERT(_finishedSaveUnmodified && _finishedSaveModified); + exitTest(TestResult::Ok); + break; + } + } + } +}; + +UnitBase *unit_create_wsd(void) +{ + return new UnitWOPI(); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/test/WopiTestServer.hpp b/test/WopiTestServer.hpp index 63cf4c7f4a..5ac79e8196 100644 --- a/test/WopiTestServer.hpp +++ b/test/WopiTestServer.hpp @@ -29,7 +29,7 @@ public: virtual void assertGetFileRequest(const Poco::Net::HTTPRequest& request) = 0; - virtual bool wopiServerFinish() = 0; + virtual void assertPutFileRequest(const Poco::Net::HTTPRequest& request) = 0; protected: /// Here we act as a WOPI server, so that we have a server that responds to @@ -81,7 +81,7 @@ protected: return true; } // GetFile - else if (uriReq.getPath() == "/wopi/files/0/contents" || uriReq.getPath() == "/wopi/files/1/contents") + else if (request.getMethod() == "GET" && (uriReq.getPath() == "/wopi/files/0/contents" || uriReq.getPath() == "/wopi/files/1/contents")) { LOG_INF("Fake wopi host request, handling GetFile: " << uriReq.getPath()); @@ -101,8 +101,22 @@ protected: socket->send(oss.str()); socket->shutdown(); - if (wopiServerFinish()) - exitTest(TestResult::Ok); + return true; + } + else if (request.getMethod() == "POST" && (uriReq.getPath() == "/wopi/files/0/contents" || uriReq.getPath() == "/wopi/files/1/contents")) + { + LOG_INF("Fake wopi host request, handling PutFile: " << uriReq.getPath()); + + assertPutFileRequest(request); + + std::ostringstream oss; + oss << "HTTP/1.1 200 OK\r\n" + << "Last-Modified: " << Poco::DateTimeFormatter::format(Poco::Timestamp(), Poco::DateTimeFormat::HTTP_FORMAT) << "\r\n" + << "User-Agent: " << WOPI_AGENT_STRING << "\r\n" + << "\r\n"; + + socket->send(oss.str()); + socket->shutdown(); return true; } |