diff options
Diffstat (limited to 'Swiften/Queries/UnitTest/RequestTest.cpp')
-rw-r--r-- | Swiften/Queries/UnitTest/RequestTest.cpp | 65 |
1 files changed, 64 insertions, 1 deletions
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_); |