[Libreoffice-commits] online.git: loolwsd/test

Ashod Nakashian ashod.nakashian at collabora.co.uk
Thu Apr 28 22:20:30 UTC 2016


 loolwsd/test/httpwstest.cpp |   97 +++++++++++++++++++++++++++++++-------------
 1 file changed, 69 insertions(+), 28 deletions(-)

New commits:
commit cf54d0d87e9acd38e5541959105bdfec1493d875
Author: Ashod Nakashian <ashod.nakashian at collabora.co.uk>
Date:   Tue Apr 26 23:46:09 2016 -0400

    loolwsd: test helpers and more code sharing
    
    Change-Id: I87e4ad23f9c2761a7a2acab4139b458696e07e64
    Reviewed-on: https://gerrit.libreoffice.org/24468
    Reviewed-by: Ashod Nakashian <ashnakash at gmail.com>
    Tested-by: Ashod Nakashian <ashnakash at gmail.com>

diff --git a/loolwsd/test/httpwstest.cpp b/loolwsd/test/httpwstest.cpp
index 5755a8d..31350a2 100644
--- a/loolwsd/test/httpwstest.cpp
+++ b/loolwsd/test/httpwstest.cpp
@@ -119,6 +119,10 @@ class HTTPWSTest : public CPPUNIT_NS::TestFixture
                             std::string& response,
                             const bool isLine);
 
+    static
+    void SocketProcessor(const std::shared_ptr<Poco::Net::WebSocket>& socket,
+                         std::function<bool(const std::string& msg)> handler);
+
     void checkTiles(Poco::Net::WebSocket& socket,
                     const std::string& type);
 
@@ -137,6 +141,8 @@ class HTTPWSTest : public CPPUNIT_NS::TestFixture
     connectLOKit(Poco::Net::HTTPRequest& request,
                  Poco::Net::HTTPResponse& response);
 
+    std::shared_ptr<Poco::Net::WebSocket> loadDocAndGetSocket(const std::string& documentURL);
+
 public:
     HTTPWSTest()
 #if ENABLE_SSL
@@ -347,38 +353,28 @@ void HTTPWSTest::loadDoc(const std::string& documentURL)
     try
     {
         // Load a document and get its status.
-        Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, documentURL);
-        Poco::Net::WebSocket socket = *connectLOKit(request, _response);
-
-        sendTextFrame(socket, "load url=" + documentURL);
-        sendTextFrame(socket, "status");
-        CPPUNIT_ASSERT_MESSAGE("cannot load the document " + documentURL, isDocumentLoaded(socket));
+        auto socket = loadDocAndGetSocket(documentURL);
 
         std::string status;
-        int flags;
-        int n;
-        do
-        {
-            char buffer[READ_BUFFER_SIZE];
-            n = socket.receiveFrame(buffer, sizeof(buffer), flags);
-            std::cout << "Got " << n << " bytes, flags: " << std::hex << flags << std::dec << std::endl;
-            if (n > 0 && (flags & Poco::Net::WebSocket::FRAME_OP_BITMASK) != Poco::Net::WebSocket::FRAME_OP_CLOSE)
-            {
-                std::cout << "Received message: " << LOOLProtocol::getAbbreviatedMessage(buffer, n) << std::endl;
-                const std::string line = LOOLProtocol::getFirstLine(buffer, n);
-                const std::string prefix = "status: ";
-                if (line.find(prefix) == 0)
+        std::string editlock;
+        SocketProcessor(socket, [&](const std::string& msg)
                 {
-                    status = line.substr(prefix.length());
-                    // Might be too strict, consider something flexible instread.
-                    CPPUNIT_ASSERT_EQUAL(std::string("type=text parts=1 current=0 width=12808 height=16408"), status);
-                    break;
-                }
-            }
-        }
-        while (n > 0 && (flags & Poco::Net::WebSocket::FRAME_OP_BITMASK) != Poco::Net::WebSocket::FRAME_OP_CLOSE);
+                    const std::string prefix = "status: ";
+                    if (msg.find(prefix) == 0)
+                    {
+                        status = msg.substr(prefix.length());
+                        return false;
+                    }
 
-        socket.shutdown();
+                    return true;
+                });
+
+        // Might be too strict, consider something flexible instread.
+        CPPUNIT_ASSERT_EQUAL(std::string("type=text parts=1 current=0 width=12808 height=16408"), status);
+        // First session always gets the lock.
+        CPPUNIT_ASSERT_EQUAL(std::string("editlock: 1"), editlock);
+
+        socket->shutdown();
     }
     catch (const Poco::Exception& exc)
     {
@@ -1560,6 +1556,51 @@ HTTPWSTest::connectLOKit(Poco::Net::HTTPRequest& request,
     return ws;
 }
 
+std::shared_ptr<Poco::Net::WebSocket> HTTPWSTest::loadDocAndGetSocket(const std::string& documentURL)
+{
+    try
+    {
+        // Load a document and get its status.
+        Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_GET, documentURL);
+        auto socket = connectLOKit(request, _response);
+
+        sendTextFrame(*socket, "load url=" + documentURL);
+        sendTextFrame(*socket, "status");
+        CPPUNIT_ASSERT_MESSAGE("cannot load the document " + documentURL, isDocumentLoaded(*socket));
+
+        return socket;
+    }
+    catch (const Poco::Exception& exc)
+    {
+        CPPUNIT_FAIL(exc.displayText());
+    }
+
+    // Really couldn't reach here, but the compiler doesn't know any better.
+    return nullptr;
+}
+
+
+void HTTPWSTest::SocketProcessor(const std::shared_ptr<Poco::Net::WebSocket>& socket, std::function<bool(const std::string& msg)> handler)
+{
+    int flags;
+    int n;
+    do
+    {
+        char buffer[READ_BUFFER_SIZE];
+        n = socket->receiveFrame(buffer, sizeof(buffer), flags);
+        std::cout << "Got " << n << " bytes, flags: " << std::hex << flags << std::dec << std::endl;
+        if (n > 0 && (flags & Poco::Net::WebSocket::FRAME_OP_BITMASK) != Poco::Net::WebSocket::FRAME_OP_CLOSE)
+        {
+            std::cout << "Received message: " << LOOLProtocol::getAbbreviatedMessage(buffer, n) << std::endl;
+            if (!handler(std::string(buffer, n)))
+            {
+                break;
+            }
+        }
+    }
+    while (n > 0 && (flags & Poco::Net::WebSocket::FRAME_OP_BITMASK) != Poco::Net::WebSocket::FRAME_OP_CLOSE);
+}
+
 CPPUNIT_TEST_SUITE_REGISTRATION(HTTPWSTest);
 
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */


More information about the Libreoffice-commits mailing list