IdentityContentLengthBodyReaderTest.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 "../IdentityContentLengthBodyReader.h"
00025 #include "../BodyEnd.h"
00026 #include "generated/IdentityContentLengthBodyReaderDriver.h"
00027 #include "Imports.cpp"
00028 
00029 namespace Protocols {
00030 namespace Http {
00031 namespace Testing {
00032 
00034 
00036 class IdentityContentLengthBodyReaderTest : public CppUnit::TestFixture
00037 {
00038     CPPUNIT_TEST_SUITE (IdentityContentLengthBodyReaderTest);
00039     CPPUNIT_TEST (testReadContentLengthBytesNoneAvailable);
00040     CPPUNIT_TEST (testReadContentLengthBytesAtOnce);
00041     CPPUNIT_TEST (testReadContentLengthBytesInTwoReads);
00042     CPPUNIT_TEST_SUITE_END();
00043 
00044     auto_ptr <TransportMock>                            transport;
00045     auto_ptr <IdentityContentLengthBodyReader>          bodyReaderReal;
00046     auto_ptr <IdentityContentLengthBodyReaderDriver>    bodyReader;
00047 
00048     static const int                                    ContentLength = 50;
00049     QByteArray                                          content;
00050 
00051 public:
00052     void setUp()
00053     {
00054         transport.reset (new TransportMock);
00055         bodyReaderReal.reset (new IdentityContentLengthBodyReader (
00056                                 ContentLength));
00057         bodyReader.reset (new IdentityContentLengthBodyReaderDriver (
00058                                 *bodyReaderReal));
00059 
00060         content = QByteArray (ContentLength, '3');
00061     }
00062 
00063     void tearDown()
00064     {
00065         bodyReader.reset();
00066         bodyReaderReal.reset();
00067         transport.reset();
00068     }
00069 
00070     void testReadContentLengthBytesNoneAvailable()
00071     {
00072         call (bodyReader->read (*transport))
00073             .willCall (transport->readAtMost (ContentLength))
00074             .willReturn (QByteArray())
00075         .willReturn (Data());
00076     }
00077 
00078     void testReadContentLengthBytesAtOnce()
00079     {
00080         call (bodyReader->read (*transport))
00081             .willCall (transport->readAtMost (ContentLength))
00082             .willReturn (content)
00083         .willReturn (Data (RawData (content)));
00084 
00085         call (bodyReader->read (*transport))
00086         .willReturn (Data (BodyEnd()));
00087     }
00088 
00089     void testReadContentLengthBytesInTwoReads()
00090     {
00091         QByteArray firstPart = content.left (ContentLength / 2);
00092         QByteArray secondPart = content.mid (ContentLength / 2);
00093 
00094         call (bodyReader->read (*transport))
00095             .willCall (transport->readAtMost (ContentLength))
00096             .willReturn (firstPart)
00097         .willReturn (Data (RawData (firstPart)));
00098 
00099         call (bodyReader->read (*transport))
00100             .willCall (transport->readAtMost (ContentLength - firstPart.size()))
00101             .willReturn (secondPart)
00102         .willReturn (Data (RawData (secondPart)));
00103 
00104         call (bodyReader->read (*transport))
00105         .willReturn (Data (BodyEnd()));
00106     }
00107 };
00108 
00109 CPPUNIT_TEST_SUITE_REGISTRATION (IdentityContentLengthBodyReaderTest);
00110 
00111 } // namespace Testing
00112 } // namespace Http
00113 } // namespace Protocols