1
/*=============================================================================
3
===============================================================================
4
Test the method registry (server) C++ facilities of XML-RPC for C/C++.
6
=============================================================================*/
10
#include "xmlrpc-c/girerr.hpp"
13
#include "xmlrpc-c/base.hpp"
14
#include "xmlrpc-c/registry.hpp"
17
#include "registry.hpp"
19
using namespace xmlrpc_c;
24
xmlPrologue("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n");
27
apacheUrl("http://ws.apache.org/xmlrpc/namespaces/extensions");
30
xmlnsApache("xmlns:ex=\"" + apacheUrl + "\"");
34
string const noElementFoundXml(
36
"<methodResponse>\r\n"
39
"<member><name>faultCode</name>\r\n"
40
"<value><i4>-503</i4></value></member>\r\n"
41
"<member><name>faultString</name>\r\n"
42
"<value><string>Call XML not a proper XML-RPC call. "
43
"Call is not valid XML. no element found</string></value>"
45
"</struct></value>\r\n"
47
"</methodResponse>\r\n"
50
string const sampleAddGoodCallXml(
53
"<methodName>sample.add</methodName>\r\n"
55
"<param><value><i4>5</i4></value></param>\r\n"
56
"<param><value><i4>7</i4></value></param>\r\n"
61
string const sampleAddGoodResponseXml(
63
"<methodResponse>\r\n"
65
"<param><value><i4>12</i4></value></param>\r\n"
67
"</methodResponse>\r\n"
71
string const sampleAddBadCallXml(
74
"<methodName>sample.add</methodName>\r\n"
76
"<param><value><i4>5</i4></value></param>\r\n"
81
string const sampleAddBadResponseXml(
83
"<methodResponse>\r\n"
86
"<member><name>faultCode</name>\r\n"
87
"<value><i4>-501</i4></value></member>\r\n"
88
"<member><name>faultString</name>\r\n"
89
"<value><string>Not enough parameters</string></value></member>\r\n"
90
"</struct></value>\r\n"
92
"</methodResponse>\r\n"
96
string const nonexistentMethodCallXml(
99
"<methodName>nosuchmethod</methodName>\r\n"
101
"<param><value><i4>5</i4></value></param>\r\n"
102
"<param><value><i4>7</i4></value></param>\r\n"
107
string const nonexistentMethodYesDefResponseXml(
109
"<methodResponse>\r\n"
111
"<param><value><string>no such method: nosuchmethod</string>"
112
"</value></param>\r\n"
114
"</methodResponse>\r\n"
117
string const nonexistentMethodNoDefResponseXml(
119
"<methodResponse>\r\n"
121
"<value><struct>\r\n"
122
"<member><name>faultCode</name>\r\n"
123
"<value><i4>-506</i4></value></member>\r\n"
124
"<member><name>faultString</name>\r\n"
125
"<value><string>Method 'nosuchmethod' not defined</string></value>"
127
"</struct></value>\r\n"
129
"</methodResponse>\r\n"
135
string const echoI8ApacheCall(
137
"<methodCall " + xmlnsApache + ">\r\n"
138
"<methodName>echo</methodName>\r\n"
140
"<param><value><ex:i8>5</ex:i8></value></param>\r\n"
145
string const echoI8ApacheResponse(
147
"<methodResponse " + xmlnsApache + ">\r\n"
149
"<param><value><ex:i8>5</ex:i8></value></param>\r\n"
151
"</methodResponse>\r\n"
154
string const echoNilApacheCall(
156
"<methodCall " + xmlnsApache + ">\r\n"
157
"<methodName>echo</methodName>\r\n"
159
"<param><value><nil/></value></param>\r\n"
164
string const echoNilApacheResponse(
166
"<methodResponse " + xmlnsApache + ">\r\n"
168
"<param><value><ex:nil/></value></param>\r\n"
170
"</methodResponse>\r\n"
174
class sampleAddMethod : public method {
177
this->_signature = "i:ii";
178
this->_help = "This method adds two integers together";
181
execute(xmlrpc_c::paramList const& paramList,
182
value * const retvalP) {
184
int const addend(paramList.getInt(0));
185
int const adder(paramList.getInt(1));
187
paramList.verifyEnd(2);
189
*retvalP = value_int(addend + adder);
195
class nameMethod : public defaultMethod {
198
execute(string const& methodName,
199
xmlrpc_c::paramList const& , // paramList
200
value * const retvalP) {
202
*retvalP = value_string(string("no such method: ") + methodName);
208
class echoMethod : public method {
211
execute(xmlrpc_c::paramList const& paramList,
212
value * const retvalP) {
214
paramList.verifyEnd(1);
216
*retvalP = paramList[0];
222
class registryRegMethodTestSuite : public testSuite {
225
virtual string suiteName() {
226
return "registryRegMethodTestSuite";
228
virtual void runtests(unsigned int const) {
230
xmlrpc_c::registry myRegistry;
232
myRegistry.addMethod("sample.add",
233
xmlrpc_c::methodPtr(new sampleAddMethod));
235
myRegistry.disableIntrospection();
238
myRegistry.processCall("", &response);
239
TEST(response == noElementFoundXml);
243
myRegistry.processCall(sampleAddGoodCallXml, &response);
244
TEST(response == sampleAddGoodResponseXml);
248
myRegistry.processCall(sampleAddBadCallXml, &response);
249
TEST(response == sampleAddBadResponseXml);
256
class registryDefaultMethodTestSuite : public testSuite {
259
virtual string suiteName() {
260
return "registryDefaultMethodTestSuite";
262
virtual void runtests(unsigned int const) {
264
xmlrpc_c::registry myRegistry;
266
myRegistry.addMethod("sample.add", methodPtr(new sampleAddMethod));
270
myRegistry.processCall(sampleAddGoodCallXml, &response);
271
TEST(response == sampleAddGoodResponseXml);
275
myRegistry.processCall(nonexistentMethodCallXml, &response);
276
TEST(response == nonexistentMethodNoDefResponseXml);
278
// We're actually violating the spirit of setDefaultMethod by
279
// doing this to a registry that's already been used, but as long
280
// as it works, it's a convenient way to implement this test.
281
myRegistry.setDefaultMethod(defaultMethodPtr(new nameMethod));
285
myRegistry.processCall(nonexistentMethodCallXml, &response);
286
TEST(response == nonexistentMethodYesDefResponseXml);
293
class registryShutdownTestSuite : public testSuite {
296
virtual string suiteName() {
297
return "registryShutdownTestSuite";
299
virtual void runtests(unsigned int const) {
301
xmlrpc_c::registry myRegistry;
303
class myshutdown : public xmlrpc_c::registry::shutdown {
305
void doit(string const&,
306
void * const) const {
313
myRegistry.setShutdown(&shutdown);
320
registryTestSuite::suiteName() {
321
return "registryTestSuite";
327
registryTestSuite::runtests(unsigned int const indentation) {
330
registryPtr myRegistryP(new registry);
332
myRegistryP->addMethod("sample.add", methodPtr(new sampleAddMethod));
335
registryRegMethodTestSuite().run(indentation+1);
336
registryDefaultMethodTestSuite().run(indentation+1);
340
myRegistry.addMethod("sample.add", methodPtr(new sampleAddMethod));
341
myRegistry.addMethod("echo", methodPtr(new echoMethod));
345
myRegistry.disableIntrospection();
347
myRegistry.setDialect(xmlrpc_dialect_i8);
349
myRegistry.setDialect(xmlrpc_dialect_apache);
351
registryShutdownTestSuite().run(indentation+1);
353
myRegistry.processCall(echoI8ApacheCall, &response);
355
TEST(response == echoI8ApacheResponse);
357
myRegistry.processCall(echoNilApacheCall, &response);
359
TEST(response == echoNilApacheResponse);
361
EXPECT_ERROR( // invalid dialect
362
myRegistry.setDialect(static_cast<xmlrpc_dialect>(300));