summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Swiften/Queries/Request.cpp32
-rw-r--r--Swiften/Queries/UnitTest/RequestTest.cpp35
2 files changed, 50 insertions, 17 deletions
diff --git a/Swiften/Queries/Request.cpp b/Swiften/Queries/Request.cpp
index 6c47725..a77cf49 100644
--- a/Swiften/Queries/Request.cpp
+++ b/Swiften/Queries/Request.cpp
@@ -33,33 +33,35 @@ void Request::send() {
catch (const std::exception&) {
router_->addHandler(this);
}
router_->sendIQ(iq);
}
bool Request::handleIQ(boost::shared_ptr<IQ> iq) {
bool handled = false;
- if (sent_ && iq->getID() == id_) {
- if (iq->getType() == IQ::Result) {
- boost::shared_ptr<Payload> payload = iq->getPayloadOfSameType(payload_);
- if (!payload && boost::dynamic_pointer_cast<RawXMLPayload>(payload_) && !iq->getPayloads().empty()) {
- payload = iq->getPayloads().front();
- }
- handleResponse(payload, ErrorPayload::ref());
- }
- else {
- ErrorPayload::ref errorPayload = iq->getPayload<ErrorPayload>();
- if (errorPayload) {
- handleResponse(boost::shared_ptr<Payload>(), errorPayload);
+ if (iq->getType() == IQ::Result || iq->getType() == IQ::Error) {
+ if (sent_ && iq->getID() == id_) {
+ if (iq->getType() == IQ::Result) {
+ boost::shared_ptr<Payload> payload = iq->getPayloadOfSameType(payload_);
+ if (!payload && boost::dynamic_pointer_cast<RawXMLPayload>(payload_) && !iq->getPayloads().empty()) {
+ payload = iq->getPayloads().front();
+ }
+ handleResponse(payload, ErrorPayload::ref());
}
else {
- handleResponse(boost::shared_ptr<Payload>(), ErrorPayload::ref(new ErrorPayload(ErrorPayload::UndefinedCondition)));
+ ErrorPayload::ref errorPayload = iq->getPayload<ErrorPayload>();
+ if (errorPayload) {
+ handleResponse(boost::shared_ptr<Payload>(), errorPayload);
+ }
+ else {
+ handleResponse(boost::shared_ptr<Payload>(), ErrorPayload::ref(new ErrorPayload(ErrorPayload::UndefinedCondition)));
+ }
}
+ router_->removeHandler(this);
+ handled = true;
}
- router_->removeHandler(this);
- handled = true;
}
return handled;
}
}
diff --git a/Swiften/Queries/UnitTest/RequestTest.cpp b/Swiften/Queries/UnitTest/RequestTest.cpp
index 34d07c9..46eb6fd 100644
--- a/Swiften/Queries/UnitTest/RequestTest.cpp
+++ b/Swiften/Queries/UnitTest/RequestTest.cpp
@@ -23,18 +23,20 @@ class RequestTest : public CppUnit::TestFixture {
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_;
};
@@ -116,26 +118,26 @@ class RequestTest : public CppUnit::TestFixture {
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::FeatureNotImplemented));
+ 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::FeatureNotImplemented, receivedErrors[0].getCondition());
+ 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"));
@@ -176,18 +178,47 @@ class RequestTest : public CppUnit::TestFixture {
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_);