1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
|
/*
* Copyright (c) 2010 Remko Tronçon
* Licensed under the GNU General Public License v3.
* See Documentation/Licenses/GPLv3.txt for more information.
*/
#include <cppunit/extensions/HelperMacros.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <boost/shared_ptr.hpp>
#include <boost/bind.hpp>
#include <boost/smart_ptr/make_shared.hpp>
#include <Swiften/Queries/GenericRequest.h>
#include <Swiften/Queries/IQRouter.h>
#include <Swiften/Queries/DummyIQChannel.h>
#include <Swiften/Elements/Payload.h>
#include <Swiften/Elements/RawXMLPayload.h>
using namespace Swift;
class RequestTest : public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE(RequestTest);
CPPUNIT_TEST(testSendGet);
CPPUNIT_TEST(testSendSet);
CPPUNIT_TEST(testHandleIQ);
CPPUNIT_TEST(testHandleIQ_InvalidID);
CPPUNIT_TEST(testHandleIQ_Error);
CPPUNIT_TEST(testHandleIQ_ErrorWithoutPayload);
CPPUNIT_TEST(testHandleIQ_BeforeSend);
CPPUNIT_TEST(testHandleIQ_DifferentPayload);
CPPUNIT_TEST(testHandleIQ_RawXMLPayload);
CPPUNIT_TEST(testHandleIQ_GetWithSameID);
CPPUNIT_TEST(testHandleIQ_SetWithSameID);
CPPUNIT_TEST_SUITE_END();
public:
class MyPayload : public Payload {
public:
MyPayload(const std::string& s = "") : text_(s) {}
std::string text_;
};
struct MyOtherPayload : public Payload {
};
class MyRequest : public Request {
public:
MyRequest(
IQ::Type type,
const JID& receiver,
boost::shared_ptr<Payload> payload,
IQRouter* router) :
Request(type, receiver, payload, router) {
}
virtual void handleResponse(boost::shared_ptr<Payload> payload, ErrorPayload::ref error) {
onResponse(payload, error);
}
public:
boost::signal<void (boost::shared_ptr<Payload>, ErrorPayload::ref)> onResponse;
};
public:
void setUp() {
channel_ = new DummyIQChannel();
router_ = new IQRouter(channel_);
payload_ = boost::shared_ptr<Payload>(new MyPayload("foo"));
responsePayload_ = boost::shared_ptr<Payload>(new MyPayload("bar"));
responsesReceived_ = 0;
}
void tearDown() {
delete router_;
delete channel_;
}
void testSendSet() {
MyRequest testling(IQ::Set, JID("foo@bar.com/baz"), payload_, router_);
testling.send();
CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(channel_->iqs_.size()));
CPPUNIT_ASSERT_EQUAL(JID("foo@bar.com/baz"), channel_->iqs_[0]->getTo());
CPPUNIT_ASSERT_EQUAL(IQ::Set, channel_->iqs_[0]->getType());
CPPUNIT_ASSERT_EQUAL(std::string("test-id"), channel_->iqs_[0]->getID());
}
void testSendGet() {
MyRequest testling(IQ::Get, JID("foo@bar.com/baz"), payload_, router_);
testling.send();
CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(channel_->iqs_.size()));
CPPUNIT_ASSERT_EQUAL(IQ::Get, channel_->iqs_[0]->getType());
}
void testHandleIQ() {
MyRequest testling(IQ::Get, JID("foo@bar.com/baz"), payload_, router_);
testling.onResponse.connect(boost::bind(&RequestTest::handleResponse, this, _1, _2));
testling.send();
channel_->onIQReceived(createResponse("test-id"));
CPPUNIT_ASSERT_EQUAL(1, responsesReceived_);
CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(receivedErrors.size()));
CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(channel_->iqs_.size()));
}
// FIXME: Doesn't test that it didn't handle the payload
void testHandleIQ_InvalidID() {
MyRequest testling(IQ::Get, JID("foo@bar.com/baz"), payload_, router_);
testling.onResponse.connect(boost::bind(&RequestTest::handleResponse, this, _1, _2));
testling.send();
channel_->onIQReceived(createResponse("different-id"));
CPPUNIT_ASSERT_EQUAL(0, responsesReceived_);
CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(receivedErrors.size()));
CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(channel_->iqs_.size()));
}
void testHandleIQ_Error() {
MyRequest testling(IQ::Get, JID("foo@bar.com/baz"), payload_, router_);
testling.onResponse.connect(boost::bind(&RequestTest::handleResponse, this, _1, _2));
testling.send();
boost::shared_ptr<IQ> error = createError("test-id");
boost::shared_ptr<Payload> errorPayload = boost::shared_ptr<ErrorPayload>(new ErrorPayload(ErrorPayload::InternalServerError));
error->addPayload(errorPayload);
channel_->onIQReceived(error);
CPPUNIT_ASSERT_EQUAL(0, responsesReceived_);
CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(receivedErrors.size()));
CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(channel_->iqs_.size()));
CPPUNIT_ASSERT_EQUAL(ErrorPayload::InternalServerError, receivedErrors[0].getCondition());
}
void testHandleIQ_ErrorWithoutPayload() {
MyRequest testling(IQ::Get, JID("foo@bar.com/baz"), payload_, router_);
testling.onResponse.connect(boost::bind(&RequestTest::handleResponse, this, _1, _2));
testling.send();
channel_->onIQReceived(createError("test-id"));
CPPUNIT_ASSERT_EQUAL(0, responsesReceived_);
CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(receivedErrors.size()));
CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(channel_->iqs_.size()));
CPPUNIT_ASSERT_EQUAL(ErrorPayload::UndefinedCondition, receivedErrors[0].getCondition());
}
void testHandleIQ_BeforeSend() {
MyRequest testling(IQ::Get, JID("foo@bar.com/baz"), payload_, router_);
testling.onResponse.connect(boost::bind(&RequestTest::handleResponse, this, _1, _2));
channel_->onIQReceived(createResponse("test-id"));
CPPUNIT_ASSERT_EQUAL(0, responsesReceived_);
CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(receivedErrors.size()));
CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(channel_->iqs_.size()));
}
void testHandleIQ_DifferentPayload() {
MyRequest testling(IQ::Get, JID("foo@bar.com/baz"), payload_, router_);
testling.onResponse.connect(boost::bind(&RequestTest::handleDifferentResponse, this, _1, _2));
testling.send();
responsePayload_ = boost::make_shared<MyOtherPayload>();
channel_->onIQReceived(createResponse("test-id"));
CPPUNIT_ASSERT_EQUAL(1, responsesReceived_);
CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(receivedErrors.size()));
CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(channel_->iqs_.size()));
}
void testHandleIQ_RawXMLPayload() {
payload_ = boost::make_shared<RawXMLPayload>("<bla/>");
MyRequest testling(IQ::Get, JID("foo@bar.com/baz"), payload_, router_);
testling.onResponse.connect(boost::bind(&RequestTest::handleRawXMLResponse, this, _1, _2));
testling.send();
responsePayload_ = boost::make_shared<MyOtherPayload>();
channel_->onIQReceived(createResponse("test-id"));
CPPUNIT_ASSERT_EQUAL(1, responsesReceived_);
CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(receivedErrors.size()));
CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(channel_->iqs_.size()));
}
void testHandleIQ_GetWithSameID() {
MyRequest testling(IQ::Get, JID("foo@bar.com/baz"), payload_, router_);
testling.onResponse.connect(boost::bind(&RequestTest::handleResponse, this, _1, _2));
testling.send();
boost::shared_ptr<IQ> response = createResponse("test-id");
response->setType(IQ::Get);
channel_->onIQReceived(response);
CPPUNIT_ASSERT_EQUAL(0, responsesReceived_);
CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(receivedErrors.size()));
CPPUNIT_ASSERT_EQUAL(2, static_cast<int>(channel_->iqs_.size()));
}
void testHandleIQ_SetWithSameID() {
MyRequest testling(IQ::Get, JID("foo@bar.com/baz"), payload_, router_);
testling.onResponse.connect(boost::bind(&RequestTest::handleResponse, this, _1, _2));
testling.send();
boost::shared_ptr<IQ> response = createResponse("test-id");
response->setType(IQ::Set);
channel_->onIQReceived(response);
CPPUNIT_ASSERT_EQUAL(0, responsesReceived_);
CPPUNIT_ASSERT_EQUAL(0, static_cast<int>(receivedErrors.size()));
CPPUNIT_ASSERT_EQUAL(2, static_cast<int>(channel_->iqs_.size()));
}
private:
void handleResponse(boost::shared_ptr<Payload> p, ErrorPayload::ref e) {
if (e) {
receivedErrors.push_back(*e);
}
else {
boost::shared_ptr<MyPayload> payload(boost::dynamic_pointer_cast<MyPayload>(p));
CPPUNIT_ASSERT(payload);
CPPUNIT_ASSERT_EQUAL(std::string("bar"), payload->text_);
++responsesReceived_;
}
}
void handleDifferentResponse(boost::shared_ptr<Payload> p, ErrorPayload::ref e) {
CPPUNIT_ASSERT(!e);
CPPUNIT_ASSERT(!p);
++responsesReceived_;
}
void handleRawXMLResponse(boost::shared_ptr<Payload> p, ErrorPayload::ref e) {
CPPUNIT_ASSERT(!e);
CPPUNIT_ASSERT(p);
CPPUNIT_ASSERT(boost::dynamic_pointer_cast<MyOtherPayload>(p));
++responsesReceived_;
}
boost::shared_ptr<IQ> createResponse(const std::string& id) {
boost::shared_ptr<IQ> iq(new IQ(IQ::Result));
iq->addPayload(responsePayload_);
iq->setID(id);
return iq;
}
boost::shared_ptr<IQ> createError(const std::string& id) {
boost::shared_ptr<IQ> iq(new IQ(IQ::Error));
iq->setID(id);
return iq;
}
private:
IQRouter* router_;
DummyIQChannel* channel_;
boost::shared_ptr<Payload> payload_;
boost::shared_ptr<Payload> responsePayload_;
int responsesReceived_;
std::vector<ErrorPayload> receivedErrors;
};
CPPUNIT_TEST_SUITE_REGISTRATION(RequestTest);
|