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

Jan Holesovsky kendy at collabora.com
Tue Apr 26 09:33:47 UTC 2016


 loolwsd/test/httpgettest.cpp   |   60 ++------------------------------------
 loolwsd/test/httpposttest.cpp  |   60 +-------------------------------------
 loolwsd/test/httptestutils.hpp |   64 +++++++++++++++++++++++++++++++++++++++++
 3 files changed, 71 insertions(+), 113 deletions(-)

New commits:
commit 2f561802b3cdb75367cf63bf267be920ec6c3bd3
Author: Jan Holesovsky <kendy at collabora.com>
Date:   Tue Apr 26 11:32:01 2016 +0200

    The branding files do not have to be present, skip them.
    
    Clean up tons of copy'n'paste when at that...

diff --git a/loolwsd/test/httpgettest.cpp b/loolwsd/test/httpgettest.cpp
index bb1862a..1daab67 100644
--- a/loolwsd/test/httpgettest.cpp
+++ b/loolwsd/test/httpgettest.cpp
@@ -26,6 +26,8 @@
 #include <Common.hpp>
 #include <Util.hpp>
 
+#include "httptestutils.hpp"
+
 /// Tests the HTTP GET API of loolwsd.
 class HTTPGetTest : public CPPUNIT_NS::TestFixture
 {
@@ -148,36 +150,8 @@ void HTTPGetTest::testScripts()
     Poco::StreamCopier::copyToString(rs, html);
 
     Poco::RegularExpression script("<script.*?src=\"(.*?)\"");
-    Poco::RegularExpression::MatchVec matches;
-    int offset = 0;
 
-    while (script.match(html, offset, matches) > 0)
-    {
-        CPPUNIT_ASSERT_EQUAL(2, (int)matches.size());
-        Poco::URI uriScript(html.substr(matches[1].offset, matches[1].length));
-        if (uriScript.getHost().empty())
-        {
-#if ENABLE_SSL
-            Poco::Net::HTTPSClientSession sessionScript(uri.getHost(), uri.getPort());
-#else
-            Poco::Net::HTTPClientSession sessionScript(uri.getHost(), uri.getPort());
-#endif
-            std::cout << "checking... " << uriScript.toString();
-            Poco::Net::HTTPRequest requestScript(Poco::Net::HTTPRequest::HTTP_GET, uriScript.toString());
-            sessionScript.sendRequest(requestScript);
-
-            Poco::Net::HTTPResponse responseScript;
-            sessionScript.receiveResponse(responseScript);
-            CPPUNIT_ASSERT_EQUAL(Poco::Net::HTTPResponse::HTTP_OK, responseScript.getStatus());
-            CPPUNIT_ASSERT_EQUAL(std::string("application/javascript"), responseScript.getContentType());
-            std::cout << " OK" << std::endl;
-        }
-        else
-        {
-            std::cout << "skip " << uriScript.toString() << std::endl;
-        }
-        offset = static_cast<int>(matches[0].offset + matches[0].length);
-    }
+    httptest::assertHTTPFilesExist(uri, script, html, "application/javascript");
 }
 
 void HTTPGetTest::testLinks()
@@ -201,34 +175,8 @@ void HTTPGetTest::testLinks()
 
     Poco::RegularExpression link("<link.*?href=\"(.*?)\"");
     Poco::RegularExpression::MatchVec matches;
-    int offset = 0;
 
-    while (link.match(html, offset, matches) > 0)
-    {
-        CPPUNIT_ASSERT_EQUAL(2, (int)matches.size());
-        Poco::URI uriLink(html.substr(matches[1].offset, matches[1].length));
-        if (uriLink.getHost().empty())
-        {
-#if ENABLE_SSL
-            Poco::Net::HTTPSClientSession sessionLink(uri.getHost(), uri.getPort());
-#else
-            Poco::Net::HTTPClientSession sessionLink(uri.getHost(), uri.getPort());
-#endif
-            std::cout << "checking... " << uriLink.toString();
-            Poco::Net::HTTPRequest requestLink(Poco::Net::HTTPRequest::HTTP_GET, uriLink.toString());
-            sessionLink.sendRequest(requestLink);
-
-            Poco::Net::HTTPResponse responseLink;
-            sessionLink.receiveResponse(responseLink);
-            CPPUNIT_ASSERT_EQUAL(Poco::Net::HTTPResponse::HTTP_OK, responseLink.getStatus());
-            std::cout << " OK" << std::endl;
-        }
-        else
-        {
-            std::cout << "skip " << uriLink.toString() << std::endl;
-        }
-        offset = static_cast<int>(matches[0].offset + matches[0].length);
-    }
+    httptest::assertHTTPFilesExist(uri, link, html);
 }
 
 CPPUNIT_TEST_SUITE_REGISTRATION(HTTPGetTest);
diff --git a/loolwsd/test/httpposttest.cpp b/loolwsd/test/httpposttest.cpp
index b8756d5..ce4f460 100644
--- a/loolwsd/test/httpposttest.cpp
+++ b/loolwsd/test/httpposttest.cpp
@@ -27,6 +27,7 @@
 #include <Util.hpp>
 
 #include "countloolkits.hpp"
+#include "httptestutils.hpp"
 
 /// Tests the HTTP POST API of loolwsd. The server has to be started manually before running this test.
 class HTTPPostTest : public CPPUNIT_NS::TestFixture
@@ -157,36 +158,8 @@ void HTTPPostTest::testScripts()
     Poco::StreamCopier::copyToString(rs, html);
 
     Poco::RegularExpression script("<script.*?src=\"(.*?)\"");
-    Poco::RegularExpression::MatchVec matches;
-    int offset = 0;
 
-    while (script.match(html, offset, matches) > 0)
-    {
-        CPPUNIT_ASSERT_EQUAL(2, (int)matches.size());
-        Poco::URI uriScript(html.substr(matches[1].offset, matches[1].length));
-        if (uriScript.getHost().empty())
-        {
-#if ENABLE_SSL
-            Poco::Net::HTTPSClientSession sessionScript(uri.getHost(), uri.getPort());
-#else
-            Poco::Net::HTTPClientSession sessionScript(uri.getHost(), uri.getPort());
-#endif
-            std::cout << "checking... " << uriScript.toString();
-            Poco::Net::HTTPRequest requestScript(Poco::Net::HTTPRequest::HTTP_GET, uriScript.toString());
-            sessionScript.sendRequest(requestScript);
-
-            Poco::Net::HTTPResponse responseScript;
-            sessionScript.receiveResponse(responseScript);
-            CPPUNIT_ASSERT_EQUAL(Poco::Net::HTTPResponse::HTTP_OK, responseScript.getStatus());
-            CPPUNIT_ASSERT_EQUAL(std::string("application/javascript"), responseScript.getContentType());
-            std::cout << " OK" << std::endl;
-        }
-        else
-        {
-            std::cout << "skip " << uriScript.toString() << std::endl;
-        }
-        offset = static_cast<int>(matches[0].offset + matches[0].length);
-    }
+    httptest::assertHTTPFilesExist(uri, script, html, "application/javascript");
 }
 
 void HTTPPostTest::testLinks()
@@ -212,35 +185,8 @@ void HTTPPostTest::testLinks()
     Poco::StreamCopier::copyToString(rs, html);
 
     Poco::RegularExpression link("<link.*?href=\"(.*?)\"");
-    Poco::RegularExpression::MatchVec matches;
-    int offset = 0;
 
-    while (link.match(html, offset, matches) > 0)
-    {
-        CPPUNIT_ASSERT_EQUAL(2, (int)matches.size());
-        Poco::URI uriLink(html.substr(matches[1].offset, matches[1].length));
-        if (uriLink.getHost().empty())
-        {
-#if ENABLE_SSL
-            Poco::Net::HTTPSClientSession sessionLink(uri.getHost(), uri.getPort());
-#else
-            Poco::Net::HTTPClientSession sessionLink(uri.getHost(), uri.getPort());
-#endif
-            std::cout << "checking... " << uriLink.toString();
-            Poco::Net::HTTPRequest requestLink(Poco::Net::HTTPRequest::HTTP_GET, uriLink.toString());
-            sessionLink.sendRequest(requestLink);
-
-            Poco::Net::HTTPResponse responseLink;
-            sessionLink.receiveResponse(responseLink);
-            CPPUNIT_ASSERT_EQUAL(Poco::Net::HTTPResponse::HTTP_OK, responseLink.getStatus());
-            std::cout << " OK" << std::endl;
-        }
-        else
-        {
-            std::cout << "skip " << uriLink.toString() << std::endl;
-        }
-        offset = static_cast<int>(matches[0].offset + matches[0].length);
-    }
+    httptest::assertHTTPFilesExist(uri, link, html);
 }
 
 void HTTPPostTest::testConvertTo()
diff --git a/loolwsd/test/httptestutils.hpp b/loolwsd/test/httptestutils.hpp
new file mode 100644
index 0000000..c6fa398
--- /dev/null
+++ b/loolwsd/test/httptestutils.hpp
@@ -0,0 +1,64 @@
+/* -*- 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/.
+ */
+
+#ifndef TEST_HTTPTESTUTILS_HPP
+#define TEST_HTTPTESTUTILS_HPP
+
+namespace httptest
+{
+
+inline void assertHTTPFilesExist(const Poco::URI& uri, Poco::RegularExpression& expr, const std::string& html, const std::string& mimetype = std::string())
+{
+    Poco::RegularExpression::MatchVec matches;
+
+    for (int offset = 0; expr.match(html, offset, matches) > 0; offset = static_cast<int>(matches[0].offset + matches[0].length))
+    {
+	CPPUNIT_ASSERT_EQUAL(2, (int)matches.size());
+	Poco::URI uriScript(html.substr(matches[1].offset, matches[1].length));
+	if (uriScript.getHost().empty())
+	{
+	    std::string scriptString(uriScript.toString());
+
+	    // ignore the branding bits, they do not have to be there
+	    if (scriptString.find("/branding.") != std::string::npos)
+	    {
+		std::cout << "skipping test for... " << scriptString << std::endl;
+		continue;
+	    }
+
+#if ENABLE_SSL
+	    Poco::Net::HTTPSClientSession sessionScript(uri.getHost(), uri.getPort());
+#else
+	    Poco::Net::HTTPClientSession sessionScript(uri.getHost(), uri.getPort());
+#endif
+	    std::cout << "checking... " << scriptString;
+	    Poco::Net::HTTPRequest requestScript(Poco::Net::HTTPRequest::HTTP_GET, scriptString);
+	    sessionScript.sendRequest(requestScript);
+
+	    Poco::Net::HTTPResponse responseScript;
+	    sessionScript.receiveResponse(responseScript);
+	    CPPUNIT_ASSERT_EQUAL(Poco::Net::HTTPResponse::HTTP_OK, responseScript.getStatus());
+
+	    if (!mimetype.empty())
+		CPPUNIT_ASSERT_EQUAL(mimetype, responseScript.getContentType());
+
+	    std::cout << " OK" << std::endl;
+	}
+	else
+	{
+	    std::cout << "skip " << uriScript.toString() << std::endl;
+	}
+    }
+}
+
+}
+
+#endif // TEST_HTTPTESTUTILS_HPP
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */


More information about the Libreoffice-commits mailing list