1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 #include <sal/config.h>
16 #include <sal/log.hxx>
17 #include <rtl/ustrbuf.hxx>
18 #include <rtl/strbuf.hxx>
19 #include <osl/socket.hxx>
20 #include <osl/thread.hxx>
21 #include <unotest/bootstrapfixturebase.hxx>
22 #include <officecfg/Office/Linguistic.hxx>
24 #include <com/sun/star/beans/PropertyValue.hpp>
25 #include <com/sun/star/connection/XAcceptor.hpp>
26 #include <com/sun/star/connection/XConnector.hpp>
27 #include <com/sun/star/linguistic2/XProofreader.hpp>
28 #include <com/sun/star/linguistic2/ProofreadingResult.hpp>
30 using namespace ::com::sun::star::uno
;
34 class MockServerThread
: public ::osl::Thread
38 : m_aSocketAddr("localhost", 2022)
42 virtual void SAL_CALL
run()
44 if (m_aAcceptorSocket
.acceptConnection(m_aStreamSocket
) != osl_Socket_Ok
)
50 Sequence
<sal_Int8
> aBuffer(512);
51 sal_Int32 nTcpNoDelay
= sal_Int32(true);
52 m_aStreamSocket
.setOption(osl_Socket_OptionTcpNoDelay
, &nTcpNoDelay
, sizeof(nTcpNoDelay
),
55 nReadBytes
= m_aStreamSocket
.recv(aBuffer
.getArray(), aBuffer
.getLength());
58 std::string
aText(reinterpret_cast<const char*>(aBuffer
.getConstArray()), nReadBytes
);
60 if (aText
.find("POST /api/check") == std::string::npos
)
64 else if (aText
.find("Content-Type: application/json") == std::string::npos
)
79 "Server: MockServer\r\n"
80 "Cache-Control: no-cache\r\n"
81 "Content-Type: application/json\r\n"
83 "{\"check-positions\":[{\"offset\":15,\"length\":6,\"errorcode\":4711,\"type\":"
85 "\"severity\":1,\"proposals\":[\"Entwurf\",\"Entw\u00fcrfe\"]},"
86 "{\"offset\":22,\"length\":3,\"errorcode\":8221,\"type\":\"orth\",\"severity\":1}]}");
88 m_aStreamSocket
.write(aResponse
.getStr(), aResponse
.getLength());
89 m_aStreamSocket
.close();
94 OString
aResponse("HTTP/1.1 404 Not Found\r\n"
95 "Connection: Closed\r\n"
98 m_aStreamSocket
.write(aResponse
.getStr(), aResponse
.getLength());
99 m_aStreamSocket
.close();
104 m_aAcceptorSocket
.close();
110 m_aAcceptorSocket
.setOption(osl_Socket_OptionReuseAddr
, 1);
111 CPPUNIT_ASSERT(m_aAcceptorSocket
.bind(m_aSocketAddr
));
112 CPPUNIT_ASSERT(m_aAcceptorSocket
.listen());
116 ::osl::SocketAddr m_aSocketAddr
;
117 ::osl::AcceptorSocket m_aAcceptorSocket
;
118 ::osl::StreamSocket m_aStreamSocket
;
122 MockServerThread aMockServer
;
124 class TestRestProtocol
: public test::BootstrapFixtureBase
127 virtual void setUp() override
;
128 virtual void tearDown() override
;
131 CPPUNIT_TEST_SUITE(TestRestProtocol
);
132 CPPUNIT_TEST(testProofreading
);
133 CPPUNIT_TEST_SUITE_END();
135 void testProofreading();
138 void TestRestProtocol::testProofreading()
140 css::lang::Locale
aLocale("en", "US", "");
141 using LanguageToolCfg
= officecfg::Office::Linguistic::GrammarChecking::LanguageTool
;
142 auto batch(comphelper::ConfigurationChanges::create());
144 LanguageToolCfg::BaseURL::set("http://127.0.0.1:2022/api", batch
);
145 LanguageToolCfg::Username::set("hcastro", batch
);
146 LanguageToolCfg::ApiKey::set("hcvhcvhcv", batch
);
147 LanguageToolCfg::IsEnabled::set(true, batch
);
148 LanguageToolCfg::SSLCertVerify::set(false, batch
);
149 LanguageToolCfg::RestProtocol::set("duden", batch
);
153 CPPUNIT_ASSERT_EQUAL(OUString("duden"), *LanguageToolCfg::RestProtocol::get());
155 Reference
<::com::sun::star::linguistic2::XProofreader
> xProofreader(
156 m_xSFactory
->createInstance("com.sun.star.linguistic2.Proofreader"), UNO_QUERY
);
157 CPPUNIT_ASSERT(xProofreader
.is());
159 com::sun::star::linguistic2::ProofreadingResult aResult
160 = xProofreader
->doProofreading("id", "ths is a tst", aLocale
, 0, 0, {});
162 CPPUNIT_ASSERT_EQUAL(sal_Int32(2), aResult
.aErrors
.getLength());
165 void TestRestProtocol::setUp()
167 test::BootstrapFixtureBase::setUp();
170 aMockServer
.create();
171 osl::Thread::wait(std::chrono::seconds(1));
174 void TestRestProtocol::tearDown()
178 test::BootstrapFixtureBase::tearDown();
181 CPPUNIT_TEST_SUITE_REGISTRATION(TestRestProtocol
);
183 CPPUNIT_PLUGIN_IMPLEMENT();
185 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */