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 <svtools/languagetoolcfg.hxx>
22 #include <unotest/bootstrapfixturebase.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 Sequence
<::com::sun::star::beans::PropertyValue
> aProperties
;
142 SvxLanguageToolOptions
& rLanguageOpts
= SvxLanguageToolOptions::Get();
143 rLanguageOpts
.setBaseURL("http://127.0.0.1:2022/api");
144 rLanguageOpts
.setUsername("hcastro");
145 rLanguageOpts
.setApiKey("hcvhcvhcv");
146 rLanguageOpts
.setEnabled(true);
147 rLanguageOpts
.setSSLVerification(false);
148 rLanguageOpts
.setRestProtocol("duden");
149 CPPUNIT_ASSERT_EQUAL(OUString("duden"), rLanguageOpts
.getRestProtocol());
151 Reference
<::com::sun::star::linguistic2::XProofreader
> xProofreader(
152 m_xSFactory
->createInstance("com.sun.star.linguistic2.Proofreader"), UNO_QUERY
);
153 CPPUNIT_ASSERT(xProofreader
.is());
155 com::sun::star::linguistic2::ProofreadingResult aResult
156 = xProofreader
->doProofreading("id", "ths is a tst", aLocale
, 0, 0, aProperties
);
158 CPPUNIT_ASSERT_EQUAL(2, aResult
.aErrors
.getLength());
161 void TestRestProtocol::setUp()
163 test::BootstrapFixtureBase::setUp();
166 aMockServer
.create();
167 osl::Thread::wait(std::chrono::seconds(1));
170 void TestRestProtocol::tearDown()
174 test::BootstrapFixtureBase::tearDown();
177 CPPUNIT_TEST_SUITE_REGISTRATION(TestRestProtocol
);
179 CPPUNIT_PLUGIN_IMPLEMENT();
181 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */