diff options
author | Remko Tronçon <git@el-tramo.be> | 2011-03-01 20:17:23 (GMT) |
---|---|---|
committer | Remko Tronçon <git@el-tramo.be> | 2011-03-01 20:17:23 (GMT) |
commit | ca3f25d09a703ff7c27267a5591ce5379886e1c9 (patch) | |
tree | b14c38655ea3c61beb79f8a895fc7e6ce2989a9e /Swiften/Queries | |
parent | 5ad284705760c451012a5d896251af538e7bcede (diff) | |
download | swift-ca3f25d09a703ff7c27267a5591ce5379886e1c9.zip swift-ca3f25d09a703ff7c27267a5591ce5379886e1c9.tar.bz2 |
Some more Sluift enhancements.
Diffstat (limited to 'Swiften/Queries')
-rw-r--r-- | Swiften/Queries/RawRequest.h | 49 | ||||
-rw-r--r-- | Swiften/Queries/Request.cpp | 7 | ||||
-rw-r--r-- | Swiften/Queries/UnitTest/RequestTest.cpp | 65 |
3 files changed, 119 insertions, 2 deletions
diff --git a/Swiften/Queries/RawRequest.h b/Swiften/Queries/RawRequest.h new file mode 100644 index 0000000..477952f --- /dev/null +++ b/Swiften/Queries/RawRequest.h @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2011 Remko Tronçon + * Licensed under the GNU General Public License v3. + * See Documentation/Licenses/GPLv3.txt for more information. + */ + +#pragma once + +#include <boost/smart_ptr/make_shared.hpp> +#include <typeinfo> + +#include <Swiften/Queries/Request.h> +#include <Swiften/Elements/RawXMLPayload.h> +#include <Swiften/Elements/ErrorPayload.h> +#include <Swiften/Serializer/PayloadSerializer.h> +#include <Swiften/Serializer/PayloadSerializers/ErrorSerializer.h> +#include <Swiften/Serializer/PayloadSerializers/FullPayloadSerializerCollection.h> + +namespace Swift { + class RawRequest : public Request { + public: + typedef boost::shared_ptr<RawRequest> ref; + + static ref create(IQ::Type type, const JID& recipient, const std::string& data, IQRouter* router) { + return ref(new RawRequest(type, recipient, data, router)); + } + + boost::signal<void (const std::string&)> onResponse; + + private: + RawRequest(IQ::Type type, const JID& receiver, const std::string& data, IQRouter* router) : Request(type, receiver, boost::make_shared<RawXMLPayload>(data), router) { + } + + virtual void handleResponse(Payload::ref payload, ErrorPayload::ref error) { + if (error) { + onResponse(ErrorSerializer().serializePayload(error)); + } + else { + assert(payload); + PayloadSerializer* serializer = serializers.getPayloadSerializer(payload); + assert(serializer); + onResponse(serializer->serialize(payload)); + } + } + + private: + FullPayloadSerializerCollection serializers; + }; +} diff --git a/Swiften/Queries/Request.cpp b/Swiften/Queries/Request.cpp index 35475c1..6c47725 100644 --- a/Swiften/Queries/Request.cpp +++ b/Swiften/Queries/Request.cpp @@ -6,6 +6,7 @@ #include "Swiften/Queries/Request.h" #include "Swiften/Queries/IQRouter.h" +#include <Swiften/Elements/RawXMLPayload.h> namespace Swift { @@ -40,7 +41,11 @@ bool Request::handleIQ(boost::shared_ptr<IQ> iq) { bool handled = false; if (sent_ && iq->getID() == id_) { if (iq->getType() == IQ::Result) { - handleResponse(iq->getPayloadOfSameType(payload_), ErrorPayload::ref()); + 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>(); diff --git a/Swiften/Queries/UnitTest/RequestTest.cpp b/Swiften/Queries/UnitTest/RequestTest.cpp index e99149e..34d07c9 100644 --- a/Swiften/Queries/UnitTest/RequestTest.cpp +++ b/Swiften/Queries/UnitTest/RequestTest.cpp @@ -8,11 +8,13 @@ #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; @@ -25,6 +27,8 @@ class RequestTest : public CppUnit::TestFixture { CPPUNIT_TEST(testHandleIQ_Error); CPPUNIT_TEST(testHandleIQ_ErrorWithoutPayload); CPPUNIT_TEST(testHandleIQ_BeforeSend); + CPPUNIT_TEST(testHandleIQ_DifferentPayload); + CPPUNIT_TEST(testHandleIQ_RawXMLPayload); CPPUNIT_TEST_SUITE_END(); public: @@ -34,7 +38,26 @@ class RequestTest : public CppUnit::TestFixture { std::string text_; }; - typedef GenericRequest<MyPayload> MyRequest; + 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() { @@ -132,6 +155,33 @@ class RequestTest : public CppUnit::TestFixture { 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())); + } + private: void handleResponse(boost::shared_ptr<Payload> p, ErrorPayload::ref e) { if (e) { @@ -145,6 +195,19 @@ class RequestTest : public CppUnit::TestFixture { } } + 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_); |