IdentityContentLengthBodyWriterTest.cpp

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 2006-2007 by Peter Dimov.
00004 
00005 This file is part of Calitko (http://www.calitko.org).
00006 
00007 Calitko is free software; you can redistribute it and/or modify
00008 it under the terms of the GNU General Public License as published by
00009 the Free Software Foundation; either version 2 of the License, or
00010 (at your option) any later version.
00011 
00012 Calitko is distributed in the hope that it will be useful,
00013 but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 GNU General Public License for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with Calitko; if not, write to the Free Software
00019 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00020 
00021 */
00022 
00023 #include "Qt.h"
00024 #include "../IdentityContentLengthBodyWriter.h"
00025 #include "../BodyEnd.h"
00026 #include "generated/IdentityContentLengthBodyWriterDriver.h"
00027 #include "Imports.cpp"
00028 
00029 namespace Protocols {
00030 namespace Http {
00031 namespace Testing {
00032 
00034 
00036 class IdentityContentLengthBodyWriterTest : public CppUnit::TestFixture
00037 {
00038     CPPUNIT_TEST_SUITE (IdentityContentLengthBodyWriterTest);
00039     CPPUNIT_TEST (testWriteBodyAtOnce);
00040     CPPUNIT_TEST (testWriteBodyFailsFirstButThenSucceeds);
00041     CPPUNIT_TEST (testWriteBodyInTwoChunksFailsFirstButThenSucceed);
00042     CPPUNIT_TEST_SUITE_END();
00043 
00044     auto_ptr <TransportMock>                                transport;
00045     auto_ptr <IdentityContentLengthBodyWriter>              writerReal;
00046     auto_ptr <IdentityContentLengthBodyWriterDriver>        writer;
00047 
00048     static const int                                        ContentLength = 40;
00049     const QByteArray                                        chunk1;
00050     const QByteArray                                        chunk2;
00051     const QByteArray                                        body;
00052 
00053 public:
00054     IdentityContentLengthBodyWriterTest()
00055      :  chunk1 (20, '1'), chunk2 (20, '2'), body (chunk1 + chunk2)
00056     {
00057     }
00058 
00059     void setUp()
00060     {
00061         transport.reset (new TransportMock);
00062         writerReal.reset (new IdentityContentLengthBodyWriter (ContentLength));
00063         writer.reset (new IdentityContentLengthBodyWriterDriver (*writerReal));
00064     }
00065 
00066     void tearDown()
00067     {
00068         writer.reset();
00069         writerReal.reset();
00070         transport.reset();
00071     }
00072 
00073     void refWriteBodyChunk (const QByteArray &chunk, bool willSucceed)
00074     {
00075         call (writer->write (RawData (chunk), *transport))
00076             .willCall (transport->write (chunk))
00077             .willReturn (willSucceed)
00078         .willReturn (willSucceed);
00079     }
00080 
00081     void refWriteBodyEnd()
00082     {
00083         call (writer->write (BodyEnd(), *transport))
00084         .willReturn (true);
00085     }
00086 
00087     void testWriteBodyAtOnce()
00088     {
00089         refWriteBodyChunk (body, true);
00090         refWriteBodyEnd();
00091     }
00092 
00093     void testWriteBodyFailsFirstButThenSucceeds()
00094     {
00095         refWriteBodyChunk (body, false);
00096         refWriteBodyChunk (body, true);
00097         refWriteBodyEnd();
00098     }
00099 
00100     void testWriteBodyInTwoChunksFailsFirstButThenSucceed()
00101     {
00102         refWriteBodyChunk (chunk1, false);
00103         refWriteBodyChunk (chunk1, true);
00104         refWriteBodyChunk (chunk2, false);
00105         refWriteBodyChunk (chunk2, true);
00106         refWriteBodyEnd();
00107     }
00108 };
00109 
00110 CPPUNIT_TEST_SUITE_REGISTRATION (IdentityContentLengthBodyWriterTest);
00111 
00112 } // namespace Testing
00113 } // namespace Http
00114 } // namespace Protocols