diff options
author | Jan Holesovsky <kendy@collabora.com> | 2017-09-26 16:12:58 +0200 |
---|---|---|
committer | Jan Holesovsky <kendy@collabora.com> | 2017-09-27 16:36:21 +0200 |
commit | a711d5b60c65042585ff85b574a526bac4ae0647 (patch) | |
tree | fb6b3bdbe610d37c829e0cfd72c7d54116257fe3 | |
parent | f658067eaa49b55b634615da157a107582e6fbd0 (diff) |
Separate the fake wopi server to an own class.
Change-Id: Ibb1b06c491be0065aa12a05a43959165d6c86398
Reviewed-on: https://gerrit.libreoffice.org/42853
Reviewed-by: pranavk <pranavk@collabora.co.uk>
Tested-by: pranavk <pranavk@collabora.co.uk>
-rw-r--r-- | test/UnitOAuth.cpp | 106 | ||||
-rw-r--r-- | test/WopiTestServer.hpp | 115 |
2 files changed, 135 insertions, 86 deletions
diff --git a/test/UnitOAuth.cpp b/test/UnitOAuth.cpp index 7a52c1ee2..baf05c0e9 100644 --- a/test/UnitOAuth.cpp +++ b/test/UnitOAuth.cpp @@ -9,24 +9,18 @@ #include "config.h" -//#include "Exceptions.hpp" +#include "WopiTestServer.hpp" #include "Log.hpp" #include "Unit.hpp" #include "UnitHTTP.hpp" #include "helpers.hpp" -#include <Poco/JSON/Object.h> -#include <Poco/LocalDateTime.h> -#include <Poco/DateTimeFormat.h> -#include <Poco/DateTimeFormatter.h> #include <Poco/Net/HTTPRequest.h> #include <Poco/Net/OAuth20Credentials.h> #include <Poco/Util/LayeredConfiguration.h> -using Poco::DateTimeFormatter; -using Poco::DateTimeFormat; using Poco::Net::OAuth20Credentials; -class UnitOAuth : public UnitWSD +class UnitOAuth : public WopiTestServer { enum class Phase { @@ -46,6 +40,7 @@ public: { } + /// The actual assert of the authentication. void assertRequest(const Poco::Net::HTTPRequest& request, int fileIndex) { // check that the request contains the Authorization: header @@ -68,91 +63,30 @@ public: } } - /// Here we act as a WOPI server, so that we have a server that responds to - /// the wopi requests without additional expensive setup. - virtual bool handleHttpRequest(const Poco::Net::HTTPRequest& request, std::shared_ptr<StreamSocket>& socket) override + void assertCheckFileInfoRequest(const Poco::Net::HTTPRequest& request) override { - static const std::string hello("Hello, world"); - - Poco::URI uriReq(request.getURI()); - LOG_INF("Fake wopi host request: " << uriReq.toString()); + std::string path = Poco::URI(request.getURI()).getPath(); + assertRequest(request, (path == "/wopi/files/0")? 0: 1); + } - // CheckFileInfo - if (uriReq.getPath() == "/wopi/files/0" || uriReq.getPath() == "/wopi/files/1") + void assertGetFileRequest(const Poco::Net::HTTPRequest& request) override + { + std::string path = Poco::URI(request.getURI()).getPath(); + if (path == "/wopi/files/0/contents") { - LOG_INF("Fake wopi host request, handling CheckFileInfo: " << uriReq.getPath()); - - assertRequest(request, (uriReq.getPath() == "/wopi/files/0")? 0: 1); - - Poco::LocalDateTime now; - Poco::JSON::Object::Ptr fileInfo = new Poco::JSON::Object(); - fileInfo->set("BaseFileName", "hello.txt"); - fileInfo->set("Size", hello.size()); - 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"); - fileInfo->set("LastModifiedTime", DateTimeFormatter::format(now, DateTimeFormat::ISO8601_FORMAT)); - - std::ostringstream jsonStream; - fileInfo->stringify(jsonStream); - std::string responseString = jsonStream.str(); - - const std::string mimeType = "application/json; charset=utf-8"; - - 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" - << "Content-Length: " << responseString.size() << "\r\n" - << "Content-Type: " << mimeType << "\r\n" - << "\r\n" - << responseString; - - socket->send(oss.str()); - socket->shutdown(); - - return true; + assertRequest(request, 0); + _finishedToken = true; } - // GetFile - else if (uriReq.getPath() == "/wopi/files/0/contents" || uriReq.getPath() == "/wopi/files/1/contents") + else { - LOG_INF("Fake wopi host request, handling GetFile: " << uriReq.getPath()); - - if (uriReq.getPath() == "/wopi/files/0/contents") - { - assertRequest(request, 0); - _finishedToken = true; - } - else - { - assertRequest(request, 1); - _finishedHeader = true; - } - - const std::string mimeType = "text/plain; charset=utf-8"; - - 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" - << "Content-Length: " << hello.size() << "\r\n" - << "Content-Type: " << mimeType << "\r\n" - << "\r\n" - << hello; - - socket->send(oss.str()); - socket->shutdown(); - - if (_finishedToken && _finishedHeader) - exitTest(TestResult::Ok); - - return true; + assertRequest(request, 1); + _finishedHeader = true; } + } - return false; + bool wopiServerFinish() override + { + return _finishedToken && _finishedHeader; } void invokeTest() override diff --git a/test/WopiTestServer.hpp b/test/WopiTestServer.hpp new file mode 100644 index 000000000..63cf4c7f4 --- /dev/null +++ b/test/WopiTestServer.hpp @@ -0,0 +1,115 @@ +/* -*- 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 "Log.hpp" +#include "Unit.hpp" +#include "UnitHTTP.hpp" +#include <Poco/DateTimeFormat.h> +#include <Poco/DateTimeFormatter.h> +#include <Poco/JSON/Object.h> +#include <Poco/Net/HTTPRequest.h> +#include <Poco/URI.h> + +class WopiTestServer : public UnitWSD +{ +public: + WopiTestServer() : UnitWSD() + { + } + + virtual void assertCheckFileInfoRequest(const Poco::Net::HTTPRequest& request) = 0; + + virtual void assertGetFileRequest(const Poco::Net::HTTPRequest& request) = 0; + + virtual bool wopiServerFinish() = 0; + +protected: + /// Here we act as a WOPI server, so that we have a server that responds to + /// the wopi requests without additional expensive setup. + virtual bool handleHttpRequest(const Poco::Net::HTTPRequest& request, std::shared_ptr<StreamSocket>& socket) override + { + static const std::string hello("Hello, world"); + + Poco::URI uriReq(request.getURI()); + LOG_INF("Fake wopi host request: " << uriReq.toString()); + + // CheckFileInfo + if (uriReq.getPath() == "/wopi/files/0" || uriReq.getPath() == "/wopi/files/1") + { + LOG_INF("Fake wopi host request, handling CheckFileInfo: " << uriReq.getPath()); + + assertCheckFileInfoRequest(request); + + Poco::LocalDateTime now; + Poco::JSON::Object::Ptr fileInfo = new Poco::JSON::Object(); + fileInfo->set("BaseFileName", "hello.txt"); + fileInfo->set("Size", hello.size()); + 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"); + fileInfo->set("LastModifiedTime", Poco::DateTimeFormatter::format(now, Poco::DateTimeFormat::ISO8601_FORMAT)); + + std::ostringstream jsonStream; + fileInfo->stringify(jsonStream); + std::string responseString = jsonStream.str(); + + const std::string mimeType = "application/json; charset=utf-8"; + + 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" + << "Content-Length: " << responseString.size() << "\r\n" + << "Content-Type: " << mimeType << "\r\n" + << "\r\n" + << responseString; + + socket->send(oss.str()); + socket->shutdown(); + + return true; + } + // GetFile + else if (uriReq.getPath() == "/wopi/files/0/contents" || uriReq.getPath() == "/wopi/files/1/contents") + { + LOG_INF("Fake wopi host request, handling GetFile: " << uriReq.getPath()); + + assertGetFileRequest(request); + + const std::string mimeType = "text/plain; charset=utf-8"; + + 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" + << "Content-Length: " << hello.size() << "\r\n" + << "Content-Type: " << mimeType << "\r\n" + << "\r\n" + << hello; + + socket->send(oss.str()); + socket->shutdown(); + + if (wopiServerFinish()) + exitTest(TestResult::Ok); + + return true; + } + + return false; + } + +}; + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |